-
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.
Add user's message to the assistant thread
- Loading branch information
Showing
11 changed files
with
311 additions
and
27 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
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,5 +1,5 @@ | ||
# Used by "mix format" | ||
[ | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"], | ||
locals_without_parens: [field: 2, field: 3] | ||
locals_without_parens: [field: 2, field: 3, type: 1, validate_with: 1] | ||
] |
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,6 +1,6 @@ | ||
import Config | ||
|
||
config :open_ai_client, OpenAiClient, | ||
base_url: System.get_env("OPENAI_BASE_URL") || "https://api.openai.com/v1", | ||
base_url: System.get_env("OPENAI_BASE_URL") || "https://api.openai.com", | ||
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,13 @@ | ||
defmodule GptAgent.Events.UserMessageAdded do | ||
@moduledoc """ | ||
An OpenAI Assistants user message was added to a thread | ||
""" | ||
|
||
use TypedStruct | ||
|
||
typedstruct do | ||
field :id, binary(), enforce: true | ||
field :thread_id, binary(), enforce: true | ||
field :content, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
defmodule GptAgent.Value do | ||
@moduledoc """ | ||
A module that provides a macro to define a new type and its validation. | ||
""" | ||
|
||
@doc """ | ||
Macro to use GptAgent.Value in the current module. | ||
## Examples | ||
defmodule MyModule do | ||
use GptAgent.Value | ||
end | ||
""" | ||
defmacro __using__(_opts) do | ||
quote do | ||
require GptAgent.Value | ||
import GptAgent.Value | ||
end | ||
end | ||
|
||
@doc """ | ||
Macro to define a new type. | ||
## Examples | ||
defmodule MyType do | ||
use GptAgent.Value | ||
type String.t() | ||
end | ||
""" | ||
@spec type(atom()) :: Macro.t() | ||
defmacro type(type) do | ||
quote do | ||
use TypedStruct | ||
|
||
typedstruct do | ||
field :value, unquote(type), enforce: true | ||
end | ||
|
||
@doc """ | ||
Creates a new instance of the type. | ||
The function validates the value and returns a new struct if the value is valid. | ||
If the value is invalid, it returns an error tuple with the error message. | ||
## Params | ||
- `value`: The value to be validated and set in the struct. | ||
## Examples | ||
iex> MyType.new("valid value") | ||
%MyType{value: "valid value"} | ||
iex> MyType.new("invalid value") | ||
{:error, "error message"} | ||
""" | ||
@spec new(any()) :: {:ok, t()} | {:error, String.t()} | ||
def new(value) do | ||
case validate(value) do | ||
:ok -> | ||
{:ok, %__MODULE__{value: value}} | ||
|
||
{:error, error} -> | ||
{:error, error} | ||
end | ||
end | ||
end | ||
end | ||
|
||
@doc """ | ||
Macro to define a validation function for the type. | ||
## Examples | ||
defmodule MyType do | ||
use GptAgent.Value | ||
type String.t() | ||
validate_with fn | ||
value when is_binary(value) -> | ||
case String.trim(value) do | ||
"" -> {:error, "must be a nonblank string"} | ||
_ -> :ok | ||
end | ||
end | ||
end | ||
""" | ||
@spec validate_with(Macro.t()) :: Macro.t() | ||
defmacro validate_with(validate_with) do | ||
quote do | ||
defp validate(value) do | ||
unquote(validate_with).(value) | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
defmodule GptAgent.Values.NonblankString do | ||
@moduledoc """ | ||
A message that a user has sent to the GPT agent | ||
""" | ||
|
||
use GptAgent.Value | ||
|
||
type String.t() | ||
|
||
validate_with fn | ||
value when is_binary(value) -> | ||
case String.trim(value) do | ||
"" -> {:error, "must be a nonblank string"} | ||
_ -> :ok | ||
end | ||
|
||
_ -> | ||
{:error, "must be a nonblank string"} | ||
end | ||
|
||
defimpl Jason.Encoder do | ||
def encode(%{value: value}, opts) do | ||
%{role: :user, content: value} | ||
|> Jason.Encoder.encode(opts) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule GptAgent.Values.NonblankStringTest do | ||
@moduledoc false | ||
|
||
use GptAgent.TestCase, async: true | ||
|
||
alias GptAgent.Values.NonblankString | ||
|
||
test "a non-blank string is valid" do | ||
check all(value <- nonblank_string()) do | ||
assert {:ok, %NonblankString{value: ^value}} = NonblankString.new(value) | ||
end | ||
end | ||
|
||
test "blank strings are invalid" do | ||
assert {:error, "must be a nonblank string"} = NonblankString.new("") | ||
assert {:error, "must be a nonblank string"} = NonblankString.new(" ") | ||
assert {:error, "must be a nonblank string"} = NonblankString.new(" \n\t ") | ||
end | ||
|
||
test "all other values are invalid" do | ||
check all(value <- term_other_than_nonblank_string()) do | ||
assert {:error, "must be a nonblank string"} = NonblankString.new(value) | ||
end | ||
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
Oops, something went wrong.