You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/models.md
+75-17Lines changed: 75 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,8 @@ PydanticAI is Model-agnostic and has built in support for the following model pr
7
7
*[Groq](#groq)
8
8
*[Mistral](#mistral)
9
9
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
+
10
12
You can also [add support for other models](#implementing-custom-models).
11
13
12
14
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)
68
70
...
69
71
```
70
72
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
-
88
73
### Custom OpenAI Client
89
74
90
75
`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)
448
433
...
449
434
```
450
435
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:
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
+
451
509
## Implementing Custom Models
452
510
453
511
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