Skip to content

Commit

Permalink
fix(chat.cr): must respond to unknown function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
stakach authored Nov 28, 2023
1 parent 4f3127d commit 5ff2299
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/openai/api/chat.cr
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,12 @@ module OpenAI
raise OpenAIError.new "OpenAI returned response with no function call details" if calls.empty?
Promise.all(
calls.map do |call|
raise OpenAIError.new "OpenAI called unknown function: name: '#{call.function.name}' with #{call.id}'" unless func = @map[call.function.name]? || @map[call.function.name.split('.', 2)[-1]]?
Promise(ChatMessage).defer(same_thread: true) do
params = call.function.arguments.as_s? || call.function.arguments.to_s
begin
func = @map[call.function.name]? || @map[call.function.name.split('.', 2)[-1]]?
raise OpenAIError.new "Unknown function: '#{call.function.name}'" unless func

params = call.function.arguments.as_s? || call.function.arguments.to_s
arg = func.first.from_json(params)
result = func.last.call(arg)
ChatMessage.new(:tool, result.to_pretty_json, call.function.name, tool_call_id: call.id)
Expand Down

0 comments on commit 5ff2299

Please sign in to comment.