Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/openai-compat-parallel-tool-calls.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/ai-openai-compat": patch
---

Group consecutive tool calls into one assistant message when using Chat Completions APIs.
40 changes: 31 additions & 9 deletions packages/ai/openai-compat/src/OpenAiLanguageModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import * as InternalUtilities from "./internal/utilities.ts"
import {
type Annotation,
type ChatCompletionContentPart,
type ChatCompletionRequestToolCall,
type CreateResponse,
type CreateResponse200,
type CreateResponse200Sse,
Expand Down Expand Up @@ -1666,7 +1667,24 @@ const toChatMessages = (
const messages: Array<CreateResponseRequestJson["messages"][number]> = []

for (const item of input) {
messages.push(...toChatMessagesFromItem(item))
if (Predicate.hasProperty(item, "type") && item.type === "function_call") {
const previous = messages.at(-1)
const toolCall = toChatToolCall(item)
if (previous?.role === "assistant" && previous.tool_calls !== undefined) {
messages[messages.length - 1] = {
...previous,
tool_calls: [...previous.tool_calls, toolCall]
}
} else {
messages.push({
role: "assistant",
content: null,
tool_calls: [toolCall]
})
}
} else {
messages.push(...toChatMessagesFromItem(item))
}
}

return messages
Expand Down Expand Up @@ -1694,14 +1712,7 @@ const toChatMessagesFromItem = (
return [{
role: "assistant",
content: null,
tool_calls: [{
id: item.call_id,
type: "function",
function: {
name: item.name,
arguments: item.arguments
}
}]
tool_calls: [toChatToolCall(item)]
}]
}

Expand All @@ -1719,6 +1730,17 @@ const toChatMessagesFromItem = (
}
}

const toChatToolCall = (
item: Extract<InputItem, { readonly type: "function_call" }>
): ChatCompletionRequestToolCall => ({
id: item.call_id,
type: "function",
function: {
name: item.name,
arguments: item.arguments
}
})

const toAssistantChatMessageContent = (
content: ReadonlyArray<{
readonly type: string
Expand Down
88 changes: 88 additions & 0 deletions packages/ai/openai-compat/test/OpenAiLanguageModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,94 @@ describe("OpenAiLanguageModel", () => {
assert.strictEqual(functionTool.function.strict, true)
}))

it.effect("groups parallel tool calls into one assistant message", () =>
Effect.gen(function*() {
let capturedRequest: HttpClientRequest.HttpClientRequest | undefined

const layer = OpenAiClient.layer({ apiKey: Redacted.make("sk-test-key") }).pipe(
Layer.provide(Layer.succeed(
HttpClient.HttpClient,
makeHttpClient((request) => {
capturedRequest = request
return Effect.succeed(jsonResponse(request, makeChatCompletion()))
})
))
)

yield* LanguageModel.generateText({
prompt: Prompt.make([
{ role: "user", content: "use both tools" },
{
role: "assistant",
content: [
Prompt.toolCallPart({
id: "call_1",
name: "TestTool",
params: { input: "first" },
providerExecuted: false
}),
Prompt.toolCallPart({
id: "call_2",
name: "TestTool",
params: { input: "second" },
providerExecuted: false
})
]
},
{
role: "tool",
content: [
Prompt.toolResultPart({
id: "call_1",
name: "TestTool",
isFailure: false,
result: { output: "first" }
}),
Prompt.toolResultPart({
id: "call_2",
name: "TestTool",
isFailure: false,
result: { output: "second" }
})
]
}
]),
toolkit: TestToolkit
}).pipe(
Effect.provide(OpenAiLanguageModel.model("gpt-4o-mini")),
Effect.provide(TestToolkitLayer),
Effect.provide(layer)
)

assert.isDefined(capturedRequest)
if (capturedRequest === undefined) {
return
}

const requestBody = yield* getRequestBody(capturedRequest)
assert.deepStrictEqual(requestBody.messages, [
{ role: "user", content: "use both tools" },
{
role: "assistant",
content: null,
tool_calls: [
{
id: "call_1",
type: "function",
function: { name: "TestTool", arguments: JSON.stringify({ input: "first" }) }
},
{
id: "call_2",
type: "function",
function: { name: "TestTool", arguments: JSON.stringify({ input: "second" }) }
}
]
},
{ role: "tool", tool_call_id: "call_1", content: JSON.stringify({ output: "first" }) },
{ role: "tool", tool_call_id: "call_2", content: JSON.stringify({ output: "second" }) }
])
}))

it.effect("converts dynamic tools to function type", () =>
Effect.gen(function*() {
let capturedRequest: HttpClientRequest.HttpClientRequest | undefined
Expand Down
Loading