-
-
Notifications
You must be signed in to change notification settings - Fork 697
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into revise_few_shot_docs
- Loading branch information
Showing
41 changed files
with
2,974 additions
and
2,453 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
docs/blog/posts/announcing-gemini-tool-calling-support.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
--- | ||
draft: False | ||
date: 2024-09-03 | ||
authors: | ||
- ivanleomk | ||
--- | ||
|
||
# Structured Outputs for Gemini now supported | ||
|
||
We're excited to announce that `instructor` now supports structured outputs using tool calling for both the Gemini SDK and the VertexAI SDK. | ||
|
||
A special shoutout to [Sonal](https://x.com/sonalsaldanha) for his contributions to the Gemini Tool Calling support. | ||
|
||
Let's walk through a simple example of how to use these new features | ||
|
||
## Installation | ||
|
||
To get started, install the latest version of `instructor`. Depending on whether you're using Gemini or VertexAI, you should install the following: | ||
|
||
=== "Gemini" | ||
|
||
```bash | ||
pip install "instructor[google-generativeai]" | ||
``` | ||
|
||
=== "VertexAI" | ||
|
||
```bash | ||
pip install "instructor[vertexai]" | ||
``` | ||
|
||
This ensures that you have the necessary dependencies to use the Gemini or VertexAI SDKs with instructor. | ||
|
||
We recommend using the Gemini SDK over the VertexAI SDK for two main reasons. | ||
|
||
1. Compared to the VertexAI SDK, the Gemini SDK comes with a free daily quota of 1.5 billion tokens to use for developers. | ||
2. The Gemini SDK is significantly easier to setup, all you need is a `GOOGLE_API_KEY` that you can generate in your GCP console. THe VertexAI SDK on the other hand requires a credentials.json file or an OAuth integration to use. | ||
|
||
## Getting Started | ||
|
||
With our provider agnostic API, you can use the same interface to interact with both SDKs, the only thing that changes here is how we initialise the client itself. | ||
|
||
Before running the following code, you'll need to make sure that you have your Gemini API Key set in your shell under the alias `GOOGLE_API_KEY`. | ||
|
||
```python | ||
import instructor | ||
import google.generativeai as genai | ||
from pydantic import BaseModel | ||
|
||
|
||
class User(BaseModel): | ||
name: str | ||
age: int | ||
|
||
|
||
client = instructor.from_gemini( | ||
client=genai.GenerativeModel( | ||
model_name="models/gemini-1.5-flash-latest", # (1)! | ||
) | ||
) | ||
|
||
resp = client.chat.completions.create( | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": "Extract Jason is 25 years old.", | ||
} | ||
], | ||
response_model=User, | ||
) | ||
|
||
print(resp) | ||
#> name='Jason' age=25 | ||
``` | ||
|
||
1. Current Gemini models that support tool calling are `gemini-1.5-flash-latest` and `gemini-1.5-pro-latest`. | ||
|
||
We can achieve a similar thing with the VertexAI SDK. For this to work, you'll need to authenticate to VertexAI. | ||
|
||
There are some instructions [here](https://cloud.google.com/vertex-ai/docs/authentication) but the easiest way I found was to simply download the GCloud cli and run `gcloud auth application-default login`. | ||
|
||
```python | ||
import instructor | ||
import vertexai # type: ignore | ||
from vertexai.generative_models import GenerativeModel # type: ignore | ||
from pydantic import BaseModel | ||
|
||
vertexai.init() | ||
|
||
|
||
class User(BaseModel): | ||
name: str | ||
age: int | ||
|
||
|
||
client = instructor.from_vertexai( | ||
client=GenerativeModel("gemini-1.5-pro-preview-0409"), # (1)! | ||
) | ||
|
||
|
||
resp = client.chat.completions.create( | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": "Extract Jason is 25 years old.", | ||
} | ||
], | ||
response_model=User, | ||
) | ||
|
||
print(resp) | ||
#> name='Jason' age=25 | ||
``` | ||
|
||
1. Current Gemini models that support tool calling are `gemini-1.5-flash-latest` and `gemini-1.5-pro-latest`. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.