1+ # Note: Do not add tools= to agents using history providers — causes duplicate item errors
2+ # with the Responses API. See https://github.com/microsoft/agent-framework/issues/3295
13import asyncio
24import logging
35import os
4- import random
56import sqlite3
67import uuid
78from collections .abc import Sequence
8- from typing import Annotated , Any
9+ from typing import Any
910
10- from agent_framework import Agent , BaseHistoryProvider , Message , tool
11+ from agent_framework import Agent , HistoryProvider , Message
1112from agent_framework .openai import OpenAIChatClient
1213from azure .identity .aio import DefaultAzureCredential , get_bearer_token_provider
1314from dotenv import load_dotenv
14- from pydantic import Field
1515from rich import print
1616from rich .logging import RichHandler
1717
3232 client = OpenAIChatClient (
3333 base_url = f"{ os .environ ['AZURE_OPENAI_ENDPOINT' ]} /openai/v1/" ,
3434 api_key = token_provider ,
35- model_id = os .environ ["AZURE_OPENAI_CHAT_DEPLOYMENT" ],
35+ model = os .environ ["AZURE_OPENAI_CHAT_DEPLOYMENT" ],
3636 )
3737elif API_HOST == "github" :
3838 client = OpenAIChatClient (
3939 base_url = "https://models.github.ai/inference" ,
4040 api_key = os .environ ["GITHUB_TOKEN" ],
41- model_id = os .getenv ("GITHUB_MODEL" , "openai/gpt-4.1-mini" ),
41+ model = os .getenv ("GITHUB_MODEL" , "openai/gpt-4.1-mini" ),
4242 )
4343else :
4444 client = OpenAIChatClient (
45- api_key = os .environ ["OPENAI_API_KEY" ], model_id = os .environ .get ("OPENAI_MODEL" , "gpt-4.1-mini" )
45+ api_key = os .environ ["OPENAI_API_KEY" ], model = os .environ .get ("OPENAI_MODEL" , "gpt-4.1-mini" )
4646 )
4747
4848
49- class SQLiteHistoryProvider (BaseHistoryProvider ):
49+ class SQLiteHistoryProvider (HistoryProvider ):
5050 """A custom history provider backed by SQLite.
5151
52- Implements the BaseHistoryProvider to persist chat messages
52+ Implements the HistoryProvider to persist chat messages
5353 in a local SQLite database — useful when you want file-based
5454 persistence without an external service like Redis.
5555 """
@@ -94,16 +94,6 @@ def close(self) -> None:
9494 self ._conn .close ()
9595
9696
97- @tool
98- def get_weather (
99- city : Annotated [str , Field (description = "The city to get the weather for." )],
100- ) -> str :
101- """Returns weather data for a given city."""
102- logger .info (f"Getting weather for { city } " )
103- conditions = ["sunny" , "cloudy" , "rainy" , "stormy" ]
104- return f"The weather in { city } is { conditions [random .randint (0 , 3 )]} with a high of { random .randint (10 , 30 )} °C."
105-
106-
10797async def main () -> None :
10898 """Demonstrate a SQLite-backed session that persists conversation history to a local file."""
10999 db_path = "chat_history.sqlite3"
@@ -117,19 +107,18 @@ async def main() -> None:
117107
118108 agent = Agent (
119109 client = client ,
120- instructions = "You are a helpful weather agent." ,
121- tools = [get_weather ],
110+ instructions = "You are a helpful assistant that remembers our conversation." ,
122111 context_providers = [sqlite_provider ],
123112 )
124113
125114 session = agent .create_session (session_id = session_id )
126115
127- print ("[blue]User:[/blue] What's the weather like in Tokyo? " )
128- response = await agent .run ("What's the weather like in Tokyo? " , session = session )
116+ print ("[blue]User:[/blue] Hello! My name is Alice and I love hiking. " )
117+ response = await agent .run ("Hello! My name is Alice and I love hiking. " , session = session )
129118 print (f"[green]Agent:[/green] { response .text } " )
130119
131- print ("\n [blue]User:[/blue] How about Paris ?" )
132- response = await agent .run ("How about Paris ?" , session = session )
120+ print ("\n [blue]User:[/blue] What are some good trails in Colorado ?" )
121+ response = await agent .run ("What are some good trails in Colorado ?" , session = session )
133122 print (f"[green]Agent:[/green] { response .text } " )
134123
135124 # Phase 2: Simulate an application restart — reconnect to the same session ID in SQLite
@@ -138,15 +127,14 @@ async def main() -> None:
138127
139128 agent2 = Agent (
140129 client = client ,
141- instructions = "You are a helpful weather agent." ,
142- tools = [get_weather ],
130+ instructions = "You are a helpful assistant that remembers our conversation." ,
143131 context_providers = [sqlite_provider2 ],
144132 )
145133
146134 session2 = agent2 .create_session (session_id = session_id )
147135
148- print ("[blue]User:[/blue] Which of the cities I asked about had better weather ?" )
149- response = await agent2 .run ("Which of the cities I asked about had better weather ?" , session = session2 )
136+ print ("[blue]User:[/blue] What do you remember about me ?" )
137+ response = await agent2 .run ("What do you remember about me ?" , session = session2 )
150138 print (f"[green]Agent:[/green] { response .text } " )
151139
152140 sqlite_provider2 .close ()
0 commit comments