Skip to content

Commit

Permalink
Merge pull request #2 from rory-linehan/0.3.0
Browse files Browse the repository at this point in the history
0.3.0
  • Loading branch information
roryl23 authored Aug 11, 2022
2 parents 9017756 + 849c357 commit 988296a
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 0.3.0

* adding completions

### 0.2.0

* engine endpoints deprecated in favor of models
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "OpenAI"
uuid = "e9f21f70-7185-4079-aca2-91159181367c"
authors = ["Rory Linehan <[email protected]>"]
version = "0.2.0"
version = "0.3.0"

[deps]
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ Provides a wrapper to the OpenAI API.

See tests for example usage.

## Feature Requests
## Feature requests

Feel free to open a PR, or file an issue if that's out of reach!
34 changes: 31 additions & 3 deletions src/OpenAI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ https://beta.openai.com/docs/api-reference/models/retrieve
# Arguments:
- `api_key::String`: OpenAI API key
- `engine_id::String`: Engine id
- `model_id::String`: Model id
"""
function retrieve_model(api_key::String, engine_id::String)
url = BASE_URL_v1*"/models/"*engine_id
function retrieve_model(api_key::String, model_id::String)
url = BASE_URL_v1*"/models/"*model_id
request_headers = Dict(
"Authorization" => "Bearer "*api_key,
"Content-Type" => "application/json",
Expand All @@ -57,8 +57,36 @@ function retrieve_model(api_key::String, engine_id::String)
return OpenAIResponse(response.status, JSON.parse(String(response.body)))
end

"""
Create completion
https://beta.openai.com/docs/api-reference/completions
# Arguments:
- `api_key::String`: OpenAI API key
- `model_id::String`: Model id
"""
function create_completion(api_key::String, model_id::String; kwargs...)
url = BASE_URL_v1*"/completions"
request_headers = Dict(
"Authorization" => "Bearer "*api_key,
"Content-Type" => "application/json",
)
params = Dict(kwargs)
params[:model] = model_id
response = HTTP.request(
"POST",
url,
request_headers,
JSON.json(params);
retry = false
)
return OpenAIResponse(response.status, JSON.parse(String(response.body)))
end

export OpenAIResponse
export list_models
export retrieve_model
export create_completion

end # module
16 changes: 16 additions & 0 deletions test/completion.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@testset "completion" begin
api_key = "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"
r = list_models(api_key)
if !=(r.status, 200)
@test false
end
r = create_completion(
api_key,
r.response["data"][begin]["id"];
prompt = "Say this is a test"
)
println(r.response["choices"][begin]["text"])
if !=(r.status, 200)
@test false
end
end
24 changes: 12 additions & 12 deletions test/models.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
@testset "models" begin
api_key = "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"
r = list_models(api_key)
println(r)
if !=(r.status, 200)
@test false
end
r = retrieve_model(api_key, r.response["data"][begin]["id"])
println(r)
if !=(r.status, 200)
@test false
end
end
api_key = "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"
r = list_models(api_key)
println(r)
if !=(r.status, 200)
@test false
end
r = retrieve_model(api_key, r.response["data"][begin]["id"])
println(r)
if !=(r.status, 200)
@test false
end
end
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ using Test
@testset "models" begin
include("models.jl")
end
@testset "completion" begin
include("completion.jl")
end
end

2 comments on commit 988296a

@roryl23
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/66108

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.0 -m "<description of version>" 988296a53ee7a28fc8d6ffe3bdd4eb9b6e9c5804
git push origin v0.3.0

Please sign in to comment.