MCPcopy Create free account
hub / github.com/coderabbit214/document-ai / chat

Function chat

pkg/api/chat.go:17–103  ·  view source on GitHub ↗
(c *gin.Context)

Source from the content-addressed store, hash-verified

15}
16
17func chat(c *gin.Context) {
18 var message Message
19 if err := c.Bind(&message); err != nil {
20 c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
21 return
22 }
23
24 //计算向量
25 embeddingRequest := openai.EmbeddingRequest{
26 Input: message.Text,
27 Model: openai.TextEmbeddingAda002,
28 }
29 response, err := openai.SendEmbeddings(embeddingRequest)
30 if err != nil {
31 common.Logger.Error(err.Error())
32 c.JSON(http.StatusOK, common.Error(err.Error()))
33 return
34 }
35 params := make(map[string]interface{})
36 params["exact"] = false
37 params["hnsw_ef"] = 128
38
39 sr := qdrant.PointSearchRequest{
40 Params: params,
41 Vector: response.Data[0].Embedding,
42 Limit: 3,
43 WithPayload: true,
44 }
45 //查询相似的
46 res, err := qdrant.SearchPoints(common.GlobalObject.Qdrant.CollectionName, sr)
47 if err != nil {
48 common.Logger.Error(err.Error())
49 c.JSON(http.StatusOK, common.Error(err.Error()))
50 return
51 }
52 //组装本地数据
53 localData := ""
54 for i, v := range res {
55 re := v.Payload.(map[string]interface{})
56 localData += "\n"
57 localData += strconv.Itoa(i)
58 localData += "."
59 localData += re["title"].(string)
60 localData += ":"
61 localData += re["text"].(string)
62 }
63 messages := make([]openai.ChatCompletionMessage, 0)
64 q := "使用以下段落来回答问题,如果段落内容不相关就返回未查到相关信息:\"" + message.Text + "\""
65 q += localData
66
67 system := openai.ChatCompletionMessage{
68 Role: "system",
69 Content: "你是一个医院问诊机器人",
70 }
71 demo_q := "使用以下段落来回答问题:\"成人头疼,流鼻涕是感冒还是过敏?\"\n1. 普通感冒:您会出现喉咙发痒或喉咙痛,流鼻涕,流清澈的稀鼻涕(液体),有时轻度发热。\n2. 常年过敏:症状包括鼻塞或流鼻涕,鼻、口或喉咙发痒,眼睛流泪、发红、发痒、肿胀,打喷嚏。"
72 demo_a := "成人出现头痛和流鼻涕的症状,可能是由于普通感冒或常年过敏引起的。如果病人出现咽喉痛和咳嗽,感冒的可能性比较大;而如果出现口、喉咙发痒、眼睛肿胀等症状,常年过敏的可能性比较大。"
73 user1 := openai.ChatCompletionMessage{
74 Role: "user",

Callers

nothing calls this directly

Calls 5

SendEmbeddingsFunction · 0.92
ErrorFunction · 0.92
SearchPointsFunction · 0.92
SendChatFunction · 0.92
SuccessFunction · 0.92

Tested by

no test coverage detected