3
3
4
4
from typing import Any
5
5
import azure .identity .aio
6
-
7
6
from quart import Blueprint , jsonify , request , Response , render_template , current_app
8
7
9
8
import asyncio
23
22
AgentStreamEvent
24
23
)
25
24
25
+ from src .quartapp .config_helper import ConfigHelper
26
+
27
+ config = ConfigHelper ()
26
28
27
29
bp = Blueprint ("chat" , __name__ , template_folder = "templates" , static_folder = "static" )
28
30
29
31
30
32
@bp .before_app_serving
31
33
async def configure_assistant_client ():
34
+
35
+
32
36
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 ),
34
39
conn_str = os .environ ["PROJECT_CONNECTION_STRING" ],
35
40
)
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)
46
41
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" )
50
43
51
- tool_set = AsyncToolSet ()
52
- # tool_set.add(file_search_tool)
44
+ agent = None
53
45
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 } " )
57
74
58
- print (f"Created agent, agent ID: { agent .id } " )
75
+ config .set ('Agent' , 'AGENT_ID' , agent .id )
76
+ config .save ()
59
77
60
78
61
79
bp .ai_client = ai_client
@@ -64,7 +82,6 @@ async def configure_assistant_client():
64
82
65
83
@bp .after_app_serving
66
84
async def shutdown_assistant_client ():
67
- await bp .ai_client .agents .delete_agent (bp .agent .id )
68
85
await bp .ai_client .close ()
69
86
70
87
@bp .get ("/" )
0 commit comments