Skip to content
Closed
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
50 changes: 50 additions & 0 deletions packages/genkit-openai/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,56 @@ vectors = await ai.embed(embedder='openrouter/openai/text-embedding-3-small', co
- `OpenAIConfig` is the config for every model. A server may ignore fields it
doesn't support, or reject them.

### Model Garden on Google Cloud

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Should explain OpenAiCompatible's main use case first and explain this after

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Restored OpenAICompatible's class docstring so OpenRouter + local keyless server come first and Model Garden follows after, and reframed this section's opening as a built-in backend on top of OpenAICompatible. Note: in the full README, ## Any OpenAI-compatible server (from base PR #187, lines 60–106) already precedes ### Model Garden on Google Cloud — it was just hidden in #188's stacked diff.


Instead of wiring `base_url=` and a token function by hand, pass
`backend=GoogleCloud()` to serve Model Garden's OpenAI-compatible models
(Llama, Mistral, and others) billed to your Google Cloud project with
application default credentials.

```bash
uv add 'genkit-openai[google-cloud]'
```

```python
from genkit import Genkit
from genkit_openai import GoogleCloud, OpenAICompatible

# 1. Model Garden through your gcloud login; the endpoint and token are handled
garden = OpenAICompatible(backend=GoogleCloud(project='bistro-prod', location='us-central1'))
ai = Genkit(plugins=[garden])

# 2. The ref helper gives the ref for any Model Garden model on the OpenAI-compatible endpoint
res = await ai.generate(model=garden.model('meta/llama-3.3-70b-instruct-maas'), prompt='Suggest a dish.')
print(res.text)
# => Grilled salmon with lemon and dill
```

First-time setup:

1. Log in and pick a project with billing: `gcloud auth login`, then
`gcloud config set project YOUR_PROJECT_ID`.
2. Turn on the API: `gcloud services enable aiplatform.googleapis.com`.
3. Enable the model on its Model Garden card in the Google Cloud console, and
accept its terms. You do this once per model, per project.
4. Give your code access to your login: `gcloud auth application-default login`.

- `project` comes from `GoogleCloud(project=...)`, then `GCLOUD_PROJECT`,
`GOOGLE_CLOUD_PROJECT`, then your credentials. `location` comes from
`GoogleCloud(location=...)`, then `GOOGLE_CLOUD_LOCATION`, then
`us-central1`; most open models are regional.
- `name` defaults to `modelgarden` (refs are `modelgarden/<publisher>/<model>`).
Pass another `name=` to use a different namespace, for example to run two
regions or projects side by side.
- Model Garden has no model list endpoint, so the Dev UI lists nothing for
this plugin. Any model id resolves when you call it.
- A missing or stale login fails the call with `FAILED_PRECONDITION` and the
`gcloud` command that fixes it. The next call retries after you log in.
- A key in `context={'secrets': {'api_key': ...}}` fails with
`FAILED_PRECONDITION`: every call bills the project.
- For Claude on Google Cloud, use `Anthropic(backend=GoogleCloud())` from
`genkit-anthropic[google-cloud]`.

## Mistral

`Mistral()` registers Mistral's models and embedders under `mistral/`, as the
Expand Down
4 changes: 4 additions & 0 deletions packages/genkit-openai/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ readme = "README.md"
requires-python = ">=3.10"
version = "0.14.0"

[project.optional-dependencies]
# OpenAICompatible(backend=GoogleCloud()): google-auth for application default credentials.
google-cloud = ["google-auth[requests]>=2.14.1"]

[project.urls]
"Bug Tracker" = "https://github.com/genkit-ai/genkit-python/issues"
Changelog = "https://github.com/genkit-ai/genkit-python/releases"
Expand Down
6 changes: 5 additions & 1 deletion packages/genkit-openai/src/genkit_openai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
models and embedders under ``mistral/``. ``OpenAICompatible(name=..., base_url=...)``
registers any other OpenAI-compatible server (OpenRouter, Together, Groq,
vLLM) under the name you give it, with no OpenAI catalog.
``OpenAICompatible(backend=GoogleCloud())`` serves Model Garden's
OpenAI-compatible models (Llama, Mistral and others) under ``modelgarden/``
by default, or any ``name=`` you pick (``uv add 'genkit-openai[google-cloud]'``).

Example:
```python
Expand Down Expand Up @@ -63,7 +66,8 @@ async def main() -> None:
"""

from ._compatible import OpenAICompatible
from ._google_cloud import GoogleCloud
from ._openai_plugin import Mistral, OpenAI
from ._typing import OpenAIConfig

__all__ = ['Mistral', 'OpenAI', 'OpenAICompatible', 'OpenAIConfig']
__all__ = ['GoogleCloud', 'Mistral', 'OpenAI', 'OpenAICompatible', 'OpenAIConfig']
Loading
Loading