Add small experiment to test genre lookup
This commit is contained in:
@@ -0,0 +1,65 @@
|
|||||||
|
defmodule OpenAI do
|
||||||
|
def gpt_stream(prompt, cb) do
|
||||||
|
fun = fn request, finch_request, finch_name, finch_options ->
|
||||||
|
fun = fn
|
||||||
|
{:status, status}, response ->
|
||||||
|
%{response | status: status}
|
||||||
|
|
||||||
|
{:headers, headers}, response ->
|
||||||
|
%{response | headers: headers}
|
||||||
|
|
||||||
|
{:data, data}, response ->
|
||||||
|
body =
|
||||||
|
data
|
||||||
|
|> String.split("data: ")
|
||||||
|
|> Enum.map(fn str ->
|
||||||
|
str
|
||||||
|
|> String.trim()
|
||||||
|
|> decode_body(cb)
|
||||||
|
end)
|
||||||
|
|> Enum.filter(fn d -> d != :ok end)
|
||||||
|
|
||||||
|
old_body = if response.body == "", do: [], else: response.body
|
||||||
|
|
||||||
|
%{response | body: old_body ++ body}
|
||||||
|
end
|
||||||
|
|
||||||
|
case Finch.stream(finch_request, finch_name, Req.Response.new(), fun, finch_options) do
|
||||||
|
{:ok, response} -> {request, response}
|
||||||
|
{:error, exception} -> {request, exception}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Req.post!("https://api.openai.com/v1/chat/completions",
|
||||||
|
json: %{
|
||||||
|
# Pick your model here
|
||||||
|
model: "gpt-4o-mini",
|
||||||
|
messages: [%{role: "user", content: prompt}],
|
||||||
|
stream: true
|
||||||
|
},
|
||||||
|
auth: {:bearer, System.fetch_env!("OPENAI_KEY")},
|
||||||
|
finch_request: fun
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp decode_body("", _), do: :ok
|
||||||
|
defp decode_body("[DONE]", _), do: :ok
|
||||||
|
defp decode_body(json, cb), do: cb.(Jason.decode!(json))
|
||||||
|
end
|
||||||
|
|
||||||
|
{:ok, collector} = Agent.start_link(fn -> "" end)
|
||||||
|
|
||||||
|
prompt = """
|
||||||
|
Please provide a list of music genres applicable to the album "Stupid Things that Mean the World" by Tim Bowness.
|
||||||
|
|
||||||
|
Return the genres as a CSV list, and only the raw value without any introduction.
|
||||||
|
"""
|
||||||
|
|
||||||
|
OpenAI.gpt_stream(prompt, fn data ->
|
||||||
|
case get_in(data, ["choices", Access.at(0), "delta", "content"]) do
|
||||||
|
nil -> :ok
|
||||||
|
data -> Agent.update(collector, fn current -> current <> data end)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
Agent.get(collector, & &1) |> IO.inspect()
|
||||||
@@ -41,6 +41,7 @@ defmodule MusicLibrary.MixProject do
|
|||||||
{:phoenix_live_reload, "~> 1.2", only: :dev},
|
{:phoenix_live_reload, "~> 1.2", only: :dev},
|
||||||
{:yaml_elixir, "~> 2.11"},
|
{:yaml_elixir, "~> 2.11"},
|
||||||
{:finch, "~> 0.19.0"},
|
{:finch, "~> 0.19.0"},
|
||||||
|
{:req, "~> 0.5.8"},
|
||||||
{:vix, "~> 0.31.1"},
|
{:vix, "~> 0.31.1"},
|
||||||
{:nimble_parsec, "~> 1.4"},
|
{:nimble_parsec, "~> 1.4"},
|
||||||
{:nimble_options, "~> 1.1"},
|
{:nimble_options, "~> 1.1"},
|
||||||
|
|||||||
@@ -39,6 +39,7 @@
|
|||||||
"phoenix_template": {:hex, :phoenix_template, "1.0.4", "e2092c132f3b5e5b2d49c96695342eb36d0ed514c5b252a77048d5969330d639", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0 or ~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "2c0c81f0e5c6753faf5cca2f229c9709919aba34fab866d3bc05060c9c444206"},
|
"phoenix_template": {:hex, :phoenix_template, "1.0.4", "e2092c132f3b5e5b2d49c96695342eb36d0ed514c5b252a77048d5969330d639", [:mix], [{:phoenix_html, "~> 2.14.2 or ~> 3.0 or ~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: true]}], "hexpm", "2c0c81f0e5c6753faf5cca2f229c9709919aba34fab866d3bc05060c9c444206"},
|
||||||
"plug": {:hex, :plug, "1.16.1", "40c74619c12f82736d2214557dedec2e9762029b2438d6d175c5074c933edc9d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a13ff6b9006b03d7e33874945b2755253841b238c34071ed85b0e86057f8cddc"},
|
"plug": {:hex, :plug, "1.16.1", "40c74619c12f82736d2214557dedec2e9762029b2438d6d175c5074c933edc9d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a13ff6b9006b03d7e33874945b2755253841b238c34071ed85b0e86057f8cddc"},
|
||||||
"plug_crypto": {:hex, :plug_crypto, "2.1.0", "f44309c2b06d249c27c8d3f65cfe08158ade08418cf540fd4f72d4d6863abb7b", [:mix], [], "hexpm", "131216a4b030b8f8ce0f26038bc4421ae60e4bb95c5cf5395e1421437824c4fa"},
|
"plug_crypto": {:hex, :plug_crypto, "2.1.0", "f44309c2b06d249c27c8d3f65cfe08158ade08418cf540fd4f72d4d6863abb7b", [:mix], [], "hexpm", "131216a4b030b8f8ce0f26038bc4421ae60e4bb95c5cf5395e1421437824c4fa"},
|
||||||
|
"req": {:hex, :req, "0.5.8", "50d8d65279d6e343a5e46980ac2a70e97136182950833a1968b371e753f6a662", [:mix], [{:brotli, "~> 0.3.1", [hex: :brotli, repo: "hexpm", optional: true]}, {:ezstd, "~> 1.0", [hex: :ezstd, repo: "hexpm", optional: true]}, {:finch, "~> 0.17", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 2.0.6 or ~> 2.1", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "d7fc5898a566477e174f26887821a3c5082b243885520ee4b45555f5d53f40ef"},
|
||||||
"table_rex": {:hex, :table_rex, "4.0.0", "3c613a68ebdc6d4d1e731bc973c233500974ec3993c99fcdabb210407b90959b", [:mix], [], "hexpm", "c35c4d5612ca49ebb0344ea10387da4d2afe278387d4019e4d8111e815df8f55"},
|
"table_rex": {:hex, :table_rex, "4.0.0", "3c613a68ebdc6d4d1e731bc973c233500974ec3993c99fcdabb210407b90959b", [:mix], [], "hexpm", "c35c4d5612ca49ebb0344ea10387da4d2afe278387d4019e4d8111e815df8f55"},
|
||||||
"tailwind": {:hex, :tailwind, "0.2.4", "5706ec47182d4e7045901302bf3a333e80f3d1af65c442ba9a9eed152fb26c2e", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}], "hexpm", "c6e4a82b8727bab593700c998a4d98cf3d8025678bfde059aed71d0000c3e463"},
|
"tailwind": {:hex, :tailwind, "0.2.4", "5706ec47182d4e7045901302bf3a333e80f3d1af65c442ba9a9eed152fb26c2e", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}], "hexpm", "c6e4a82b8727bab593700c998a4d98cf3d8025678bfde059aed71d0000c3e463"},
|
||||||
"telemetry": {:hex, :telemetry, "1.3.0", "fedebbae410d715cf8e7062c96a1ef32ec22e764197f70cda73d82778d61e7a2", [:rebar3], [], "hexpm", "7015fc8919dbe63764f4b4b87a95b7c0996bd539e0d499be6ec9d7f3875b79e6"},
|
"telemetry": {:hex, :telemetry, "1.3.0", "fedebbae410d715cf8e7062c96a1ef32ec22e764197f70cda73d82778d61e7a2", [:rebar3], [], "hexpm", "7015fc8919dbe63764f4b4b87a95b7c0996bd539e0d499be6ec9d7f3875b79e6"},
|
||||||
|
|||||||
Reference in New Issue
Block a user