Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/OpenAI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -100,6 +125,7 @@ end
export OpenAIResponse
export list_models
export retrieve_model
export create_chat
export create_completion
export create_edit

Expand Down
10 changes: 10 additions & 0 deletions test/completion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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