From 757cea12706ace6ceb84bf4a5679b6961877cba5 Mon Sep 17 00:00:00 2001 From: chenzh Date: Wed, 18 Oct 2023 16:13:58 +0800 Subject: [PATCH 1/2] fix stream, docs upload --- modules/llm_chat/resty.go | 14 ++++++-------- services/llm_chat/llm_chat.go | 3 +++ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/modules/llm_chat/resty.go b/modules/llm_chat/resty.go index 1e0141880e..e1b50f799e 100644 --- a/modules/llm_chat/resty.go +++ b/modules/llm_chat/resty.go @@ -489,7 +489,6 @@ func UploadDocs(modelName string, form api.LLMChatUploadForm) (*map[string]inter var requestBody bytes.Buffer writer := multipart.NewWriter(&requestBody) writer.WriteField("knowledge_base_name", form.KnowledgeBaseName) - defer writer.Close() for _, fileHeader := range form.Files { filename := fileHeader.Filename file, err := fileHeader.Open() @@ -506,9 +505,9 @@ func UploadDocs(modelName string, form api.LLMChatUploadForm) (*map[string]inter _, err = io.Copy(part, file) log.Info("########## Added to writer: %s\n", filename) } - log.Info("################ whole request body: %s\n", requestBody.String()) + writer.Close() + //fmt.Printf("################ whole request body: %s\n", requestBody.String()) - // Create the HTTP request endpoint := GetEndpoint(modelName) req, err := http.NewRequest("POST", endpoint+urlKnowledgeBaseUploadDoc, &requestBody) if err != nil { @@ -516,10 +515,8 @@ func UploadDocs(modelName string, form api.LLMChatUploadForm) (*map[string]inter return nil, err } - // Set the Content-Type header for multipart/form-data req.Header.Set("Content-Type", writer.FormDataContentType()) - // Perform the request client := &http.Client{} resp, err := client.Do(req) if err != nil { @@ -530,15 +527,16 @@ func UploadDocs(modelName string, form api.LLMChatUploadForm) (*map[string]inter log.Info("############## Response Status:", resp.Status) var errResult map[string]interface{} - if resp.StatusCode == http.StatusUnprocessableEntity { + //if resp.StatusCode == http.StatusUnprocessableEntity { + if resp.StatusCode != http.StatusOK { bodyBytes, err := io.ReadAll(resp.Body) if err != nil { log.Info("Error reading response body:", err) return nil, err } err = json.Unmarshal(bodyBytes, &errResult) - log.Info("##############errResult: %+v\n", errResult) - return nil, fmt.Errorf("upload_docs(): %v", errResult) + log.Error("##############upload_docs() errResult: %+v\n", errResult) + return &errResult, nil } log.Info("############## Response Body: %+v\n", resp.Body) diff --git a/services/llm_chat/llm_chat.go b/services/llm_chat/llm_chat.go index f3f3aeb145..bbdfb5c7c8 100644 --- a/services/llm_chat/llm_chat.go +++ b/services/llm_chat/llm_chat.go @@ -127,6 +127,7 @@ func StreamLLMChatService(ctx *context.Context, data api.LLMChatMessage, chat *m var answer string ctx.Resp.Header().Set("Content-Type", "application/octet-stream; charset=utf-8") + ctx.Resp.Header().Set(" X-Accel-Buffering ", "no") //call baiduai api to check legality of query if setting.LLM_CHAT_API.LEGAL_CHECK { @@ -220,6 +221,7 @@ func StreamKBChatService(ctx *context.Context, data api.KBChatMessage, chat *mod var answer string var docs string ctx.Resp.Header().Set("Content-Type", "application/octet-stream; charset=utf-8") + ctx.Resp.Header().Set(" X-Accel-Buffering ", "no") //call baiduai api to check legality of query if setting.LLM_CHAT_API.LEGAL_CHECK { @@ -475,6 +477,7 @@ func RecreateVectorStoreService(ctx *context.Context) { go llmChatAPI.KBRecreateVectorStore(data, resultChan, errChan, done) ctx.Resp.Header().Set("Content-Type", "application/octet-stream; charset=utf-8") + ctx.Resp.Header().Set(" X-Accel-Buffering ", "no") for { select { -- 2.34.1 From d52be809ca76b62abd29f0db4fd1632ec96ec6a2 Mon Sep 17 00:00:00 2001 From: chenzh Date: Wed, 18 Oct 2023 16:19:56 +0800 Subject: [PATCH 2/2] fix --- services/llm_chat/llm_chat.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/llm_chat/llm_chat.go b/services/llm_chat/llm_chat.go index bbdfb5c7c8..43ea3c8f86 100644 --- a/services/llm_chat/llm_chat.go +++ b/services/llm_chat/llm_chat.go @@ -127,7 +127,7 @@ func StreamLLMChatService(ctx *context.Context, data api.LLMChatMessage, chat *m var answer string ctx.Resp.Header().Set("Content-Type", "application/octet-stream; charset=utf-8") - ctx.Resp.Header().Set(" X-Accel-Buffering ", "no") + ctx.Resp.Header().Set("X-Accel-Buffering", "no") //call baiduai api to check legality of query if setting.LLM_CHAT_API.LEGAL_CHECK { @@ -221,7 +221,7 @@ func StreamKBChatService(ctx *context.Context, data api.KBChatMessage, chat *mod var answer string var docs string ctx.Resp.Header().Set("Content-Type", "application/octet-stream; charset=utf-8") - ctx.Resp.Header().Set(" X-Accel-Buffering ", "no") + ctx.Resp.Header().Set("X-Accel-Buffering", "no") //call baiduai api to check legality of query if setting.LLM_CHAT_API.LEGAL_CHECK { @@ -477,7 +477,7 @@ func RecreateVectorStoreService(ctx *context.Context) { go llmChatAPI.KBRecreateVectorStore(data, resultChan, errChan, done) ctx.Resp.Header().Set("Content-Type", "application/octet-stream; charset=utf-8") - ctx.Resp.Header().Set(" X-Accel-Buffering ", "no") + ctx.Resp.Header().Set("X-Accel-Buffering", "no") for { select { -- 2.34.1