6b7e6b44f1
Python Build and Type Check / python-ci (ubuntu-latest, 3.11) (push) Has been cancelled
Python Build and Type Check / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Build and Type Check / python-ci (windows-latest, 3.11) (push) Has been cancelled
Python Build and Type Check / python-ci (windows-latest, 3.13) (push) Has been cancelled
Python Integration Tests / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Integration Tests / python-ci (windows-latest, 3.13) (push) Has been cancelled
Python Notebook Tests / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Notebook Tests / python-ci (windows-latest, 3.13) (push) Has been cancelled
Python Smoke Tests / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Smoke Tests / python-ci (windows-latest, 3.13) (push) Has been cancelled
Python Unit Tests / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Unit Tests / python-ci (windows-latest, 3.13) (push) Has been cancelled
gh-pages / build (push) Has been cancelled
Python Publish (pypi) / Upload release to PyPI (push) Has been cancelled
Spellcheck / spellcheck (push) Has been cancelled
30 lines
758 B
Python
30 lines
758 B
Python
# Copyright (c) 2024 Microsoft Corporation.
|
|
# Licensed under the MIT License
|
|
|
|
"""Structure response as pydantic base model."""
|
|
|
|
import json
|
|
from typing import Any, TypeVar
|
|
|
|
from pydantic import BaseModel
|
|
|
|
T = TypeVar("T", bound=BaseModel, covariant=True)
|
|
|
|
|
|
def structure_completion_response(response: str, model: type[T]) -> T:
|
|
"""Structure completion response as pydantic base model.
|
|
|
|
Args
|
|
----
|
|
response: str
|
|
The completion response as a JSON string.
|
|
model: type[T]
|
|
The pydantic base model type to structure the response into.
|
|
|
|
Returns
|
|
-------
|
|
The structured response as a pydantic base model.
|
|
"""
|
|
parsed_dict: dict[str, Any] = json.loads(response)
|
|
return model(**parsed_dict)
|