-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add Zhipu AI support with models, provider, and documentation #3086
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
* [Cohere](cohere.md) | ||
* [Bedrock](bedrock.md) | ||
* [Hugging Face](huggingface.md) | ||
* [Zhipu AI](zhipu.md) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this doc to a section in models/openai.md
, and move the link to the "OpenAI-compatible Providers" list below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move a simplified version of this doc to a section in models/openai.md
, along the same lines of the existing sections for other OpenAI-compatible providers. There's no need to document all the supported models (as the lists will get out of date) or things like streaming (as it works the same as with any other model, so we already have extensive docs).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Example file not necessary, this can just be an example in the docs
# Some OpenAI-compatible providers (currently only Zhipu/Z.ai, see issue #2723) omit the `object` field even | ||
# though the OpenAI schema includes it. We only patch it for that provider to avoid changing validation | ||
# error counts in tests that purposefully feed invalid OpenAI responses (which expect 4 errors, including | ||
# a missing `object`). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know exactly what this is referring to, but I'd rather unconditionally set response.object = 'chat.completion'
here if response.object is None
, and update the tests to fake invalidity with a different field. So please simplify the check and the commend, similar to the Ollama workaround we have below
# error counts in tests that purposefully feed invalid OpenAI responses (which expect 4 errors, including | ||
# a missing `object`). | ||
if self._provider.name == 'zhipu' and not getattr(response, 'object', None): # pragma: no branch | ||
try: # defensive, in case attribute is read-only in future SDK versions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessary, it's a Pydantic BaseModel
so fields should always be writable. If that stops being the case, we can fix it when we bump the SDK.
# Ref: https://docs.bigmodel.cn/cn/guide/develop/openai/introduction | ||
is_vision_model = model_name.startswith(('glm-4.5v', 'glm-4v')) or ( | ||
'v' in model_name and ('glm-4.5v' in model_name or 'glm-4v' in model_name) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is very redundant: 'v' in model_name
and model_name.startswith('glm-4.5v')
are both subsets of 'glm-4.5v' in model_name
. Should we just check for the v
?
|
||
# Zhipu AI models support JSON schema and object output | ||
# All GLM-4 series models support function calling | ||
supports_tools = model_name.startswith(('glm-4', 'codegeex-4')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable name is misleading then right?
|
||
# Zhipu AI doesn't support temperature=0 (must be in range (0, 1)) | ||
# This is a known difference from OpenAI | ||
openai_unsupported_model_settings = () |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is already the default so no need to include it like this right?
return OpenAIModelProfile( | ||
supports_json_schema_output=supports_tools, | ||
supports_json_object_output=supports_tools, | ||
supports_image_output=is_vision_model, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do the vision models actually support image output, or just input?
This pull request adds first-class support for Zhipu AI (智谱AI) models to Pydantic AI, including documentation, provider implementation, model profiles, and tests. Users can now use Zhipu's OpenAI-compatible API seamlessly in the framework, with support for advanced features like vision and function calling. The documentation and example scripts guide users through setup and usage.
Key changes in this pull request:
Zhipu AI Provider and Model Support:
ZhipuProvider
class for Zhipu AI, enabling authentication via API key or environment variable, custom base URL, and HTTP client configuration. This allows users to access Zhipu's OpenAI-compatible API directly from Pydantic AI.zhipu:glm-4.6
,zhipu:glm-4.5
, vision and code models) to the list of known models and updated model inference logic to support Zhipu as a provider. [1] [2]Documentation and Examples:
examples/zhipu_example.py
). [1] [2] [3]Compatibility and Validation:
object
field in API responses, ensuring schema validation succeeds.Testing:
Fixes #2723