From 830f26aa0e2d726fa6d52f30b8b336f1d68c85e9 Mon Sep 17 00:00:00 2001 From: Xin Date: Sat, 4 Jan 2025 01:10:59 +0000 Subject: [PATCH 1/4] docs: add OpenAI-compatible models sections for Grok (xAI) and DeepSeek --- docs/models.md | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/docs/models.md b/docs/models.md index af27118141..4d9aa3bf09 100644 --- a/docs/models.md +++ b/docs/models.md @@ -7,6 +7,8 @@ PydanticAI is Model-agnostic and has built in support for the following model pr * [Groq](#groq) * [Mistral](#mistral) +See [OpenAI-compatible models](#openai-compatible-models) for more examples on how to use models such as [Grok (xAI)](#grok-xai) and [DeepSeek](#deepseek) that support the OpenAI SDK. + You can also [add support for other models](#implementing-custom-models). PydanticAI also comes with [`TestModel`](api/models/test.md) and [`FunctionModel`](api/models/function.md) for testing and development. @@ -448,6 +450,60 @@ agent = Agent(model) ... ``` +## OpenAI-compatible models + +Before getting started, check the [OpenAI](#openai) section for installation and configuration instructions. + +### Grok (xAI) + +Go to [xAI API Console](https://console.x.ai/) and create an API key. +Once you have the API key, you can set it as an environment variable: + +```bash +export XAI_API_KEY='your-api-key' +``` + +Follow the [xAI API Documentation](https://docs.x.ai/docs/overview), and set the `base_url` and `api_key` arguments appropriately. + +```python {title="grok_model_init.py"} +import os +from pydantic_ai import Agent +from pydantic_ai.models.openai import OpenAIModel + +model = OpenAIModel( + 'grok-2-1212', + base_url='https://api.x.ai/v1', + api_key=os.getenv('XAI_API_KEY'), +) +agent = Agent(model) +... +``` + +### DeepSeek + +Go to [DeepSeek API Platform](https://platform.deepseek.com/api_keys) and create an API key. +Once you have the API key, you can set it as an environment variable: + +```bash +export DEEPSEEK_API_KEY='your-api-key' +``` + +Follow the [DeepSeek API Documentation](https://platform.deepseek.com/docs/api/overview), and set the `base_url` and `api_key` arguments appropriately. + +```python {title="deepseek_model_init.py"} +import os +from pydantic_ai import Agent +from pydantic_ai.models.openai import OpenAIModel + +model = OpenAIModel( + 'deepseek-chat', + base_url='https://api.deepseek.com', + api_key=os.getenv('DEEPSEEK_API_KEY'), +) +agent = Agent(model) +... +``` + ## Implementing Custom Models To implement support for models not already supported, you will need to subclass the [`Model`][pydantic_ai.models.Model] abstract base class. From 0c46551db0a0b6475de957b3ec346109cf10fbbc Mon Sep 17 00:00:00 2001 From: Xin Date: Sat, 4 Jan 2025 01:16:24 +0000 Subject: [PATCH 2/4] docs: update OpenAI-compatible models section --- docs/models.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/models.md b/docs/models.md index 4d9aa3bf09..00d06f05ef 100644 --- a/docs/models.md +++ b/docs/models.md @@ -450,8 +450,9 @@ agent = Agent(model) ... ``` -## OpenAI-compatible models +## OpenAI-compatible Models +Many of the models are compatible with OpenAI SDK, and thus can be used with [`OpenAIModel`][pydantic_ai.models.openai.OpenAIModel] in PydanticAI. Before getting started, check the [OpenAI](#openai) section for installation and configuration instructions. ### Grok (xAI) From 240f27363bbdfc1d2235351d59459ddc96d477b5 Mon Sep 17 00:00:00 2001 From: Xin Date: Sat, 4 Jan 2025 01:25:58 +0000 Subject: [PATCH 3/4] fix: code block format --- docs/models.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/models.md b/docs/models.md index 00d06f05ef..4e3e8c9922 100644 --- a/docs/models.md +++ b/docs/models.md @@ -468,6 +468,7 @@ Follow the [xAI API Documentation](https://docs.x.ai/docs/overview), and set the ```python {title="grok_model_init.py"} import os + from pydantic_ai import Agent from pydantic_ai.models.openai import OpenAIModel @@ -493,6 +494,7 @@ Follow the [DeepSeek API Documentation](https://platform.deepseek.com/docs/api/o ```python {title="deepseek_model_init.py"} import os + from pydantic_ai import Agent from pydantic_ai.models.openai import OpenAIModel From c190346eed9edd8ed1b9abcd90f92dc157427983 Mon Sep 17 00:00:00 2001 From: Xin Date: Mon, 6 Jan 2025 21:19:53 +0000 Subject: [PATCH 4/4] docs: move base_url section to the OpenAI-compatible models section --- docs/models.md | 75 +++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/docs/models.md b/docs/models.md index 4e3e8c9922..d55c3959f9 100644 --- a/docs/models.md +++ b/docs/models.md @@ -7,7 +7,7 @@ PydanticAI is Model-agnostic and has built in support for the following model pr * [Groq](#groq) * [Mistral](#mistral) -See [OpenAI-compatible models](#openai-compatible-models) for more examples on how to use models such as [Grok (xAI)](#grok-xai) and [DeepSeek](#deepseek) that support the OpenAI SDK. +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. You can also [add support for other models](#implementing-custom-models). @@ -70,23 +70,6 @@ agent = Agent(model) ... ``` -### `base_url` argument - -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__]: - -```python {title="openai_model_base_url.py"} -from pydantic_ai import Agent -from pydantic_ai.models.openai import OpenAIModel - -model = OpenAIModel( - 'anthropic/claude-3.5-sonnet', - base_url='https://openrouter.ai/api/v1', - api_key='your-api-key', -) -agent = Agent(model) -... -``` - ### Custom OpenAI Client `OpenAIModel` also accepts a custom `AsyncOpenAI` client via the [`openai_client` parameter][pydantic_ai.models.openai.OpenAIModel.__init__], @@ -452,30 +435,54 @@ agent = Agent(model) ## OpenAI-compatible Models -Many of the models are compatible with OpenAI SDK, and thus can be used with [`OpenAIModel`][pydantic_ai.models.openai.OpenAIModel] in PydanticAI. +Many of the models are compatible with OpenAI API, and thus can be used with [`OpenAIModel`][pydantic_ai.models.openai.OpenAIModel] in PydanticAI. Before getting started, check the [OpenAI](#openai) section for installation and configuration instructions. -### Grok (xAI) +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: -Go to [xAI API Console](https://console.x.ai/) and create an API key. -Once you have the API key, you can set it as an environment variable: +```python {title="openai_model_base_url.py" hl_lines="5-6"} +from pydantic_ai.models.openai import OpenAIModel -```bash -export XAI_API_KEY='your-api-key' +model = OpenAIModel( + 'model_name', + base_url='https://.com', + api_key='your-api-key', +) +... ``` -Follow the [xAI API Documentation](https://docs.x.ai/docs/overview), and set the `base_url` and `api_key` arguments appropriately. +### OpenRouter -```python {title="grok_model_init.py"} -import os +To use [OpenRouter](https://openrouter.ai), first create an API key at [openrouter.ai/keys](https://openrouter.ai/keys). +Once you have the API key, you can pass it to [`OpenAIModel`][pydantic_ai.models.openai.OpenAIModel] as the `api_key` argument: + +```python {title="openrouter_model_init.py"} +from pydantic_ai import Agent +from pydantic_ai.models.openai import OpenAIModel + +model = OpenAIModel( + 'anthropic/claude-3.5-sonnet', + base_url='https://openrouter.ai/api/v1', + api_key='your-openrouter-api-key', +) +agent = Agent(model) +... +``` + +### Grok (xAI) + +Go to [xAI API Console](https://console.x.ai/) and create an API key. +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: + +```python {title="grok_model_init.py"} from pydantic_ai import Agent from pydantic_ai.models.openai import OpenAIModel model = OpenAIModel( 'grok-2-1212', base_url='https://api.x.ai/v1', - api_key=os.getenv('XAI_API_KEY'), + api_key='your-xai-api-key', ) agent = Agent(model) ... @@ -484,24 +491,16 @@ agent = Agent(model) ### DeepSeek Go to [DeepSeek API Platform](https://platform.deepseek.com/api_keys) and create an API key. -Once you have the API key, you can set it as an environment variable: - -```bash -export DEEPSEEK_API_KEY='your-api-key' -``` - -Follow the [DeepSeek API Documentation](https://platform.deepseek.com/docs/api/overview), and set the `base_url` and `api_key` arguments appropriately. +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: ```python {title="deepseek_model_init.py"} -import os - from pydantic_ai import Agent from pydantic_ai.models.openai import OpenAIModel model = OpenAIModel( 'deepseek-chat', base_url='https://api.deepseek.com', - api_key=os.getenv('DEEPSEEK_API_KEY'), + api_key='your-deepseek-api-key', ) agent = Agent(model) ...