Skip to content

Commit

Permalink
feat(llm): complete comments in 'service/llm.go'
Browse files Browse the repository at this point in the history
  • Loading branch information
creeper12356 committed Sep 24, 2024
1 parent c345dae commit 0d3f77b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
4 changes: 2 additions & 2 deletions handler/llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ func GetCourseSummaryHandler(c *gin.Context) {

c.JSON(http.StatusOK, response)
}
func VectorizeCourseReviewsHandler(c *gin.Context) {
func VectorizeCourseHandler(c *gin.Context) {
var request dto.CourseDetailRequest
if err := c.ShouldBindUri(&request); err != nil {
c.JSON(http.StatusNotFound, dto.BaseResponse{Message: "参数错误"})
return
}

err := service.VectorizeCourseReviews(c, request.CourseID)
err := service.VectorizeCourse(c, request.CourseID)
if err != nil {
fmt.Println(err)
c.JSON(http.StatusInternalServerError, dto.BaseResponse{Message: "内部错误。"})
Expand Down
2 changes: 1 addition & 1 deletion router.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ func registerRouter(r *gin.Engine) {
llmGroup := needAuthGroup.Group(("/llm"))
llmGroup.GET("/review/opt", handler.OptCourseReviewHandler)
llmGroup.GET("/course/summary/:courseID", handler.GetCourseSummaryHandler)
llmGroup.GET("/vectorize/:courseID", handler.VectorizeCourseReviewsHandler)
llmGroup.GET("/vectorize/:courseID", handler.VectorizeCourseHandler)
llmGroup.GET("/match", handler.GetMatchCoursesHandler)
}
24 changes: 23 additions & 1 deletion service/llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ import (
"github.com/tmc/langchaingo/vectorstores"

Check failure on line 19 in service/llm.go

View workflow job for this annotation

GitHub Actions / build

github.com/tmc/[email protected]: replacement directory /home/creeper/jcourse/langchaingo does not exist
)

// OptCourseReview使用LLM提示词对课程评价内容进行优化。
// courseName为课程名称,
// reviewContent为评价的内容,
// 函数返回值包含两个字段:
// Suggestion为修改建议,
// Result为根据修改建议给出的一种修改结果。
func OptCourseReview(courseName string, reviewContent string) (dto.OptCourseReviewResponse, error) {
llm, err := openai.New()
if err != nil {
Expand Down Expand Up @@ -50,6 +56,11 @@ func OptCourseReview(courseName string, reviewContent string) (dto.OptCourseRevi
return response, err
}

// GetCourseSummary使用LLM提示词基于课程评价生成课程总结。
// courseID为课程的ID,
// 返回值包含课程的总结。
// TODO: 此处基于课程最近的100条评价内容生成课程总结,后续可
// 以进一步调整和优化。
func GetCourseSummary(ctx context.Context, courseID int64) (*dto.GetCourseSummaryResponse, error) {
courseQuery := repository.NewCourseQuery(dal.GetDBClient())
coursePOs, err := courseQuery.GetCourse(ctx, repository.WithID(courseID))
Expand Down Expand Up @@ -118,7 +129,12 @@ func GetCourseSummary(ctx context.Context, courseID int64) (*dto.GetCourseSummar

}

func VectorizeCourseReviews(ctx context.Context, courseID int64) error {
// VectorizeCourse对课程进行向量化,
// 用于后续GetMatchCourses进行向量匹配。
// courseID为课程ID。
// TODO: 此处使用课程最近100条评论和课程名进行向量化,后续可以
// 优化和调整;
func VectorizeCourse(ctx context.Context, courseID int64) error {
courseQuery := repository.NewCourseQuery(dal.GetDBClient())
coursePOs, err := courseQuery.GetCourse(ctx, repository.WithID(courseID))
if err != nil || len(coursePOs) == 0 {
Expand Down Expand Up @@ -176,6 +192,12 @@ func VectorizeCourseReviews(ctx context.Context, courseID int64) error {
return err
}

// GetMatchCourses使用向量匹配,
// 根据自然语言描述找到最匹配的课程列表。
// description为用户提供的自然语言描述。
// TODO: 此处向量相似性计算(SimilaritySearch)中,
// 输出的课程列表数量为2,后续可以修改。

func GetMatchCourses(ctx context.Context, description string) ([]model.CourseSummary, error) {
vectorStore, err := rpc.OpenVectorStoreConn()

Expand Down

0 comments on commit 0d3f77b

Please sign in to comment.