From 6fa13b0c1d210746533ede2aed7c4e2536edc8f5 Mon Sep 17 00:00:00 2001 From: Fernando Mendes Date: Wed, 17 Jan 2024 08:02:31 +0000 Subject: [PATCH] Add API support for file contents --- lib/openai.ex | 427 +++++++++++++++++++++++--------------------- lib/openai/files.ex | 6 + mix.lock | 8 +- 3 files changed, 233 insertions(+), 208 deletions(-) diff --git a/lib/openai.ex b/lib/openai.ex index f2bea0b..f663b1c 100644 --- a/lib/openai.ex +++ b/lib/openai.ex @@ -34,7 +34,7 @@ defmodule OpenAI do ```elixir OpenAI.models() ``` - + ## Example response ```elixir %{ @@ -54,15 +54,15 @@ defmodule OpenAI do "root" => "davinci-search-query" } ``` - + See: https://platform.openai.com/docs/api-reference/models/retrieve - + Retrieve specific model info ## Example request ```elixir OpenAI.models("davinci-search-query") ``` - + ## Example response ```elixir {:ok, %{ @@ -86,7 +86,7 @@ defmodule OpenAI do object: "list" }} ``` - + See: https://platform.openai.com/docs/api-reference/models/retrieve """ def models(config) when is_struct(config), do: Models.fetch(config) @@ -102,12 +102,12 @@ defmodule OpenAI do @doc """ Retrieves the list of assistants. - + ## Example request ```elixir OpenAI.assistants() ``` - + ## Example response ```elixir {:ok, @@ -132,14 +132,14 @@ defmodule OpenAI do object: "list" }} ``` - + Retrieves the list of assistants filtered by query params. - + ## Example request ```elixir OpenAI.assistants(after: "", limit: 10) ``` - + ## Example response ```elixir {:ok, @@ -165,16 +165,16 @@ defmodule OpenAI do object: "list" }} ``` - + See: https://platform.openai.com/docs/api-reference/assistants/listAssistants - + Retrieves an assistant by its id. - + ## Example request ```elixir OpenAI.assistants("asst_...") ``` - + ## Example response ```elixir {:ok, @@ -191,7 +191,7 @@ defmodule OpenAI do tools: [%{"type" => "retrieval"}] }} ``` - + See: https://platform.openai.com/docs/api-reference/assistants/getAssistant """ def assistants(params \\ [], config \\ %Config{}) @@ -204,7 +204,7 @@ defmodule OpenAI do @doc """ Creates a new assistant. - + ## Example request ```elixir OpenAI.assistants_create( @@ -217,7 +217,7 @@ defmodule OpenAI do file_ids: ["file-..."] ) ``` - + ## Example response ```elixir {:ok, @@ -234,7 +234,7 @@ defmodule OpenAI do tools: [%{"type" => "retrieval"}] }} ``` - + See: https://platform.openai.com/docs/api-reference/assistants/createAssistant """ def assistants_create(params, config \\ %Config{}) @@ -244,7 +244,7 @@ defmodule OpenAI do @doc """ Modifies an existing assistant. - + ## Example request ```elixir OpenAI.assistants_modify( @@ -253,7 +253,7 @@ defmodule OpenAI do name: "My upgraded assistant" ) ``` - + ## Example response ```elixir {:ok, @@ -270,7 +270,7 @@ defmodule OpenAI do tools: [%{"type" => "retrieval"}] }} ``` - + See: https://platform.openai.com/docs/api-reference/assistants/modifyAssistant """ def assistants_modify(assistant_id, params, config \\ %Config{}) @@ -280,12 +280,12 @@ defmodule OpenAI do @doc """ Deletes an assistant. - + ## Example request ```elixir OpenAI.assistants_delete("asst_...") ``` - + ## Example response ```elixir {:ok, @@ -295,7 +295,7 @@ defmodule OpenAI do object: "assistant.deleted" }} ``` - + See: https://platform.openai.com/docs/api-reference/assistants/deleteAssistant """ def assistants_delete(assistant_id, config \\ %Config{}) @@ -305,12 +305,12 @@ defmodule OpenAI do @doc """ Retrieves the list of files associated with a particular assistant. - + ## Example request ```elixir OpenAI.assistant_files("asst_...") ``` - + ## Example response ```elixir {:ok, @@ -329,15 +329,15 @@ defmodule OpenAI do object: "list" }} ``` - + Retrieves the list of files associated with a particular assistant, filtered by query params. - + ## Example request ```elixir OpenAI.assistant_files("asst_...", order: "desc") ``` - + ## Example response ```elixir {:ok, @@ -356,7 +356,7 @@ defmodule OpenAI do object: "list" }} ``` - + See: https://platform.openai.com/docs/api-reference/assistants/listAssistantFiles """ def assistant_files(assistant_id, params \\ [], config \\ %Config{}) @@ -366,12 +366,12 @@ defmodule OpenAI do @doc """ Retrieves an assistant file by its id. - + ## Example request ```elixir OpenAI.assistant_file("asst_...", "file-...") ``` - + ## Example response ```elixir {:ok, @@ -382,7 +382,7 @@ defmodule OpenAI do object: "assistant.file" }} ``` - + See: https://platform.openai.com/docs/api-reference/assistants/getAssistantFile """ def assistant_file(assistant_id, file_id, config \\ %Config{}) @@ -392,12 +392,12 @@ defmodule OpenAI do @doc """ Attaches a previously uploaded file to the assistant. - + ## Example request ```elixir OpenAI.assistant_file_create("asst_...", file_id: "file-...") ``` - + ## Example response ```elixir {:ok, @@ -408,7 +408,7 @@ defmodule OpenAI do object: "assistant.file" }} ``` - + See: https://platform.openai.com/docs/api-reference/assistants/createAssistantFile """ def assistant_file_create(assistant_id, params, config \\ %Config{}) @@ -418,12 +418,12 @@ defmodule OpenAI do @doc """ Detaches a file from the assistant. The file itself is not automatically deleted. - + ## Example request ```elixir OpenAI.assistant_file_delete("asst_...", "file-...") ``` - + ## Example response ```elixir {:ok, @@ -433,7 +433,7 @@ defmodule OpenAI do object: "assistant.file.deleted" }} ``` - + See: https://platform.openai.com/docs/api-reference/assistants/deleteAssistantFile """ def assistant_file_delete(assistant_id, file_id, config \\ %Config{}) @@ -443,14 +443,14 @@ defmodule OpenAI do @doc """ Retrieves the list of threads. - + **NOTE:** At the time of this writing this functionality remains undocumented by OpenAI. - + ## Example request ```elixir OpenAI.threads() ``` - + ## Example response ```elixir {:ok, @@ -470,16 +470,16 @@ defmodule OpenAI do object: "list" }} ``` - + Retrieves a filtered list of threads. - + **NOTE:** At the time of this writing this functionality remains undocumented by OpenAI. - + ## Example request ```elixir OpenAI.threads(limit: 2) ``` - + ## Example response ```elixir {:ok, @@ -504,14 +504,14 @@ defmodule OpenAI do object: "list" }} ``` - + Retrieves a thread by its id. - + ## Example request ```elixir OpenAI.threads("thread_..."") ``` - + ## Example response ```elixir {:ok, @@ -522,7 +522,7 @@ defmodule OpenAI do object: "thread" }} ``` - + See: https://platform.openai.com/docs/api-reference/threads/getThread """ def threads(params \\ [], config \\ %Config{}) @@ -535,12 +535,12 @@ defmodule OpenAI do @doc """ Creates a new thread. - + ## Example request ```elixir OpenAI.threads_create() ``` - + ## Example response ```elixir {:ok, @@ -551,9 +551,9 @@ defmodule OpenAI do object: "thread" }} ``` - + Creates a new thread with some messages and metadata. - + ## Example request ```elixir messages = [ @@ -567,15 +567,15 @@ defmodule OpenAI do content: "How does AI work? Explain it in simple terms." }, ] - + metadata = %{ key_1: "value 1", key_2: "value 2" } - + OpenAI.threads_create(messages: messages, metadata: metadata) ``` - + ## Example response ```elixir {:ok, @@ -586,7 +586,7 @@ defmodule OpenAI do object: "thread" }} ``` - + See: https://platform.openai.com/docs/api-reference/threads/createThread """ def threads_create(params \\ [], config \\ %Config{}) @@ -596,7 +596,7 @@ defmodule OpenAI do @doc """ Creates a new thread and runs it. - + ## Example request ```elixir messages = [ @@ -610,21 +610,21 @@ defmodule OpenAI do content: "How does AI work? Explain it in simple terms." }, ] - + thread_metadata = %{ key_1: "value 1", key_2: "value 2" } - + thread = %{ messages: messages, metadata: thread_metadata } - + run_metadata = %{ key_3: "value 3" } - + params = [ assistant_id: "asst_...", thread: thread, @@ -635,10 +635,10 @@ defmodule OpenAI do }], metadata: run_metadata ] - + OpenAI.threads_create_and_run(params) ``` - + ## Example response ```elixir {:ok, @@ -662,7 +662,7 @@ defmodule OpenAI do tools: [%{"type" => "retrieval"}] }} ``` - + See: https://platform.openai.com/docs/api-reference/runs/createThreadAndRun """ def threads_create_and_run(params \\ [], config \\ %Config{}) @@ -672,16 +672,16 @@ defmodule OpenAI do @doc """ Modifies an existing thread. - + ## Example request ```elixir metadata = %{ key_3: "value 3" } - + OpenAI.threads_modify("thread_...", metadata: metadata) ``` - + ## Example response ```elixir {:ok, @@ -692,7 +692,7 @@ defmodule OpenAI do object: "thread" }} ``` - + See: https://platform.openai.com/docs/api-reference/threads/modifyThread """ def threads_modify(thread_id, params, config \\ %Config{}) @@ -702,12 +702,12 @@ defmodule OpenAI do @doc """ Deletes a thread. - + ## Example request ```elixir OpenAI.threads_delete("thread_...") ``` - + ## Example response ```elixir {:ok, @@ -717,7 +717,7 @@ defmodule OpenAI do object: "thread.deleted" }} ``` - + See: https://platform.openai.com/docs/api-reference/threads/deleteThread """ def threads_delete(thread_id, config \\ %Config{}) @@ -727,12 +727,12 @@ defmodule OpenAI do @doc """ Retrieves the list of messages associated with a particular thread. - + ## Example request ```elixir OpenAI.thread_messages("thread_...") ``` - + ## Example response ```elixir {:ok, @@ -766,15 +766,15 @@ defmodule OpenAI do object: "list" }} ``` - + Retrieves the list of messages associated with a particular thread, filtered by query params. - + ## Example request ```elixir OpenAI.thread_messages("thread_...", after: "msg_...") ``` - + ## Example response ```elixir {:ok, @@ -804,7 +804,7 @@ defmodule OpenAI do object: "list" }} ``` - + See: https://platform.openai.com/docs/api-reference/messages/listMessages """ def thread_messages(thread_id, params \\ [], config \\ %Config{}) @@ -814,12 +814,12 @@ defmodule OpenAI do @doc """ Retrieves a thread message by its id. - + ## Example request ```elixir OpenAI.thread_message("thread_...", "msg_...") ``` - + ## Example response ```elixir {:ok, @@ -841,7 +841,7 @@ defmodule OpenAI do thread_id: "thread_..." }} ``` - + See: https://platform.openai.com/docs/api-reference/threads/getThread """ def thread_message(thread_id, message_id, config \\ %Config{}) @@ -851,7 +851,7 @@ defmodule OpenAI do @doc """ Creates a message within a thread. - + ## Example request ```elixir params = [ @@ -863,10 +863,10 @@ defmodule OpenAI do key_2: "value 2" } ] - + OpenAI.thread_message_create("thread_...", params) ``` - + ## Example response ```elixir {:ok, @@ -888,7 +888,7 @@ defmodule OpenAI do thread_id: "thread_..." }} ``` - + See: https://platform.openai.com/docs/api-reference/messages/createMessage """ def thread_message_create(thread_id, params, config \\ %Config{}) @@ -898,7 +898,7 @@ defmodule OpenAI do @doc """ Modifies an existing thread message. - + ## Example request ```elixir params = [ @@ -906,10 +906,10 @@ defmodule OpenAI do key_3: "value 3" } ] - + OpenAI.thread_message_modify("thread_...", "msg_...", params) ``` - + ## Example response ```elixir {:ok, @@ -931,7 +931,7 @@ defmodule OpenAI do thread_id: "thread_..." }} ``` - + See: https://platform.openai.com/docs/api-reference/messages/modifyMessage """ def thread_message_modify(thread_id, message_id, params, config \\ %Config{}) @@ -942,12 +942,12 @@ defmodule OpenAI do @doc """ Retrieves the list of files associated with a particular message of a thread. - + ## Example request ```elixir OpenAI.thread_message_files("thread_...", "msg_...") ``` - + ## Example response ```elixir {:ok, @@ -966,20 +966,20 @@ defmodule OpenAI do object: "list" }} ``` - + Retrieves the list of files associated with a particular message of a thread, filtered by query params. - + ## Example request ```elixir OpenAI.thread_message_files("thread_...", "msg_...", after: "file-...") ``` - + ## Example response ```elixir {:ok, %{data: [], first_id: nil, has_more: false, last_id: nil, object: "list"}} ``` - + See: https://platform.openai.com/docs/api-reference/messages/listMessageFiles """ def thread_message_files(thread_id, message_id, params \\ [], config \\ %Config{}) @@ -990,12 +990,12 @@ defmodule OpenAI do @doc """ Retrieves the message file object. - + ## Example request ```elixir OpenAI.thread_message_file("thread_...", "msg_...", "file-...") ``` - + ## Example response ```elixir {:ok, @@ -1006,7 +1006,7 @@ defmodule OpenAI do object: "thread.message.file" }} ``` - + See: https://platform.openai.com/docs/api-reference/messages/getMessageFile """ def thread_message_file(thread_id, message_id, file_id, config \\ %Config{}) @@ -1017,30 +1017,30 @@ defmodule OpenAI do @doc """ Retrieves the list of runs associated with a particular thread. - + ## Example request ```elixir OpenAI.thread_runs("thread_...") ``` - + ## Example response ```elixir {:ok, %{data: [], first_id: nil, has_more: false, last_id: nil, object: "list"}} ``` - + Retrieves the list of runs associated with a particular thread, filtered by query params. - + ## Example request ```elixir OpenAI.thread_runs("thread_...", limit: 10) ``` - + ## Example response ```elixir {:ok, %{data: [], first_id: nil, has_more: false, last_id: nil, object: "list"}} ``` - + See: https://platform.openai.com/docs/api-reference/runs/listRuns """ def thread_runs(thread_id, params \\ [], config \\ %Config{}) @@ -1050,12 +1050,12 @@ defmodule OpenAI do @doc """ Retrieves a particular thread run by its id. - + ## Example request ```elixir OpenAI.thread_run("thread_...", "run_...") ``` - + ## Example response ```elixir {:ok, @@ -1079,7 +1079,7 @@ defmodule OpenAI do tools: [%{"type" => "retrieval"}] }} ``` - + See: https://platform.openai.com/docs/api-reference/runs/getRun """ def thread_run(thread_id, run_id, config \\ %Config{}) @@ -1089,7 +1089,7 @@ defmodule OpenAI do @doc """ Creates a run for a thread using a particular assistant. - + ## Example request ```elixir params = [ @@ -1099,10 +1099,10 @@ defmodule OpenAI do "type" => "retrieval" }] ] - + OpenAI.thread_run_create("thread_...", params) ``` - + ## Example response ```elixir {:ok, @@ -1126,7 +1126,7 @@ defmodule OpenAI do tools: [%{"type" => "retrieval"}] }} ``` - + See: https://platform.openai.com/docs/api-reference/runs/createRun """ def thread_run_create(thread_id, params, config \\ %Config{}) @@ -1136,7 +1136,7 @@ defmodule OpenAI do @doc """ Modifies an existing thread run. - + ## Example request ```elixir params = [ @@ -1144,10 +1144,10 @@ defmodule OpenAI do key_3: "value 3" } ] - + OpenAI.thread_run_modify("thread_...", "run_...", params) ``` - + ## Example response ```elixir {:ok, @@ -1171,7 +1171,7 @@ defmodule OpenAI do tools: [%{"type" => "retrieval"}] }} ``` - + See: https://platform.openai.com/docs/api-reference/runs/modifyRun """ def thread_run_modify(thread_id, run_id, params, config \\ %Config{}) @@ -1182,8 +1182,8 @@ defmodule OpenAI do @doc """ When a run has the status: "requires_action" and required_action.type is submit_tool_outputs, this endpoint can be used to submit the outputs from the tool calls once they're all completed. All outputs must be submitted in a single request. - - + + ## Example request ```elixir params = [ @@ -1192,10 +1192,10 @@ defmodule OpenAI do output: "test" }] ] - + OpenAI.thread_run_submit_tool_outputs("thread_...", "run_...", params) ``` - + ## Example response ```elixir {:ok, @@ -1238,7 +1238,7 @@ defmodule OpenAI do ] }} ``` - + See: https://platform.openai.com/docs/api-reference/runs/submitToolOutputs """ def thread_run_submit_tool_outputs(thread_id, run_id, params, config \\ %Config{}) @@ -1249,12 +1249,12 @@ defmodule OpenAI do @doc """ Cancels an `in_progress` run. - + ## Example request ```elixir OpenAI.thread_run_cancel("thread_...", "run_...") ``` - + ## Example response ```elixir {:ok, @@ -1278,7 +1278,7 @@ defmodule OpenAI do tools: [%{"type" => "retrieval"}] }} ``` - + See: https://platform.openai.com/docs/api-reference/runs/cancelRun """ def thread_run_cancel(thread_id, run_id, config \\ %Config{}) @@ -1288,12 +1288,12 @@ defmodule OpenAI do @doc """ Retrieves the list of steps associated with a particular run of a thread. - + ## Example request ```elixir OpenAI.thread_run_steps("thread_...", "run_...") ``` - + ## Example response ```elixir {:ok, @@ -1325,15 +1325,15 @@ defmodule OpenAI do object: "list" }} ``` - + Retrieves the list of steps associated with a particular run of a thread, filtered by query params. - + ## Example request ```elixir OpenAI.thread_run_steps("thread_...", "run_...", order: "asc") ``` - + ## Example response ```elixir {:ok, @@ -1365,7 +1365,7 @@ defmodule OpenAI do object: "list" }} ``` - + See: https://platform.openai.com/docs/api-reference/runs/listRunSteps """ def thread_run_steps(thread_id, run_id, params \\ [], config \\ %Config{}) @@ -1376,12 +1376,12 @@ defmodule OpenAI do @doc """ Retrieves a thread run step by its id. - + ## Example request ```elixir OpenAI.thread_run_step("thread_...", "run_...", "step_...") ``` - + ## Example response ```elixir {:ok, @@ -1405,7 +1405,7 @@ defmodule OpenAI do type: "message_creation" }} ``` - + See: https://platform.openai.com/docs/api-reference/runs/getRunStep """ def thread_run_step(thread_id, run_id, step_id, config \\ %Config{}) @@ -1417,7 +1417,7 @@ defmodule OpenAI do @doc """ It returns one or more predicted completions given a prompt. The function accepts as arguments the "engine_id" and the set of parameters used by the Completions OpenAI api - + ## Example request ```elixir OpenAI.completions( @@ -1428,7 +1428,7 @@ defmodule OpenAI do ... ) ``` - + ## Example response ```elixir {:ok, %{ @@ -1447,7 +1447,7 @@ defmodule OpenAI do } } ``` - + See: https://platform.openai.com/docs/api-reference/completions/create """ def completions(params) when is_list(params), @@ -1459,7 +1459,7 @@ defmodule OpenAI do @doc """ It returns one or more predicted completions given a prompt. The function accepts as arguments the "engine_id" and the set of parameters used by the Completions OpenAI api - + ## Example request ```elixir OpenAI.completions( @@ -1470,7 +1470,7 @@ defmodule OpenAI do ... ) ``` - + ## Example response ```elixir {:ok, %{ @@ -1489,7 +1489,7 @@ defmodule OpenAI do } } ``` - + See: https://beta.openai.com/docs/api-reference/completions/create for the complete list of parameters you can pass to the completions function """ def completions(engine_id, params) when is_bitstring(engine_id) and is_list(params), @@ -1500,7 +1500,7 @@ defmodule OpenAI do @doc """ Creates a completion for the chat message - + ## Example request ```elixir OpenAI.chat_completion( @@ -1513,7 +1513,7 @@ defmodule OpenAI do ] ) ``` - + ## Example response ```elixir {:ok, @@ -1540,15 +1540,15 @@ defmodule OpenAI do } } ``` - + N.B. to use "stream" mode you must be set http_options as below when you want to treat the chat completion as a stream: - + ```elixir config :openai, api_key: "your-api-key", http_options: [recv_timeout: :infinity, stream_to: self(), async: :once] ``` - + ## Example request (stream) ```elixir OpenAI.chat_completion([ @@ -1567,7 +1567,7 @@ defmodule OpenAI do end) |> Stream.run() ``` - + ## Example response (stream) ```elixir %{ @@ -1579,7 +1579,7 @@ defmodule OpenAI do "model" => "gpt-3.5-turbo-0301", "object" => "chat.completion.chunk" } - + %{ "choices" => [ %{"delta" => %{"content" => "The"}, "finish_reason" => nil, "index" => 0} @@ -1621,7 +1621,7 @@ defmodule OpenAI do "object" => "chat.completion.chunk" } ``` - + See: https://platform.openai.com/docs/api-reference/chat/create for the complete list of parameters you can pass to the completions function """ def chat_completion(params, config \\ %Config{}) do @@ -1630,7 +1630,7 @@ defmodule OpenAI do @doc """ Creates a new edit for the provided input, instruction, and parameters - + ## Example request ```elixir OpenAI.edits( @@ -1639,7 +1639,7 @@ defmodule OpenAI do instruction: "Fix the spelling mistakes" ) ``` - + ## Example response ```elixir {:ok, @@ -1654,7 +1654,7 @@ defmodule OpenAI do } }} ``` - + See: https://platform.openai.com/docs/api-reference/edits/create """ def edits(params, config \\ %Config{}) do @@ -1663,7 +1663,7 @@ defmodule OpenAI do @doc """ Creates an embedding vector representing the input text. - + ## Example request ```elixir OpenAI.embeddings( @@ -1671,7 +1671,7 @@ defmodule OpenAI do input: "The food was delicious and the waiter..." ) ``` - + ## Example response ```elixir {:ok, @@ -1701,7 +1701,7 @@ defmodule OpenAI do usage: %{"prompt_tokens" => 8, "total_tokens" => 8} }} ``` - + See: https://platform.openai.com/docs/api-reference/embeddings/create """ def embeddings(params, config \\ %Config{}) do @@ -1710,17 +1710,17 @@ defmodule OpenAI do @doc """ Generates audio from the input text. - + ## Example request OpenAI.audio_speech( model: "tts-1", input: "You know that Voight-Kampf test of yours. Did you ever take that test yourself?", voice: "alloy" ) - + ## Example response {:ok, <<255, 255, ...>>} - + See: https://platform.openai.com/docs/api-reference/audio/create """ def audio_speech(params, config \\ %Config{}) do @@ -1729,7 +1729,7 @@ defmodule OpenAI do @doc """ Transcribes audio into the input language. - + ## Example request ```elixir OpenAI.audio_transcription( @@ -1737,7 +1737,7 @@ defmodule OpenAI do model: "whisper-1" ) ``` - + ## Example response ```elixir {:ok, @@ -1745,7 +1745,7 @@ defmodule OpenAI do text: "I've seen things you people wouldn't believe.." }} ``` - + See: https://platform.openai.com/docs/api-reference/audio/create """ def audio_transcription(file_path, params, config \\ %Config{}) do @@ -1754,7 +1754,7 @@ defmodule OpenAI do @doc """ Translates audio into into English. - + ## Example request ```elixir OpenAI.audio_translation( @@ -1762,7 +1762,7 @@ defmodule OpenAI do model: "whisper-1" ) ``` - + ## Example response ```elixir {:ok, @@ -1770,7 +1770,7 @@ defmodule OpenAI do text: "I thought if I walked, I would be saved. It was almost like a pilgrimage. I will definitely continue to walk long distances. It is a very unique form of life and existence that we have lost almost entirely from our normal life." }} ``` - + See: https://platform.openai.com/docs/api-reference/audio/create """ def audio_translation(file_path, params, config \\ %Config{}) do @@ -1779,12 +1779,12 @@ defmodule OpenAI do @doc """ Classifies if text violates OpenAI's Content Policy - + ## Example request ```elixir OpenAI.moderations(input: "I want to kill everyone!") ``` - + ## Example response ```elixir {:ok, @@ -1816,7 +1816,7 @@ defmodule OpenAI do ] }} ``` - + See: https://platform.openai.com/docs/api-reference/moderations/create """ def moderations(params, config \\ %Config{}) do @@ -1829,7 +1829,7 @@ defmodule OpenAI do ```elixir OpenAI.finetunes() ``` - + ## Example response ```elixir {:ok, %{ @@ -1840,15 +1840,15 @@ defmodule OpenAI do ] } ``` - + See: https://beta.openai.com/docs/api-reference/fine-tunes/list - + Gets info about the fine-tune job. ## Example request ```elixir OpenAI.finetunes("ft-AF1WoRqd3aJAHsqc9NY7iL8F") ``` - + ## Example response ```elixir {:ok, %{ @@ -1870,7 +1870,7 @@ defmodule OpenAI do ... } ``` - + See: https://beta.openai.com/docs/api-reference/fine-tunes/retrieve """ def finetunes(config) when is_struct(config), do: Finetunes.fetch(config) @@ -1883,14 +1883,14 @@ defmodule OpenAI do @doc """ Creates a job that fine-tunes a specified model from a given dataset. - + ## Example request ```elixir OpenAI.finetunes_create(training_file: "file-123", model: "curie", validation_file: "file-456") ``` - + ## Example response - + See: https://platform.openai.com/docs/api-reference/fine-tunes/create """ def finetunes_create(params, config \\ %Config{}) do @@ -1899,12 +1899,12 @@ defmodule OpenAI do @doc """ Immediately cancel a fine-tune job. - + ## Example request ```elixir OpenAI.finetunes_cancel("ft-AF1WoRqd3aJAHsqc9NY7iL8F") ``` - + ## Example response ```elixir {:ok, @@ -1948,7 +1948,7 @@ defmodule OpenAI do validation_files: [] }} ``` - + See: https://platform.openai.com/docs/api-reference/fine-tunes/cancel """ def finetunes_cancel(finetune_id, config \\ %Config{}) do @@ -1957,12 +1957,12 @@ defmodule OpenAI do @doc """ Delete a fine-tuned model. You must have the Owner role in your organization. - + ## Example request ```elixir OpenAI.finetunes_delete_model("model-id") ``` - + ## Example response ```elixir {:ok, @@ -1972,7 +1972,7 @@ defmodule OpenAI do deleted: true }} ``` - + See: https://platform.openai.com/docs/api-reference/fine-tunes/delete-model """ def finetunes_delete_model(model_id, config \\ %Config{}) do @@ -1981,12 +1981,12 @@ defmodule OpenAI do @doc """ Get fine-grained status updates for a fine-tune job. - + ## Example request ```elixir OpenAI.finetunes_list_events("ft-AF1WoRqd3aJAHsqc9NY7iL8F") ``` - + ## Example response ```elixir {:ok, @@ -2015,7 +2015,7 @@ defmodule OpenAI do } } ``` - + See: https://platform.openai.com/docs/api-reference/fine-tunes/events """ def finetunes_list_events(finetune_id, config \\ %Config{}) do @@ -2025,7 +2025,7 @@ defmodule OpenAI do @doc """ This generates an image based on the given prompt. Image functions require some times to execute, and API may return a timeout error, if needed you can pass a configuration object with HTTPoison http_options as second argument of the function to increase the timeout. - + ## Example Request ```elixir OpenAI.images_generations( @@ -2033,7 +2033,7 @@ defmodule OpenAI do %OpenAI.config{http_options: [recv_timeout: 10 * 60 * 1000]} # optional! ) ``` - + ## Example Response ```elixir {:ok, @@ -2046,9 +2046,9 @@ defmodule OpenAI do ] }} ``` - + See: https://beta.openai.com/docs/api-reference/images/create for the complete list of parameters you can pass to the image creation function - + note: the official way of passing http_options changed in v0.5.0 to be compliant with the conventions of other APIs, the alias OpenAI.images_generations(file_path, params, request_options), but is still available for retrocompatibility. If you are using it consider to switch to OpenAI.images_variations(params, config) """ def images_generations(params) do @@ -2081,7 +2081,7 @@ defmodule OpenAI do @doc """ This edits an image based on the given prompt. Image functions require some times to execute, and API may return a timeout error, if needed you can pass a configuration object with HTTPoison http_options as second argument of the function to increase the timeout. - + ## Example Request ```elixir OpenAI.images_edits( @@ -2090,7 +2090,7 @@ defmodule OpenAI do %OpenAI.config{http_options: [recv_timeout: 10 * 60 * 1000]} # optional! ) ``` - + ## Example Response ```elixir {:ok, @@ -2136,7 +2136,7 @@ defmodule OpenAI do @doc """ Creates a variation of a given image. Image functions require some times to execute, and API may return a timeout error, if needed you can pass a configuration object with HTTPoison http_options as second argument of the function to increase the timeout. - + ## Example Request ```elixir OpenAI.images_variations( @@ -2145,7 +2145,7 @@ defmodule OpenAI do %OpenAI.config{http_options: [recv_timeout: 10 * 60 * 1000]} # optional! ) ``` - + ## Example Response ```elixir {:ok, @@ -2190,12 +2190,12 @@ defmodule OpenAI do @doc """ Returns a list of files that belong to the user's organization. - + ## Example request ```elixir OpenAI.files() ``` - + ## Example response ```elixir {:ok, @@ -2217,14 +2217,14 @@ defmodule OpenAI do } ``` See: https://platform.openai.com/docs/api-reference/files - + Returns a file that belong to the user's organization, given a file id - + ## Example Request ```elixir OpenAI.files("file-123321") ``` - + ## Example Response ```elixir {:ok, @@ -2250,14 +2250,33 @@ defmodule OpenAI do def files(), do: Files.fetch() def files(file_id, config \\ %Config{}), do: Files.fetch_by_id(file_id, config) + @doc """ + Returns the contents of the specified file. + + ## Example request + ```elixir + OpenAI.files_content("file-123321") + ``` + + ## Example response + ```elixir + {:ok, + <<137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0...>>} + ``` + See: https://platform.openai.com/docs/api-reference/files/retrieve-contents + """ + + def files_content(file_id, config \\ %Config{}), + do: Files.fetch_content(file_id, config) + @doc """ Upload a file that contains document(s) to be used across various endpoints/features. Currently, the size of all the files uploaded by one organization can be up to 1 GB. Please contact OpenAI if you need to increase the storage limit. - + ## Example request ```elixir OpenAI.files_upload("./file.jsonl", purpose: "fine-tune") ``` - + ## Example response ```elixir {:ok, @@ -2281,12 +2300,12 @@ defmodule OpenAI do @doc """ delete a file - + ## Example Request ```elixir OpenAI.files_delete("file-123") ``` - + ## Example Response ```elixir {:ok, %{deleted: true, id: "file-123", object: "file"}} @@ -2304,7 +2323,7 @@ defmodule OpenAI do ```elixir OpenAI.engines() ``` - + ## Example response ```elixir {:ok, %{ @@ -2315,15 +2334,15 @@ defmodule OpenAI do ] } ``` - + See: https://beta.openai.com/docs/api-reference/engines/list - + Retrieve specific engine info ## Example request ```elixir OpenAI.engines("davinci") ``` - + ## Example response ```elixir {:ok, %{ @@ -2333,7 +2352,7 @@ defmodule OpenAI do } } ``` - + See: https://beta.openai.com/docs/api-reference/engines/retrieve """ def engines(config) when is_struct(config), do: Engines.fetch(config) diff --git a/lib/openai/files.ex b/lib/openai/files.ex index 131b10a..1be244a 100644 --- a/lib/openai/files.ex +++ b/lib/openai/files.ex @@ -7,6 +7,7 @@ defmodule OpenAI.Files do def url(), do: @files_base_url def url(file_id), do: "#{@files_base_url}/#{file_id}" + def url(file_id, path), do: "#{url(file_id)}/#{path}" def fetch(config \\ %Config{}) do url() @@ -18,6 +19,11 @@ defmodule OpenAI.Files do |> Client.api_get(config) end + def fetch_content(file_id, config \\ %Config{}) do + url(file_id, "content") + |> Client.api_get(config) + end + def delete(file_id, config \\ %Config{}) do url(file_id) |> Client.api_delete(config) diff --git a/mix.lock b/mix.lock index a83ab35..36399da 100644 --- a/mix.lock +++ b/mix.lock @@ -1,9 +1,9 @@ %{ - "certifi": {:hex, :certifi, "2.9.0", "6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21", [:rebar3], [], "hexpm", "266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"}, + "certifi": {:hex, :certifi, "2.12.0", "2d1cca2ec95f59643862af91f001478c9863c2ac9cb6e2f89780bfd8de987329", [:rebar3], [], "hexpm", "ee68d85df22e554040cdb4be100f33873ac6051387baf6a8f6ce82272340ff1c"}, "earmark_parser": {:hex, :earmark_parser, "1.4.31", "a93921cdc6b9b869f519213d5bc79d9e218ba768d7270d46fdcf1c01bacff9e2", [:mix], [], "hexpm", "317d367ee0335ef037a87e46c91a2269fef6306413f731e8ec11fc45a7efd059"}, "ex_doc": {:hex, :ex_doc, "0.29.3", "f07444bcafb302db86e4f02d8bbcd82f2e881a0dcf4f3e4740e4b8128b9353f7", [:mix], [{:earmark_parser, "~> 1.4.31", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "3dc6787d7b08801ec3b51e9bd26be5e8826fbf1a17e92d1ebc252e1a1c75bfe1"}, "file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"}, - "hackney": {:hex, :hackney, "1.18.1", "f48bf88f521f2a229fc7bae88cf4f85adc9cd9bcf23b5dc8eb6a1788c662c4f6", [:rebar3], [{:certifi, "~>2.9.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a4ecdaff44297e9b5894ae499e9a070ea1888c84afdd1fd9b7b2bc384950128e"}, + "hackney": {:hex, :hackney, "1.20.1", "8d97aec62ddddd757d128bfd1df6c5861093419f8f7a4223823537bad5d064e2", [:rebar3], [{:certifi, "~> 2.12.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.4.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "fe9094e5f1a2a2c0a7d10918fee36bfec0ec2a979994cff8cfe8058cd9af38e3"}, "httpoison": {:hex, :httpoison, "2.1.0", "655fd9a7b0b95ee3e9a3b535cf7ac8e08ef5229bab187fa86ac4208b122d934b", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "fc455cb4306b43827def4f57299b2d5ac8ac331cb23f517e734a4b78210a160c"}, "idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"}, "jason": {:hex, :jason, "1.4.0", "e855647bc964a44e2f67df589ccf49105ae039d4179db7f6271dfd3843dc27e6", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "79a3791085b2a0f743ca04cec0f7be26443738779d09302e01318f97bdb82121"}, @@ -17,8 +17,8 @@ "mix_test_watch": {:hex, :mix_test_watch, "1.0.2", "34900184cbbbc6b6ed616ed3a8ea9b791f9fd2088419352a6d3200525637f785", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}], "hexpm", "47ac558d8b06f684773972c6d04fcc15590abdb97aeb7666da19fcbfdc441a07"}, "mock": {:hex, :mock, "0.3.6", "e810a91fabc7adf63ab5fdbec5d9d3b492413b8cda5131a2a8aa34b4185eb9b4", [:mix], [{:meck, "~> 0.8.13", [hex: :meck, repo: "hexpm", optional: false]}], "hexpm", "bcf1d0a6826fb5aee01bae3d74474669a3fa8b2df274d094af54a25266a1ebd2"}, "nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"}, - "parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"}, + "parse_trans": {:hex, :parse_trans, "3.4.1", "6e6aa8167cb44cc8f39441d05193be6e6f4e7c2946cb2759f015f8c56b76e5ff", [:rebar3], [], "hexpm", "620a406ce75dada827b82e453c19cf06776be266f5a67cff34e1ef2cbb60e49a"}, "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm", "fec8660eb7733ee4117b85f55799fd3833eb769a6df71ccf8903e8dc5447cfce"}, - "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"}, + "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"}, "unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"}, }