Skip to content

Commit 8a4f63b

Browse files
committed
Merge branch 'feature/chat-completions-reasoning' into all
2 parents 6e4ed13 + 5175245 commit 8a4f63b

1 file changed

Lines changed: 31 additions & 11 deletions

File tree

src/routes/messages/non-stream-translation.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
import {
1515
type AnthropicAssistantContentBlock,
1616
type AnthropicAssistantMessage,
17-
type AnthropicMessage,
1817
type AnthropicMessagesPayload,
1918
type AnthropicResponse,
2019
type AnthropicTextBlock,
@@ -38,9 +37,9 @@ export function translateToOpenAI(
3837
return {
3938
model: modelId,
4039
messages: translateAnthropicMessagesToOpenAI(
41-
payload.messages,
42-
payload.system,
40+
payload,
4341
modelId,
42+
thinkingBudget,
4443
),
4544
max_tokens: payload.max_tokens,
4645
stop: payload.stop_sequences,
@@ -86,32 +85,53 @@ function translateModelName(model: string): string {
8685
}
8786

8887
function translateAnthropicMessagesToOpenAI(
89-
anthropicMessages: Array<AnthropicMessage>,
90-
system: string | Array<AnthropicTextBlock> | undefined,
88+
payload: AnthropicMessagesPayload,
9189
modelId: string,
90+
thinkingBudget: number | undefined,
9291
): Array<Message> {
93-
const systemMessages = handleSystemPrompt(system)
94-
95-
const otherMessages = anthropicMessages.flatMap((message) =>
92+
const systemMessages = handleSystemPrompt(payload.system, modelId)
93+
const otherMessages = payload.messages.flatMap((message) =>
9694
message.role === "user" ?
9795
handleUserMessage(message)
9896
: handleAssistantMessage(message, modelId),
9997
)
100-
98+
if (modelId.startsWith("claude") && thinkingBudget) {
99+
const thinkingMessage = {
100+
role: "user",
101+
content: "Please strictly follow Interleaved thinking",
102+
} as Message
103+
return [...systemMessages, thinkingMessage, ...otherMessages]
104+
}
101105
return [...systemMessages, ...otherMessages]
102106
}
103107

104108
function handleSystemPrompt(
105109
system: string | Array<AnthropicTextBlock> | undefined,
110+
modelId: string,
106111
): Array<Message> {
107112
if (!system) {
108113
return []
109114
}
110115

116+
let extraPrompt = `
117+
## Interleaved thinking
118+
- Interleaved thinking is enabled
119+
- You MUST think after receiving tool results before deciding the next action or final answer.
120+
`
121+
if (!modelId.startsWith("claude")) {
122+
extraPrompt = ""
123+
}
111124
if (typeof system === "string") {
112-
return [{ role: "system", content: system }]
125+
return [{ role: "system", content: system + extraPrompt }]
113126
} else {
114-
const systemText = system.map((block) => block.text).join("\n\n")
127+
const systemText = system
128+
.map((block, index) => {
129+
if (index === 0) {
130+
return block.text + extraPrompt
131+
}
132+
return block.text
133+
})
134+
.join("\n\n")
115135
return [{ role: "system", content: systemText }]
116136
}
117137
}

0 commit comments

Comments
 (0)