From 082bd0695a9d1aadb8ab4eb2d9636bc6ba24ff04 Mon Sep 17 00:00:00 2001
From: Zhang Minghan
Date: Sun, 18 Feb 2024 17:34:38 +0800
Subject: [PATCH] feat: `tools_call` finish reason
---
app/src/components/ThemeProvider.tsx | 6 +++++-
app/src/dialogs/SubscriptionDialog.tsx | 4 ++--
manager/chat_completions.go | 25 ++++++++++++++++++++++---
3 files changed, 29 insertions(+), 6 deletions(-)
diff --git a/app/src/components/ThemeProvider.tsx b/app/src/components/ThemeProvider.tsx
index 4e77afc0..6cd380f0 100644
--- a/app/src/components/ThemeProvider.tsx
+++ b/app/src/components/ThemeProvider.tsx
@@ -5,6 +5,8 @@ import { Button } from "./ui/button";
import { getMemory, setMemory } from "@/utils/memory.ts";
import { themeEvent } from "@/events/theme.ts";
+const defaultTheme = "dark";
+
type Theme = "dark" | "light" | "system";
type ThemeProviderProps = {
@@ -37,7 +39,9 @@ const initialState: ThemeProviderState = {
activeTheme(theme);
},
toggleTheme: () => {
- const theme = getMemory("theme") as Theme;
+ const key = getMemory("theme");
+ const theme = (key.length > 0 ? key : defaultTheme) as Theme;
+
activeTheme(theme === "dark" ? "light" : "dark");
},
};
diff --git a/app/src/dialogs/SubscriptionDialog.tsx b/app/src/dialogs/SubscriptionDialog.tsx
index 0330bb4d..b8c2e308 100644
--- a/app/src/dialogs/SubscriptionDialog.tsx
+++ b/app/src/dialogs/SubscriptionDialog.tsx
@@ -144,9 +144,9 @@ function SubscriptionDialog() {
{!relayPlan && (
-
+
{t("sub.plan-not-support-relay")}
)}
diff --git a/manager/chat_completions.go b/manager/chat_completions.go
index 6a80492c..57e80348 100644
--- a/manager/chat_completions.go
+++ b/manager/chat_completions.go
@@ -16,6 +16,11 @@ import (
"time"
)
+const (
+ ReasonStop = "stop"
+ ReasonToolsCall = "tools_call"
+)
+
func supportRelayPlan() bool {
return channel.SystemInstance.SupportRelayPlan()
}
@@ -111,6 +116,8 @@ func sendTranshipmentResponse(c *gin.Context, form RelayForm, messages []globals
CollectQuota(c, user, buffer, plan, err)
}
+ tools := buffer.GetToolCalls()
+
c.JSON(http.StatusOK, RelayResponse{
Id: fmt.Sprintf("chatcmpl-%s", id),
Object: "chat.completion",
@@ -122,10 +129,10 @@ func sendTranshipmentResponse(c *gin.Context, form RelayForm, messages []globals
Message: globals.Message{
Role: globals.Assistant,
Content: buffer.Read(),
- ToolCalls: buffer.GetToolCalls(),
+ ToolCalls: tools,
FunctionCall: buffer.GetFunctionCall(),
},
- FinishReason: "stop",
+ FinishReason: utils.Multi(tools != nil, ReasonToolsCall, ReasonStop),
},
},
Usage: Usage{
@@ -137,6 +144,18 @@ func sendTranshipmentResponse(c *gin.Context, form RelayForm, messages []globals
})
}
+func getFinishReason(data *globals.Chunk, end bool) interface{} {
+ if end {
+ return nil
+ }
+
+ if data.ToolCall != nil {
+ return ReasonToolsCall
+ }
+
+ return ReasonStop
+}
+
func getStreamTranshipmentForm(id string, created int64, form RelayForm, data *globals.Chunk, buffer *utils.Buffer, end bool, err error) RelayStreamResponse {
return RelayStreamResponse{
Id: fmt.Sprintf("chatcmpl-%s", id),
@@ -152,7 +171,7 @@ func getStreamTranshipmentForm(id string, created int64, form RelayForm, data *g
ToolCalls: data.ToolCall,
FunctionCall: data.FunctionCall,
},
- FinishReason: utils.Multi[interface{}](end, "stop", nil),
+ FinishReason: getFinishReason(data, end),
},
},
Usage: Usage{