-
-
Save ajparsons/45de8fc2b5ebeca22626b67d72865eaf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from __future__ import annotations | |
| from dataclasses import dataclass | |
| import jsonref | |
| from huggingface_hub import AsyncInferenceClient | |
| from pydantic_ai.models.huggingface import HuggingFaceModel | |
| from pydantic_ai.profiles import ModelProfile | |
| from pydantic_ai.profiles._json_schema import JsonSchemaTransformer | |
| from pydantic_ai.providers.huggingface import HuggingFaceProvider | |
| from .config import settings | |
| @dataclass(init=False) | |
| class ExpandedSchemaTransformer(JsonSchemaTransformer): | |
| def transform(self, schema): | |
| return schema | |
| def walk(self): | |
| """ | |
| Expand out all $defs in complex schemas. | |
| """ | |
| schema = super().walk() | |
| items = jsonref.replace_refs(schema, proxies=False, jsonschema=True) | |
| # delete "$defs" if it exists | |
| if "$defs" in items: # type: ignore | |
| del items["$defs"] # type: ignore | |
| return items | |
| hf_endpoint_model = HuggingFaceModel( | |
| model_name="olmo-2-1124-7b-instruct", # nothing uses this this, but for the record | |
| profile=ModelProfile( | |
| json_schema_transformer=ExpandedSchemaTransformer, | |
| ), | |
| provider=HuggingFaceProvider( | |
| hf_client=AsyncInferenceClient( | |
| api_key=settings.HF_TOKEN, model=settings.HF_INFERENCE_ENDPOINT | |
| ) | |
| ), | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment