39 lines
980 B
Go
39 lines
980 B
Go
package ai
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/gin-gonic/gin"
|
|
"kefu/lib"
|
|
"kefu/models"
|
|
)
|
|
|
|
func PostSearchPoints(c *gin.Context) {
|
|
entId, _ := c.Get("ent_id")
|
|
collectName, _ := c.Get("collect_name")
|
|
content := c.PostForm("search")
|
|
config := models.GetEntConfigsMap(entId.(string), "chatGPTUrl", "chatGPTSecret", "")
|
|
|
|
gpt := lib.NewChatGptTool(config["chatGPTUrl"], config["chatGPTSecret"])
|
|
response, err := gpt.GetEmbedding(content, "text-embedding-ada-002")
|
|
if err != nil {
|
|
return
|
|
}
|
|
var embeddingResponse lib.EmbeddingResponse
|
|
json.Unmarshal([]byte(response), &embeddingResponse)
|
|
if len(embeddingResponse.Data) == 0 {
|
|
return
|
|
}
|
|
params := map[string]interface{}{"exact": false, "hnsw_ef": 128}
|
|
vector := embeddingResponse.Data[0].Embedding
|
|
limit := 10
|
|
points, err := lib.SearchPoints(collectName.(string), params, vector, limit, 0.8)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if err != nil {
|
|
c.Writer.Write([]byte(err.Error()))
|
|
return
|
|
}
|
|
c.Writer.Write(points)
|
|
}
|