Skip to content

Commit 869b77e

Browse files
Update FunctionCall arguments mapping logic
1 parent 07b3718 commit 869b77e

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

ballerina/provider.bal

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ public isolated client class OpenAiProvider {
107107
}
108108
chat:ChatCompletionResponseMessage? message = choices[0].message;
109109
ai:ChatAssistantMessage chatAssistantMessage = {role: ai:ASSISTANT, content: message?.content};
110-
chat:ChatCompletionFunctionCall? function_call = message?.function_call;
111-
if function_call is chat:ChatCompletionFunctionCall {
112-
chatAssistantMessage.toolCalls = [{name: function_call.name, arguments: function_call.arguments}];
110+
chat:ChatCompletionFunctionCall? functionCall = message?.function_call;
111+
if functionCall is chat:ChatCompletionFunctionCall {
112+
chatAssistantMessage.toolCalls = [check self.mapToFunctionCall(functionCall)];
113113
}
114114
return chatAssistantMessage;
115115
}
@@ -134,4 +134,15 @@ public isolated client class OpenAiProvider {
134134
}
135135
return chatCompletionRequestMessages;
136136
}
137+
138+
private isolated function mapToFunctionCall(chat:ChatCompletionFunctionCall functionCall)
139+
returns ai:FunctionCall|ai:LlmError {
140+
do {
141+
json jsonArgs = check functionCall.arguments.fromJsonString();
142+
map<json>? arguments = check jsonArgs.cloneWithType();
143+
return {name: functionCall.name, arguments};
144+
} on fail error e {
145+
return error ai:LlmError("Invalid or malformed arguments received in function call response.", e);
146+
}
147+
}
137148
}

0 commit comments

Comments
 (0)