eta_llm_client.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. package eta_llm
  2. import (
  3. "bufio"
  4. "bytes"
  5. "encoding/json"
  6. "errors"
  7. "eta/eta_api/models"
  8. "eta/eta_api/utils"
  9. "eta/eta_api/utils/llm"
  10. "eta/eta_api/utils/llm/eta_llm/eta_llm_http"
  11. "fmt"
  12. "io"
  13. "mime/multipart"
  14. "net/http"
  15. "os"
  16. "strings"
  17. "sync"
  18. )
  19. var (
  20. dsOnce sync.Once
  21. etaLlmClient *ETALLMClient
  22. )
  23. const (
  24. KNOWLEDEG_CHAT_MODE = "local_kb"
  25. DEFALUT_PROMPT_NAME = "default"
  26. CONTENT_TYPE_JSON = "application/json"
  27. KNOWLEDGE_BASE_CHAT_API = "/chat/kb_chat"
  28. DOCUMENT_CHAT_API = "/chat/file_chat"
  29. KNOWLEDGE_BASE_SEARCH_DOCS_API = "/knowledge_base/search_docs"
  30. UPLOAD_TEMP_DOCS_API = "/knowledge_base/upload_temp_docs"
  31. )
  32. type ETALLMClient struct {
  33. *llm.LLMClient
  34. LlmModel string
  35. }
  36. type LLMConfig struct {
  37. LlmAddress string `json:"llm_server"`
  38. LlmModel string `json:"llm_model"`
  39. }
  40. func GetInstance() llm.LLMService {
  41. dsOnce.Do(func() {
  42. confStr := models.BusinessConfMap[models.LLMInitConfig]
  43. if confStr == "" {
  44. utils.FileLog.Error("LLM配置为空")
  45. return
  46. }
  47. var config LLMConfig
  48. err := json.Unmarshal([]byte(confStr), &config)
  49. if err != nil {
  50. utils.FileLog.Error("LLM配置错误")
  51. }
  52. if etaLlmClient == nil {
  53. etaLlmClient = &ETALLMClient{
  54. LLMClient: llm.NewLLMClient(config.LlmAddress, 300),
  55. LlmModel: config.LlmModel,
  56. }
  57. }
  58. })
  59. return etaLlmClient
  60. }
  61. func (ds *ETALLMClient) DocumentChat(query string, KnowledgeId string, history []json.RawMessage, stream bool) (llmRes *http.Response, err error) {
  62. ChatHistory := make([]eta_llm_http.HistoryContent, 0)
  63. for _, historyItemStr := range history {
  64. var historyItem eta_llm_http.HistoryContent
  65. parseErr := json.Unmarshal(historyItemStr, &historyItem)
  66. if parseErr != nil {
  67. continue
  68. }
  69. //str := strings.Split(historyItemStr, "-")
  70. //historyItem := eta_llm_http.HistoryContent{
  71. // Role: str[0],
  72. // Content: str[1],
  73. //}
  74. ChatHistory = append(ChatHistory, historyItem)
  75. }
  76. kbReq := eta_llm_http.DocumentChatRequest{
  77. Query: query,
  78. KnowledgeId: KnowledgeId,
  79. History: ChatHistory,
  80. TopK: 3,
  81. //ScoreThreshold: 0.5,
  82. ScoreThreshold: 2,
  83. Stream: stream,
  84. ModelName: ds.LlmModel,
  85. //Temperature: 0.7,
  86. Temperature: 0.01,
  87. MaxTokens: 0,
  88. //PromptName: DEFALUT_PROMPT_NAME,
  89. }
  90. //fmt.Printf("%v", kbReq.History)
  91. body, err := json.Marshal(kbReq)
  92. fmt.Println(string(body))
  93. if err != nil {
  94. return
  95. }
  96. return ds.DoStreamPost(DOCUMENT_CHAT_API, body)
  97. }
  98. func (ds *ETALLMClient) KnowledgeBaseChat(query string, KnowledgeBaseName string, history []json.RawMessage) (llmRes *http.Response, err error) {
  99. ChatHistory := make([]eta_llm_http.HistoryContent, 0)
  100. for _, historyItemStr := range history {
  101. var historyItem eta_llm_http.HistoryContentWeb
  102. parseErr := json.Unmarshal(historyItemStr, &historyItem)
  103. if parseErr != nil {
  104. continue
  105. }
  106. ChatHistory = append(ChatHistory, eta_llm_http.HistoryContent{
  107. Content: historyItem.Content,
  108. Role: historyItem.Role,
  109. })
  110. }
  111. kbReq := eta_llm_http.KbChatRequest{
  112. Query: query,
  113. Mode: KNOWLEDEG_CHAT_MODE,
  114. KbName: KnowledgeBaseName,
  115. History: ChatHistory,
  116. TopK: 3,
  117. ScoreThreshold: 0.5,
  118. Stream: true,
  119. Model: ds.LlmModel,
  120. Temperature: 0.7,
  121. MaxTokens: 0,
  122. PromptName: DEFALUT_PROMPT_NAME,
  123. ReturnDirect: false,
  124. }
  125. fmt.Printf("%v", kbReq.History)
  126. body, err := json.Marshal(kbReq)
  127. if err != nil {
  128. return
  129. }
  130. return ds.DoStreamPost(KNOWLEDGE_BASE_CHAT_API, body)
  131. }
  132. func (ds *ETALLMClient) FileChat(query string, KnowledgeId string, llmModel string, history []json.RawMessage) (resp eta_llm_http.BaseResponse, err error) {
  133. ChatHistory := make([]eta_llm_http.HistoryContent, 0)
  134. for _, historyItemStr := range history {
  135. var historyItem eta_llm_http.HistoryContentWeb
  136. parseErr := json.Unmarshal(historyItemStr, &historyItem)
  137. if parseErr != nil {
  138. continue
  139. }
  140. ChatHistory = append(ChatHistory, eta_llm_http.HistoryContent{
  141. Content: historyItem.Content,
  142. Role: historyItem.Role,
  143. })
  144. }
  145. var model string
  146. if llmModel != "" {
  147. model = llmModel
  148. } else {
  149. model = ds.LlmModel
  150. }
  151. kbReq := eta_llm_http.DocumentChatRequest{
  152. ModelName: model,
  153. Query: query,
  154. KnowledgeId: KnowledgeId,
  155. History: ChatHistory,
  156. TopK: 20,
  157. ScoreThreshold: 2,
  158. Stream: false,
  159. Temperature: 0.01,
  160. MaxTokens: 0,
  161. PromptName: DEFALUT_PROMPT_NAME,
  162. }
  163. body, err := json.Marshal(kbReq)
  164. if err != nil {
  165. utils.FileLog.Error("内容生成失败,序列化请求参数失败,err", err.Error())
  166. err = fmt.Errorf("内容生成失败,序列化请求参数失败,err:%v", err)
  167. return
  168. }
  169. return ds.DoPost(DOCUMENT_CHAT_API, body)
  170. }
  171. func (ds *ETALLMClient) UploadFileToTemplate(files []*os.File, param map[string]interface{}) (data interface{}, err error) {
  172. var pervId string
  173. if value, ok := param["PrevId"]; ok {
  174. pervId = value.(string)
  175. }
  176. docReq := eta_llm_http.UploadTempDocsRequest{
  177. ChunkOverlap: "150",
  178. ChunkSize: "750",
  179. PrevId: pervId,
  180. ZhTitleEnhance: "false",
  181. }
  182. body, err := json.Marshal(docReq)
  183. if err != nil {
  184. return
  185. }
  186. resp, err := ds.DoFile(UPLOAD_TEMP_DOCS_API, body, files)
  187. if !resp.Success {
  188. err = errors.New(resp.Msg)
  189. return
  190. }
  191. if resp.Data != nil {
  192. var uploadDocsRes eta_llm_http.RagBaseResponse
  193. err = json.Unmarshal(resp.Data, &uploadDocsRes)
  194. if err != nil {
  195. err = errors.New("上传临时文件失败,err:" + err.Error())
  196. return
  197. }
  198. if uploadDocsRes.Code != 200 {
  199. err = errors.New("上传临时文件失败,err:" + uploadDocsRes.Msg)
  200. return
  201. }
  202. var uploadResult eta_llm_http.UploadDocsResponse
  203. err = json.Unmarshal(uploadDocsRes.Data, &uploadResult)
  204. if len(uploadResult.FiledFiles) > 0 {
  205. utils.FileLog.Warn("上传临时文件失败:", uploadResult.FiledFiles)
  206. }
  207. data = uploadResult
  208. return
  209. }
  210. return
  211. }
  212. func (ds *ETALLMClient) SearchKbDocs(query string, KnowledgeBaseName string) (content interface{}, err error) {
  213. kbReq := eta_llm_http.KbSearchDocsRequest{
  214. Query: query,
  215. KnowledgeBaseName: KnowledgeBaseName,
  216. TopK: 10,
  217. ScoreThreshold: 0.5,
  218. Metadata: struct{}{},
  219. }
  220. body, err := json.Marshal(kbReq)
  221. if err != nil {
  222. return
  223. }
  224. resp, err := ds.DoPost(KNOWLEDGE_BASE_SEARCH_DOCS_API, body)
  225. if !resp.Success {
  226. err = errors.New(resp.Msg)
  227. return
  228. }
  229. if resp.Data != nil {
  230. var kbSearchRes []eta_llm_http.SearchDocsResponse
  231. err = json.Unmarshal(resp.Data, &kbSearchRes)
  232. if err != nil {
  233. err = errors.New("搜索知识库失败")
  234. return
  235. }
  236. content = kbSearchRes
  237. return
  238. }
  239. err = errors.New("搜索知识库失败")
  240. return
  241. }
  242. func init() {
  243. err := llm.Register(llm.ETA_LLM_CLIENT, GetInstance())
  244. if err != nil {
  245. utils.FileLog.Error("注册eta_llm_server服务失败:", err)
  246. }
  247. }
  248. func (ds *ETALLMClient) DoPost(apiUrl string, body []byte) (baseResp eta_llm_http.BaseResponse, err error) {
  249. requestReader := bytes.NewReader(body)
  250. response, err := ds.HttpClient.Post(ds.BaseURL+apiUrl, CONTENT_TYPE_JSON, requestReader)
  251. if err != nil {
  252. return
  253. }
  254. return parseResponse(response)
  255. }
  256. func (ds *ETALLMClient) DoFile(apiUrl string, body []byte, files []*os.File) (baseResp eta_llm_http.BaseResponse, err error) {
  257. var requestBody bytes.Buffer
  258. writer := multipart.NewWriter(&requestBody)
  259. // 添加文件到请求体
  260. for _, file := range files {
  261. filePath, err := writer.CreateFormFile("files", file.Name())
  262. if err != nil {
  263. return baseResp, fmt.Errorf("创建文件表单字段失败: %w", err)
  264. }
  265. _, err = io.Copy(filePath, file)
  266. if err != nil {
  267. return baseResp, fmt.Errorf("写入文件内容失败: %w", err)
  268. }
  269. }
  270. var params = make(map[string]string)
  271. err = json.Unmarshal(body, &params)
  272. if err != nil {
  273. return
  274. }
  275. // 添加其他参数到请求体
  276. for key, value := range params {
  277. err := writer.WriteField(key, value)
  278. if err != nil {
  279. return baseResp, fmt.Errorf("写入表单字段失败: %w", err)
  280. }
  281. }
  282. // 关闭 writer,完成请求体的构建
  283. err = writer.Close()
  284. if err != nil {
  285. return baseResp, fmt.Errorf("关闭 multipart writer 失败: %w", err)
  286. }
  287. request, err := http.NewRequest("POST", ds.BaseURL+apiUrl, &requestBody)
  288. request.Header.Set("Content-Type", writer.FormDataContentType())
  289. response, err := ds.HttpClient.Do(request)
  290. if err != nil {
  291. return
  292. }
  293. return parseResponse(response)
  294. }
  295. func (ds *ETALLMClient) DoStreamPost(apiUrl string, body []byte) (baseResp *http.Response, err error) {
  296. requestReader := bytes.NewReader(body)
  297. return ds.HttpClient.Post(ds.BaseURL+apiUrl, CONTENT_TYPE_JSON, requestReader)
  298. }
  299. func parseResponse(response *http.Response) (baseResp eta_llm_http.BaseResponse, err error) {
  300. defer func() {
  301. _ = response.Body.Close()
  302. }()
  303. baseResp.Ret = response.StatusCode
  304. if response.StatusCode != http.StatusOK {
  305. baseResp.Msg = fmt.Sprintf("请求失败,状态码:%d, 状态信息:%s", response.StatusCode, http.StatusText(response.StatusCode))
  306. return
  307. }
  308. bodyBytes, err := io.ReadAll(response.Body)
  309. if err != nil {
  310. err = fmt.Errorf("读取响应体失败: %w", err)
  311. return
  312. }
  313. baseResp.Success = true
  314. baseResp.Data = bodyBytes
  315. return
  316. }
  317. func ParseStreamResponse(response *http.Response) (contentChan chan string, errChan chan error, closeChan chan struct{}) {
  318. contentChan = make(chan string, 10)
  319. errChan = make(chan error, 10)
  320. closeChan = make(chan struct{})
  321. go func() {
  322. defer close(contentChan)
  323. defer close(errChan)
  324. defer close(closeChan)
  325. scanner := bufio.NewScanner(response.Body)
  326. scanner.Split(bufio.ScanLines)
  327. for scanner.Scan() {
  328. line := scanner.Text()
  329. if line == "" {
  330. continue
  331. }
  332. // 忽略 "ping" 行
  333. if strings.HasPrefix(line, ": ping") {
  334. continue
  335. }
  336. // 去除 "data: " 前缀
  337. if strings.HasPrefix(line, "data: ") {
  338. line = strings.TrimPrefix(line, "data: ")
  339. }
  340. var chunk eta_llm_http.ChunkResponse
  341. if err := json.Unmarshal([]byte(line), &chunk); err != nil {
  342. fmt.Println("解析错误的line:" + line)
  343. errChan <- fmt.Errorf("解析 JSON 块失败: %w", err)
  344. return
  345. }
  346. // 处理每个 chunk
  347. if chunk.Choices != nil && len(chunk.Choices) > 0 {
  348. for _, choice := range chunk.Choices {
  349. if choice.Delta.Content != "" {
  350. contentChan <- choice.Delta.Content
  351. }
  352. }
  353. }
  354. }
  355. if err := scanner.Err(); err != nil {
  356. errChan <- fmt.Errorf("读取响应体失败: %w", err)
  357. return
  358. }
  359. }()
  360. return
  361. }