From 66a0d96163ca78b6b9e75e5cb3b753989e64e215 Mon Sep 17 00:00:00 2001 From: Jerry Ling Date: Wed, 1 Mar 2023 23:32:05 -0500 Subject: [PATCH] add chat/completion --- src/OpenAI.jl | 26 ++++++++++++++++++++++++++ test/completion.jl | 10 ++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/OpenAI.jl b/src/OpenAI.jl index 2a999c5..676c6b2 100644 --- a/src/OpenAI.jl +++ b/src/OpenAI.jl @@ -81,6 +81,31 @@ function create_completion(api_key::String, model_id::String; kwargs...) return openai_request("completions", api_key; method = "POST", model = model_id, kwargs...) end +""" +Create chat + +https://platform.openai.com/docs/api-reference/chat + +# Arguments: +- `api_key::String`: OpenAI API key +- `model_id::String`: Model id +- `messages::Vector`: The chat history so far. + +## Example: + +```julia +julia> CC = create_chat("..........", "gpt-3.5-turbo", + [Dict("role" => "user", "content"=> "What is the OpenAI mission?")] +); + +julia> CC.response.choices[1][:message][:content] +"\n\nThe OpenAI mission is to create safe and beneficial artificial intelligence (AI) that can help humanity achieve its full potential. The organization aims to discover and develop technical approaches to AI that are safe and aligned with human values. OpenAI believes that AI can help to solve some of the world's most pressing problems, such as climate change, disease, inequality, and poverty. The organization is committed to advancing research and development in AI while ensuring that it is used ethically and responsibly." +``` +""" +function create_chat(api_key::String, model_id::String, messages; kwargs...) + return openai_request("chat/completions", api_key; method = "POST", model = model_id, messages=messages, kwargs...) +end + """ Create edit @@ -100,6 +125,7 @@ end export OpenAIResponse export list_models export retrieve_model +export create_chat export create_completion export create_edit diff --git a/test/completion.jl b/test/completion.jl index 86a418b..14de7c8 100644 --- a/test/completion.jl +++ b/test/completion.jl @@ -12,4 +12,14 @@ if !=(r.status, 200) @test false end + + r = create_chat( + ENV["OPENAI_API_KEY"], + "gpt-3.5-turbo", + [Dict("role" => "user", "content"=> "What is the OpenAI mission?")] + ) + println(r.response["choices"][begin]["message"]["content"]) + if !=(r.status, 200) + @test false + end end