From f0a1460a56a807ee80cdb3073191fa1b5e8cf274 Mon Sep 17 00:00:00 2001 From: Petrus Janse van Rensburg Date: Mon, 24 Jun 2024 19:45:57 +0200 Subject: [PATCH] use Req.post/2 rather than Req.post!/2 (#54) --- lib/instructor/adapters/llamacpp.ex | 13 ++++++++----- lib/instructor/adapters/openai.ex | 10 +++++----- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/instructor/adapters/llamacpp.ex b/lib/instructor/adapters/llamacpp.ex index c0c9f91..8c30214 100644 --- a/lib/instructor/adapters/llamacpp.ex +++ b/lib/instructor/adapters/llamacpp.ex @@ -53,7 +53,7 @@ defmodule Instructor.Adapters.Llamacpp do Stream.resource( fn -> Task.async(fn -> - Req.post!(url(), + Req.post(url(), json: %{ grammar: grammar, prompt: prompt, @@ -96,7 +96,7 @@ defmodule Instructor.Adapters.Llamacpp do defp do_chat_completion(prompt, grammar) do response = - Req.post!(url(), + Req.post(url(), json: %{ grammar: grammar, prompt: prompt @@ -105,11 +105,14 @@ defmodule Instructor.Adapters.Llamacpp do ) case response do - %{status: 200, body: %{"content" => params}} -> + {:ok, %{status: 200, body: %{"content" => params}}} -> {:ok, to_openai_response(params)} - _ -> - nil + {:ok, %{status: status}} -> + {:error, "Unexpected HTTP response code: #{status}"} + + {:error, reason} -> + {:error, reason} end end diff --git a/lib/instructor/adapters/openai.ex b/lib/instructor/adapters/openai.ex index 68a42b1..c752e6d 100644 --- a/lib/instructor/adapters/openai.ex +++ b/lib/instructor/adapters/openai.ex @@ -55,7 +55,7 @@ defmodule Instructor.Adapters.OpenAI do end ) - Req.post!(url(config), options) + Req.post(url(config), options) send(pid, :done) end) end, @@ -77,11 +77,11 @@ defmodule Instructor.Adapters.OpenAI do defp do_chat_completion(params, config) do options = Keyword.merge(http_options(config), json: params, auth: {:bearer, api_key(config)}) - response = Req.post!(url(config), options) - case response.status do - 200 -> {:ok, response.body} - _ -> {:error, response.body} + case Req.post(url(config), options) do + {:ok, %{status: 200, body: body}} -> {:ok, body} + {:ok, %{status: status}} -> {:error, "Unexpected HTTP response code: #{status}"} + {:error, reason} -> {:error, reason} end end