more tool calling working
This commit is contained in:
@@ -2,24 +2,52 @@ defmodule ElixirAi.ChatUtils do
|
||||
require Logger
|
||||
import ElixirAi.AiUtils.StreamLineUtils
|
||||
|
||||
def ai_tool(
|
||||
name: name,
|
||||
description: description,
|
||||
function: function,
|
||||
parameters: parameters
|
||||
) do
|
||||
schema = %{
|
||||
"type" => "function",
|
||||
"function" => %{
|
||||
"name" => name,
|
||||
"description" => description,
|
||||
"parameters" => parameters
|
||||
# %{
|
||||
# "type" => "object",
|
||||
# "properties" => %{
|
||||
# "name" => %{"type" => "string"},
|
||||
# "value" => %{"type" => "string"}
|
||||
# },
|
||||
# "required" => ["name", "value"]
|
||||
# }
|
||||
}
|
||||
}
|
||||
|
||||
%{
|
||||
name: name,
|
||||
definition: schema,
|
||||
function: function
|
||||
}
|
||||
end
|
||||
|
||||
def request_ai_response(server, messages, tools) do
|
||||
Task.start(fn ->
|
||||
api_url = Application.fetch_env!(:elixir_ai, :ai_endpoint)
|
||||
api_key = Application.fetch_env!(:elixir_ai, :ai_token)
|
||||
model = Application.fetch_env!(:elixir_ai, :ai_model)
|
||||
|
||||
tool_definition = tools |> Enum.map(fn {_name, definition} -> definition end)
|
||||
|
||||
body = %{
|
||||
model: model,
|
||||
stream: true,
|
||||
messages: messages |> Enum.map(&api_message/1),
|
||||
tools: tool_definition
|
||||
tools: Enum.map(tools, & &1.definition)
|
||||
}
|
||||
|
||||
headers = [{"authorization", "Bearer #{api_key}"}]
|
||||
|
||||
Logger.info("sending AI request with body: #{inspect(body)}")
|
||||
# Logger.info("sending AI request with body: #{inspect(body)}")
|
||||
case Req.post(api_url,
|
||||
json: body,
|
||||
headers: headers,
|
||||
|
||||
@@ -96,7 +96,7 @@ defmodule ElixirAi.AiUtils.StreamLineUtils do
|
||||
"type" => "function",
|
||||
"function" => %{"name" => tool_name, "arguments" => tool_args_start}
|
||||
} ->
|
||||
Logger.info("Received tool call start for tool #{tool_name}")
|
||||
# Logger.info("Received tool call start for tool #{tool_name}")
|
||||
|
||||
send(
|
||||
server,
|
||||
@@ -104,7 +104,7 @@ defmodule ElixirAi.AiUtils.StreamLineUtils do
|
||||
)
|
||||
|
||||
%{"index" => tool_index, "function" => %{"arguments" => tool_args_diff}} ->
|
||||
Logger.info("Received tool call middle for index #{tool_index}")
|
||||
# Logger.info("Received tool call middle for index #{tool_index}")
|
||||
send(server, {:ai_tool_call_middle, id, {tool_args_diff, tool_index}})
|
||||
|
||||
other ->
|
||||
@@ -117,7 +117,7 @@ defmodule ElixirAi.AiUtils.StreamLineUtils do
|
||||
"choices" => [%{"finish_reason" => "tool_calls"}],
|
||||
"id" => id
|
||||
}) do
|
||||
Logger.info("Received tool call end")
|
||||
# Logger.info("Received tool call end")
|
||||
send(server, {:ai_tool_call_end, id})
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user