feat: claude 3.7 model support

This commit is contained in:
Hk-Gosuto
2025-02-25 23:33:44 +08:00
parent b7e26ba18f
commit cdbbcb6ac3
11 changed files with 125 additions and 7 deletions

View File

@@ -25,9 +25,10 @@ import { ANTHROPIC_BASE_URL } from "@/app/constant";
import {
getMessageTextContent,
getWebReferenceMessageTextContent,
isClaudeThinkingModel,
isVisionModel,
} from "@/app/utils";
import { preProcessImageContent, stream } from "@/app/utils/chat";
import { preProcessImageContent, streamWithThink } from "@/app/utils/chat";
import { cloudflareAIGatewayUrl } from "@/app/utils/cloudflare";
import { RequestPayload } from "./openai";
import { fetch } from "@/app/utils/stream";
@@ -62,6 +63,10 @@ export interface AnthropicChatRequest {
top_k?: number; // Only sample from the top K options for each subsequent token.
metadata?: object; // An object describing metadata about the request.
stream?: boolean; // Whether to incrementally stream the response using server-sent events.
thinking?: {
type: "enabled";
budget_tokens: number;
};
}
export interface ChatRequest {
@@ -269,10 +274,9 @@ export class ClaudeApi implements LLMApi {
return res?.content?.[0]?.text;
}
async chat(options: ChatOptions): Promise<void> {
const thinkingModel = isClaudeThinkingModel(options.config.model);
const visionModel = isVisionModel(options.config.model);
const accessStore = useAccessStore.getState();
const shouldStream = !!options.config.stream;
const modelConfig = {
@@ -376,6 +380,21 @@ export class ClaudeApi implements LLMApi {
top_k: 5,
};
// extended-thinking
// https://docs.anthropic.com/zh-CN/docs/build-with-claude/extended-thinking
if (
thinkingModel &&
useChatStore.getState().currentSession().mask.claudeThinking
) {
requestBody.thinking = {
type: "enabled",
budget_tokens: modelConfig.budget_tokens,
};
requestBody.temperature = undefined;
requestBody.top_p = undefined;
requestBody.top_k = undefined;
}
const path = this.path(Anthropic.ChatPath);
const controller = new AbortController();
@@ -390,7 +409,7 @@ export class ClaudeApi implements LLMApi {
// .getAsTools(
// useChatStore.getState().currentSession().mask?.plugin || [],
// );
return stream(
return streamWithThink(
path,
requestBody,
{
@@ -418,8 +437,9 @@ export class ClaudeApi implements LLMApi {
name: string;
};
delta?: {
type: "text_delta" | "input_json_delta";
type: "text_delta" | "input_json_delta" | "thinking_delta";
text?: string;
thinking?: string;
partial_json?: string;
};
index: number;
@@ -447,7 +467,24 @@ export class ClaudeApi implements LLMApi {
runTools[index]["function"]["arguments"] +=
chunkJson?.delta?.partial_json;
}
return chunkJson?.delta?.text;
console.log("chunkJson", chunkJson);
const isThinking = chunkJson?.delta?.type === "thinking_delta";
const content = isThinking
? chunkJson?.delta?.thinking
: chunkJson?.delta?.text;
if (!content || content.trim().length === 0) {
return {
isThinking: false,
content: "",
};
}
return {
isThinking,
content,
};
},
// processToolMessage, include tool_calls message and tool call results
(