From 44a8650c6c53de5b12941b37c38d4db0b5276416 Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Tue, 10 Dec 2024 16:50:37 +0300 Subject: [PATCH] Move API Key to configuration --- config/runtime.exs | 4 ++++ lib/open_ai.ex | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/config/runtime.exs b/config/runtime.exs index 8414fca1..b1a87853 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -28,6 +28,10 @@ if user = System.get_env("LAST_FM_USER") do config :music_library, LastFm, user: user end +if openai_key = System.get_env("OPENAI_KEY") do + config :music_library, OpenAI, api_key: openai_key +end + config :music_library, MusicLibrary.Repo, load_extensions: [ MusicLibrary.Repo.extension_path("unicode") diff --git a/lib/open_ai.ex b/lib/open_ai.ex index 5aac8e2c..496a1139 100644 --- a/lib/open_ai.ex +++ b/lib/open_ai.ex @@ -53,7 +53,7 @@ defmodule OpenAI do stream: true, temperature: 0.2 }, - auth: {:bearer, System.fetch_env!("OPENAI_KEY")}, + auth: {:bearer, api_key()}, finch_request: fun ) end @@ -61,4 +61,9 @@ defmodule OpenAI do defp decode_body("", _), do: :ok defp decode_body("[DONE]", _), do: :ok defp decode_body(json, cb), do: cb.(Jason.decode!(json)) + + defp api_key do + Application.get_env(:music_library, OpenAI) + |> Keyword.fetch!(:api_key) + end end