eta_llm_client.go 11 KB

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