diff options
Diffstat (limited to 'lib/open_ai.ex')
-rw-r--r-- | lib/open_ai.ex | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/open_ai.ex b/lib/open_ai.ex new file mode 100644 index 0000000..9feb9a4 --- /dev/null +++ b/lib/open_ai.ex @@ -0,0 +1,17 @@ +defmodule OpenAi do + + def post(path, data, options \\ []) do + config = Application.get_env(:lsg, :openai, []) + url = "https://api.openai.com#{path}" + headers = [{"user-agent", "internal private experiment bot, href@random.sh"}, + {"content-type", "application/json"}, + {"authorization", "Bearer " <> Keyword.get(config, :key, "unset-api-key")}] + options = options ++ [timeout: :timer.seconds(180), recv_timeout: :timer.seconds(180)] + with {:ok, json} <- Poison.encode(data), + {:ok, %HTTPoison.Response{status_code: 200, body: body}} <- HTTPoison.post(url, json, headers, options), + {:ok, data} <- Poison.decode(body) do + {:ok, data} + end + end + +end |