fix elixir openai_ex 0.9.18 API: use Chat.Completions module and string keys for response parsing
This commit is contained in:
parent
b4148cbaf6
commit
c30b3af2de
1 changed files with 43 additions and 35 deletions
|
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/env elixir
|
||||
|
||||
Mix.install([{:openai_ex, "~> 0.9.17"}, {:httpoison, "~> 2.2"}, {:jason, "~> 1.4"}])
|
||||
Mix.install([{:openai_ex, "~> 0.9.18"}, {:httpoison, "~> 2.2"}, {:jason, "~> 1.4"}])
|
||||
|
||||
IO.puts("=== uncloseai. Elixir Client (OpenAI Ex Library) ===\n")
|
||||
|
||||
|
|
@ -35,7 +35,7 @@ IO.puts("=== Non-Streaming Chat (Model 1) ===")
|
|||
client1 = OpenaiEx.new("dummy-key")
|
||||
|> OpenaiEx.with_base_url(model_endpoint_1)
|
||||
|
||||
req1 = OpenaiEx.ChatCompletion.new(
|
||||
req1 = OpenaiEx.Chat.Completions.new(
|
||||
model: model_1_id,
|
||||
messages: [
|
||||
%{role: "user", content: "Give a Python Fizzbuzz solution in one line of code?"}
|
||||
|
|
@ -44,9 +44,9 @@ req1 = OpenaiEx.ChatCompletion.new(
|
|||
max_tokens: 150
|
||||
)
|
||||
|
||||
case OpenaiEx.ChatCompletion.create(client1, req1) do
|
||||
case OpenaiEx.Chat.Completions.create(client1, req1) do
|
||||
{:ok, response} ->
|
||||
content = response.choices |> List.first() |> Map.get(:message) |> Map.get(:content)
|
||||
content = response["choices"] |> List.first() |> Map.get("message") |> Map.get("content")
|
||||
IO.puts("Response: #{content}\n")
|
||||
{:error, error} ->
|
||||
IO.puts("Error: #{inspect(error)}\n")
|
||||
|
|
@ -55,7 +55,7 @@ end
|
|||
# Streaming chat with Model 1
|
||||
IO.puts("=== Streaming Chat (Model 1) ===")
|
||||
|
||||
stream_req1 = OpenaiEx.ChatCompletion.new(
|
||||
stream_req1 = OpenaiEx.Chat.Completions.new(
|
||||
model: model_1_id,
|
||||
messages: [
|
||||
%{role: "user", content: "Explain quantum entanglement in one sentence."}
|
||||
|
|
@ -67,21 +67,25 @@ stream_req1 = OpenaiEx.ChatCompletion.new(
|
|||
|
||||
IO.write("Response: ")
|
||||
|
||||
OpenaiEx.ChatCompletion.create(client1, stream_req1)
|
||||
|> Stream.each(fn
|
||||
{:data, chunk} ->
|
||||
case chunk do
|
||||
%{choices: [%{delta: %{content: content}} | _]} when not is_nil(content) ->
|
||||
IO.write(content)
|
||||
case OpenaiEx.Chat.Completions.create(client1, stream_req1) do
|
||||
{:ok, stream} ->
|
||||
stream
|
||||
|> Stream.each(fn
|
||||
{:data, chunk} ->
|
||||
case chunk do
|
||||
%{"choices" => [%{"delta" => %{"content" => content}} | _]} when not is_nil(content) ->
|
||||
IO.write(content)
|
||||
_ ->
|
||||
:ok
|
||||
end
|
||||
_ ->
|
||||
:ok
|
||||
end
|
||||
_ ->
|
||||
:ok
|
||||
end)
|
||||
|> Stream.run()
|
||||
|
||||
IO.puts("\n")
|
||||
end)
|
||||
|> Stream.run()
|
||||
IO.puts("\n")
|
||||
{:error, error} ->
|
||||
IO.puts("Error: #{inspect(error)}\n")
|
||||
end
|
||||
|
||||
# Non-streaming chat with Model 2
|
||||
IO.puts("=== Non-Streaming Chat (Model 2) ===")
|
||||
|
|
@ -89,7 +93,7 @@ IO.puts("=== Non-Streaming Chat (Model 2) ===")
|
|||
client2 = OpenaiEx.new("dummy-key")
|
||||
|> OpenaiEx.with_base_url(model_endpoint_2)
|
||||
|
||||
req2 = OpenaiEx.ChatCompletion.new(
|
||||
req2 = OpenaiEx.Chat.Completions.new(
|
||||
model: model_2_id,
|
||||
messages: [
|
||||
%{role: "user", content: "Write a JavaScript function to check if a number is prime"}
|
||||
|
|
@ -98,9 +102,9 @@ req2 = OpenaiEx.ChatCompletion.new(
|
|||
max_tokens: 150
|
||||
)
|
||||
|
||||
case OpenaiEx.ChatCompletion.create(client2, req2) do
|
||||
case OpenaiEx.Chat.Completions.create(client2, req2) do
|
||||
{:ok, response} ->
|
||||
content = response.choices |> List.first() |> Map.get(:message) |> Map.get(:content)
|
||||
content = response["choices"] |> List.first() |> Map.get("message") |> Map.get("content")
|
||||
IO.puts("Response: #{content}\n")
|
||||
{:error, error} ->
|
||||
IO.puts("Error: #{inspect(error)}\n")
|
||||
|
|
@ -109,7 +113,7 @@ end
|
|||
# Streaming chat with Model 2
|
||||
IO.puts("=== Streaming Chat (Model 2) ===")
|
||||
|
||||
stream_req2 = OpenaiEx.ChatCompletion.new(
|
||||
stream_req2 = OpenaiEx.Chat.Completions.new(
|
||||
model: model_2_id,
|
||||
messages: [
|
||||
%{role: "user", content: "Give a Python Fizzbuzz solution in one line of code?"}
|
||||
|
|
@ -121,21 +125,25 @@ stream_req2 = OpenaiEx.ChatCompletion.new(
|
|||
|
||||
IO.write("Response: ")
|
||||
|
||||
OpenaiEx.ChatCompletion.create(client2, stream_req2)
|
||||
|> Stream.each(fn
|
||||
{:data, chunk} ->
|
||||
case chunk do
|
||||
%{choices: [%{delta: %{content: content}} | _]} when not is_nil(content) ->
|
||||
IO.write(content)
|
||||
case OpenaiEx.Chat.Completions.create(client2, stream_req2) do
|
||||
{:ok, stream} ->
|
||||
stream
|
||||
|> Stream.each(fn
|
||||
{:data, chunk} ->
|
||||
case chunk do
|
||||
%{"choices" => [%{"delta" => %{"content" => content}} | _]} when not is_nil(content) ->
|
||||
IO.write(content)
|
||||
_ ->
|
||||
:ok
|
||||
end
|
||||
_ ->
|
||||
:ok
|
||||
end
|
||||
_ ->
|
||||
:ok
|
||||
end)
|
||||
|> Stream.run()
|
||||
|
||||
IO.puts("\n")
|
||||
end)
|
||||
|> Stream.run()
|
||||
IO.puts("\n")
|
||||
{:error, error} ->
|
||||
IO.puts("Error: #{inspect(error)}\n")
|
||||
end
|
||||
|
||||
# TTS example
|
||||
IO.puts("=== TTS Speech Generation ===")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue