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
150 lines
4.7 KiB
Plaintext
150 lines
4.7 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "6945138b",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Templating\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "3322e6a7",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"The rendered message to parse: \n",
|
|
"It is sunny and 52 degrees fahrenheit in Seattle.\n",
|
|
"\n",
|
|
"It is cloudy and 75 degrees fahrenheit in San Francisco.\n",
|
|
"\n",
|
|
"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 (\n",
|
|
" AuthMethod,\n",
|
|
" ModelConfig,\n",
|
|
" TemplateEngineConfig,\n",
|
|
" TemplateEngineType,\n",
|
|
" TemplateManagerType,\n",
|
|
")\n",
|
|
"from graphrag_llm.templating import create_template_engine\n",
|
|
"from graphrag_llm.types import LLMCompletionResponse\n",
|
|
"from pydantic import BaseModel, Field\n",
|
|
"\n",
|
|
"load_dotenv()\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",
|
|
"\n",
|
|
"template_engine = create_template_engine()\n",
|
|
"\n",
|
|
"# The above default is the same as the following configuration:\n",
|
|
"template_engine = create_template_engine(\n",
|
|
" TemplateEngineConfig(\n",
|
|
" type=TemplateEngineType.Jinja,\n",
|
|
" template_manager=TemplateManagerType.File,\n",
|
|
" base_dir=\"templates\",\n",
|
|
" template_extension=\".jinja\",\n",
|
|
" encoding=\"utf-8\",\n",
|
|
" )\n",
|
|
")\n",
|
|
"\n",
|
|
"msg = template_engine.render(\n",
|
|
" # Name of the template file without extension\n",
|
|
" template_name=\"weather_listings\",\n",
|
|
" # Values to fill in the template\n",
|
|
" context={\n",
|
|
" \"weather_reports\": [\n",
|
|
" {\"city\": \"Seattle\", \"temperature_f\": 52, \"condition\": \"sunny\"},\n",
|
|
" {\"city\": \"San Francisco\", \"temperature_f\": 75, \"condition\": \"cloudy\"},\n",
|
|
" ]\n",
|
|
" },\n",
|
|
")\n",
|
|
"\n",
|
|
"\n",
|
|
"print(f\"The rendered message to parse: {msg}\")\n",
|
|
"\n",
|
|
"\n",
|
|
"# Structured response parsing using pydantic\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",
|
|
"response: LLMCompletionResponse[WeatherReports] = llm_completion.completion(\n",
|
|
" messages=msg,\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}\")"
|
|
]
|
|
}
|
|
],
|
|
"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
|
|
}
|