-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Starting the GptAgent either creates new thread or uses existing one
It depends on whether you pass a thread ID or not.
- Loading branch information
Showing
7 changed files
with
154 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Used by "mix format" | ||
[ | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"], | ||
locals_without_parens: [field: 2, field: 3] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Config | ||
|
||
config :open_ai_client, OpenAiClient, | ||
base_url: System.get_env("OPENAI_BASE_URL") || "https://api.openai.com/v1", | ||
openai_api_key: System.get_env("OPENAI_API_KEY") || raise("OPENAI_API_KEY is not set"), | ||
openai_organization_id: System.get_env("OPENAI_ORGANIZATION_ID") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule GptAgent.Events.ThreadCreated do | ||
@moduledoc """ | ||
An OpenAI Assistants thread was created | ||
""" | ||
|
||
use TypedStruct | ||
|
||
typedstruct do | ||
field :id, String.t(), enforce: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,92 @@ | ||
defmodule GptAgentTest do | ||
@moduledoc false | ||
|
||
use ExUnit.Case | ||
doctest GptAgent | ||
|
||
describe "start_link/0" do | ||
alias GptAgent.Events.ThreadCreated | ||
|
||
setup _context do | ||
bypass = Bypass.open() | ||
|
||
Application.put_env(:open_ai_client, OpenAiClient, | ||
base_url: "http://localhost:#{bypass.port}", | ||
openai_api_key: "test", | ||
openai_organization_id: "test" | ||
) | ||
|
||
thread_id = Faker.Lorem.word() | ||
|
||
Bypass.stub(bypass, "POST", "/v1/threads", fn conn -> | ||
conn | ||
|> Plug.Conn.put_resp_content_type("application/json") | ||
|> Plug.Conn.resp( | ||
201, | ||
Jason.encode!(%{ | ||
"id" => thread_id, | ||
"object" => "thread", | ||
"created_at" => "1699012949", | ||
"metadata" => %{} | ||
}) | ||
) | ||
end) | ||
|
||
{:ok, bypass: bypass, thread_id: thread_id} | ||
end | ||
|
||
describe "start_link/1" do | ||
test "starts the agent" do | ||
assert {:ok, pid} = GptAgent.start_link() | ||
{:ok, pid} = GptAgent.start_link(self()) | ||
assert Process.alive?(pid) | ||
end | ||
|
||
test "creates a thread via the OpenAI API", %{bypass: bypass} do | ||
Bypass.expect_once(bypass, "POST", "/v1/threads", fn conn -> | ||
conn | ||
|> Plug.Conn.put_resp_content_type("application/json") | ||
|> Plug.Conn.resp( | ||
201, | ||
Jason.encode!(%{ | ||
"id" => Faker.Lorem.word(), | ||
"object" => "thread", | ||
"created_at" => "1699012949", | ||
"metadata" => %{} | ||
}) | ||
) | ||
end) | ||
|
||
{:ok, pid} = GptAgent.start_link(self()) | ||
|
||
assert_receive {GptAgent, ^pid, :ready}, 5_000 | ||
end | ||
|
||
test "sends the ThreadCreated event to the callback handler", %{thread_id: thread_id} do | ||
{:ok, pid} = GptAgent.start_link(self()) | ||
|
||
assert_receive {GptAgent, ^pid, %ThreadCreated{id: ^thread_id}}, 5_000 | ||
end | ||
end | ||
|
||
describe "start_link/2" do | ||
test "starts the agent" do | ||
{:ok, pid} = GptAgent.start_link(self(), Faker.Lorem.word()) | ||
assert Process.alive?(pid) | ||
end | ||
|
||
test "does not create a thread via the OpenAI API", %{bypass: bypass} do | ||
Bypass.stub(bypass, "POST", "/v1/threads", fn _conn -> | ||
raise "Should not have called the OpenAI API to create a thread" | ||
end) | ||
|
||
{:ok, pid} = GptAgent.start_link(self(), Faker.Lorem.word()) | ||
|
||
assert_receive {GptAgent, ^pid, :ready}, 5_000 | ||
end | ||
|
||
test "does not send the ThreadCreated event to the callback handler" do | ||
{:ok, pid} = GptAgent.start_link(self(), Faker.Lorem.word()) | ||
|
||
refute_receive {GptAgent, ^pid, %ThreadCreated{}}, 100 | ||
end | ||
end | ||
end |