Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small refactor, add safety settings, expand GoogleTextResponse, allow multi-turn convos #4

Merged
merged 10 commits into from
Feb 24, 2024
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Documentation
name: Documenter

on:
push:
Expand Down
32 changes: 32 additions & 0 deletions .github/workflows/FormatCheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Format Check
on:
workflow_dispatch:
push:
branches:
- 'main'
- 'master'
- /^release-.*$/
tags: '*'
pull_request:
jobs:
format-check:
name: Format check
runs-on: ubuntu-latest
steps:
- uses: julia-actions/setup-julia@latest
- uses: actions/checkout@v4
- name: Instantiate `format` environment and format
run: |
julia --project=format --color=yes -e 'using Pkg; Pkg.instantiate()'
julia --project=format --color=yes 'format/run.jl'
- name: Format check
run: |
julia -e '
out = read(`git diff --name-only`, String)
if out == ""
exit(0)
else
@error "Some files have not been formatted!"
write(stdout, out)
exit(1)
end'
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ authors = ["Tyler Thomas <[email protected]>"]
version = "0.1.0"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"

[compat]
Aqua = "0.8"
Base64 = "1"
Dates = "1"
HTTP = "1"
JSON3 = "1"
Expand Down
69 changes: 64 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ prompt = "Hello"
response = generate_content(secret_key, model, prompt)
println(response.text)
```
returns
outputs
```julia
"Hello there! How may I assist you today? Feel free to ask me any questions you may have or give me a command. I'm here to help! 😊"
```
Expand All @@ -46,18 +46,60 @@ returns
response = generate_content(secret_key, model, prompt; max_output_tokens=10)
println(response.text)
```
returns
outputs
```julia
"Hello! How can I assist you today?"
```

```julia
using GoogleGenAI

secret_key = ENV["GOOGLE_API_KEY"]
model = "gemini-pro-vision"
prompt = "What is this image?"
image_path = "test/example.jpg"
response = generate_content(secret_key, model, prompt, image_path)
println(response.text)
```
outputs
```julia
"The logo for the Julia programming language."
```

### Multi-turn conversations

```julia
# Define the provider with your API key (placeholder here)
provider = GoogleProvider(api_key=ENV["GOOGLE_API_KEY"])
model_name = "gemini-pro"
conversation = [
Dict(:role => "user", :parts => [Dict(:text => "When was Julia 1.0 released?")])
]

response = generate_content(provider, model_name, conversation)
push!(conversation, Dict(:role => "model", :parts => [Dict(:text => response.text)]))
println("Model: ", response.text)

push!(conversation, Dict(:role => "user", :parts => [Dict(:text => "Who created the language?")]))
response = generate_content(provider, model_name, conversation, max_output_tokens=100)
println("Model: ", response.text)
```
outputs
```julia
"Model: August 8, 2018"

"Model: Jeff Bezanson, Alan Edelman, Viral B. Shah, Stefan Karpinski, and Keno Fischer

Julia Computing, Inc. is the company that provides commercial support for Julia."
```

### Count Tokens
```julia
using GoogleGenAI
n_tokens = count_tokens(ENV["GOOGLE_API_KEY"], "gemini-pro", "Hello")
println(n_tokens)
```
returns
outputs
```julia
1
```
Expand All @@ -69,7 +111,7 @@ using GoogleGenAI
embeddings = create_embeddings(ENV["GOOGLE_API_KEY"], "gemini-pro", "Hello")
println(size(embeddings.values))
```
returns
outputs
```julia
(768,)
```
Expand All @@ -85,9 +127,26 @@ for m in models
end
end
```
returns
outputs
```julia
gemini-pro
gemini-pro-vision
```

### Safety Settings

More information about the safety settings can be found [here](https://ai.google.dev/docs/safety_setting_gemini).

```julia
using GoogleGenAI
secret_key = ENV["GOOGLE_API_KEY"]
safety_settings = [
Dict("category" => "HARM_CATEGORY_HATE_SPEECH", "threshold" => "HARM_BLOCK_THRESHOLD_UNSPECIFIED"),
Dict("category" => "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold" => "BLOCK_ONLY_HIGH"),
Dict("category" => "HARM_CATEGORY_HARASSMENT", "threshold" => "BLOCK_MEDIUM_AND_ABOVE"),
Dict("category" => "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold" => "BLOCK_LOW_AND_ABOVE")
]
model = "gemini-pro"
prompt = "Hello"
response = generate_content(secret_key, model, prompt; safety_settings=safety_settings)
```
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ makedocs(;
canonical="https://tylerjthomas9.github.io/GoogleGenAI.jl",
assets=String[],
),
pages=["Home" => "index.md"],
pages=["Home" => "index.md", "API" => "api.md"],
warnonly=true,
)

Expand Down
12 changes: 12 additions & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
```@meta
CurrentModule = GoogleGenAI
```

# API
```
GoogleProvider
generate_content
count_tokens
embed_content
list_models
```
Loading
Loading