Files
wehub-resource-sync 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
chore: import upstream snapshot with attribution
2026-07-13 12:37:31 +08:00

145 lines
4.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "5b094500",
"metadata": {},
"source": [
"# Structured Response\n",
"\n",
"`LLMCompletion.completion` accepts a `response_format` parameter that is a pydantic model for parsing and returning structured responses.\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "a79c242b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"City: Seattle\n",
" Temperature: 11.1 °C\n",
" Condition: Sunny\n",
"City: San Francisco\n",
" Temperature: 23.9 °C\n",
" Condition: Cloudy\n"
]
}
],
"source": [
"# Copyright (c) 2024 Microsoft Corporation.\n",
"# Licensed under the MIT License\n",
"\n",
"import os\n",
"\n",
"from dotenv import load_dotenv\n",
"from graphrag_llm.completion import LLMCompletion, create_completion\n",
"from graphrag_llm.config import AuthMethod, ModelConfig\n",
"from graphrag_llm.types import LLMCompletionResponse\n",
"from pydantic import BaseModel, Field\n",
"\n",
"load_dotenv()\n",
"\n",
"\n",
"class LocalWeather(BaseModel):\n",
" \"\"\"City weather information model.\"\"\"\n",
"\n",
" city: str = Field(description=\"The name of the city\")\n",
" temperature: float = Field(description=\"The temperature in Celsius\")\n",
" condition: str = Field(description=\"The weather condition description\")\n",
"\n",
"\n",
"class WeatherReports(BaseModel):\n",
" \"\"\"Weather information model.\"\"\"\n",
"\n",
" reports: list[LocalWeather] = Field(\n",
" description=\"The weather reports for multiple cities\"\n",
" )\n",
"\n",
"\n",
"api_key = os.getenv(\"GRAPHRAG_API_KEY\")\n",
"model_config = ModelConfig(\n",
" model_provider=\"azure\",\n",
" model=os.getenv(\"GRAPHRAG_MODEL\", \"gpt-4o\"),\n",
" azure_deployment_name=os.getenv(\"GRAPHRAG_MODEL\", \"gpt-4o\"),\n",
" api_base=os.getenv(\"GRAPHRAG_API_BASE\"),\n",
" api_version=os.getenv(\"GRAPHRAG_API_VERSION\", \"2025-04-01-preview\"),\n",
" api_key=api_key,\n",
" auth_method=AuthMethod.AzureManagedIdentity if not api_key else AuthMethod.ApiKey,\n",
")\n",
"llm_completion: LLMCompletion = create_completion(model_config)\n",
"\n",
"response: LLMCompletionResponse[WeatherReports] = llm_completion.completion(\n",
" messages=\"It is sunny and 52 degrees fahrenheit in Seattle. It is cloudy and 75 degrees fahrenheit in San Francisco.\",\n",
" response_format=WeatherReports,\n",
") # type: ignore\n",
"\n",
"local_weather_reports: WeatherReports = response.formatted_response # type: ignore\n",
"for report in local_weather_reports.reports:\n",
" print(f\"City: {report.city}\")\n",
" print(f\" Temperature: {report.temperature} °C\")\n",
" print(f\" Condition: {report.condition}\")"
]
},
{
"cell_type": "markdown",
"id": "6360f512",
"metadata": {},
"source": [
"## Streaming\n",
"\n",
"Streaming is not supported when using `response_format`.\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "e08b9ba6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Error during streaming completion: response_format is not supported for streaming completions.\n"
]
}
],
"source": [
"try:\n",
" response = llm_completion.completion(\n",
" messages=\"It is sunny and 52 degrees fahrenheit in Seattle. It is cloudy and 75 degrees fahrenheit in San Francisco.\",\n",
" response_format=WeatherReports,\n",
" stream=True,\n",
" )\n",
"except Exception as e: # noqa: BLE001\n",
" print(f\"Error during streaming completion: {e}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}