Cohere
Cohere
이 문서에서는 pydantic-ai에서 CohereModel을 설치하고 설정하는 방법을 알려드려요. Cohere의 API를 사용하려면 먼저 API 키를 생성하고, 환경 변수로 설정한 뒤 모델을 이름으로 사용하거나 직접 초기화하면 돼요. 커스텀 Provider나 http_client를 넘겨서 구성을 더 세밀하게 조정할 수도 있어요.
출처: 문서
본문
Install
CohereModel을 사용하려면 pydantic-ai를 설치하거나, cohere 옵션 그룹과 함께 pydantic-ai-slim을 설치해야 해요:
Terminal
pip install "pydantic-ai-slim[cohere]"
Terminal
uv add "pydantic-ai-slim[cohere]"
Configuration
Cohere를 API를 통해 사용하려면 dashboard.cohere.com/api-keys로 이동해서 API 키를 생성할 수 있는 곳을 찾아보세요.
CohereModelName에는 가장 인기 있는 Cohere 모델 목록이 들어 있어요.
Environment variable
API 키를 확보했다면 환경 변수로 설정할 수 있어요:
Terminal
export CO_API_KEY='your-api-key'
그 다음 이름으로 CohereModel을 사용할 수 있어요:
from pydantic_ai import Agent
agent = Agent('cohere:command-r7b-12-2024')
...
또는 모델 이름만으로 모델을 직접 초기화할 수도 있어요:
from pydantic_ai import Agent
from pydantic_ai.models.cohere import CohereModel
model = CohereModel('command-r7b-12-2024')
agent = Agent(model)
...
provider argument
provider 인자를 통해 커스텀 Provider를 제공할 수 있어요:
from pydantic_ai import Agent
from pydantic_ai.models.cohere import CohereModel
from pydantic_ai.providers.cohere import CohereProvider
model = CohereModel('command-r7b-12-2024', 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-r7b-12-2024',
provider=CohereProvider(api_key='your-api-key', http_client=custom_http_client),
)
agent = Agent(model)
...
SDK retries
OpenAI, Anthropic, Groq 클라이언트와 달리 Cohere 클라이언트는 max_retries 설정을 노출하지 않아요. 내장된 클라이언트가 여기에 전달하는 어떤 전송 계층 위에서도 서버 오류와 rate limit을 두 번 재시도하며, 이 동작은 끌 수 없어요. 전송 계층을 설계할 때 이 점을 염두에 두세요 — Provider SDK 재시도를 참고하세요.
Model settings
CohereModelSettings를 사용해 모델 동작을 맞춤 설정할 수 있어요:
from pydantic_ai import Agent
from pydantic_ai.models.cohere import CohereModel, CohereModelSettings
model = CohereModel('command-r7b-12-2024')
settings = CohereModelSettings(
temperature=0.2,
top_k=40,
)
agent = Agent(model, model_settings=settings)
...