Openai REST APIの非公式のコミュニティにメンテナンスされたラッパーhttps://platform.openai.com/docs/api-reference/introduction RESTエンドポイントに関する詳細については
あなたの理解とサポートに感謝し、これまで図書館に貢献してくれたすべての人に感謝します!
追加:mix.exsファイルの依存関係としてのopenai 。
def deps do
[
{ :openai , "~> 0.6.2" }
]
end mix config.exs(デフォルト$ project_root/config/config.exs)でopenaiを構成できます。 Phoenixを使用している場合、config/dev.exs | test.exs | prod.exsファイルに構成を追加します。例の構成は次のとおりです。
import Config
config :openai ,
# find it at https://platform.openai.com/account/api-keys
api_key: "your-api-key" ,
# find it at https://platform.openai.com/account/org-settings under "Organization ID"
organization_key: "your-organization-key" ,
# optional, use when required by an OpenAI API beta, e.g.:
beta: "assistants=v1" ,
# optional, passed to [HTTPoison.Request](https://hexdocs.pm/httpoison/HTTPoison.Request.html) options
http_options: [ recv_timeout: 30_000 ] ,
# optional, useful if you want to do local integration tests using Bypass or similar
# (https://github.com/PSPDFKit-labs/bypass), do not use it for production code,
# but only in your test config!
api_url: "http://localhost/"注: OPENAI_API_KEYという名前のAPIキーのENV変数を設定した場合、 System.get_env("OPENAI_API_KEY")を実行してコードで入手できる場合、構成ファイルにOS env変数をロードできます。
config.exsコンパイル時間であるため、 get_env/1関数はビルド中に実行されます。実行時間中にenv変数を取得する場合は、アプリケーションのconfig.exsの代わりにruntime.exsを使用してください(elixir doc ref)。
クライアントライブラリの構成は、使用する必要がある関数の最後の引数として%OpenAI.Config{} structを渡すことにより、ランタイムで上書きできます。たとえば、別のapi_key 、 organization_key 、またはhttp_optionsを使用する必要がある場合は、単に実行できます。
config_override = % OpenAI.Config { api_key: "test-api-key" } # this will return a config struct with "test-api-key" as api_key, all the other config are defaulted by the client by using values taken from config.exs, so you don't need to set the defaults manually
# chat_completion with overriden config
OpenAI . chat_completion ( [
model: "gpt-3.5-turbo" ,
messages: [
% { role: "system" , content: "You are a helpful assistant." } ,
% { role: "user" , content: "Who won the world series in 2020?" } ,
% { role: "assistant" , content: "The Los Angeles Dodgers won the World Series in 2020." } ,
% { role: "user" , content: "Where was it played?" }
]
] ,
config_override # <--- pass the overriden configuration as last argument of the function
)
# chat_completion with standard config
OpenAI . chat_completion (
model: "gpt-3.5-turbo" ,
messages: [
% { role: "system" , content: "You are a helpful assistant." } ,
% { role: "user" , content: "Who won the world series in 2020?" } ,
% { role: "assistant" , content: "The Los Angeles Dodgers won the World Series in 2020." } ,
% { role: "user" , content: "Where was it played?" }
]
)すべての機能で構成オーバーライドを実行できます。上記の例のように、構成を上書きする場合は、 params引数を四角いブラケットのリストとして明示的に渡す必要があることに注意してください。
https://platform.openai.com/account/api-keysからAPIキーを入手してください
利用可能なモデルのリストを取得します
OpenAI . models ( ) { :ok , % {
data: [ % {
"created" => 1651172505 ,
"id" => "davinci-search-query" ,
"object" => "model" ,
"owned_by" => "openai-dev" ,
"parent" => nil ,
"permission" => [
% {
"allow_create_engine" => false ,
"allow_fine_tuning" => false ,
"allow_logprobs" => true ,
...
}
] ,
"root" => "davinci-search-query"
} ,
... . ] ,
object: "list"
} }参照:https://platform.openai.com/docs/api-reference/models/list
特定のモデル情報を取得します
OpenAI . models ( "davinci-search-query" ) { :ok ,
% {
created: 1651172505 ,
id: "davinci-search-query" ,
object: "model" ,
owned_by: "openai-dev" ,
parent: nil ,
permission: [
% {
"allow_create_engine" => false ,
"allow_fine_tuning" => false ,
"allow_logprobs" => true ,
"allow_sampling" => true ,
"allow_search_indices" => true ,
"allow_view" => true ,
"created" => 1669066353 ,
"group" => nil ,
"id" => "modelperm-lYkiTZMmJMWm8jvkPx2duyHE" ,
"is_blocking" => false ,
"object" => "model_permission" ,
"organization" => "*"
}
] ,
root: "davinci-search-query"
} }参照:https://platform.openai.com/docs/api-reference/models/retrieve
プロンプトを与えられた1つ以上の予測完了を返します。関数は、「Engine_Id」と完了Openai APIで使用されるパラメーターのセットを引数として受け入れます
OpenAI . completions (
model: "finetuned-model" ,
prompt: "once upon a time" ,
max_tokens: 5 ,
temperature: 1 ,
...
) ## Example response
{ :ok , % {
choices: [
% {
"finish_reason" => "length" ,
"index" => 0 ,
"logprobs" => nil ,
"text" => " " thing we are given"
}
] ,
created: 1617147958 ,
id: "..." ,
model: "..." ,
object: "text_completion"
}
}参照:https://platform.openai.com/docs/api-reference/completions/create
このAPIは、 engines modelsに置き換えられているため、OpenAIによって非推奨されています。使用している場合は、できるだけ早くcompletions(params)に切り替えることを検討してください!
OpenAI . completions (
"davinci" , # engine_id
prompt: "once upon a time" ,
max_tokens: 5 ,
temperature: 1 ,
...
) { :ok , % {
choices: [
% {
"finish_reason" => "length" ,
"index" => 0 ,
"logprobs" => nil ,
"text" => " " thing we are given"
}
] ,
created: 1617147958 ,
id: "..." ,
model: "..." ,
object: "text_completion"
}
}参照:https://beta.openai.com/docs/api-reference/completions/create完了関数に渡すことができるパラメーターの完全なリストについては
チャットメッセージの完了を作成します
OpenAI . chat_completion (
model: "gpt-3.5-turbo" ,
messages: [
% { role: "system" , content: "You are a helpful assistant." } ,
% { role: "user" , content: "Who won the world series in 2020?" } ,
% { role: "assistant" , content: "The Los Angeles Dodgers won the World Series in 2020." } ,
% { role: "user" , content: "Where was it played?" }
]
) { :ok ,
% {
choices: [
% {
"finish_reason" => "stop" ,
"index" => 0 ,
"message" => % {
"content" =>
"The 2020 World Series was played at Globe Life Field in Arlington, Texas due to the COVID-19 pandemic." ,
"role" => "assistant"
}
}
] ,
created: 1_677_773_799 ,
id: "chatcmpl-6pftfA4NO9pOQIdxao6Z4McDlx90l" ,
model: "gpt-3.5-turbo-0301" ,
object: "chat.completion" ,
usage: % {
"completion_tokens" => 26 ,
"prompt_tokens" => 56 ,
"total_tokens" => 82
}
} }参照:https://platform.openai.com/docs/api-reference/chat/create完了関数に渡すことができるパラメーターの完全なリストについては
チャットメッセージの完了を作成します。デフォルトではself()にストリーミングしますが、異なるstream_to http_optionsパラメーターで構成オーバーライドを関数に渡すことで構成をオーバーライドできます。
import Config
config :openai ,
api_key: "your-api-key" ,
http_options: [ recv_timeout: :infinity , async: :once ] ,
...チャットの完了をストリームとして扱いたい場合は、 http_options上記のように設定する必要があります。
OpenAI . chat_completion ( [
model: "gpt-3.5-turbo" ,
messages: [
% { role: "system" , content: "You are a helpful assistant." } ,
% { role: "user" , content: "Who won the world series in 2020?" } ,
% { role: "assistant" , content: "The Los Angeles Dodgers won the World Series in 2020." } ,
% { role: "user" , content: "Where was it played?" }
] ,
stream: true , # set this param to true
]
)
|> Stream . each ( fn res ->
IO . inspect ( res )
end )
|> Stream . run ( ) % {
"choices" => [
% { "delta" => % { "role" => "assistant" } , "finish_reason" => nil , "index" => 0 }
] ,
"created" => 1682700668 ,
"id" => "chatcmpl-7ALbIuLju70hXy3jPa3o5VVlrxR6a" ,
"model" => "gpt-3.5-turbo-0301" ,
"object" => "chat.completion.chunk"
}
% {
"choices" => [
% { "delta" => % { "content" => "The" } , "finish_reason" => nil , "index" => 0 }
] ,
"created" => 1682700668 ,
"id" => "chatcmpl-7ALbIuLju70hXy3jPa3o5VVlrxR6a" ,
"model" => "gpt-3.5-turbo-0301" ,
"object" => "chat.completion.chunk"
}
% {
"choices" => [
% { "delta" => % { "content" => " World" } , "finish_reason" => nil , "index" => 0 }
] ,
"created" => 1682700668 ,
"id" => "chatcmpl-7ALbIuLju70hXy3jPa3o5VVlrxR6a" ,
"model" => "gpt-3.5-turbo-0301" ,
"object" => "chat.completion.chunk"
}
% {
"choices" => [
% {
"delta" => % { "content" => " Series" } ,
"finish_reason" => nil ,
"index" => 0
}
] ,
"created" => 1682700668 ,
"id" => "chatcmpl-7ALbIuLju70hXy3jPa3o5VVlrxR6a" ,
"model" => "gpt-3.5-turbo-0301" ,
"object" => "chat.completion.chunk"
}
% {
"choices" => [
% { "delta" => % { "content" => " in" } , "finish_reason" => nil , "index" => 0 }
] ,
"created" => 1682700668 ,
"id" => "chatcmpl-7ALbIuLju70hXy3jPa3o5VVlrxR6a" ,
"model" => "gpt-3.5-turbo-0301" ,
"object" => "chat.completion.chunk"
}
...提供された入力、命令、およびパラメーターの新しい編集を作成します
OpenAI . edits (
model: "text-davinci-edit-001" ,
input: "What day of the wek is it?" ,
instruction: "Fix the spelling mistakes"
) { :ok ,
% {
choices: [ % { "index" => 0 , "text" => "What day of the week is it? n " } ] ,
created: 1675443483 ,
object: "edit" ,
usage: % {
"completion_tokens" => 28 ,
"prompt_tokens" => 25 ,
"total_tokens" => 53
}
} }参照:https://platform.openai.com/docs/api-reference/edits/create
これにより、指定されたプロンプトに基づいて画像が生成されます。画像関数は実行に何度か必要であり、APIはタイムアウトエラーを返す場合があります。必要に応じて、機能の2番目の引数としてHTTPOison http_optionsを使用してオプションの構成構造を渡すことができます。
OpenAI . images_generations (
[ prompt: "A developer writing a test" , size: "256x256" ] ,
% OpenAI.Config { http_options: [ recv_timeout: 10 * 60 * 1000 ] } # optional!
) { :ok ,
% {
created: 1670341737 ,
data: [
% {
"url" => ... Returned url
}
]
} }注:このAPI署名は、他のAPIの慣習に準拠するためにv0.3.0で変更されました。エイリアスOpenAI.image_generations(params, request_options)再構成のために引き続き利用できます。使用している場合はOpenAI.images_generations(params, request_options)に切り替えることを検討してください。
注2: v0.5.0でhttp_optionsを通過する公式の方法OpenAI.images_generations(file_path, params, request_options) 、他のAPIの慣習に準拠するように変更されました。使用している場合はOpenAI.images_variations(params, config)に切り替えることを検討してください
参照:https://platform.openai.com/docs/api-reference/images/create
プロンプト画像関数に基づいて既存の画像を編集するには、実行するのに何度も必要となり、APIはタイムアウトエラーを返す場合があります。必要に応じて、関数の2番目の引数としてHTTPoison http_optionsを使用してオプションの構成構造を渡すことができます。
OpenAI . images_edits (
"/home/developer/myImg.png" ,
[ prompt: "A developer writing a test" , size: "256x256" ] ,
% OpenAI.Config { http_options: [ recv_timeout: 10 * 60 * 1000 ] } # optional!
) { :ok ,
% {
created: 1670341737 ,
data: [
% {
"url" => ... Returned url
}
]
} }注: v0.5.0でhttp_optionsを通過する公式の方法はOpenAI.images_edits(file_path, params, request_options)他のAPIの規則に準拠するように変更されました。使用している場合はOpenAI.images_edits(file_path, params, config)に切り替えることを検討してください
参照:https://platform.openai.com/docs/api-reference/images/create-edit
画像関数は実行に何度か必要であり、APIはタイムアウトエラーを返す場合があります。必要に応じて、機能の2番目の引数としてHTTPOison http_optionsを使用してオプションの構成構造を渡すことができます。
OpenAI . images_variations (
"/home/developer/myImg.png" ,
[ n: "5" ] ,
% OpenAI.Config { http_options: [ recv_timeout: 10 * 60 * 1000 ] } # optional!
) { :ok ,
% {
created: 1670341737 ,
data: [
% {
"url" => ... Returned url
}
]
} }注: v0.5.0でhttp_optionsを通過する公式の方法OpenAI.images_variations(file_path, params, request_options) 、他のAPIの規則に準拠するように変更されました。使用している場合はOpenAI.images_edits(file_path, params, config)に切り替えることを検討してください
参照:https://platform.openai.com/docs/api-reference/images/create-variation
OpenAI . embeddings (
model: "text-embedding-ada-002" ,
input: "The food was delicious and the waiter..."
) { :ok ,
% {
data: [
% {
"embedding" => [ 0.0022523515000000003 , - 0.009276069000000001 ,
0.015758524000000003 , - 0.007790373999999999 , - 0.004714223999999999 ,
0.014806155000000001 , - 0.009803046499999999 , - 0.038323310000000006 ,
- 0.006844355 , - 0.028672641 , 0.025345700000000002 , 0.018145794000000003 ,
- 0.0035904291999999997 , - 0.025498080000000003 , 5.142790000000001e-4 ,
- 0.016317246 , 0.028444072 , 0.0053713582 , 0.009631619999999999 ,
- 0.016469626 , - 0.015390275 , 0.004301531 , 0.006984035499999999 ,
- 0.007079272499999999 , - 0.003926933 , 0.018602932000000003 , 0.008666554 ,
- 0.022717162999999995 , 0.011460166999999997 , 0.023860006 ,
0.015568050999999998 , - 0.003587254600000001 , - 0.034843990000000005 ,
- 0.0041555012999999995 , - 0.026107594000000005 , - 0.02151083 ,
- 0.0057618289999999996 , 0.011714132499999998 , 0.008355445999999999 ,
0.004098358999999999 , 0.019199749999999998 , - 0.014336321 , 0.008952264 ,
0.0063395994 , - 0.04576447999999999 , ... ] ,
"index" => 0 ,
"object" => "embedding"
}
] ,
model: "text-embedding-ada-002-v2" ,
object: "list" ,
usage: % { "prompt_tokens" => 8 , "total_tokens" => 8 }
} }参照:https://platform.openai.com/docs/api-reference/embeddings/create
入力テキストからオーディオを生成します。
OpenAI . audio_speech (
model: "tts-1" ,
input: "You know that Voight-Kampf test of yours. Did you ever take that test yourself?" ,
voice: "alloy"
) { :ok , << 255 , 255 , ... >> }参照:https://platform.openai.com/docs/api-reference/audio/create
オーディオを入力言語に転写します。
OpenAI . audio_transcription (
"./path_to_file/blade_runner.mp3" , # file path
model: "whisper-1"
) { :ok ,
% {
text: "I've seen things you people wouldn't believe.."
} }参照:https://platform.openai.com/docs/api-reference/audio/create
オーディオを英語に変換します。
OpenAI . audio_translation (
"./path_to_file/werner_herzog_interview.mp3" , # file path
model: "whisper-1"
) { :ok ,
% {
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."
}
}参照:https://platform.openai.com/docs/api-reference/audio/create
ユーザーの組織に属するファイルのリストを返します。
OpenAI . files ( ) { :ok ,
% {
data: [
% {
"bytes" => 123 ,
"created_at" => 213 ,
"filename" => "file.jsonl" ,
"id" => "file-123321" ,
"object" => "file" ,
"purpose" => "fine-tune" ,
"status" => "processed" ,
"status_details" => nil
}
] ,
object: "list"
}
}参照:https://platform.openai.com/docs/api-reference/files
ファイルIDが与えられた場合、ユーザーの組織に属するファイルを返します
OpenAI . files ( "file-123321" ) { :ok ,
% {
bytes: 923 ,
created_at: 1675370979 ,
filename: "file.jsonl" ,
id: "file-123321" ,
object: "file" ,
purpose: "fine-tune" ,
status: "processed" ,
status_details: nil
}
}参照:https://platform.openai.com/docs/api-reference/files/retrieve
さまざまなエンドポイント/機能で使用するドキュメントを含むファイルをアップロードします。現在、1つの組織によってアップロードされたすべてのファイルのサイズは、最大1 GBです。ストレージ制限を増やす必要がある場合は、OpenAIにお問い合わせください。
OpenAI . files_upload ( "./file.jsonl" , purpose: "fine-tune" ) { :ok ,
% {
bytes: 923 ,
created_at: 1675373519 ,
filename: "file.jsonl" ,
id: "file-123" ,
object: "file" ,
purpose: "fine-tune" ,
status: "uploaded" ,
status_details: nil
}
}参照:https://platform.openai.com/docs/api-reference/files/upload
ファイルを削除します
OpenAI . files_delete ( "file-123" ) { :ok , % { deleted: true , id: "file-123" , object: "file" } }参照:https://platform.openai.com/docs/api-reference/files/delete
組織の微調整の仕事をリストします。
OpenAI . finetunes ( ) { :ok ,
% {
object: "list" ,
data: [ % {
"id" => "t-AF1WoRqd3aJAHsqc9NY7iL8F" ,
"object" => "fine-tune" ,
"model" => "curie" ,
"created_at" => 1614807352 ,
"fine_tuned_model" => null ,
"hyperparams" => { ... } ,
"organization_id" => "org-..." ,
"result_files" = [ ] ,
"status": "pending" ,
"validation_files" => [ ] ,
"training_files" => [ { ... } ] ,
"updated_at" => 1614807352 ,
} ] ,
}
}参照:https://platform.openai.com/docs/api-reference/fine-tunes/list
微調整の仕事に関する情報を取得します。
OpenAI . finetunes ( "t-AF1WoRqd3aJAHsqc9NY7iL8F" ) { :ok ,
% {
object: "list" ,
data: [ % {
"id" => "t-AF1WoRqd3aJAHsqc9NY7iL8F" ,
"object" => "fine-tune" ,
"model" => "curie" ,
"created_at" => 1614807352 ,
"fine_tuned_model" => null ,
"hyperparams" => { ... } ,
"organization_id" => "org-..." ,
"result_files" = [ ] ,
"status": "pending" ,
"validation_files" => [ ] ,
"training_files" => [ { ... } ] ,
"updated_at" => 1614807352 ,
} ] ,
}
}参照:https://platform.openai.com/docs/api-reference/fine-tunes/retrieve
特定のデータセットから指定されたモデルを微調整するジョブを作成します。
OpenAI . finetunes_create (
training_file: "file-123213231" ,
model: "curie" ,
) { :ok ,
% {
created_at: 1675527767 ,
events: [
% {
"created_at" => 1675527767 ,
"level" => "info" ,
"message" => "Created fine-tune: ft-IaBYfSSAK47UUCbebY5tBIEj" ,
"object" => "fine-tune-event"
}
] ,
fine_tuned_model: nil ,
hyperparams: % {
"batch_size" => nil ,
"learning_rate_multiplier" => nil ,
"n_epochs" => 4 ,
"prompt_loss_weight" => 0.01
} ,
id: "ft-IaBYfSSAK47UUCbebY5tBIEj" ,
model: "curie" ,
object: "fine-tune" ,
organization_id: "org-1iPTOIak4b5fpuIB697AYMmO" ,
result_files: [ ] ,
status: "pending" ,
training_files: [
% {
"bytes" => 923 ,
"created_at" => 1675373519 ,
"filename" => "file-12321323.jsonl" ,
"id" => "file-12321323" ,
"object" => "file" ,
"purpose" => "fine-tune" ,
"status" => "processed" ,
"status_details" => nil
}
] ,
updated_at: 1675527767 ,
validation_files: [ ]
} }参照:https://platform.openai.com/docs/api-reference/fine-tunes/create
微調整されたジョブの細かいステータスの更新を取得します。
OpenAI . finetunes_list_events ( "ft-AF1WoRqd3aJAHsqc9NY7iL8F" ) { :ok ,
% {
data: [
% {
"created_at" => 1675376995 ,
"level" => "info" ,
"message" => "Created fine-tune: ft-123" ,
"object" => "fine-tune-event"
} ,
% {
"created_at" => 1675377104 ,
"level" => "info" ,
"message" => "Fine-tune costs $0.00" ,
"object" => "fine-tune-event"
} ,
% {
"created_at" => 1675377105 ,
"level" => "info" ,
"message" => "Fine-tune enqueued. Queue number: 18" ,
"object" => "fine-tune-event"
} ,
... ,
]
}
}参照:https://platform.openai.com/docs/api-reference/fine-tunes/events
すぐに微調整ジョブをキャンセルします。
OpenAI . finetunes_cancel ( "ft-AF1WoRqd3aJAHsqc9NY7iL8F" ) { :ok ,
% {
created_at: 1675527767 ,
events: [
...
% {
"created_at" => 1675528080 ,
"level" => "info" ,
"message" => "Fine-tune cancelled" ,
"object" => "fine-tune-event"
}
] ,
fine_tuned_model: nil ,
hyperparams: % {
"batch_size" => 1 ,
"learning_rate_multiplier" => 0.1 ,
"n_epochs" => 4 ,
"prompt_loss_weight" => 0.01
} ,
id: "ft-IaBYfSSAK47UUCbebY5tBIEj" ,
model: "curie" ,
object: "fine-tune" ,
organization_id: "org-1iPTOIak4b5fpuIB697AYMmO" ,
result_files: [ ] ,
status: "cancelled" ,
training_files: [
% {
"bytes" => 923 ,
"created_at" => 1675373519 ,
"filename" => "file123.jsonl" ,
"id" => "file-123" ,
"object" => "file" ,
"purpose" => "fine-tune" ,
"status" => "processed" ,
"status_details" => nil
}
] ,
updated_at: 1675528080 ,
validation_files: [ ]
} }すぐに微調整ジョブをキャンセルします。
OpenAI . finetunes_delete_model ( "model-id" ) { :ok ,
% {
id: "model-id" ,
object: "model" ,
deleted: true
}
}参照:https://platform.openai.com/docs/api-reference/fine-tunes/delete-model
テキストがOpenaiのコンテンツポリシーに違反している場合に分類します
OpenAI . moderations ( input: "I want to kill everyone!" ) { :ok ,
% {
id: "modr-6gEWXyuaU8dqiHpbAHIsdru0zuC88" ,
model: "text-moderation-004" ,
results: [
% {
"categories" => % {
"hate" => false ,
"hate/threatening" => false ,
"self-harm" => false ,
"sexual" => false ,
"sexual/minors" => false ,
"violence" => true ,
"violence/graphic" => false
} ,
"category_scores" => % {
"hate" => 0.05119025334715844 ,
"hate/threatening" => 0.00321022979915142 ,
"self-harm" => 7.337320857914165e-5 ,
"sexual" => 1.1111642379546538e-6 ,
"sexual/minors" => 3.588798147546868e-10 ,
"violence" => 0.9190407395362855 ,
"violence/graphic" => 1.2791929293598514e-7
} ,
"flagged" => true
}
]
} }参照:https://platform.openai.com/docs/api-reference/moderations/create
現在、次のAPIはベータ版です。それらを使用するには、構成にbetaパラメーターを設定してください。
config :openai ,
# optional, use when required by an OpenAI API beta, e.g.:
beta: "assistants=v1"アシスタントのリストを取得します。
OpenAI . assistants ( ) { :ok ,
% {
data: [
% {
"created_at" => 1699472932 ,
"description" => nil ,
"file_ids" => [ "file-..." ] ,
"id" => "asst_..." ,
"instructions" => "..." ,
"metadata" => % { } ,
"model" => "gpt-4-1106-preview" ,
"name" => "..." ,
"object" => "assistant" ,
"tools" => [ % { "type" => "retrieval" } ]
}
] ,
first_id: "asst_..." ,
has_more: false ,
last_id: "asst_..." ,
object: "list"
} }参照:https://platform.openai.com/docs/api-reference/assistants/listassistants
クエリパラメーションでフィルタリングされたアシスタントのリストを取得します。
OpenAI . assistants ( after: "" , limit: 10 ) { :ok ,
% {
data: [
% {
"created_at" => 1699472932 ,
"description" => nil ,
"file_ids" => [ "file-..." ] ,
"id" => "asst_..." ,
"instructions" => "..." ,
"metadata" => % { } ,
"model" => "gpt-4-1106-preview" ,
"name" => "..." ,
"object" => "assistant" ,
"tools" => [ % { "type" => "retrieval" } ]
} ,
...
] ,
first_id: "asst_..." ,
has_more: false ,
last_id: "asst_..." ,
object: "list"
} }参照:https://platform.openai.com/docs/api-reference/assistants/listassistants
IDでアシスタントを取得します。
OpenAI . assistants ( "asst_..." ) { :ok ,
% {
created_at: 1699472932 ,
description: nil ,
file_ids: [ "file-..." ] ,
id: "asst_..." ,
instructions: "..." ,
metadata: % { } ,
model: "gpt-4-1106-preview" ,
name: "..." ,
object: "assistant" ,
tools: [ % { "type" => "retrieval" } ]
} }参照:https://platform.openai.com/docs/api-reference/assistants/getassistant
新しいアシスタントを作成します。
OpenAI . assistants_create (
model: "gpt-3.5-turbo-1106" ,
name: "My assistant" ,
instructions: "You are a research assistant." ,
tools: [
% { type: "retrieval" }
] ,
file_ids: [ "file-..." ]
) { :ok ,
% {
created_at: 1699640038 ,
description: nil ,
file_ids: [ "file-..." ] ,
id: "asst_..." ,
instructions: "You are a research assistant." ,
metadata: % { } ,
model: "gpt-3.5-turbo-1106" ,
name: "My assistant" ,
object: "assistant" ,
tools: [ % { "type" => "retrieval" } ]
} }参照:https://platform.openai.com/docs/api-reference/assistants/createassistant
既存のアシスタントを変更します。
OpenAI . assistants_modify (
"asst_..." ,
model: "gpt-4-1106-preview" ,
name: "My upgraded assistant"
) { :ok ,
% {
created_at: 1699640038 ,
description: nil ,
file_ids: [ "file-..." ] ,
id: "asst_..." ,
instructions: "You are a research assistant." ,
metadata: % { } ,
model: "gpt-4-1106-preview" ,
name: "My upgraded assistant"
object: "assistant" ,
tools: [ % { "type" => "retrieval" } ]
} }参照:https://platform.openai.com/docs/api-reference/assistants/modifyassistant
アシスタントを削除します。
OpenAI . assistants_delete ( "asst_..." ) { :ok ,
% {
deleted: true ,
id: "asst_..." ,
object: "assistant.deleted"
} }参照:https://platform.openai.com/docs/api-reference/assistants/deleteassistant
特定のアシスタントに関連付けられたファイルのリストを取得します。
OpenAI . assistant_files ( "asst_..." ) { :ok ,
% {
data: [
% {
"assistant_id" => "asst_..." ,
"created_at" => 1699472933 ,
"id" => "file-..." ,
"object" => "assistant.file"
}
] ,
first_id: "file-..." ,
has_more: false ,
last_id: "file-..." ,
object: "list"
} }参照:https://platform.openai.com/docs/api-reference/assistants/listassistantfiles
クエリパラメーションでフィルタリングされた特定のアシスタントに関連付けられたファイルのリストを取得します。
OpenAI . assistant_files ( "asst_..." , order: "desc" ) { :ok ,
% {
data: [
% {
"assistant_id" => "asst_..." ,
"created_at" => 1699472933 ,
"id" => "file-..." ,
"object" => "assistant.file"
}
] ,
first_id: "file-..." ,
has_more: false ,
last_id: "file-..." ,
object: "list"
} }参照:https://platform.openai.com/docs/api-reference/assistants/listassistantfiles
IDでアシスタントファイルを取得します
OpenAI . assistant_file ( "asst_..." , "file_..." ) { :ok ,
% {
assistant_id: "asst_..." ,
created_at: 1699472933 ,
id: "file-..." ,
object: "assistant.file"
} }参照:https://platform.openai.com/docs/api-reference/assistants/getassistantfile
以前にアップロードされたファイルをアシスタントに添付します。
OpenAI . assistant_file_create ( "asst_..." , file_id: "file-..." ) { :ok ,
% {
assistant_id: "asst_..." ,
created_at: 1699472933 ,
id: "file-..." ,
object: "assistant.file"
} }参照:https://platform.openai.com/docs/api-reference/assistants/createassistantfile
アシスタントからファイルを分離します。ファイル自体は自動的に削除されません。
OpenAI . assistant_file_delete ( "asst_..." , "file-..." ) { :ok ,
% {
deleted: true ,
id: "file-..." ,
object: "assistant.file.deleted"
} }参照:https://platform.openai.com/docs/api-reference/assistants/deleteassistantfile
スレッドのリストを取得します。注:この執筆時点では、この機能はOpenaiによって文書化されていないままです。
OpenAI . threads ( ) { :ok ,
% {
data: [
% {
"created_at" => 1699705727 ,
"id" => "thread_..." ,
"metadata" => % { "key_1" => "value 1" , "key_2" => "value 2" } ,
"object" => "thread"
} ,
...
] ,
first_id: "thread_..." ,
has_more: false ,
last_id: "thread_..." ,
object: "list"
} }クエリパラメーションでスレッドのリストを取得します。注:この執筆時点では、この機能はOpenaiによって文書化されていないままです。
OpenAI . threads ( limit: 2 ) { :ok ,
% {
data: [
% {
"created_at" => 1699705727 ,
"id" => "thread_..." ,
"metadata" => % { "key_1" => "value 1" , "key_2" => "value 2" } ,
"object" => "thread"
} ,
...
] ,
first_id: "thread_..." ,
has_more: false ,
last_id: "thread_..." ,
object: "list"
} }いくつかのメッセージとメタデータを備えた新しいスレッドを作成します。
messages = [
% {
role: "user" ,
content: "Hello, what is AI?" ,
file_ids: [ "file-..." ]
} ,
% {
role: "user" ,
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 ) { :ok ,
% {
created_at: 1699703890 ,
id: "thread_..." ,
metadata: % { "key_1" => "value 1" , "key_2" => "value 2" } ,
object: "thread"
} }参照:https://platform.openai.com/docs/api-reference/threads/createthread
新しいスレッドを作成して実行します。
messages = [
% {
role: "user" ,
content: "Hello, what is AI?" ,
file_ids: [ "file-..." ]
} ,
% {
role: "user" ,
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 ,
model: "gpt-4-1106-preview" ,
instructions: "You are an AI learning assistant." ,
tools: [ % {
"type" => "retrieval"
} ] ,
metadata: run_metadata
]
OpenAI . threads_create_and_run ( params ) { :ok ,
% {
assistant_id: "asst_..." ,
cancelled_at: nil ,
completed_at: nil ,
created_at: 1699897907 ,
expires_at: 1699898507 ,
failed_at: nil ,
file_ids: [ "file-..." ] ,
id: "run_..." ,
instructions: "You are an AI learning assistant." ,
last_error: nil ,
metadata: % { "key_3" => "value 3" } ,
model: "gpt-4-1106-preview" ,
object: "thread.run" ,
started_at: nil ,
status: "queued" ,
thread_id: "thread_..." ,
tools: [ % { "type" => "retrieval" } ]
} }参照:https://platform.openai.com/docs/api-reference/runs/createthreadandrun
既存のスレッドを変更します。
metadata = % {
key_3: "value 3"
}
OpenAI . threads_modify ( "thread_..." , metadata: metadata ) { :ok ,
% {
created_at: 1699704406 ,
id: "thread_..." ,
metadata: % { "key_1" => "value 1" , "key_2" => "value 2" , "key_3" => "value 3" } ,
object: "thread"
} }参照:https://platform.openai.com/docs/api-reference/threads/modifythread
既存のスレッドを変更します。
OpenAI . threads_delete ( "thread_..." ) { :ok ,
% {
deleted: true ,
id: "thread_..." ,
object: "thread.deleted"
} }参照:https://platform.openai.com/docs/api-reference/threads/deletethread
特定のスレッドに関連付けられたメッセージのリストを取得します。
OpenAI . thread_messages ( "thread_..." ) { :ok ,
% {
data: [
% {
"assistant_id" => nil ,
"content" => [
% {
"text" => % {
"annotations" => [ ] ,
"value" => "How does AI work? Explain it in simple terms."
} ,
"type" => "text"
}
] ,
"created_at" => 1699705727 ,
"file_ids" => [ ] ,
"id" => "msg_..." ,
"metadata" => % { } ,
"object" => "thread.message" ,
"role" => "user" ,
"run_id" => nil ,
"thread_id" => "thread_..."
} ,
...
] ,
first_id: "msg_..." ,
has_more: false ,
last_id: "msg_..." ,
object: "list"
} }参照:https://platform.openai.com/docs/api-reference/messages/listmessages
クエリパラメーションでフィルタリングされた特定のスレッドに関連付けられたメッセージのリストを取得します。
OpenAI . thread_messages ( "thread_..." , after: "msg_..." ) { :ok ,
% {
data: [
% {
"assistant_id" => nil ,
"content" => [
% {
"text" => % {
"annotations" => [ ] ,
"value" => "How does AI work? Explain it in simple terms."
} ,
"type" => "text"
}
] ,
"created_at" => 1699705727 ,
"file_ids" => [ ] ,
"id" => "msg_..." ,
"metadata" => % { } ,
"object" => "thread.message" ,
"role" => "user" ,
"run_id" => nil ,
"thread_id" => "thread_..."
} ,
...
] ,
first_id: "msg_..." ,
has_more: false ,
last_id: "msg_..." ,
object: "list"
} }参照:https://platform.openai.com/docs/api-reference/messages/listmessages
IDでスレッドメッセージを取得します。
OpenAI . thread_message ( "thread_..." , "msg_..." ) { :ok ,
% {
assistant_id: nil ,
content: [
% {
"text" => % { "annotations" => [ ] , "value" => "Hello, what is AI?" } ,
"type" => "text"
}
] ,
created_at: 1699705727 ,
file_ids: [ "file-..." ] ,
id: "msg_..." ,
metadata: % { } ,
object: "thread.message" ,
role: "user" ,
run_id: nil ,
thread_id: "thread_..."
} }参照:https://platform.openai.com/docs/api-reference/messages/getmessage
スレッド内にメッセージを作成します。
params = [
role: "user" ,
content: "Hello, what is AI?" ,
file_ids: [ "file-9Riyo515uf9KVfwdSrIQiqtC" ] ,
metadata: % {
key_1: "value 1" ,
key_2: "value 2"
}
]
OpenAI . thread_message_create ( "thread_..." , params ) { :ok ,
% {
assistant_id: nil ,
content: [
% {
"text" => % { "annotations" => [ ] , "value" => "Hello, what is AI?" } ,
"type" => "text"
}
] ,
created_at: 1699706818 ,
file_ids: [ "file-..." ] ,
id: "msg_..." ,
metadata: % { "key_1" => "value 1" , "key_2" => "value 2" } ,
object: "thread.message" ,
role: "user" ,
run_id: nil ,
thread_id: "thread_..."
} }参照:https://platform.openai.com/docs/api-reference/messages/createmessage
スレッド内にメッセージを作成します。
params = [
metadata: % {
key_3: "value 3"
}
]
OpenAI . thread_message_modify ( "thread_..." , "msg_..." , params ) { :ok ,
% {
assistant_id: nil ,
content: [
% {
"text" => % { "annotations" => [ ] , "value" => "Hello, what is AI?" } ,
"type" => "text"
}
] ,
created_at: 1699706818 ,
file_ids: [ "file-..." ] ,
id: "msg_..." ,
metadata: % { "key_1" => "value 1" , "key_2" => "value 2" , "key_3" => "value 3" } ,
object: "thread.message" ,
role: "user" ,
run_id: nil ,
thread_id: "thread_..."
} }参照:https://platform.openai.com/docs/api-reference/messages/modifymessage
スレッドの特定のメッセージに関連付けられたファイルのリストを取得します。
OpenAI . thread_message_files ( "thread_..." , "msg_..." ) { :ok ,
% {
data: [
% {
"created_at" => 1699706818 ,
"id" => "file-..." ,
"message_id" => "msg_..." ,
"object" => "thread.message.file"
}
] ,
first_id: "file-..." ,
has_more: false ,
last_id: "file-..." ,
object: "list"
} }参照:https://platform.openai.com/docs/api-reference/messages/listmessagefiles
クエリパラメーションでフィルタリングされたスレッドの特定のメッセージに関連付けられたファイルのリストを取得します。
OpenAI . thread_message_files ( "thread_..." , "msg_..." , after: "file-..." ) { :ok ,
% {
data: [
% {
"created_at" => 1699706818 ,
"id" => "file-..." ,
"message_id" => "msg_..." ,
"object" => "thread.message.file"
}
] ,
first_id: "file-..." ,
has_more: false ,
last_id: "file-..." ,
object: "list"
} }参照:https://platform.openai.com/docs/api-reference/messages/listmessagefiles
メッセージファイルオブジェクトを取得します。
OpenAI . thread_message_file ( "thread_..." , "msg_..." , "file-..." ) { :ok ,
% {
created_at: 1699706818 ,
id: "file-..." ,
message_id: "msg_..." ,
object: "thread.message.file"
} }参照:https://platform.openai.com/docs/api-reference/messages/getMessageFile
クエリパラメーションでフィルタリングされた特定のスレッドに関連付けられた実行のリストを取得します。
OpenAI . thread_runs ( "thread_..." , limit: 10 ) { :ok , % {
data: [ ] ,
first_id: nil ,
has_more: false ,
last_id: nil ,
object: "list"
} }参照:https://platform.openai.com/docs/api-reference/runs/listruns
IDで実行される特定のスレッドを取得します。
OpenAI . thread_run ( "thread_..." , "run_..." ) { :ok ,
% {
assistant_id: "asst_J" ,
cancelled_at: nil ,
completed_at: 1700234149 ,
created_at: 1700234128 ,
expires_at: nil ,
failed_at: nil ,
file_ids: [ ] ,
id: "run_" ,
instructions: "You are an AI learning assistant." ,
last_error: nil ,
metadata: % { "key_3" => "value 3" } ,
model: "gpt-4-1106-preview" ,
object: "thread.run" ,
started_at: 1700234129 ,
status: "expired" ,
thread_id: "thread_" ,
tools: [ % { "type" => "retrieval" } ]
} }参照:https://platform.openai.com/docs/api-reference/runs/getrun
特定のアシスタントを使用してスレッドの実行を作成します。
params = [
assistant_id: "asst_..." ,
model: "gpt-4-1106-preview" ,
tools: [ % {
"type" => "retrieval"
} ]
]
OpenAI . thread_run_create ( "thread_..." , params ) { :ok ,
% {
assistant_id: "asst_..." ,
cancelled_at: nil ,
completed_at: nil ,
created_at: 1699711115 ,
expires_at: 1699711715 ,
failed_at: nil ,
file_ids: [ "file-..." ] ,
id: "run_..." ,
instructions: "..." ,
last_error: nil ,
metadata: % { } ,
model: "gpt-4-1106-preview" ,
object: "thread.run" ,
started_at: nil ,
status: "queued" ,
thread_id: "thread_..." ,
tools: [ % { "type" => "retrieval" } ]
} }参照:https://platform.openai.com/docs/api-reference/runs/createrun
既存のスレッドの実行を変更します。
params = [
metadata: % {
key_3: "value 3"
}
]
OpenAI . thread_run_modify ( "thread_..." , "run_..." , params ) { :ok ,
% {
assistant_id: "asst_..." ,
cancelled_at: nil ,
completed_at: 1699711125 ,
created_at: 1699711115 ,
expires_at: nil ,
failed_at: nil ,
file_ids: [ "file-..." ] ,
id: "run_..." ,
instructions: "..." ,
last_error: nil ,
metadata: % { "key_3" => "value 3" } ,
model: "gpt-4-1106-preview" ,
object: "thread.run" ,
started_at: 1699711115 ,
status: "expired" ,
thread_id: "thread_..." ,
tools: [ % { "type" => "retrieval" } ]
} }参照:https://platform.openai.com/docs/api-reference/runs/modifyrun
実行がステータスを持っている場合: "requires_action"およびrequired_action.typeがsubmit_tool_outputsである場合、このエンドポイントを使用して、すべて完了したらツールコールから出力を送信できます。すべての出力は、単一のリクエストで提出する必要があります。
params = [
tool_outputs: [ % {
tool_call_id: "call_abc123" ,
output: "test"
} ]
]
OpenAI . thread_run_submit_tool_outputs ( "thread_..." , "run_..." , params ) { :ok ,
% {
assistant_id: "asst_abc123" ,
cancelled_at: nil ,
completed_at: nil ,
created_at: 1699075592 ,
expires_at: 1699076192 ,
failed_at: nil ,
file_ids: [ ] ,
id: "run_abc123" ,
instructions: "You tell the weather." ,
last_error: nil ,
metadata: % { } ,
model: "gpt-4" ,
object: "thread.run" ,
started_at: 1699075592 ,
status: "queued" ,
thread_id: "thread_abc123" ,
tools: [
% {
"function" => % {
"description" => "Determine weather in my location" ,
"name" => "get_weather" ,
"parameters" => % {
"properties" => % {
"location" => % {
"description" => "The city and state e.g. San Francisco, CA" ,
"type" => "string"
} ,
"unit" => % { "enum" => [ "c" , "f" ] , "type" => "string" }
} ,
"required" => [ "location" ] ,
"type" => "object"
}
} ,
"type" => "function"
}
]
}
}参照:https://platform.openai.com/docs/api-reference/runs/submittooloutputs
in_progress実行をキャンセルします。
OpenAI . thread_run_cancel ( "thread_..." , "run_..." ) { :ok ,
% {
assistant_id: "asst_..." ,
cancelled_at: nil ,
completed_at: 1699711125 ,
created_at: 1699711115 ,
expires_at: nil ,
failed_at: nil ,
file_ids: [ "file-..." ] ,
id: "run_..." ,
instructions: "..." ,
last_error: nil ,
metadata: % { "key_3" => "value 3" } ,
model: "gpt-4-1106-preview" ,
object: "thread.run" ,
started_at: 1699711115 ,
status: "expired" ,
thread_id: "thread_..." ,
tools: [ % { "type" => "retrieval" } ]
} }参照:https://platform.openai.com/docs/api-reference/runs/cancelrun
スレッドの特定の実行に関連付けられたステップのリストを取得します。
OpenAI . thread_run_steps ( "thread_..." , "run_..." ) { :ok ,
% {
data: [
% {
"assistant_id" => "asst_..." ,
"cancelled_at" => nil ,
"completed_at" => 1699897927 ,
"created_at" => 1699897908 ,
"expires_at" => nil ,
"failed_at" => nil ,
"id" => "step_..." ,
"last_error" => nil ,
"object" => "thread.run.step" ,
"run_id" => "run_..." ,
"status" => "completed" ,
"step_details" => % {
"message_creation" => % { "message_id" => "msg_..." } ,
"type" => "message_creation"
} ,
"thread_id" => "thread_..." ,
"type" => "message_creation"
}
] ,
first_id: "step_..." ,
has_more: false ,
last_id: "step_..." ,
object: "list"
} }参照:https://platform.openai.com/docs/api-reference/runs/listrunsteps
クエリパラメーションでフィルタリングされたスレッドの特定の実行に関連付けられたステップのリストを取得します。
OpenAI . thread_run_steps ( "thread_..." , "run_..." , order: "asc" ) { :ok ,
% {
data: [
% {
"assistant_id" => "asst_..." ,
"cancelled_at" => nil ,
"completed_at" => 1699897927 ,
"created_at" => 1699897908 ,
"expires_at" => nil ,
"failed_at" => nil ,
"id" => "step_..." ,
"last_error" => nil ,
"object" => "thread.run.step" ,
"run_id" => "run_..." ,
"status" => "completed" ,
"step_details" => % {
"message_creation" => % { "message_id" => "msg_..." } ,
"type" => "message_creation"
} ,
"thread_id" => "thread_..." ,
"type" => "message_creation"
}
] ,
first_id: "step_..." ,
has_more: false ,
last_id: "step_..." ,
object: "list"
} }参照:https://platform.openai.com/docs/api-reference/runs/listrunsteps
クエリパラメーションでフィルタリングされたスレッドの特定の実行に関連付けられたステップのリストを取得します。
OpenAI . thread_run_steps ( "thread_..." , "run_..." , order: "asc" ) { :ok ,
% {
data: [
% {
"assistant_id" => "asst_..." ,
"cancelled_at" => nil ,
"completed_at" => 1699897927 ,
"created_at" => 1699897908 ,
"expires_at" => nil ,
"failed_at" => nil ,
"id" => "step_..." ,
"last_error" => nil ,
"object" => "thread.run.step" ,
"run_id" => "run_..." ,
"status" => "completed" ,
"step_details" => % {
"message_creation" => % { "message_id" => "msg_..." } ,
"type" => "message_creation"
} ,
"thread_id" => "thread_..." ,
"type" => "message_creation"
}
] ,
first_id: "step_..." ,
has_more: false ,
last_id: "step_..." ,
object: "list"
} }参照:https://platform.openai.com/docs/api-reference/runs/listrunsteps
IDでスレッド実行ステップを取得します。
OpenAI . thread_run_step ( "thread_..." , "run_..." , "step_..." ) { :ok ,
% {
assistant_id: "asst_..." ,
cancelled_at: nil ,
completed_at: 1699897927 ,
created_at: 1699897908 ,
expires_at: nil ,
failed_at: nil ,
id: "step_..." ,
last_error: nil ,
object: "thread.run.step" ,
run_id: "run_..." ,
status: "completed" ,
step_details: % {
"message_creation" => % { "message_id" => "msg_..." } ,
"type" => "message_creation"
} ,
thread_id: "thread_..." ,
type: "message_creation"
} }参照:https://platform.openai.com/docs/api-reference/runs/getrunstep
以下のAPIは非推奨ですが、現在、古いバージョンとの翻訳性のためにライブラリによってサポートされています。次のAPIを使用している場合は、プロジェクトからできるだけ早く削除することを検討してください!
注:バージョン0.5.0の検索、回答、分類APIはサポートされていません(Openaiによって削除されたため)。まだV0.4.2を使用することを検討する必要がある場合
利用可能なエンジンのリストを取得します
OpenAI . engines ( ) { :ok , % {
"data" => [
% { "id" => "davinci" , "object" => "engine" , "max_replicas": ... } ,
... ,
...
]
}
参照:https://beta.openai.com/docs/api-reference/engines/list
特定のエンジン情報を取得します
OpenAI . engines ( "davinci" ) { :ok , % {
"id" => "davinci" ,
"object" => "engine" ,
"max_replicas": ...
}
}参照:https://beta.openai.com/docs/api-reference/engines/retrieve
このパッケージは、MITライセンスの条件の下でオープンソースとして利用できます。