Packages
openai_ex
0.5.8
0.9.21
0.9.20
0.9.19
0.9.18
0.9.17
0.9.16
0.9.15
0.9.14
0.9.13
0.9.12
0.9.11
0.9.10
0.9.9
0.9.8
0.9.7
0.9.6
0.9.5
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.0
0.6.5
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.9
0.5.8
0.5.7
0.5.6
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.2
0.4.1
0.4.0
0.3.0
0.2.3
0.2.2
0.2.1
0.2.0
0.1.9
0.1.8
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
Community maintained Elixir library for OpenAI API
Current section
Files
Jump to
Current section
Files
lib/openai_ex/beta/run.ex
defmodule OpenaiEx.Beta.Thread.Run 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.
## API Fields
The following fields can be used as parameters for the messages API:
- `:assistant_id`
- `:model`
- `:instructions`
- `:tools`
- `:metadata`
"""
@api_fields [
:assistant_id,
:model,
:instructions,
:tools,
:metadata
]
defp ep_url(thread_id, run_id \\ nil, action \\ nil) do
"/threads/#{thread_id}/runs" <>
if(is_nil(run_id), do: "", else: "/#{run_id}") <>
if(is_nil(action), do: "", else: "/#{action}")
end
@doc """
Creates a new run request with the given arguments.
## Arguments
- `args`: A list of key-value pairs, or a map, representing the fields of the run request.
## Returns
A map containing the fields of the run request.
The `:thread_id` and `:assistant_id` fields are required.
Example usage:
iex> _request = OpenaiEx.Beta.Thread.Run.new(thread_id: "thread_foo", assistant_id: "assistant_bar")
%{assistant_id: "assistant_bar", thread_id: "thread_foo"}
"""
def new(args = [_ | _]) do
args |> Enum.into(%{}) |> new()
end
def new(args = %{thread_id: _}) do
args |> Map.take([:thread_id | @api_fields])
end
@doc """
Calls the run create endpoint.
## Arguments
- `openai`: The OpenAI configuration.
- `run`: The run request, as a map with keys corresponding to the API fields.
## Returns
A map containing the API response.
See https://platform.openai.com/docs/api-reference/runs/createRun for more information.
"""
def create(openai = %OpenaiEx{}, run = %{thread_id: thread_id, assistant_id: _}) do
openai
|> OpenaiEx.with_assistants_beta()
|> OpenaiEx.Http.post(ep_url(thread_id), json: run |> Map.take(@api_fields))
end
@doc """
Calls the run retrieve endpoint.
## Arguments
- `openai`: The OpenAI configuration.
- `params`: Specification of the run to retrieve.
## Returns
A map containing the fields of the run retrieve response.
https://platform.openai.com/docs/api-reference/runs/getRun
"""
def retrieve(openai = %OpenaiEx{}, %{thread_id: thread_id, run_id: run_id}) do
openai
|> OpenaiEx.with_assistants_beta()
|> OpenaiEx.Http.get(ep_url(thread_id, run_id))
end
@doc """
Calls the run update endpoint.
## Arguments
- `openai`: The OpenAI configuration.
- `params`: run to update and new field values.
## Returns
A map containing the API response.
See https://platform.openai.com/docs/api-reference/assistants/modifyAssistant for more information.
"""
def update(openai = %OpenaiEx{}, %{thread_id: thread_id, run_id: run_id, metadata: metadata}) do
openai
|> OpenaiEx.with_assistants_beta()
|> OpenaiEx.Http.post(ep_url(thread_id, run_id), json: %{metadata: metadata})
end
@doc """
Creates a new list runs request with the given arguments.
## Arguments
- `args`: A list of key-value pairs, or a map, representing the fields of the list runs request.
## Returns
A map containing the fields of the list runs request.
"""
def new_list(args = [_ | _]) do
args |> Enum.into(%{}) |> new_list()
end
def new_list(args = %{}) do
args
|> Map.take(OpenaiEx.list_query_fields())
end
@doc """
Returns a list of runs objects.
https://platform.openai.com/docs/api-reference/runs/listRuns
"""
def list(openai = %OpenaiEx{}, thread_id, params = %{} \\ %{}) do
openai
|> OpenaiEx.with_assistants_beta()
|> OpenaiEx.Http.get(ep_url(thread_id), params |> Map.take(OpenaiEx.list_query_fields()))
end
def submit_tool_outputs(
openai = %OpenaiEx{},
%{thread_id: thread_id, run_id: run_id, tool_outputs: tool_outputs}
) do
openai
|> OpenaiEx.with_assistants_beta()
|> OpenaiEx.Http.post(
ep_url(thread_id, run_id, "submit_tool_outputs"),
json: %{tool_outputs: tool_outputs}
)
end
def cancel(openai = %OpenaiEx{}, %{thread_id: thread_id, run_id: run_id}) do
openai
|> OpenaiEx.with_assistants_beta()
|> OpenaiEx.Http.post(ep_url(thread_id, run_id, "cancel"))
end
def create_and_run(openai = %OpenaiEx{}, params = %{assistant_id: _}) do
openai
|> OpenaiEx.with_assistants_beta()
|> OpenaiEx.Http.post("/threads/runs",
json: params |> Map.take([:assistant_id | [:thread | @api_fields]])
)
end
end