Skip to content

Commit

Permalink
refactor: Added error handling and logging into executor execute
Browse files Browse the repository at this point in the history
  • Loading branch information
naqvis committed Nov 28, 2023
1 parent 9ba725a commit 4f3127d
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/openai/api/chat.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "json"
require "log"
require "promise"
require "./usage"
require "../stream"
Expand Down Expand Up @@ -321,6 +322,7 @@ module OpenAI
# Being able to deal any User defined Types, it requires ADT to extend `OpenAI::FuncMarker` a marker module.
# And requires Block to accept and return Types as `JSON::Serializable`
class FunctionExecutor
Log = ::Log.for(self)
alias Callback = JSON::Serializable -> JSON::Serializable
getter functions : Array(ChatFunction)
getter tools : Array(ChatTool)
Expand Down Expand Up @@ -354,9 +356,15 @@ module OpenAI
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
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)
begin
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)
rescue ex
Log.error(exception: ex) { "executing function call #{call.function.name}" }
reply = {"body" => "Encountered error while executing function: #{ex.message}"}
ChatMessage.new(:tool, reply.to_pretty_json, call.function.name, tool_call_id: call.id)
end
end
end
).get
Expand Down

0 comments on commit 4f3127d

Please sign in to comment.