Merge pull request #21 from heimoshuiyu/master

sync 0124
This commit is contained in:
Zhenghao Wu
2025-01-24 12:28:03 +00:00
committed by GitHub
8 changed files with 88 additions and 13 deletions

View File

@@ -22,6 +22,7 @@ export interface ToolCall {
export interface Message { export interface Message {
role: "system" | "user" | "assistant" | "tool"; role: "system" | "user" | "assistant" | "tool";
content: string | MessageDetail[]; content: string | MessageDetail[];
reasoning_content?: string | null;
name?: "example_user" | "example_assistant"; name?: "example_user" | "example_assistant";
tool_calls?: ToolCall[]; tool_calls?: ToolCall[];
tool_call_id?: string; tool_call_id?: string;
@@ -30,6 +31,7 @@ export interface Message {
interface Delta { interface Delta {
role?: string; role?: string;
content?: string; content?: string;
reasoning_content?: string;
tool_calls?: ToolCall[]; tool_calls?: ToolCall[];
} }
@@ -162,7 +164,9 @@ class Chat {
top_p: number; top_p: number;
enable_top_p: boolean; enable_top_p: boolean;
presence_penalty: number; presence_penalty: number;
presence_penalty_enabled: boolean;
frequency_penalty: number; frequency_penalty: number;
frequency_penalty_enabled: boolean;
json_mode: boolean; json_mode: boolean;
constructor( constructor(
@@ -181,7 +185,9 @@ class Chat {
top_p = 1, top_p = 1,
enable_top_p = false, enable_top_p = false,
presence_penalty = 0, presence_penalty = 0,
presence_penalty_enabled = false,
frequency_penalty = 0, frequency_penalty = 0,
frequency_penalty_enabled = false,
json_mode = false, json_mode = false,
} = {} } = {}
) { ) {
@@ -201,7 +207,9 @@ class Chat {
this.top_p = top_p; this.top_p = top_p;
this.enable_top_p = enable_top_p; this.enable_top_p = enable_top_p;
this.presence_penalty = presence_penalty; this.presence_penalty = presence_penalty;
this.presence_penalty_enabled = presence_penalty_enabled;
this.frequency_penalty = frequency_penalty; this.frequency_penalty = frequency_penalty;
this.frequency_penalty_enabled = frequency_penalty_enabled;
this.json_mode = json_mode; this.json_mode = json_mode;
} }
@@ -239,8 +247,6 @@ class Chat {
model: this.model, model: this.model,
messages, messages,
stream, stream,
presence_penalty: this.presence_penalty,
frequency_penalty: this.frequency_penalty,
}; };
if (stream) { if (stream) {
body["stream_options"] = { body["stream_options"] = {
@@ -256,6 +262,12 @@ class Chat {
if (this.enable_max_gen_tokens) { if (this.enable_max_gen_tokens) {
body["max_tokens"] = this.max_gen_tokens; body["max_tokens"] = this.max_gen_tokens;
} }
if (this.presence_penalty_enabled) {
body["presence_penalty"] = this.presence_penalty;
}
if (this.frequency_penalty_enabled) {
body["frequency_penalty"] = this.frequency_penalty;
}
if (this.json_mode) { if (this.json_mode) {
body["response_format"] = { body["response_format"] = {
type: "json_object", type: "json_object",

View File

@@ -208,6 +208,7 @@ export function ImageGenDrawer({ disableFactor }: Props) {
audio: null, audio: null,
logprobs: null, logprobs: null,
response_model_name: imageGenModel, response_model_name: imageGenModel,
reasoning_content: null,
}); });
setChatStore({ ...chatStore }); setChatStore({ ...chatStore });

View File

@@ -408,6 +408,30 @@ const Number = (props: {
}} }}
/> />
)} )}
{props.field === "presence_penalty" && (
<Checkbox
checked={chatStore.presence_penalty_enabled}
onCheckedChange={() => {
const newChatStore = { ...chatStore };
newChatStore.presence_penalty_enabled =
!newChatStore.presence_penalty_enabled;
setChatStore({ ...newChatStore });
}}
/>
)}
{props.field === "frequency_penalty" && (
<Checkbox
checked={chatStore.frequency_penalty_enabled}
onCheckedChange={() => {
const newChatStore = { ...chatStore };
newChatStore.frequency_penalty_enabled =
!newChatStore.frequency_penalty_enabled;
setChatStore({ ...newChatStore });
}}
/>
)}
</Label> </Label>
<Input <Input

View File

@@ -87,12 +87,15 @@ const Navbar: React.FC = () => {
<MenubarItem> <MenubarItem>
<ReceiptIcon className="w-4 h-4 mr-2" /> <ReceiptIcon className="w-4 h-4 mr-2" />
Price:{" "} Price:{" "}
{models[chatStore.model]?.price?.prompt * 1000 * 1000}$ {models[chatStore.model]?.price?.prompt *
/ 1M input tokens 1000 *
1000}$ / 1M input tokens
</MenubarItem> </MenubarItem>
<MenubarItem> <MenubarItem>
<WalletIcon className="w-4 h-4 mr-2" /> <WalletIcon className="w-4 h-4 mr-2" />
Total: {getTotalCost().toFixed(2)}$ Total: {getTotalCost().toFixed(
2
)}$
</MenubarItem> </MenubarItem>
<MenubarItem> <MenubarItem>
<ArrowUpDownIcon className="w-4 h-4 mr-2" /> <ArrowUpDownIcon className="w-4 h-4 mr-2" />
@@ -112,7 +115,9 @@ const Navbar: React.FC = () => {
</MenubarItem> </MenubarItem>
<MenubarItem> <MenubarItem>
<ScissorsIcon className="w-4 h-4 mr-2" /> <ScissorsIcon className="w-4 h-4 mr-2" />
{chatStore.postBeginIndex} / {chatStore.history.length} {
chatStore.postBeginIndex
} / {chatStore.history.length}
</MenubarItem> </MenubarItem>
<MenubarSeparator /> <MenubarSeparator />
<MenubarItem disabled> <MenubarItem disabled>

View File

@@ -74,6 +74,7 @@ const AddToolMsg = (props: {
audio: null, audio: null,
logprobs: null, logprobs: null,
response_model_name: null, response_model_name: null,
reasoning_content: null,
}); });
setChatStore({ ...chatStore }); setChatStore({ ...chatStore });
setNewToolCallID(""); setNewToolCallID("");

View File

@@ -80,8 +80,9 @@ export default function ChatBOX() {
const _completeWithStreamMode = async ( const _completeWithStreamMode = async (
response: Response response: Response
): Promise<Usage> => { ): Promise<Usage> => {
let responseTokenCount = 0; let responseTokenCount = 0; // including reasoning content and normal content
const allChunkMessage: string[] = []; const allChunkMessage: string[] = [];
const allReasoningContentChunk: string[] = [];
const allChunkTool: ToolCall[] = []; const allChunkTool: ToolCall[] = [];
setShowGenerating(true); setShowGenerating(true);
const logprobs: Logprobs = { const logprobs: Logprobs = {
@@ -110,7 +111,13 @@ export default function ChatBOX() {
console.log(c?.delta?.content, logprob); console.log(c?.delta?.content, logprob);
} }
allChunkMessage.push(c?.delta?.content ?? ""); if (c?.delta?.content) {
allChunkMessage.push(c?.delta?.content ?? "");
}
if (c?.delta?.reasoning_content) {
allReasoningContentChunk.push(c?.delta?.reasoning_content ?? "");
}
const tool_calls = c?.delta?.tool_calls; const tool_calls = c?.delta?.tool_calls;
if (tool_calls) { if (tool_calls) {
for (const tool_call of tool_calls) { for (const tool_call of tool_calls) {
@@ -142,7 +149,12 @@ export default function ChatBOX() {
} }
} }
setGeneratingMessage( setGeneratingMessage(
allChunkMessage.join("") + (allReasoningContentChunk.length
? "----------\nreasoning:\n" +
allReasoningContentChunk.join("") +
"\n----------\n"
: "") +
allChunkMessage.join("") +
allChunkTool.map((tool) => { allChunkTool.map((tool) => {
return `Tool Call ID: ${tool.id}\nType: ${tool.type}\nFunction: ${tool.function.name}\nArguments: ${tool.function.arguments}`; return `Tool Call ID: ${tool.id}\nType: ${tool.type}\nFunction: ${tool.function.name}\nArguments: ${tool.function.arguments}`;
}) })
@@ -150,13 +162,17 @@ export default function ChatBOX() {
} }
setShowGenerating(false); setShowGenerating(false);
const content = allChunkMessage.join(""); const content = allChunkMessage.join("");
const reasoning_content = allReasoningContentChunk.join("");
console.log("save logprobs", logprobs); console.log("save logprobs", logprobs);
const newMsg: ChatStoreMessage = { const newMsg: ChatStoreMessage = {
role: "assistant", role: "assistant",
content, content,
reasoning_content,
hide: false, hide: false,
token: responseTokenCount, token:
responseTokenCount -
(usage?.completion_tokens_details?.reasoning_tokens ?? 0),
example: false, example: false,
audio: null, audio: null,
logprobs, logprobs,
@@ -205,12 +221,15 @@ export default function ChatBOX() {
content: msg.content, content: msg.content,
tool_calls: msg.tool_calls, tool_calls: msg.tool_calls,
hide: false, hide: false,
token: token: data.usage?.completion_tokens_details
data.usage.completion_tokens ?? calculate_token_length(msg.content), ? data.usage.completion_tokens -
data.usage.completion_tokens_details.reasoning_tokens
: (data.usage.completion_tokens ?? calculate_token_length(msg.content)),
example: false, example: false,
audio: null, audio: null,
logprobs: data.choices[0]?.logprobs, logprobs: data.choices[0]?.logprobs,
response_model_name: data.model, response_model_name: data.model,
reasoning_content: data.choices[0]?.message?.reasoning_content ?? null,
}); });
setShowGenerating(false); setShowGenerating(false);
@@ -238,7 +257,9 @@ export default function ChatBOX() {
client.top_p = chatStore.top_p; client.top_p = chatStore.top_p;
client.enable_top_p = chatStore.top_p_enabled; client.enable_top_p = chatStore.top_p_enabled;
client.frequency_penalty = chatStore.frequency_penalty; client.frequency_penalty = chatStore.frequency_penalty;
client.frequency_penalty_enabled = chatStore.frequency_penalty_enabled;
client.presence_penalty = chatStore.presence_penalty; client.presence_penalty = chatStore.presence_penalty;
client.presence_penalty_enabled = chatStore.presence_penalty_enabled;
client.json_mode = chatStore.json_mode; client.json_mode = chatStore.json_mode;
client.messages = chatStore.history client.messages = chatStore.history
// only copy non hidden message // only copy non hidden message
@@ -373,6 +394,7 @@ export default function ChatBOX() {
audio: null, audio: null,
logprobs: null, logprobs: null,
response_model_name: null, response_model_name: null,
reasoning_content: null,
}); });
// manually calculate token length // manually calculate token length

View File

@@ -26,7 +26,9 @@ export interface ChatStore {
top_p: number; top_p: number;
top_p_enabled: boolean; top_p_enabled: boolean;
presence_penalty: number; presence_penalty: number;
presence_penalty_enabled: boolean;
frequency_penalty: number; frequency_penalty: number;
frequency_penalty_enabled: boolean;
develop_mode: boolean; develop_mode: boolean;
whisper_api: string; whisper_api: string;
whisper_key: string; whisper_key: string;
@@ -72,6 +74,7 @@ export interface ChatStoreMessage {
role: "system" | "user" | "assistant" | "tool"; role: "system" | "user" | "assistant" | "tool";
content: string | MessageDetail[]; content: string | MessageDetail[];
reasoning_content: string | null;
name?: "example_user" | "example_assistant"; name?: "example_user" | "example_assistant";
tool_calls?: ToolCall[]; tool_calls?: ToolCall[];
tool_call_id?: string; tool_call_id?: string;

View File

@@ -18,7 +18,9 @@ interface NewChatStoreOptions {
top_p?: number; top_p?: number;
top_p_enabled?: boolean; top_p_enabled?: boolean;
presence_penalty?: number; presence_penalty?: number;
presence_penalty_enabled?: boolean;
frequency_penalty?: number; frequency_penalty?: number;
frequency_penalty_enabled?: boolean;
dev?: boolean; dev?: boolean;
whisper_api?: string; whisper_api?: string;
whisper_key?: string; whisper_key?: string;
@@ -33,6 +35,7 @@ interface NewChatStoreOptions {
image_gen_key?: string; image_gen_key?: string;
json_mode?: boolean; json_mode?: boolean;
logprobs?: boolean; logprobs?: boolean;
maxTokens?: number;
} }
export const newChatStore = (options: NewChatStoreOptions): ChatStore => { export const newChatStore = (options: NewChatStoreOptions): ChatStore => {
@@ -50,7 +53,9 @@ export const newChatStore = (options: NewChatStoreOptions): ChatStore => {
maxTokens: getDefaultParams( maxTokens: getDefaultParams(
"max", "max",
models[getDefaultParams("model", options.model ?? DefaultModel)] models[getDefaultParams("model", options.model ?? DefaultModel)]
?.maxToken ?? 2048 ?.maxToken ??
options.maxTokens ??
2048
), ),
maxGenTokens: 2048, maxGenTokens: 2048,
maxGenTokens_enabled: false, maxGenTokens_enabled: false,
@@ -67,7 +72,9 @@ export const newChatStore = (options: NewChatStoreOptions): ChatStore => {
top_p: options.top_p ?? 1, top_p: options.top_p ?? 1,
top_p_enabled: options.top_p_enabled ?? false, top_p_enabled: options.top_p_enabled ?? false,
presence_penalty: options.presence_penalty ?? 0, presence_penalty: options.presence_penalty ?? 0,
presence_penalty_enabled: options.presence_penalty_enabled ?? false,
frequency_penalty: options.frequency_penalty ?? 0, frequency_penalty: options.frequency_penalty ?? 0,
frequency_penalty_enabled: options.frequency_penalty_enabled ?? false,
develop_mode: getDefaultParams("dev", options.dev ?? false), develop_mode: getDefaultParams("dev", options.dev ?? false),
whisper_api: getDefaultParams( whisper_api: getDefaultParams(
"whisper-api", "whisper-api",