diff --git a/lib/openai_ex/beta/assistant.ex b/lib/openai_ex/beta/assistants.ex similarity index 97% rename from lib/openai_ex/beta/assistant.ex rename to lib/openai_ex/beta/assistants.ex index f6211bc..96f7cd9 100644 --- a/lib/openai_ex/beta/assistant.ex +++ b/lib/openai_ex/beta/assistants.ex @@ -1,4 +1,4 @@ -defmodule OpenaiEx.Beta.Assistant do +defmodule OpenaiEx.Beta.Assistants do @moduledoc """ This module provides an implementation of the OpenAI assistants API. The API reference can be found at https://platform.openai.com/docs/api-reference/assistants. @@ -43,7 +43,7 @@ defmodule OpenaiEx.Beta.Assistant do Example usage: - iex> _request = OpenaiEx.Beta.Assistant.new(model: "gpt-4-turbo") + iex> _request = OpenaiEx.Beta.Assistants.new(model: "gpt-4-turbo") %{model: "gpt-4-turbo"} """ diff --git a/lib/openai_ex/beta/assistant_file.ex b/lib/openai_ex/beta/assistants_file.ex similarity index 98% rename from lib/openai_ex/beta/assistant_file.ex rename to lib/openai_ex/beta/assistants_file.ex index 53f8315..9b4f706 100644 --- a/lib/openai_ex/beta/assistant_file.ex +++ b/lib/openai_ex/beta/assistants_file.ex @@ -1,4 +1,4 @@ -defmodule OpenaiEx.Beta.Assistant.File do +defmodule OpenaiEx.Beta.Assistants.File do @moduledoc """ This module provides an implementation of the OpenAI assistants files API. The API reference can be found at https://platform.openai.com/docs/api-reference/assistants. diff --git a/lib/openai_ex/beta/thread.ex b/lib/openai_ex/beta/threads.ex similarity index 98% rename from lib/openai_ex/beta/thread.ex rename to lib/openai_ex/beta/threads.ex index c9ce237..567efb8 100644 --- a/lib/openai_ex/beta/thread.ex +++ b/lib/openai_ex/beta/threads.ex @@ -1,4 +1,4 @@ -defmodule OpenaiEx.Beta.Thread do +defmodule OpenaiEx.Beta.Threads do @moduledoc """ This module provides an implementation of the OpenAI threads API. The API reference can be found at https://platform.openai.com/docs/api-reference/threads. diff --git a/lib/openai_ex/beta/message.ex b/lib/openai_ex/beta/threads_messages.ex similarity index 98% rename from lib/openai_ex/beta/message.ex rename to lib/openai_ex/beta/threads_messages.ex index 529f4ca..7e7509d 100644 --- a/lib/openai_ex/beta/message.ex +++ b/lib/openai_ex/beta/threads_messages.ex @@ -1,4 +1,4 @@ -defmodule OpenaiEx.Beta.Thread.Message do +defmodule OpenaiEx.Beta.Threads.Messages do @moduledoc """ This module provides an implementation of the OpenAI messages API. The API reference can be found at https://platform.openai.com/docs/api-reference/messages. diff --git a/lib/openai_ex/beta/message_file.ex b/lib/openai_ex/beta/threads_messages_file.ex similarity index 96% rename from lib/openai_ex/beta/message_file.ex rename to lib/openai_ex/beta/threads_messages_file.ex index 3500ee3..0e106d8 100644 --- a/lib/openai_ex/beta/message_file.ex +++ b/lib/openai_ex/beta/threads_messages_file.ex @@ -1,4 +1,4 @@ -defmodule OpenaiEx.Beta.Thread.Message.File do +defmodule OpenaiEx.Beta.Threads.Messages.File do @moduledoc """ This module provides an implementation of the OpenAI messages files API. The API reference can be found at https://platform.openai.com/docs/api-reference/messages. """ diff --git a/lib/openai_ex/beta/run.ex b/lib/openai_ex/beta/threads_runs.ex similarity index 96% rename from lib/openai_ex/beta/run.ex rename to lib/openai_ex/beta/threads_runs.ex index f942b72..e73f801 100644 --- a/lib/openai_ex/beta/run.ex +++ b/lib/openai_ex/beta/threads_runs.ex @@ -1,4 +1,4 @@ -defmodule OpenaiEx.Beta.Thread.Run do +defmodule OpenaiEx.Beta.Threads.Runs do @moduledoc """ This module provides an implementation of the OpenAI run API. The API reference can be found at https://platform.openai.com/docs/api-reference/runs. @@ -42,7 +42,7 @@ defmodule OpenaiEx.Beta.Thread.Run do Example usage: - iex> _request = OpenaiEx.Beta.Thread.Run.new(thread_id: "thread_foo", assistant_id: "assistant_bar") + iex> _request = OpenaiEx.Beta.Threads.Runs.new(thread_id: "thread_foo", assistant_id: "assistant_bar") %{assistant_id: "assistant_bar", thread_id: "thread_foo"} """ diff --git a/lib/openai_ex/beta/run_step.ex b/lib/openai_ex/beta/threads_runs_step.ex similarity index 93% rename from lib/openai_ex/beta/run_step.ex rename to lib/openai_ex/beta/threads_runs_step.ex index a21e3ba..4eb11a8 100644 --- a/lib/openai_ex/beta/run_step.ex +++ b/lib/openai_ex/beta/threads_runs_step.ex @@ -1,4 +1,4 @@ -defmodule OpenaiEx.Beta.Run.Step do +defmodule OpenaiEx.Beta.Threads.Runs.Steps do @moduledoc false defp ep_url(thread_id, run_id, step_id \\ nil) do "/threads/#{thread_id}/runs/#{run_id}/steps" <> diff --git a/lib/openai_ex/chat_completion.ex b/lib/openai_ex/chat_completions.ex similarity index 89% rename from lib/openai_ex/chat_completion.ex rename to lib/openai_ex/chat_completions.ex index f76c817..3eccf17 100644 --- a/lib/openai_ex/chat_completion.ex +++ b/lib/openai_ex/chat_completions.ex @@ -1,4 +1,4 @@ -defmodule OpenaiEx.ChatCompletion do +defmodule OpenaiEx.Chat.Completions do @moduledoc """ This module provides an implementation of the OpenAI chat completions API. The API reference can be found at https://platform.openai.com/docs/api-reference/chat/completions. @@ -55,10 +55,10 @@ defmodule OpenaiEx.ChatCompletion do Example usage: - iex> _request = OpenaiEx.ChatCompletion.new(model: "davinci", messages: [OpenaiEx.ChatMessage.user("Hello, world!")]) + iex> _request = OpenaiEx.Chat.Completions.new(model: "davinci", messages: [OpenaiEx.ChatMessage.user("Hello, world!")]) %{messages: [%{content: "Hello, world!", role: "user"}], model: "davinci"} - iex> _request = OpenaiEx.ChatCompletion.new(%{model: "davinci", messages: [OpenaiEx.ChatMessage.user("Hello, world!")]}) + iex> _request = OpenaiEx.Chat.Completions.new(%{model: "davinci", messages: [OpenaiEx.ChatMessage.user("Hello, world!")]}) %{messages: [%{content: "Hello, world!", role: "user"}], model: "davinci"} """ diff --git a/lib/openai_ex/embedding.ex b/lib/openai_ex/embeddings.ex similarity index 88% rename from lib/openai_ex/embedding.ex rename to lib/openai_ex/embeddings.ex index cc9fc9e..39ecb2a 100644 --- a/lib/openai_ex/embedding.ex +++ b/lib/openai_ex/embeddings.ex @@ -1,4 +1,4 @@ -defmodule OpenaiEx.Embedding do +defmodule OpenaiEx.Embeddings do @moduledoc """ This module provides an implementation of the OpenAI embeddings API. The API reference can be found at https://platform.openai.com/docs/api-reference/embeddings. @@ -35,10 +35,10 @@ defmodule OpenaiEx.Embedding do Example usage: - iex> _request = OpenaiEx.Embedding.new(model: "davinci", input: "This is a test") + iex> _request = OpenaiEx.Embeddings.new(model: "davinci", input: "This is a test") %{input: "This is a test", model: "davinci"} - iex> _request = OpenaiEx.Embedding.new(%{model: "davinci", input: "This is a test"}) + iex> _request = OpenaiEx.Embeddings.new(%{model: "davinci", input: "This is a test"}) %{input: "This is a test", model: "davinci"} """ diff --git a/lib/openai_ex/file.ex b/lib/openai_ex/files.ex similarity index 99% rename from lib/openai_ex/file.ex rename to lib/openai_ex/files.ex index 85ecbfb..1067948 100644 --- a/lib/openai_ex/file.ex +++ b/lib/openai_ex/files.ex @@ -1,4 +1,4 @@ -defmodule OpenaiEx.File do +defmodule OpenaiEx.Files do @moduledoc """ This module provides an implementation of the OpenAI files API. The API reference can be found at https://platform.openai.com/docs/api-reference/files. diff --git a/lib/openai_ex/fine_tuning.ex b/lib/openai_ex/fine_tuning_jobs.ex similarity index 98% rename from lib/openai_ex/fine_tuning.ex rename to lib/openai_ex/fine_tuning_jobs.ex index 722d674..fb9d81c 100644 --- a/lib/openai_ex/fine_tuning.ex +++ b/lib/openai_ex/fine_tuning_jobs.ex @@ -1,4 +1,4 @@ -defmodule OpenaiEx.FineTuning.Job do +defmodule OpenaiEx.FineTuning.Jobs do @moduledoc """ This module provides an implementation of the OpenAI fine-tuning job API. The API reference can be found at https://platform.openai.com/docs/api-reference/fine-tuning. diff --git a/lib/openai_ex/image.ex b/lib/openai_ex/images.ex similarity index 94% rename from lib/openai_ex/image.ex rename to lib/openai_ex/images.ex index a6b0abc..9153b15 100644 --- a/lib/openai_ex/image.ex +++ b/lib/openai_ex/images.ex @@ -1,8 +1,8 @@ -defmodule OpenaiEx.Image do +defmodule OpenaiEx.Images do @moduledoc """ This module provides an implementation of the OpenAI images API. The API reference can be found at https://platform.openai.com/docs/api-reference/images. """ - alias OpenaiEx.Image + alias OpenaiEx.Images @doc """ Calls the image generation endpoint. @@ -39,7 +39,7 @@ defmodule OpenaiEx.Image do def edit(openai = %OpenaiEx{}, image_edit = %{}) do openai |> OpenaiEx.Http.post("/images/edits", - multipart: image_edit |> OpenaiEx.Http.to_multi_part_form_data(Image.Edit.file_fields()) + multipart: image_edit |> OpenaiEx.Http.to_multi_part_form_data(Images.Edit.file_fields()) ) end @@ -61,7 +61,7 @@ defmodule OpenaiEx.Image do openai |> OpenaiEx.Http.post("/images/variations", multipart: - image_variation |> OpenaiEx.Http.to_multi_part_form_data(Image.Variation.file_fields()) + image_variation |> OpenaiEx.Http.to_multi_part_form_data(Images.Variation.file_fields()) ) end end diff --git a/lib/openai_ex/image_edit.ex b/lib/openai_ex/images_edit.ex similarity index 96% rename from lib/openai_ex/image_edit.ex rename to lib/openai_ex/images_edit.ex index 14a5305..29e8e9d 100644 --- a/lib/openai_ex/image_edit.ex +++ b/lib/openai_ex/images_edit.ex @@ -1,4 +1,4 @@ -defmodule OpenaiEx.Image.Edit do +defmodule OpenaiEx.Images.Edit do @moduledoc """ This module provides constructors for OpenAI Image Edit API request structure. The API reference can be found at https://platform.openai.com/docs/api-reference/images/create-edit. diff --git a/lib/openai_ex/image_generate.ex b/lib/openai_ex/images_generate.ex similarity index 84% rename from lib/openai_ex/image_generate.ex rename to lib/openai_ex/images_generate.ex index c91d8ed..ec40b77 100644 --- a/lib/openai_ex/image_generate.ex +++ b/lib/openai_ex/images_generate.ex @@ -1,4 +1,4 @@ -defmodule OpenaiEx.Image.Generate do +defmodule OpenaiEx.Images.Generate do @moduledoc """ This module provides constructors for the OpenAI image generation API. The API reference can be found at https://platform.openai.com/docs/api-reference/images/create. @@ -42,10 +42,10 @@ defmodule OpenaiEx.Image.Generate do Example usage: - iex> _request = OpenaiEx.Image.Generate.new(prompt: "This is a test") + iex> _request = OpenaiEx.Images.Generate.new(prompt: "This is a test") %{prompt: "This is a test"} - iex> _request = OpenaiEx.Image.Generate.new(%{prompt: "This is a test"}) + iex> _request = OpenaiEx.Images.Generate.new(%{prompt: "This is a test"}) %{prompt: "This is a test"} """ def new(args = [_ | _]) do diff --git a/lib/openai_ex/image_variation.ex b/lib/openai_ex/images_variation.ex similarity index 95% rename from lib/openai_ex/image_variation.ex rename to lib/openai_ex/images_variation.ex index 40f9481..422bb52 100644 --- a/lib/openai_ex/image_variation.ex +++ b/lib/openai_ex/images_variation.ex @@ -1,4 +1,4 @@ -defmodule OpenaiEx.Image.Variation do +defmodule OpenaiEx.Images.Variation do @moduledoc """ This module provides constructors for OpenAI Image Variation API request structure. The API reference can be found at https://platform.openai.com/docs/api-reference/images/create-variation. diff --git a/lib/openai_ex/model.ex b/lib/openai_ex/models.ex similarity index 96% rename from lib/openai_ex/model.ex rename to lib/openai_ex/models.ex index 2ae584d..52024e5 100644 --- a/lib/openai_ex/model.ex +++ b/lib/openai_ex/models.ex @@ -1,4 +1,4 @@ -defmodule OpenaiEx.Model do +defmodule OpenaiEx.Models do @moduledoc """ This module provides an implementation of the OpenAI Models API. Information about these models can be found at https://platform.openai.com/docs/models. """ diff --git a/lib/openai_ex/moderation.ex b/lib/openai_ex/moderations.ex similarity index 89% rename from lib/openai_ex/moderation.ex rename to lib/openai_ex/moderations.ex index 6ddc261..894cde1 100644 --- a/lib/openai_ex/moderation.ex +++ b/lib/openai_ex/moderations.ex @@ -1,4 +1,4 @@ -defmodule OpenaiEx.Moderation do +defmodule OpenaiEx.Moderations do @moduledoc """ This module provides an implementation of the OpenAI moderation API. The API reference can be found at https://platform.openai.com/docs/api-reference/moderations. @@ -29,12 +29,12 @@ defmodule OpenaiEx.Moderation do Example usage: - iex> OpenaiEx.Moderation.new(input: "This is a test") + iex> OpenaiEx.Moderations.new(input: "This is a test") %{ input: "This is a test" } - iex> OpenaiEx.Moderation.new(%{input: "This is a test"}) + iex> OpenaiEx.Moderations.new(%{input: "This is a test"}) %{ input: "This is a test" } diff --git a/notebooks/dlai_orderbot.livemd b/notebooks/dlai_orderbot.livemd index d869479..0b91874 100644 --- a/notebooks/dlai_orderbot.livemd +++ b/notebooks/dlai_orderbot.livemd @@ -7,7 +7,7 @@ Mix.install([ ]) alias OpenaiEx -alias OpenaiEx.ChatCompletion +alias OpenaiEx.Chat.Completions alias OpenaiEx.ChatMessage ``` @@ -35,7 +35,7 @@ openai = ```elixir defmodule OpenaiEx.Notebooks.DlaiOrderbot do alias OpenaiEx - alias OpenaiEx.ChatCompletion + alias OpenaiEx.Chat.Completions def create_chat_req(args = [_ | _]) do args diff --git a/notebooks/images.livemd b/notebooks/images.livemd index 7ab1b0a..2eafdfb 100644 --- a/notebooks/images.livemd +++ b/notebooks/images.livemd @@ -9,7 +9,7 @@ Mix.install([ ]) alias OpenaiEx -alias OpenaiEx.Image +alias OpenaiEx.Images ``` ## Simple Kino UI diff --git a/notebooks/streaming_orderbot.livemd b/notebooks/streaming_orderbot.livemd index 834fca8..a5baa7c 100644 --- a/notebooks/streaming_orderbot.livemd +++ b/notebooks/streaming_orderbot.livemd @@ -7,7 +7,7 @@ Mix.install([ ]) alias OpenaiEx -alias OpenaiEx.ChatCompletion +alias OpenaiEx.Chat.Completions alias OpenaiEx.ChatMessage ``` @@ -27,7 +27,7 @@ openai = System.fetch_env!("LB_OPENAI_API_KEY") |> OpenaiEx.new() ```elixir defmodule OpenaiEx.Notebooks.StreamingOrderbot do alias OpenaiEx - alias OpenaiEx.ChatCompletion + alias OpenaiEx.Chat.Completions require Logger def set_task_pid(task_pid) do diff --git a/notebooks/userguide.livemd b/notebooks/userguide.livemd index ab4c81c..7fd9b26 100644 --- a/notebooks/userguide.livemd +++ b/notebooks/userguide.livemd @@ -2,8 +2,8 @@ ```elixir Mix.install([ - {:openai_ex, "~> 0.5.9"}, - # {:openai_ex, path: Path.join(__DIR__, "..")}, + # {:openai_ex, "~> 0.5.9"}, + {:openai_ex, path: Path.join(__DIR__, "..")}, {:kino, "~> 0.12.3"} ]) ``` @@ -158,9 +158,9 @@ These methods will be supported as long as the Azure version does not deviate to To list all available models, use the [`Model.list()`](https://platform.openai.com/docs/api-reference/models/list) function: ```elixir -alias OpenaiEx.Model +alias OpenaiEx.Models -openai |> Model.list() +openai |> Models.list() ``` ### Retrieve Models @@ -168,7 +168,7 @@ openai |> Model.list() To retrieve information about a specific model, use the [`Model.retrieve()`](https://platform.openai.com/docs/api-reference/models/retrieve) function: ```elixir -openai |> Model.retrieve("gpt-3.5-turbo") +openai |> Models.retrieve("gpt-3.5-turbo") ``` For more information on using models, see the [OpenAI API Models reference](https://platform.openai.com/docs/api-reference/models). @@ -178,12 +178,12 @@ For more information on using models, see the [OpenAI API Models reference](http To generate a chat completion, you need to define a chat completion request structure using the `ChatCompletion.new()` function. This function takes several parameters, such as the model ID and a list of chat messages. We have a module `ChatMessage` which helps create messages in the [chat format](https://platform.openai.com/docs/guides/chat/introduction). ```elixir -alias OpenaiEx.ChatCompletion +alias OpenaiEx.Chat.Completions alias OpenaiEx.ChatMessage alias OpenaiEx.MsgContent chat_req = - ChatCompletion.new( + Completions.new( model: "gpt-3.5-turbo", messages: [ ChatMessage.user( @@ -206,7 +206,7 @@ ChatMessage.user( You can generate a chat completion using the [`ChatCompletion.create()`](https://platform.openai.com/docs/api-reference/chat/completions/create) function: ```elixir -chat_response = openai |> ChatCompletion.create(chat_req) +chat_response = openai |> Completions.create(chat_req) ``` For a more in-depth example of `ChatCompletion`, check out the [Deeplearning.AI OrderBot Livebook](https://hexdocs.pm/openai_ex/dlai_orderbot.html). @@ -216,7 +216,7 @@ You can also call the endpoint and have it stream the response. This returns the To use the stream option, call the `ChatCompletion.create()` function with `stream: true` ```elixir -chat_stream = openai |> ChatCompletion.create(chat_req, stream: true) +chat_stream = openai |> Completions.create(chat_req, stream: true) IO.puts(inspect(chat_stream)) IO.puts(inspect(chat_stream.task_pid)) chat_stream.body_stream |> Stream.flat_map(& &1) |> Enum.each(fn x -> IO.puts(inspect(x)) end) @@ -264,7 +264,7 @@ rev_msgs = [ ] fn_req = - ChatCompletion.new( + Completions.new( model: "gpt-3.5-turbo", messages: rev_msgs |> Enum.reverse(), tools: [tool_spec], @@ -275,7 +275,7 @@ fn_req = Next, we call the OpenAI endpoint to get a response that includes the function call. ```elixir -fn_response = openai |> ChatCompletion.create(fn_req) +fn_response = openai |> Completions.create(fn_req) ``` We extract the function call from the response and call the appropriate function with the given parameters. In this example, we define a map of functions that maps function names to their implementations. We then use the function name and arguments from the function call to look up the appropriate function and call it with the given parameters. @@ -313,12 +313,12 @@ We then pass the returned value back to the ChatCompletion endpoint with the con latest_msgs = [ChatMessage.tool(tool_id, fn_name, fn_value) | [fn_message | rev_msgs]] fn_req_2 = - ChatCompletion.new( + Completions.new( model: "gpt-3.5-turbo", messages: latest_msgs |> Enum.reverse() ) -fn_response_2 = openai |> ChatCompletion.create(fn_req_2) +fn_response_2 = openai |> Completions.create(fn_req_2) ``` The final response includes the result of the function call integrated into the conversation. @@ -330,15 +330,15 @@ The final response includes the result of the function call integrated into the We define the image creation request structure using the `Image.new` function ```elixir -alias OpenaiEx.Image +alias OpenaiEx.Images -img_req = Image.Generate.new(prompt: "An adorable baby sea otter", size: "256x256", n: 1) +img_req = Images.Generate.new(prompt: "An adorable baby sea otter", size: "256x256", n: 1) ``` Then call the [`Image.create()`](https://platform.openai.com/docs/api-reference/images/create) function to generate the images. ```elixir -img_response = openai |> Image.generate(img_req) +img_response = openai |> Images.generate(img_req) ``` For more information on generating images, see the [OpenAI API Image reference](https://platform.openai.com/docs/api-reference/images). @@ -372,7 +372,7 @@ img_to_expmt = fetched_images |> List.first() ### Edit Image -We define an image edit request structure using the `Image.Edit.new()` function. This function requires an image and a mask. For the image, we will use the one that we received. Let's load the mask from a URL. +We define an image edit request structure using the `Images.Edit.new()` function. This function requires an image and a mask. For the image, we will use the one that we received. Let's load the mask from a URL. ```elixir # if you're having problems downloading raw github content, you may need to manually set your DNS server to "8.8.8.8" (google) @@ -388,7 +388,7 @@ Set up the image edit request with image, mask and prompt. ```elixir img_edit_req = - Image.Edit.new( + Images.Edit.new( image: img_to_expmt, mask: star_mask, size: "256x256", @@ -399,7 +399,7 @@ img_edit_req = We then call the [`Image.create_edit()`]() function ```elixir -img_edit_response = openai |> Image.edit(img_edit_req) +img_edit_response = openai |> Images.edit(img_edit_req) ``` and view the result @@ -411,10 +411,10 @@ img_edit_response["data"] ### Image Variations -We define an image variation request structure using the `Image.Variation.new()` function. This function requires an image. +We define an image variation request structure using the `Images.Variation.new()` function. This function requires an image. ```elixir -img_var_req = Image.Variation.new(image: img_to_expmt, size: "256x256") +img_var_req = Images.Variation.new(image: img_to_expmt, size: "256x256") ``` Then call the [`Image.create_variation()`](https://platform.openai.com/docs/api-reference/images/create-variation) function to generate the images. @@ -424,7 +424,7 @@ Then call the [`Image.create_variation()`](https://platform.openai.com/docs/api- ### ```elixir -img_var_response = openai |> Image.create_variation(img_var_req) +img_var_response = openai |> Images.create_variation(img_var_req) ``` ```elixir @@ -439,10 +439,10 @@ For more information on images variations, see the [OpenAI API Image Variations Define the embedding request structure using `Embedding.new`. ```elixir -alias OpenaiEx.Embedding +alias OpenaiEx.Embeddings emb_req = - Embedding.new( + Embeddings.new( model: "text-embedding-ada-002", input: "The food was delicious and the waiter..." ) @@ -451,7 +451,7 @@ emb_req = Then call the [`Embedding.create()`]() function. ```elixir -emb_response = openai |> Embedding.create(emb_req) +emb_response = openai |> Embeddings.create(emb_req) ``` For more information on generating embeddings, see the [OpenAI API Embedding reference](https://platform.openai.com/docs/api-reference/embeddings/create) @@ -527,9 +527,9 @@ For more information on the audio endpoints see the [Openai API Audio Reference] To request all files that belong to the user organization, call the [`File.list()`](https://platform.openai.com/docs/api-reference/files/list) function ```elixir -alias OpenaiEx.File +alias OpenaiEx.Files -openai |> File.list() +openai |> Files.list() ``` ### Upload files @@ -543,19 +543,19 @@ fine_tune_file = OpenaiEx.new_file(name: ftf_url, content: fetch_blob.(ftf_url)) # fine_tune_file = OpenaiEx.new_file(path: Path.join(__DIR__, "../assets/fine-tune.jsonl")) -upload_req = File.new_upload(file: fine_tune_file, purpose: "fine-tune") +upload_req = Files.new_upload(file: fine_tune_file, purpose: "fine-tune") ``` Then we call the [`File.create()`](https://platform.openai.com/docs/api-reference/files/upload) function to upload the file ```elixir -upload_res = openai |> File.create(upload_req) +upload_res = openai |> Files.create(upload_req) ``` We can verify that the file has been uploaded by calling ```elixir -openai |> File.list() +openai |> Files.list() ``` We grab the file id from the previous response value to use in the following samples @@ -569,7 +569,7 @@ file_id = upload_res["id"] In order to retrieve meta information on a file, we simply call the [`File.retrieve()`]() function with the given id ```elixir -openai |> File.retrieve(file_id) +openai |> Files.retrieve(file_id) ``` ### Retrieve file content @@ -577,7 +577,7 @@ openai |> File.retrieve(file_id) Similarly to download the file contents, we call [`File.download()`]() ```elixir -openai |> File.download(file_id) +openai |> Files.download(file_id) ``` ### Delete file @@ -585,13 +585,13 @@ openai |> File.download(file_id) Finally, we can delete the file by calling [`File.delete()`](https://platform.openai.com/docs/api-reference/files/delete) ```elixir -openai |> File.delete(file_id) +openai |> Files.delete(file_id) ``` Verify that the file has been deleted by listing files again ```elixir -openai |> File.list() +openai |> Files.list() ``` ## FineTuning Job @@ -599,46 +599,46 @@ openai |> File.list() To run a fine-tuning job, we minimally need a training file. We will re-run the file creation request above. ```elixir -upload_res = openai |> File.create(upload_req) +upload_res = openai |> Files.create(upload_req) ``` -Next we call `FineTuning.Job.new()` to create a new request structure +Next we call `FineTuning.Jobs.new()` to create a new request structure ```elixir alias OpenaiEx.FineTuning -ft_req = FineTuning.Job.new(model: "davinci-002", training_file: upload_res["id"]) +ft_req = FineTuning.Jobs.new(model: "davinci-002", training_file: upload_res["id"]) ``` -To begin the fine tune, we call the [`FineTune.create()`](https://platform.openai.com/docs/api-reference/fine-tunes/create) function +To begin the fine tune, we call the [`FineTuning.Jobs.create()`](https://platform.openai.com/docs/api-reference/fine-tunes/create) function ```elixir -ft_res = openai |> FineTuning.Job.create(ft_req) +ft_res = openai |> FineTuning.Jobs.create(ft_req) ``` -We can list all fine tunes by calling [`FineTune.list()`](https://platform.openai.com/docs/api-reference/fine-tunes/list) +We can list all fine tunes by calling [`FineTuning.Jobs.list()`](https://platform.openai.com/docs/api-reference/fine-tunes/list) ```elixir -openai |> FineTuning.Job.list() +openai |> FineTuning.Jobs.list() ``` The function [`FineTune.retrieve()`](https://platform.openai.com/docs/api-reference/fine-tunes/retrieve) gets the details of a particular fine tune. ```elixir ft_id = ft_res["id"] -openai |> FineTuning.Job.retrieve(fine_tuning_job_id: ft_id) +openai |> FineTuning.Jobs.retrieve(fine_tuning_job_id: ft_id) ``` -and [`FineTune.list_events()`](https://platform.openai.com/docs/api-reference/fine-tunes/events) can be called to get the events +and [`FineTuning.Jobs.list_events()`](https://platform.openai.com/docs/api-reference/fine-tunes/events) can be called to get the events ```elixir -openai |> FineTuning.Job.list_events(fine_tuning_job_id: ft_id) +openai |> FineTuning.Jobs.list_events(fine_tuning_job_id: ft_id) ``` -To cancel a Fine Tune job, call [`FineTune.cancel()`](https://platform.openai.com/docs/api-reference/fine-tunes/cancel) +To cancel a Fine Tune job, call [`FineTuning.Jobs.cancel()`](https://platform.openai.com/docs/api-reference/fine-tunes/cancel) ```elixir -openai |> FineTuning.Job.cancel(fine_tuning_job_id: ft_id) +openai |> FineTuning.Jobs.cancel(fine_tuning_job_id: ft_id) ``` A fine tuned model can be deleted by calling the [`Model.delete()`](https://platform.openai.com/docs/api-reference/fine-tunes/delete-model) @@ -647,7 +647,7 @@ A fine tuned model can be deleted by calling the [`Model.delete()`](https://plat ft_model = ft_res["fine_tuned_model"] unless is_nil(ft_model) do - openai |> Model.delete(ft_model) + openai |> Models.delete(ft_model) end ``` @@ -658,15 +658,15 @@ For more information on the fine tune endpoints see the [Openai API Moderation R We use the moderation API by calling `Moderation.new()` to create a new request ```elixir -alias OpenaiEx.Moderation +alias OpenaiEx.Moderations -mod_req = Moderation.new(input: "I want to kill people") +mod_req = Moderations.new(input: "I want to kill people") ``` The call the function [`Moderation.create()`](https://platform.openai.com/docs/api-reference/moderations/create) ```elixir -mod_res = openai |> Moderation.create(mod_req) +mod_res = openai |> Moderations.create(mod_req) ``` For more information on the moderation endpoints see the [Openai API Moderation Reference](https://platform.openai.com/docs/api-reference/moderations) @@ -674,7 +674,7 @@ For more information on the moderation endpoints see the [Openai API Moderation ## Assistant ```elixir -alias OpenaiEx.Beta.Assistant +alias OpenaiEx.Beta.Assistants ``` ### Create Assistant @@ -685,7 +685,7 @@ First, we setup the create request parameters. This request sets up an Assistant ```elixir math_assistant_req = - Assistant.new( + Assistants.new( instructions: "You are a personal math tutor. When asked a question, write and run Python code to answer the question.", name: "Math Tutor", @@ -697,7 +697,7 @@ math_assistant_req = Then we call the create function ```elixir -asst = openai |> Assistant.create(math_assistant_req) +asst = openai |> Assistants.create(math_assistant_req) ``` ### Retrieve Assistant @@ -711,7 +711,7 @@ assistant_id = asst["id"] which can then be used to retrieve the Assistant fields, using the [`Assistant.retrieve()`](https://platform.openai.com/docs/api-reference/assistants/getAssistant) function. ```elixir -openai |> Assistant.retrieve(assistant_id) +openai |> Assistants.retrieve(assistant_id) ``` ### Modify Assistant @@ -721,11 +721,11 @@ Once created, an assistant can be modified using the [`Assistant.update()`](http Now we will show an example assistant request using the retrieval tool with a set of files. First we set up the files (in this case a sample HR document) by uploading using the `File` API. ```elixir -alias OpenaiEx.File +alias OpenaiEx.Files hr_file = OpenaiEx.new_file(path: Path.join(__DIR__, "../assets/cyberdyne.txt")) -hr_upload_req = File.new_upload(file: hr_file, purpose: "assistants") -hr_upload_res = openai |> File.create(hr_upload_req) +hr_upload_req = Files.new_upload(file: hr_file, purpose: "assistants") +hr_upload_res = openai |> Files.create(hr_upload_req) ``` ```elixir @@ -736,7 +736,7 @@ Next we create the update request ```elixir hr_assistant_req = - Assistant.new( + Assistants.new( instructions: "You are an HR bot, and you have access to files to answer employee questions about company policies. Always respond with info from one of the files.", name: "HR Helper", @@ -749,7 +749,7 @@ hr_assistant_req = Finally we call the endpoint to modify the `Assistant` ```elixir -asst = openai |> Assistant.update(assistant_id, hr_assistant_req) +asst = openai |> Assistants.update(assistant_id, hr_assistant_req) ``` ### Delete Assistant @@ -757,7 +757,7 @@ asst = openai |> Assistant.update(assistant_id, hr_assistant_req) Finally we can delete assistants using the [`Assistant.delete()`](https://platform.openai.com/docs/api-reference/assistants/deleteAssistant) function ```elixir -openai |> Assistant.delete(assistant_id) +openai |> Assistants.delete(assistant_id) ``` ### List Assistants @@ -765,7 +765,7 @@ openai |> Assistant.delete(assistant_id) We use [`Assistant.list()`](https://platform.openai.com/docs/api-reference/assistants/listAssistants) to get a list of assistants ```elixir -assts = openai |> Assistant.list() +assts = openai |> Assistants.list() ``` ### Create Assistant File @@ -775,21 +775,21 @@ Use [`Assistants.File.create()`](https://platform.openai.com/docs/api-reference/ Let us re-create the first assistant above ```elixir -math_asst = openai |> Assistant.create(math_assistant_req) +math_asst = openai |> Assistants.create(math_assistant_req) ``` Now we can attach the file that we used earlier to this new assistant. ```elixir math_assistant_id = math_asst["id"] -asst_f = Assistant.File.new(assistant_id: math_assistant_id, file_id: file_id) -asst_file = openai |> Assistant.File.create(asst_f) +asst_f = Assistants.File.new(assistant_id: math_assistant_id, file_id: file_id) +asst_file = openai |> Assistants.File.create(asst_f) ``` Let's retrieve the assistant to see if it was updated with the file id (check `file_ids` field value). ```elixir -openai |> Assistant.retrieve(math_assistant_id) +openai |> Assistants.retrieve(math_assistant_id) ``` ### Retreive Assistant File @@ -797,7 +797,7 @@ openai |> Assistant.retrieve(math_assistant_id) We can retrieve an assistant file by using [`Assistants.File.retrieve()`](https://platform.openai.com/docs/api-reference/assistants/getAssistantFile) ```elixir -openai |> Assistant.File.retrieve(asst_f) +openai |> Assistants.File.retrieve(asst_f) ``` ### Delete Assistant File @@ -805,13 +805,13 @@ openai |> Assistant.File.retrieve(asst_f) [`Assistant.File.delete()`] can be used to detach a file from the assistant. ```elixir -openai |> Assistant.File.delete(asst_f) +openai |> Assistants.File.delete(asst_f) ``` Verify detach by retrieving the assistant again (check `file_ids` field value) ```elixir -openai |> Assistant.retrieve(math_assistant_id) +openai |> Assistants.retrieve(math_assistant_id) ``` ### List Assistant Files @@ -819,15 +819,15 @@ openai |> Assistant.retrieve(math_assistant_id) We can list all files attached to an assistant with [`Assistant.File.list()`](https://platform.openai.com/docs/api-reference/assistants/listAssistantFiles) ```elixir -list_req = Assistant.File.new_list(assistant_id: math_assistant_id) +list_req = Assistants.File.new_list(assistant_id: math_assistant_id) -openai |> Assistant.File.list(list_req) +openai |> Assistants.File.list(list_req) ``` ## Thread ```elixir -alias OpenaiEx.Beta.Thread +alias OpenaiEx.Beta.Threads ``` ### Create thread @@ -837,14 +837,14 @@ Use the [`Thread.create()`] function to create threads. A thread can be created ```elixir alias OpenaiEx.ChatMessage -empty_thread = openai |> Thread.create() +empty_thread = openai |> Threads.create() msg_hr = ChatMessage.user("What company do we work at?", [file_id]) msg_ai = ChatMessage.user("How does AI work? Explain it in simple terms.") -thrd_req = Thread.new(messages: [msg_hr, msg_ai]) +thrd_req = Threads.new(messages: [msg_hr, msg_ai]) -thread = openai |> Thread.create(thrd_req) +thread = openai |> Threads.create(thrd_req) ``` ### Retrieve thread @@ -853,7 +853,7 @@ thread = openai |> Thread.create(thrd_req) ```elixir thread_id = thread["id"] -openai |> Thread.retrieve(thread_id) +openai |> Threads.retrieve(thread_id) ``` ### Modify thread @@ -861,7 +861,7 @@ openai |> Thread.retrieve(thread_id) The metadata for a thread can be modified using [`Thread.update()`](https://platform.openai.com/docs/api-reference/threads/modifyThread) ```elixir -openai |> Thread.update(thread_id, %{metadata: %{modified: "true", user: "abc123"}}) +openai |> Threads.update(thread_id, %{metadata: %{modified: "true", user: "abc123"}}) ``` ### Delete thread @@ -869,19 +869,19 @@ openai |> Thread.update(thread_id, %{metadata: %{modified: "true", user: "abc123 Use [`Thread.delete()`](https://platform.openai.com/docs/api-reference/threads/deleteThread) to delete a thread ```elixir -openai |> Thread.delete(thread_id) +openai |> Threads.delete(thread_id) ``` Verify deletion ```elixir -openai |> Thread.retrieve(thread_id) +openai |> Threads.retrieve(thread_id) ``` ## Messages ```elixir -alias OpenaiEx.Beta.Thread.Message +alias OpenaiEx.Beta.Threads.Messages ``` ### Create message @@ -891,7 +891,7 @@ You can create a single message for a thread using [`Message.create()`](https:// ```elixir thread_id = empty_thread["id"] -message = openai |> Message.create(thread_id, msg_hr) +message = openai |> Messages.create(thread_id, msg_hr) ``` ### Retrieve message @@ -900,7 +900,7 @@ Use [`Message.retrieve()`] to retrieve a message ```elixir message_id = message["id"] -openai |> Message.retrieve(%{thread_id: thread_id, message_id: message_id}) +openai |> Messages.retrieve(%{thread_id: thread_id, message_id: message_id}) ``` ### Modify message @@ -909,9 +909,9 @@ The metadata for a message can be modified by [`Message.update()`] ```elixir metadata = %{modified: "true", user: "abc123"} -upd_msg_req = Message.new(thread_id: thread_id, message_id: message_id, metadata: metadata) +upd_msg_req = Messages.new(thread_id: thread_id, message_id: message_id, metadata: metadata) -message = openai |> Message.update(upd_msg_req) +message = openai |> Messages.update(upd_msg_req) ``` ### List messages @@ -919,7 +919,7 @@ message = openai |> Message.update(upd_msg_req) Use [`Message.list()`] to get all the messages for a given thread ```elixir -openai |> Message.list(thread_id) +openai |> Messages.list(thread_id) ``` ### Retrieve message file @@ -927,7 +927,8 @@ openai |> Message.list(thread_id) Retrieve a message file using [`Message.File.retrieve()`] ```elixir -openai |> Message.File.retrieve(%{thread_id: thread_id, message_id: message_id, file_id: file_id}) +openai +|> Messages.File.retrieve(%{thread_id: thread_id, message_id: message_id, file_id: file_id}) ``` ### List message files @@ -935,13 +936,13 @@ openai |> Message.File.retrieve(%{thread_id: thread_id, message_id: message_id, You can list all message files using [`Message.File.list()`] ```elixir -openai |> Message.File.list(%{thread_id: thread_id, message_id: message_id}) +openai |> Messages.File.list(%{thread_id: thread_id, message_id: message_id}) ``` ## Runs ```elixir -alias OpenaiEx.Beta.Thread.Run +alias OpenaiEx.Beta.Threads.Runs ``` ### Create run @@ -949,9 +950,9 @@ alias OpenaiEx.Beta.Thread.Run A run represents an execution on a thread. Use to [`Run.create()`](https://platform.openai.com/docs/api-reference/runs/createRun) with an assistant on a thread ```elixir -run_req = Run.new(thread_id: thread_id, assistant_id: math_assistant_id) +run_req = Runs.new(thread_id: thread_id, assistant_id: math_assistant_id) -run = openai |> Run.create(run_req) +run = openai |> Runs.create(run_req) ``` ### Retrieve run @@ -960,7 +961,7 @@ Retrieve a run using [`Run.retrieve()`](https://platform.openai.com/docs/api-ref ```elixir run_id = run["id"] -openai |> Run.retrieve(%{thread_id: thread_id, run_id: run_id}) +openai |> Runs.retrieve(%{thread_id: thread_id, run_id: run_id}) ``` ### Modify run @@ -969,7 +970,7 @@ The run metadata can be modified using the [`Run.update()`](https://platform.ope ```elixir openai -|> Run.update(%{ +|> Runs.update(%{ thread_id: thread_id, run_id: run_id, metadata: %{user_id: "user_zmVY6FvuBDDwIqM4KgH"} @@ -981,7 +982,7 @@ openai List the runs belonging to a thread using [`Run.list()`](https://platform.openai.com/docs/api-reference/runs/listRuns) ```elixir -openai |> Run.list(thread_id) +openai |> Runs.list(thread_id) ``` ### Submit tool outputs to a run @@ -990,7 +991,7 @@ When a run has the `status`: "requires_action" and `required_action.type` is `su ```elixir openai -|> Run.submit_tool_outputs(%{ +|> Runs.submit_tool_outputs(%{ thread_id: thread_id, run_id: run_id, tool_outputs: [%{tool_call_id: "foobar", output: "28C"}] @@ -1002,7 +1003,7 @@ openai You can cancel a run `in_progress` using [`Run.cancel()`](https://platform.openai.com/docs/api-reference/runs/cancelRun) ```elixir -openai |> Run.cancel(%{thread_id: thread_id, run_id: run_id}) +openai |> Runs.cancel(%{thread_id: thread_id, run_id: run_id}) ``` ### Create thread and run @@ -1013,4 +1014,4 @@ Use [`Run.create_and_run()`](https://platform.openai.com/docs/api-reference/runs ``` - + diff --git a/test/openai_ex_test.exs b/test/openai_ex_test.exs index d4f22fc..89c4090 100644 --- a/test/openai_ex_test.exs +++ b/test/openai_ex_test.exs @@ -1,12 +1,12 @@ defmodule OpenaiExTest do use ExUnit.Case - doctest OpenaiEx.ChatCompletion + doctest OpenaiEx.Chat.Completions doctest OpenaiEx.Completion doctest OpenaiEx.ChatMessage - doctest OpenaiEx.Embedding - doctest OpenaiEx.Image.Generate - doctest OpenaiEx.Moderation + doctest OpenaiEx.Embeddings + doctest OpenaiEx.Images.Generate + doctest OpenaiEx.Moderations doctest OpenaiEx.MsgContent - doctest OpenaiEx.Beta.Assistant - doctest OpenaiEx.Beta.Thread.Run + doctest OpenaiEx.Beta.Assistants + doctest OpenaiEx.Beta.Threads.Runs end