#4806 fix stream, docs upload

Merged
ychao_1983 merged 3 commits from fix-4554 into V20231018 6 months ago
  1. +6
    -8
      modules/llm_chat/resty.go
  2. +3
    -0
      services/llm_chat/llm_chat.go

+ 6
- 8
modules/llm_chat/resty.go View File

@@ -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)



+ 3
- 0
services/llm_chat/llm_chat.go View File

@@ -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 {


Loading…
Cancel
Save