Skip to content

Commit f8f23d6

Browse files
committed
Fix reasoning tokens in Anthropic API
1 parent c0f917d commit f8f23d6

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

src/api/Providers/special/custom_openai.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ export const CustomOpenAIProvider = {
4949
try {
5050
const { getCustomAPIInfo } = await import("#root/src/api/providers_custom.js");
5151
apiData = await getCustomAPIInfo(url);
52+
this.info.type = this.info.type ?? apiData.type;
5253
// eslint-disable-next-line no-empty
5354
} catch {}
5455

5556
// Initialize
56-
const api_url = getChatCompletionsURL(url, this.info?.type ?? apiData.type);
57+
const api_url = getChatCompletionsURL(url, this.info?.type);
5758

5859
apiKey = apiKey || apiData.apiKey;
5960
// The order for applying configs: reqBody -> apiData.config -> reqConfig
@@ -103,8 +104,7 @@ export const CustomOpenAIProvider = {
103104

104105
try {
105106
let json = JSON.parse(line);
106-
let chunk = json["choices"][0]["delta"] ?? json["choices"][0]["message"];
107-
chunk = chunk?.content;
107+
let chunk = this.getChunk(json);
108108
if (chunk) {
109109
yield chunk;
110110
}
@@ -114,4 +114,32 @@ export const CustomOpenAIProvider = {
114114
}
115115
}
116116
},
117+
getChunk: function (json) {
118+
switch (this.info?.type) {
119+
case "Anthropic": {
120+
let chunk = "";
121+
if (json["delta"]["thinking"]) {
122+
if (!this.isThinking) {
123+
this.isThinking = true;
124+
chunk += "\n<thinking>\n";
125+
}
126+
chunk += json["delta"]["thinking"];
127+
}
128+
if (json["delta"]["text"]) {
129+
if (this.isThinking) {
130+
this.isThinking = false;
131+
chunk += "\n</thinking>\n";
132+
}
133+
chunk += json["delta"]["text"];
134+
}
135+
return chunk;
136+
}
137+
case "OpenAI":
138+
default: {
139+
let chunk = json["choices"][0]["delta"] ?? json["choices"][0]["message"];
140+
chunk = chunk?.content;
141+
return chunk;
142+
}
143+
}
144+
},
117145
};

src/components/preferences/manageCustomAPIs.jsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,9 @@ const EditAPIConfig = ({ customAPIData, setCustomAPIData, url }) => {
6969
<Form.TextField id="name" title="API Name" defaultValue={APIData.name} />
7070
<Form.TextField id="apiKey" title="API Key" defaultValue={APIData.apiKey} />
7171

72-
<Form.Dropdown
73-
id="type"
74-
title="Type"
75-
defaultValue={APIData.type}
76-
>
77-
<Form.Dropdown.Item title="OpenAI" value="OpenAI" />
78-
<Form.Dropdown.Item title="Anthropic" value="Anthropic" />
72+
<Form.Dropdown id="type" title="Type" defaultValue={APIData.type}>
73+
<Form.Dropdown.Item title="OpenAI" value="OpenAI" />
74+
<Form.Dropdown.Item title="Anthropic" value="Anthropic" />
7975
</Form.Dropdown>
8076

8177
{/* Options */}

0 commit comments

Comments
 (0)