Skip to content

Commit 532f22d

Browse files
committed
added config ini to save and reuse agent, restore file search
1 parent 17d2377 commit 532f22d

File tree

3 files changed

+70
-22
lines changed

3 files changed

+70
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
__pycache__/
22

33
# Environments
4+
config.ini
45
.env
56
.venv
67
env/

src/quartapp/chat.py

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
from typing import Any
55
import azure.identity.aio
6-
76
from quart import Blueprint, jsonify, request, Response, render_template, current_app
87

98
import asyncio
@@ -23,39 +22,58 @@
2322
AgentStreamEvent
2423
)
2524

25+
from src.quartapp.config_helper import ConfigHelper
26+
27+
config = ConfigHelper()
2628

2729
bp = Blueprint("chat", __name__, template_folder="templates", static_folder="static")
2830

2931

3032
@bp.before_app_serving
3133
async def configure_assistant_client():
34+
35+
3236
ai_client = AIProjectClient.from_connection_string(
33-
credential=DefaultAzureCredential( exclude_shared_token_cache_credential=True),
37+
credential=DefaultAzureCredential(
38+
exclude_shared_token_cache_credential=True),
3439
conn_str=os.environ["PROJECT_CONNECTION_STRING"],
3540
)
36-
37-
print(f"Current dir is {os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'files', 'product_info_1.md'))}")
38-
39-
# files = ["product_info_1.md", "product_info_2.md"]
40-
# file_ids =[]
41-
# for file in files:
42-
# file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'files', file))
43-
# print(f"Uploading file {file_path}")
44-
# file_id = await ai_client.agents.upload_file_and_poll(file_path=file_path, purpose=FilePurpose.AGENTS)
45-
# file_ids.append(file_id)
4641

47-
# vector_store = await ai_client.agents.create_vector_store(file_ids=file_ids, name="sample_store")
48-
49-
# file_search_tool = FileSearchTool(vector_store_ids=[vector_store.id])
42+
agent_id = config.get("Agent", "AGENT_ID")
5043

51-
tool_set = AsyncToolSet()
52-
# tool_set.add(file_search_tool)
44+
agent = None
5345

54-
agent = await ai_client.agents.create_agent(
55-
model="gpt-4-1106-preview", name="my-assistant", instructions="You are helpful assistant", tools = tool_set.definitions, tool_resources=tool_set.resources
56-
)
46+
if agent_id:
47+
try:
48+
agent = await ai_client.agents.get_agent(agent_id)
49+
print(f"Agent already exists, agent ID: {agent.id}")
50+
except Exception as e:
51+
print(f"Agent not found: {e}")
52+
53+
if agent is None:
54+
files = ["product_info_1.md", "product_info_2.md"]
55+
file_ids = []
56+
for file in files:
57+
file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'files', file))
58+
print(f"Uploading file {file_path}")
59+
file = await ai_client.agents.upload_file_and_poll(file_path=file_path, purpose=FilePurpose.AGENTS)
60+
file_ids.append(file.id)
61+
62+
vector_store = await ai_client.agents.create_vector_store(file_ids=file_ids, name="sample_store")
63+
64+
file_search_tool = FileSearchTool(vector_store_ids=[vector_store.id])
65+
66+
tool_set = AsyncToolSet()
67+
tool_set.add(file_search_tool)
68+
69+
agent = await ai_client.agents.create_agent(
70+
model="gpt-4-1106-preview", name="my-assistant", instructions="You are helpful assistant", tools = tool_set.definitions, tool_resources=tool_set.resources
71+
)
72+
73+
print(f"Created agent, agent ID: {agent.id}")
5774

58-
print(f"Created agent, agent ID: {agent.id}")
75+
config.set('Agent', 'AGENT_ID', agent.id)
76+
config.save()
5977

6078

6179
bp.ai_client = ai_client
@@ -64,7 +82,6 @@ async def configure_assistant_client():
6482

6583
@bp.after_app_serving
6684
async def shutdown_assistant_client():
67-
await bp.ai_client.agents.delete_agent(bp.agent.id)
6885
await bp.ai_client.close()
6986

7087
@bp.get("/")

src/quartapp/config_helper.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
# Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
3+
4+
import configparser
5+
import os
6+
7+
class ConfigHelper:
8+
9+
def __init__(self):
10+
self.config = configparser.ConfigParser()
11+
config_file_path = 'config.ini'
12+
if os.path.exists(config_file_path):
13+
self.config.read(config_file_path)
14+
else:
15+
print(f"Config file {config_file_path} does not exist.")
16+
17+
def get(self, section: str, key: str):
18+
if self.config.has_option(section, key):
19+
return self.config.get(section, key)
20+
return None
21+
22+
def set(self, section, key, value):
23+
if not self.config.has_section(section):
24+
self.config.add_section(section)
25+
self.config.set(section, key, value)
26+
27+
def save(self):
28+
with open('config.ini', 'w') as configfile:
29+
self.config.write(configfile)
30+

0 commit comments

Comments
 (0)