From 3c85f9e974bdbd500a76be3f189e3c6ca2ca4b49 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Wed, 11 Dec 2024 23:24:16 +0300 Subject: [PATCH] Extract OpenAI.Completion struct --- lib/music_library/records.ex | 14 ++++++++------ lib/open_ai.ex | 10 +++++----- lib/open_ai/prompt.ex | 5 +++++ 3 files changed, 18 insertions(+), 11 deletions(-) create mode 100644 lib/open_ai/prompt.ex diff --git a/lib/music_library/records.ex b/lib/music_library/records.ex index d69261cb..e9f4d6af 100644 --- a/lib/music_library/records.ex +++ b/lib/music_library/records.ex @@ -191,15 +191,17 @@ defmodule MusicLibrary.Records do |> Enum.map(fn a -> a.name end) |> Enum.join(",") - prompt = """ - Provide a list of music genres applicable to the album "#{record.title}" by #{artists}. + completion = %OpenAI.Completion{ + content: """ + Provide a list of music genres applicable to the album "#{record.title}" by #{artists}. - Limit the list to 5 genres, ordered by decreasing specificity, all lowercase. + Limit the list to 5 genres, ordered by decreasing specificity, all lowercase. - Return a valid JSON list. - """ + Return a valid JSON list. + """ + } - {:ok, response} = OpenAI.gpt(prompt) + {:ok, response} = OpenAI.gpt(completion) record |> Record.add_genres(response["genres"]) diff --git a/lib/open_ai.ex b/lib/open_ai.ex index 1489f087..33bd70cf 100644 --- a/lib/open_ai.ex +++ b/lib/open_ai.ex @@ -1,8 +1,8 @@ defmodule OpenAI do - def gpt(prompt) do + def gpt(completion) do {:ok, collector} = Agent.start_link(fn -> "" end) - gpt_stream(prompt, fn data -> + gpt_stream(completion, fn data -> case get_in(data, ["choices", Access.at(0), "delta", "content"]) do nil -> :ok data -> Agent.update(collector, fn current -> current <> data end) @@ -15,7 +15,7 @@ defmodule OpenAI do end # Lifted from https://fly.io/phoenix-files/streaming-openai-responses/ - defp gpt_stream(prompt, cb) do + defp gpt_stream(completion, cb) do fun = fn request, finch_request, finch_name, finch_options -> fun = fn {:status, status}, response -> @@ -49,10 +49,10 @@ defmodule OpenAI do Req.post!("https://api.openai.com/v1/chat/completions", json: %{ model: "gpt-4o-mini", - messages: [%{role: "user", content: prompt}], + messages: [Map.take(completion, [:content, :role])], response_format: %{type: "json_object"}, stream: true, - temperature: 0.2 + temperature: completion.temperature }, auth: {:bearer, api_key()}, finch_request: fun diff --git a/lib/open_ai/prompt.ex b/lib/open_ai/prompt.ex new file mode 100644 index 00000000..7f238230 --- /dev/null +++ b/lib/open_ai/prompt.ex @@ -0,0 +1,5 @@ +defmodule OpenAI.Completion do + defstruct content: "", + temperature: 0.2, + role: "user" +end