|
| 1 | +from __future__ import annotations as _annotations |
| 2 | + |
| 3 | +import os |
| 4 | +from typing import Literal, overload |
| 5 | + |
| 6 | +from httpx import AsyncClient as AsyncHTTPClient |
| 7 | +from openai import AsyncOpenAI |
| 8 | + |
| 9 | +from pydantic_ai.exceptions import UserError |
| 10 | +from pydantic_ai.models import cached_async_http_client |
| 11 | +from pydantic_ai.profiles import ModelProfile |
| 12 | +from pydantic_ai.profiles.moonshotai import moonshotai_model_profile |
| 13 | +from pydantic_ai.profiles.openai import ( |
| 14 | + OpenAIJsonSchemaTransformer, |
| 15 | + OpenAIModelProfile, |
| 16 | +) |
| 17 | +from pydantic_ai.providers import Provider |
| 18 | + |
| 19 | +MoonshotAIModelName = Literal[ |
| 20 | + 'moonshot-v1-8k', |
| 21 | + 'moonshot-v1-32k', |
| 22 | + 'moonshot-v1-128k', |
| 23 | + 'moonshot-v1-8k-vision-preview', |
| 24 | + 'moonshot-v1-32k-vision-preview', |
| 25 | + 'moonshot-v1-128k-vision-preview', |
| 26 | + 'kimi-latest', |
| 27 | + 'kimi-thinking-preview', |
| 28 | + 'kimi-k2-0711-preview', |
| 29 | +] |
| 30 | + |
| 31 | + |
| 32 | +class MoonshotAIProvider(Provider[AsyncOpenAI]): |
| 33 | + """Provider for MoonshotAI platform (Kimi models).""" |
| 34 | + |
| 35 | + @property |
| 36 | + def name(self) -> str: |
| 37 | + return 'moonshotai' |
| 38 | + |
| 39 | + @property |
| 40 | + def base_url(self) -> str: |
| 41 | + # OpenAI-compatible endpoint, see MoonshotAI docs |
| 42 | + return 'https://api.moonshot.ai/v1' |
| 43 | + |
| 44 | + @property |
| 45 | + def client(self) -> AsyncOpenAI: |
| 46 | + return self._client |
| 47 | + |
| 48 | + def model_profile(self, model_name: str) -> ModelProfile | None: |
| 49 | + profile = moonshotai_model_profile(model_name) |
| 50 | + |
| 51 | + # As the MoonshotAI API is OpenAI-compatible, let's assume we also need OpenAIJsonSchemaTransformer, |
| 52 | + # unless json_schema_transformer is set explicitly. |
| 53 | + # Also, MoonshotAI does not support strict tool definitions |
| 54 | + # https://platform.moonshot.ai/docs/guide/migrating-from-openai-to-kimi#about-tool_choice |
| 55 | + # "Please note that the current version of Kimi API does not support the tool_choice=required parameter." |
| 56 | + return OpenAIModelProfile( |
| 57 | + json_schema_transformer=OpenAIJsonSchemaTransformer, |
| 58 | + openai_supports_tool_choice_required=False, |
| 59 | + supports_json_object_output=True, |
| 60 | + ).update(profile) |
| 61 | + |
| 62 | + # --------------------------------------------------------------------- |
| 63 | + # Construction helpers |
| 64 | + # --------------------------------------------------------------------- |
| 65 | + @overload |
| 66 | + def __init__(self) -> None: ... |
| 67 | + |
| 68 | + @overload |
| 69 | + def __init__(self, *, api_key: str) -> None: ... |
| 70 | + |
| 71 | + @overload |
| 72 | + def __init__(self, *, api_key: str, http_client: AsyncHTTPClient) -> None: ... |
| 73 | + |
| 74 | + @overload |
| 75 | + def __init__(self, *, openai_client: AsyncOpenAI | None = None) -> None: ... |
| 76 | + |
| 77 | + def __init__( |
| 78 | + self, |
| 79 | + *, |
| 80 | + api_key: str | None = None, |
| 81 | + openai_client: AsyncOpenAI | None = None, |
| 82 | + http_client: AsyncHTTPClient | None = None, |
| 83 | + ) -> None: |
| 84 | + api_key = api_key or os.getenv('MOONSHOTAI_API_KEY') |
| 85 | + if not api_key and openai_client is None: |
| 86 | + raise UserError( |
| 87 | + 'Set the `MOONSHOTAI_API_KEY` environment variable or pass it via ' |
| 88 | + '`MoonshotAIProvider(api_key=...)` to use the MoonshotAI provider.' |
| 89 | + ) |
| 90 | + |
| 91 | + if openai_client is not None: |
| 92 | + self._client = openai_client |
| 93 | + elif http_client is not None: |
| 94 | + self._client = AsyncOpenAI(base_url=self.base_url, api_key=api_key, http_client=http_client) |
| 95 | + else: |
| 96 | + http_client = cached_async_http_client(provider='moonshotai') |
| 97 | + self._client = AsyncOpenAI(base_url=self.base_url, api_key=api_key, http_client=http_client) |
0 commit comments