|
1 | 1 | # `pydantic_ai.models.vertexai`
|
2 | 2 |
|
| 3 | +Custom interface to the `*-aiplatform.googleapis.com` API for Gemini models. |
| 4 | + |
| 5 | +This model uses [`GeminiAgentModel`][pydantic_ai.models.gemini.GeminiAgentModel] with just the URL and auth method |
| 6 | +changed from [`GeminiModel`][pydantic_ai.models.gemini.GeminiModel], it relies on the VertexAI |
| 7 | +[`generateContent`](https://cloud.google.com/vertex-ai/docs/reference/rest/v1/projects.locations.endpoints/generateContent) |
| 8 | +and |
| 9 | +[`streamGenerateContent`](https://cloud.google.com/vertex-ai/docs/reference/rest/v1/projects.locations.endpoints/streamGenerateContent) |
| 10 | +function endpoints |
| 11 | +having the same schemas as the equivalent [Gemini endpoints][pydantic_ai.models.gemini.GeminiModel]. |
| 12 | + |
| 13 | +There are four advantages of using this API over the `generativelanguage.googleapis.com` API which |
| 14 | +[`GeminiModel`][pydantic_ai.models.gemini.GeminiModel] uses, and one big disadvantage. |
| 15 | + |
| 16 | +## Setup |
| 17 | + |
| 18 | +For details on how to set up authentication with this model as well as a comparison with the `generativelanguage.googleapis.com` API used by [`GeminiModel`][pydantic_ai.models.gemini.GeminiModel], |
| 19 | +see [model configuration for Gemini via VertexAI](../../install.md#gemini-via-vertexai). |
| 20 | + |
| 21 | +## Example Usage |
| 22 | + |
| 23 | +With the default google project already configured in your environment using "application default credentials": |
| 24 | + |
| 25 | +```py title="vertex_example_env.py" |
| 26 | +from pydantic_ai import Agent |
| 27 | +from pydantic_ai.models.vertexai import VertexAIModel |
| 28 | + |
| 29 | +model = VertexAIModel('gemini-1.5-flash') |
| 30 | +agent = Agent(model) |
| 31 | +result = agent.run_sync('Tell me a joke.') |
| 32 | +print(result.data) |
| 33 | +#> Did you hear about the toothpaste scandal? They called it Colgate. |
| 34 | +``` |
| 35 | + |
| 36 | +Or using a service account JSON file: |
| 37 | + |
| 38 | +```py title="vertex_example_service_account.py" |
| 39 | +from pydantic_ai import Agent |
| 40 | +from pydantic_ai.models.vertexai import VertexAIModel |
| 41 | + |
| 42 | +model = VertexAIModel( |
| 43 | + 'gemini-1.5-flash', |
| 44 | + service_account_file='path/to/service-account.json', |
| 45 | +) |
| 46 | +agent = Agent(model) |
| 47 | +result = agent.run_sync('Tell me a joke.') |
| 48 | +print(result.data) |
| 49 | +#> Did you hear about the toothpaste scandal? They called it Colgate. |
| 50 | +``` |
| 51 | + |
3 | 52 | ::: pydantic_ai.models.vertexai
|
0 commit comments