Cohere
安装
要使用 CohereModel
,您需要安装 pydantic-ai
,或者安装带有 cohere
可选组的 pydantic-ai-slim
。
pip install "pydantic-ai-slim[cohere]"
uv add "pydantic-ai-slim[cohere]"
配置
要通过 API 使用 Cohere,请访问 dashboard.cohere.com/api-keys 并按照指示生成一个 API 密钥。
CohereModelName
包含最受欢迎的 Cohere 模型列表。
环境变量
获得 API 密钥后,可以将其设置为环境变量
export CO_API_KEY='your-api-key'
然后您就可以通过名称来使用 CohereModel
。
from pydantic_ai import Agent
agent = Agent('cohere:command')
...
或者直接用模型名称初始化模型
from pydantic_ai import Agent
from pydantic_ai.models.cohere import CohereModel
model = CohereModel('command')
agent = Agent(model)
...
provider
参数
您可以通过 provider
参数提供一个自定义的 Provider
。
from pydantic_ai import Agent
from pydantic_ai.models.cohere import CohereModel
from pydantic_ai.providers.cohere import CohereProvider
model = CohereModel('command', provider=CohereProvider(api_key='your-api-key'))
agent = Agent(model)
...
您还可以使用自定义的 http_client
来定制 CohereProvider
。
from httpx import AsyncClient
from pydantic_ai import Agent
from pydantic_ai.models.cohere import CohereModel
from pydantic_ai.providers.cohere import CohereProvider
custom_http_client = AsyncClient(timeout=30)
model = CohereModel(
'command',
provider=CohereProvider(api_key='your-api-key', http_client=custom_http_client),
)
agent = Agent(model)
...