Skip to content
Draft
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
27 changes: 21 additions & 6 deletions apps/gateway/src/chat/tools/transform-streaming-to-openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,27 @@ export function transformStreamingToOpenai(
};
} else if (data.candidates?.[0]?.finishReason) {
const finishReason = data.candidates[0].finishReason;
const parts = data.candidates[0].content?.parts || [];
// Check if there are function calls in this response
const hasFunctionCalls = data.candidates?.[0]?.content?.parts?.some(
(part: any) => part.functionCall,
);
const hasFunctionCalls = parts.some((part: any) => part.functionCall);

const delta: any = {
role: "assistant",
};

// If there are function calls, include the function_call object in delta
if (hasFunctionCalls) {
const firstFunctionCall = parts.find(
(part: any) => part.functionCall,
)?.functionCall;
if (firstFunctionCall) {
delta.function_call = {
name: firstFunctionCall.name,
arguments: JSON.stringify(firstFunctionCall.args || {}),
};
}
}

transformedData = {
id: data.responseId || `chatcmpl-${Date.now()}`,
object: "chat.completion.chunk",
Expand All @@ -271,9 +288,7 @@ export function transformStreamingToOpenai(
choices: [
{
index: data.candidates[0].index || 0,
delta: {
role: "assistant",
},
delta,
finish_reason:
finishReason === "STOP"
? hasFunctionCalls
Expand Down