eta_llm_client.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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, 120),
  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, 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. kbReq := eta_llm_http.DocumentChatRequest{
  146. ModelName: ds.LlmModel,
  147. Query: query,
  148. KnowledgeId: KnowledgeId,
  149. History: ChatHistory,
  150. TopK: 20,
  151. ScoreThreshold: 2,
  152. Stream: false,
  153. Temperature: 0.01,
  154. MaxTokens: 0,
  155. PromptName: DEFALUT_PROMPT_NAME,
  156. }
  157. body, err := json.Marshal(kbReq)
  158. if err != nil {
  159. utils.FileLog.Error("内容生成失败,序列化请求参数失败,err", err.Error())
  160. err = fmt.Errorf("内容生成失败,序列化请求参数失败,err:%v", err)
  161. return
  162. }
  163. return ds.DoPost(DOCUMENT_CHAT_API, body)
  164. }
  165. func (ds *ETALLMClient) UploadFileToTemplate(files []*os.File, param map[string]interface{}) (data interface{}, err error) {
  166. var pervId string
  167. if value, ok := param["PrevId"]; ok {
  168. pervId = value.(string)
  169. }
  170. docReq := eta_llm_http.UploadTempDocsRequest{
  171. ChunkOverlap: "150",
  172. ChunkSize: "750",
  173. PrevId: pervId,
  174. ZhTitleEnhance: "false",
  175. }
  176. body, err := json.Marshal(docReq)
  177. if err != nil {
  178. return
  179. }
  180. resp, err := ds.DoFile(UPLOAD_TEMP_DOCS_API, body, files)
  181. if !resp.Success {
  182. err = errors.New(resp.Msg)
  183. return
  184. }
  185. if resp.Data != nil {
  186. var uploadDocsRes eta_llm_http.RagBaseResponse
  187. err = json.Unmarshal(resp.Data, &uploadDocsRes)
  188. if err != nil {
  189. err = errors.New("上传临时文件失败,err:" + err.Error())
  190. return
  191. }
  192. if uploadDocsRes.Code != 200 {
  193. err = errors.New("上传临时文件失败,err:" + uploadDocsRes.Msg)
  194. return
  195. }
  196. var uploadResult eta_llm_http.UploadDocsResponse
  197. err = json.Unmarshal(uploadDocsRes.Data, &uploadResult)
  198. if len(uploadResult.FiledFiles) > 0 {
  199. utils.FileLog.Warn("上传临时文件失败:", uploadResult.FiledFiles)
  200. }
  201. data = uploadResult
  202. return
  203. }
  204. return
  205. }
  206. func (ds *ETALLMClient) SearchKbDocs(query string, KnowledgeBaseName string) (content interface{}, err error) {
  207. kbReq := eta_llm_http.KbSearchDocsRequest{
  208. Query: query,
  209. KnowledgeBaseName: KnowledgeBaseName,
  210. TopK: 10,
  211. ScoreThreshold: 0.5,
  212. Metadata: struct{}{},
  213. }
  214. body, err := json.Marshal(kbReq)
  215. if err != nil {
  216. return
  217. }
  218. resp, err := ds.DoPost(KNOWLEDGE_BASE_SEARCH_DOCS_API, body)
  219. if !resp.Success {
  220. err = errors.New(resp.Msg)
  221. return
  222. }
  223. if resp.Data != nil {
  224. var kbSearchRes []eta_llm_http.SearchDocsResponse
  225. err = json.Unmarshal(resp.Data, &kbSearchRes)
  226. if err != nil {
  227. err = errors.New("搜索知识库失败")
  228. return
  229. }
  230. content = kbSearchRes
  231. return
  232. }
  233. err = errors.New("搜索知识库失败")
  234. return
  235. }
  236. func init() {
  237. err := llm.Register(llm.ETA_LLM_CLIENT, GetInstance())
  238. if err != nil {
  239. utils.FileLog.Error("注册eta_llm_server服务失败:", err)
  240. }
  241. }
  242. func (ds *ETALLMClient) DoPost(apiUrl string, body []byte) (baseResp eta_llm_http.BaseResponse, err error) {
  243. requestReader := bytes.NewReader(body)
  244. response, err := ds.HttpClient.Post(ds.BaseURL+apiUrl, CONTENT_TYPE_JSON, requestReader)
  245. if err != nil {
  246. return
  247. }
  248. return parseResponse(response)
  249. }
  250. func (ds *ETALLMClient) DoFile(apiUrl string, body []byte, files []*os.File) (baseResp eta_llm_http.BaseResponse, err error) {
  251. var requestBody bytes.Buffer
  252. writer := multipart.NewWriter(&requestBody)
  253. // 添加文件到请求体
  254. for _, file := range files {
  255. filePath, err := writer.CreateFormFile("files", file.Name())
  256. if err != nil {
  257. return baseResp, fmt.Errorf("创建文件表单字段失败: %w", err)
  258. }
  259. _, err = io.Copy(filePath, file)
  260. if err != nil {
  261. return baseResp, fmt.Errorf("写入文件内容失败: %w", err)
  262. }
  263. }
  264. var params = make(map[string]string)
  265. err = json.Unmarshal(body, &params)
  266. if err != nil {
  267. return
  268. }
  269. // 添加其他参数到请求体
  270. for key, value := range params {
  271. err := writer.WriteField(key, value)
  272. if err != nil {
  273. return baseResp, fmt.Errorf("写入表单字段失败: %w", err)
  274. }
  275. }
  276. // 关闭 writer,完成请求体的构建
  277. err = writer.Close()
  278. if err != nil {
  279. return baseResp, fmt.Errorf("关闭 multipart writer 失败: %w", err)
  280. }
  281. request, err := http.NewRequest("POST", ds.BaseURL+apiUrl, &requestBody)
  282. request.Header.Set("Content-Type", writer.FormDataContentType())
  283. response, err := ds.HttpClient.Do(request)
  284. if err != nil {
  285. return
  286. }
  287. return parseResponse(response)
  288. }
  289. func (ds *ETALLMClient) DoStreamPost(apiUrl string, body []byte) (baseResp *http.Response, err error) {
  290. requestReader := bytes.NewReader(body)
  291. return ds.HttpClient.Post(ds.BaseURL+apiUrl, CONTENT_TYPE_JSON, requestReader)
  292. }
  293. func parseResponse(response *http.Response) (baseResp eta_llm_http.BaseResponse, err error) {
  294. defer func() {
  295. _ = response.Body.Close()
  296. }()
  297. baseResp.Ret = response.StatusCode
  298. if response.StatusCode != http.StatusOK {
  299. baseResp.Msg = fmt.Sprintf("请求失败,状态码:%d, 状态信息:%s", response.StatusCode, http.StatusText(response.StatusCode))
  300. return
  301. }
  302. bodyBytes, err := io.ReadAll(response.Body)
  303. if err != nil {
  304. err = fmt.Errorf("读取响应体失败: %w", err)
  305. return
  306. }
  307. baseResp.Success = true
  308. baseResp.Data = bodyBytes
  309. return
  310. }
  311. func ParseStreamResponse(response *http.Response) (contentChan chan string, errChan chan error, closeChan chan struct{}) {
  312. contentChan = make(chan string, 10)
  313. errChan = make(chan error, 10)
  314. closeChan = make(chan struct{})
  315. go func() {
  316. defer close(contentChan)
  317. defer close(errChan)
  318. defer close(closeChan)
  319. scanner := bufio.NewScanner(response.Body)
  320. scanner.Split(bufio.ScanLines)
  321. for scanner.Scan() {
  322. line := scanner.Text()
  323. if line == "" {
  324. continue
  325. }
  326. // 忽略 "ping" 行
  327. if strings.HasPrefix(line, ": ping") {
  328. continue
  329. }
  330. // 去除 "data: " 前缀
  331. if strings.HasPrefix(line, "data: ") {
  332. line = strings.TrimPrefix(line, "data: ")
  333. }
  334. var chunk eta_llm_http.ChunkResponse
  335. if err := json.Unmarshal([]byte(line), &chunk); err != nil {
  336. fmt.Println("解析错误的line:" + line)
  337. errChan <- fmt.Errorf("解析 JSON 块失败: %w", err)
  338. return
  339. }
  340. // 处理每个 chunk
  341. if chunk.Choices != nil && len(chunk.Choices) > 0 {
  342. for _, choice := range chunk.Choices {
  343. if choice.Delta.Content != "" {
  344. contentChan <- choice.Delta.Content
  345. }
  346. }
  347. }
  348. }
  349. if err := scanner.Err(); err != nil {
  350. errChan <- fmt.Errorf("读取响应体失败: %w", err)
  351. return
  352. }
  353. }()
  354. return
  355. }