kefu/knowledge/mysql/main.go

59 lines
1.5 KiB
Go

package main
import (
"encoding/json"
"fmt"
"github.com/joho/godotenv"
uuid "github.com/satori/go.uuid"
"knowledge/utils"
"log"
"os"
)
func main() {
err := godotenv.Load(".env")
// 读取环境变量
OPENAI_API_KEY := os.Getenv("OPENAI_KEY")
OPENAI_API_BASE := os.Getenv("OPENAI_API_BASE")
utils.QdrantBase = os.Getenv("QDRANT_BASE")
utils.QdrantPort = os.Getenv("QDRANT_PORT")
sqlTool := utils.DBTool{
Username: os.Getenv("MySQL_Username"),
Password: os.Getenv("MySQL_Password"),
Server: os.Getenv("MySQL_Server"),
Port: os.Getenv("MySQL_Port"),
Database: os.Getenv("MySQL_Database"),
}
gpt := utils.NewChatGptTool(OPENAI_API_BASE, OPENAI_API_KEY)
list, err := sqlTool.QuerySql("select id, content from article where ent_id=5 limit 2,1000")
if err != nil {
fmt.Println("Error:", err)
return
}
for _, row := range list {
if row["content"] == "" {
continue
}
// 打印文件内容
fileData := row["content"].(string)
response, err := gpt.GetEmbedding(fileData, "text-embedding-ada-002")
var embeddingResponse utils.EmbeddingResponse
json.Unmarshal([]byte(response), &embeddingResponse)
collectionName := "data_collection"
id := uuid.NewV4().String()
points := []map[string]interface{}{
{
"id": id,
"payload": map[string]interface{}{"text": fileData},
"vector": embeddingResponse.Data[0].Embedding,
},
}
res, err := utils.PutPoints(collectionName, points)
log.Println(fileData, res, err)
}
}