Skip to content

Commit 87beb9c

Browse files
authored
docs: add OpenAI-compatible models section for Grok (xAI) and DeepSeek (#613)
1 parent c40cc2c commit 87beb9c

File tree

1 file changed

+75
-17
lines changed

1 file changed

+75
-17
lines changed

docs/models.md

Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ PydanticAI is Model-agnostic and has built in support for the following model pr
77
* [Groq](#groq)
88
* [Mistral](#mistral)
99

10+
See [OpenAI-compatible models](#openai-compatible-models) for more examples on how to use models such as [OpenRouter](#openrouter), [Grok (xAI)](#grok-xai) and [DeepSeek](#deepseek) that support the OpenAI SDK.
11+
1012
You can also [add support for other models](#implementing-custom-models).
1113

1214
PydanticAI also comes with [`TestModel`](api/models/test.md) and [`FunctionModel`](api/models/function.md) for testing and development.
@@ -68,23 +70,6 @@ agent = Agent(model)
6870
...
6971
```
7072

71-
### `base_url` argument
72-
73-
To use another OpenAI-compatible API, such as [OpenRouter](https://openrouter.ai), you can make use of the [`base_url` argument][pydantic_ai.models.openai.OpenAIModel.__init__]:
74-
75-
```python {title="openai_model_base_url.py"}
76-
from pydantic_ai import Agent
77-
from pydantic_ai.models.openai import OpenAIModel
78-
79-
model = OpenAIModel(
80-
'anthropic/claude-3.5-sonnet',
81-
base_url='https://openrouter.ai/api/v1',
82-
api_key='your-api-key',
83-
)
84-
agent = Agent(model)
85-
...
86-
```
87-
8873
### Custom OpenAI Client
8974

9075
`OpenAIModel` also accepts a custom `AsyncOpenAI` client via the [`openai_client` parameter][pydantic_ai.models.openai.OpenAIModel.__init__],
@@ -448,6 +433,79 @@ agent = Agent(model)
448433
...
449434
```
450435

436+
## OpenAI-compatible Models
437+
438+
Many of the models are compatible with OpenAI API, and thus can be used with [`OpenAIModel`][pydantic_ai.models.openai.OpenAIModel] in PydanticAI.
439+
Before getting started, check the [OpenAI](#openai) section for installation and configuration instructions.
440+
441+
To use another OpenAI-compatible API, you can make use of the [`base_url`][pydantic_ai.models.openai.OpenAIModel.__init__] and [`api_key`][pydantic_ai.models.openai.OpenAIModel.__init__] arguments:
442+
443+
```python {title="openai_model_base_url.py" hl_lines="5-6"}
444+
from pydantic_ai.models.openai import OpenAIModel
445+
446+
model = OpenAIModel(
447+
'model_name',
448+
base_url='https://<openai-compatible-api-endpoint>.com',
449+
api_key='your-api-key',
450+
)
451+
...
452+
```
453+
454+
### OpenRouter
455+
456+
To use [OpenRouter](https://openrouter.ai), first create an API key at [openrouter.ai/keys](https://openrouter.ai/keys).
457+
458+
Once you have the API key, you can pass it to [`OpenAIModel`][pydantic_ai.models.openai.OpenAIModel] as the `api_key` argument:
459+
460+
```python {title="openrouter_model_init.py"}
461+
from pydantic_ai import Agent
462+
from pydantic_ai.models.openai import OpenAIModel
463+
464+
model = OpenAIModel(
465+
'anthropic/claude-3.5-sonnet',
466+
base_url='https://openrouter.ai/api/v1',
467+
api_key='your-openrouter-api-key',
468+
)
469+
agent = Agent(model)
470+
...
471+
```
472+
473+
### Grok (xAI)
474+
475+
Go to [xAI API Console](https://console.x.ai/) and create an API key.
476+
Once you have the API key, follow the [xAI API Documentation](https://docs.x.ai/docs/overview), and set the `base_url` and `api_key` arguments appropriately:
477+
478+
```python {title="grok_model_init.py"}
479+
from pydantic_ai import Agent
480+
from pydantic_ai.models.openai import OpenAIModel
481+
482+
model = OpenAIModel(
483+
'grok-2-1212',
484+
base_url='https://api.x.ai/v1',
485+
api_key='your-xai-api-key',
486+
)
487+
agent = Agent(model)
488+
...
489+
```
490+
491+
### DeepSeek
492+
493+
Go to [DeepSeek API Platform](https://platform.deepseek.com/api_keys) and create an API key.
494+
Once you have the API key, follow the [DeepSeek API Documentation](https://platform.deepseek.com/docs/api/overview), and set the `base_url` and `api_key` arguments appropriately:
495+
496+
```python {title="deepseek_model_init.py"}
497+
from pydantic_ai import Agent
498+
from pydantic_ai.models.openai import OpenAIModel
499+
500+
model = OpenAIModel(
501+
'deepseek-chat',
502+
base_url='https://api.deepseek.com',
503+
api_key='your-deepseek-api-key',
504+
)
505+
agent = Agent(model)
506+
...
507+
```
508+
451509
## Implementing Custom Models
452510

453511
To implement support for models not already supported, you will need to subclass the [`Model`][pydantic_ai.models.Model] abstract base class.

0 commit comments

Comments
 (0)