From dc96278fda047a6e1260394668c07e66beeb239e Mon Sep 17 00:00:00 2001 From: Zhang Minghan Date: Tue, 24 Oct 2023 20:28:29 +0800 Subject: [PATCH] fix stream output --- adapter/chatgpt/chat.go | 2 +- adapter/claude/chat.go | 7 +++++-- app/src/conf.ts | 2 +- app/src/store/quota.ts | 4 ++-- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/adapter/chatgpt/chat.go b/adapter/chatgpt/chat.go index 46ddcf9b..d10e095d 100644 --- a/adapter/chatgpt/chat.go +++ b/adapter/chatgpt/chat.go @@ -57,7 +57,7 @@ func (c *ChatInstance) ProcessLine(data string) string { var form *ChatStreamResponse if form = utils.UnmarshalForm[ChatStreamResponse](item); form == nil { - if form = utils.UnmarshalForm[ChatStreamResponse](item); form == nil { + if form = utils.UnmarshalForm[ChatStreamResponse](item[:len(item)-1]); form == nil { fmt.Println(fmt.Sprintf("chatgpt error: cannot parse response: %s", item)) return "" } diff --git a/adapter/claude/chat.go b/adapter/claude/chat.go index 595b8c6b..5410adaf 100644 --- a/adapter/claude/chat.go +++ b/adapter/claude/chat.go @@ -77,15 +77,18 @@ func (c *ChatInstance) CreateStreamChatRequest(props *ChatProps, hook globals.Ho // response example: // // event:completion - // {"completion":"么","stop_reason":null,"model":"claude-1.3","stop":null,"log_id":"605b37b2bbab7a557b811ce47d8beeab959cbab22e46b515cb15e3f00afcbe24"} // data:{"completion":"!","stop_reason":null,"model":"claude-2.0","stop":null,"log_id":"f5f659a5807419c94cfac4a9f2f79a66e95733975714ce7f00e30689dd136b02"} - if !strings.HasPrefix(data, "data:") { + if !strings.HasPrefix(data, "data:") && strings.HasPrefix(data, "event:") { return nil } else { data = strings.TrimSpace(strings.TrimPrefix(data, "data:")) } + if len(data) == 0 { + return nil + } + if form := utils.UnmarshalForm[ChatResponse](data); form != nil { if err := hook(form.Completion); err != nil { return err diff --git a/app/src/conf.ts b/app/src/conf.ts index 206897f2..9cf2523f 100644 --- a/app/src/conf.ts +++ b/app/src/conf.ts @@ -1,7 +1,7 @@ import axios from "axios"; import { Model } from "./conversation/types.ts"; -export const version = "3.5.4"; +export const version = "3.5.5"; export const dev: boolean = window.location.hostname === "localhost"; export const deploy: boolean = true; export let rest_api: string = "http://localhost:8094"; diff --git a/app/src/store/quota.ts b/app/src/store/quota.ts index e72dc928..22c4d22c 100644 --- a/app/src/store/quota.ts +++ b/app/src/store/quota.ts @@ -52,7 +52,7 @@ export const quotaSelector = (state: RootState): string => const refreshQuota = async (dispatch: AppDispatch) => { const current = new Date().getTime(); //@ts-ignore - if (window.hasOwnProperty("quota") && current - window.quota < 2500) return; //@ts-ignore + if (window.hasOwnProperty("quota") && current - window.quota < 5000) return; //@ts-ignore window.quota = current; const response = await axios.get("/quota"); @@ -60,6 +60,6 @@ const refreshQuota = async (dispatch: AppDispatch) => { }; export const refreshQuotaTask = (dispatch: AppDispatch) => { - setInterval(() => refreshQuota(dispatch), 5000); + setInterval(() => refreshQuota(dispatch), 8000); refreshQuota(dispatch).then(); };