-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
138 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,5 @@ | ||
defmodule Canary do | ||
@moduledoc """ | ||
Canary keeps the contexts that define your domain | ||
and business logic. | ||
Contexts are also responsible for managing your data, regardless | ||
if it comes from the database, an external API or others. | ||
""" | ||
def rest_client(opts \\ []) do | ||
Req.new(opts) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
defmodule Canary.AI do | ||
use Retry | ||
|
||
defp client() do | ||
proxy_url = Application.fetch_env!(:canary, :openai_api_base) | ||
proxy_key = Application.fetch_env!(:canary, :openai_api_key) | ||
|
||
Canary.rest_client( | ||
base_url: proxy_url, | ||
headers: [{"Authorization", "Bearer #{proxy_key}"}] | ||
) | ||
end | ||
|
||
def embedding(request) do | ||
resp = | ||
retry with: exponential_backoff() |> randomize |> cap(1_000) |> expiry(4_000) do | ||
client() | ||
|> Req.post( | ||
url: "/v1/embeddings", | ||
json: request | ||
) | ||
end | ||
|
||
case resp do | ||
{:ok, %{status: 200, body: %{"data" => data}}} -> | ||
{:ok, data |> Enum.map(& &1["embedding"])} | ||
|
||
{:ok, data} -> | ||
{:error, data} | ||
|
||
{:error, error} -> | ||
{:error, error} | ||
end | ||
end | ||
|
||
def chat(request, opts \\ []) do | ||
opts = Keyword.merge([callback: fn data -> IO.inspect(data) end], opts) | ||
into = if request[:stream], do: get_handler(opts[:callback]), else: nil | ||
|
||
request = | ||
request | ||
|> Map.update!(:messages, &trim/1) | ||
|> Map.update(:tools, nil, &trim/1) | ||
|> Map.reject(fn {_k, v} -> is_nil(v) end) | ||
|
||
resp = | ||
retry with: exponential_backoff() |> randomize |> cap(1_000) |> expiry(4_000) do | ||
client() | ||
|> Req.post( | ||
url: "/v1/chat/completions", | ||
json: request, | ||
into: into | ||
) | ||
end | ||
|
||
case resp do | ||
{:ok, %{body: %{"choices" => [%{"finish_reason" => "tool_calls", "message" => message}]}}} -> | ||
{:ok, parse_tool_calls(message)} | ||
|
||
{:ok, %{body: %{"choices" => [%{"delta" => delta}]}}} -> | ||
{:ok, delta["content"]} | ||
|
||
{:ok, %{body: %{"choices" => [%{"message" => message}]}}} -> | ||
tool_calls = parse_tool_calls(message) | ||
|
||
if tool_calls != [] do | ||
{:ok, tool_calls} | ||
else | ||
{:ok, message["content"]} | ||
end | ||
|
||
{:ok, %{body: body}} -> | ||
{:ok, body} | ||
|
||
{:error, error} -> | ||
{:error, error} | ||
end | ||
end | ||
|
||
defp parse_tool_calls(message) do | ||
message | ||
|> get_in([Access.key("tool_calls", [])]) | ||
|> Enum.map(fn %{"function" => f} -> | ||
%{ | ||
name: f["name"], | ||
args: Jason.decode!(f["arguments"]) | ||
} | ||
end) | ||
end | ||
|
||
defp get_handler(callback) do | ||
fn {:data, data}, acc -> | ||
Enum.each(parse(data), callback) | ||
{:cont, acc} | ||
end | ||
end | ||
|
||
defp parse(chunk) do | ||
chunk | ||
|> String.split("data: ") | ||
|> Enum.map(&String.trim/1) | ||
|> Enum.map(&decode/1) | ||
|> Enum.reject(&is_nil/1) | ||
end | ||
|
||
defp decode(""), do: nil | ||
defp decode("[DONE]"), do: nil | ||
|
||
defp decode(data) do | ||
case Jason.decode(data) do | ||
{:ok, r} -> r | ||
_ -> nil | ||
end | ||
end | ||
|
||
def trim(data) when is_list(data), do: Enum.map(data, &trim/1) | ||
|
||
def trim(data) when is_map(data) do | ||
data |> Map.new(fn {k, v} -> {k, trim(v)} end) | ||
end | ||
|
||
def trim(data) when is_binary(data), do: String.trim(data) | ||
def trim(data), do: data | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters