1
1
from datetime import datetime
2
2
3
+ from azure .ai .agents .models import CodeInterpreterTool
3
4
from azure .ai .projects import AIProjectClient
4
5
from azure .identity import DefaultAzureCredential
5
6
10
11
AgentStatus ,
11
12
ChatRequest ,
12
13
ChatResponse ,
14
+ ThreadListResponse ,
15
+ ThreadRequest ,
16
+ ThreadResponse ,
13
17
)
14
18
from template_fastapi .settings .azure_ai_foundry import get_azure_ai_foundry_settings
15
19
20
+ code_interpreter = CodeInterpreterTool ()
21
+
16
22
17
23
class AgentRepository :
18
24
"""Repository for handling agent-related operations with Azure AI Foundry."""
19
25
20
26
def __init__ (self ):
21
27
self .settings = get_azure_ai_foundry_settings ()
22
28
self .client = AIProjectClient (
23
- self .settings .azure_ai_foundry_project_connection_string ,
29
+ endpoint = self .settings .azure_ai_foundry_project_endpoint ,
24
30
credential = DefaultAzureCredential (),
25
31
)
26
32
@@ -32,7 +38,7 @@ def create_agent(self, request: AgentRequest) -> AgentResponse:
32
38
name = request .name ,
33
39
description = request .description ,
34
40
instructions = request .instructions ,
35
- tools = request . tools or [] ,
41
+ tools = code_interpreter . definitions ,
36
42
)
37
43
38
44
now = datetime .now ().isoformat ()
@@ -72,15 +78,17 @@ def get_agent(self, agent_id: str) -> AgentResponse:
72
78
except Exception as e :
73
79
raise Exception (f"Failed to get agent: { str (e )} " )
74
80
75
- def list_agents (self , limit : int = 10 , offset : int = 0 ) -> AgentListResponse :
81
+ def list_agents (self , limit : int = 10 ) -> AgentListResponse :
76
82
"""List agents."""
77
83
try :
78
- agents_response = self .client .agents .list_agents ()
84
+ agents_response = self .client .agents .list_agents (
85
+ limit = limit ,
86
+ )
79
87
80
88
now = datetime .now ().isoformat ()
81
89
82
90
agents = []
83
- for agent in agents_response . data :
91
+ for agent in agents_response :
84
92
agents .append (
85
93
AgentResponse (
86
94
id = agent .id ,
@@ -96,7 +104,7 @@ def list_agents(self, limit: int = 10, offset: int = 0) -> AgentListResponse:
96
104
)
97
105
98
106
return AgentListResponse (
99
- agents = agents [ offset : offset + limit ] ,
107
+ agents = agents ,
100
108
total = len (agents ),
101
109
)
102
110
except Exception as e :
@@ -113,46 +121,82 @@ def delete_agent(self, agent_id: str) -> bool:
113
121
def chat_with_agent (self , agent_id : str , request : ChatRequest ) -> ChatResponse :
114
122
"""Chat with an agent."""
115
123
try :
116
- # Create a thread if not provided
117
- if request .thread_id :
118
- thread_id = request .thread_id
119
- else :
120
- thread_response = self .client .agents .create_thread ()
121
- thread_id = thread_response .id
122
-
123
- # Create a message
124
- message_response = self .client .agents .create_message (
125
- thread_id = thread_id ,
124
+ _ = self .client .agents .messages .create (
125
+ thread_id = request .thread_id ,
126
126
role = "user" ,
127
127
content = request .message ,
128
128
)
129
129
130
- # Create a run
131
- run_response = self .client .agents .create_run (
132
- thread_id = thread_id ,
133
- assistant_id = agent_id ,
130
+ run = self .client .agents .runs .create_and_process (
131
+ agent_id = agent_id ,
132
+ thread_id = request .thread_id ,
134
133
)
135
134
136
- # Poll for completion
137
- while run_response .status in ["queued" , "in_progress" ]:
138
- run_response = self .client .agents .get_run (
139
- thread_id = thread_id ,
140
- run_id = run_response .id ,
135
+ print (f"Run ID: { run .id } m status: { run .last_error } " )
136
+
137
+ messages = self .client .agents .messages .list (
138
+ thread_id = request .thread_id ,
139
+ )
140
+
141
+ # FIXME: Iterable object の最初のメッセージを取得
142
+ for message in messages :
143
+ return ChatResponse (
144
+ id = run .id ,
145
+ agent_id = agent_id ,
146
+ thread_id = request .thread_id ,
147
+ message = request .message ,
148
+ response = message .content .__str__ () if messages else "" ,
149
+ created_at = message .created_at .isoformat () if messages else "" ,
141
150
)
142
151
143
- # Get the response
144
- messages = self .client .agents .list_messages (thread_id = thread_id )
145
- response_message = messages .data [0 ].content [0 ].text .value
152
+ except Exception as e :
153
+ raise Exception (f"Failed to chat with agent: { str (e )} " )
146
154
147
- now = datetime .now ().isoformat ()
155
+ def create_thread (self , request : ThreadRequest ) -> ThreadResponse :
156
+ """Create a new thread for chatting with an agent."""
157
+ try :
158
+ thread_response = self .client .agents .threads .create (** request .model_dump ())
159
+ return ThreadResponse (
160
+ id = thread_response .id ,
161
+ created_at = thread_response .created_at .isoformat (),
162
+ )
163
+ except Exception as e :
164
+ raise Exception (f"Failed to create thread: { str (e )} " )
148
165
149
- return ChatResponse (
150
- id = message_response . id ,
151
- agent_id = agent_id ,
152
- thread_id = thread_id ,
153
- message = request . message ,
154
- response = response_message ,
155
- created_at = now ,
166
+ def get_thread ( self , thread_id : str ) -> ThreadResponse :
167
+ """Get a specific thread by ID."""
168
+ try :
169
+ thread_response = self . client . agents . threads . get ( thread_id = thread_id )
170
+ return ThreadResponse (
171
+ id = thread_response . id ,
172
+ created_at = thread_response . created_at . isoformat () ,
156
173
)
157
174
except Exception as e :
158
- raise Exception (f"Failed to chat with agent: { str (e )} " )
175
+ raise Exception (f"Failed to get thread: { str (e )} " )
176
+
177
+ def delete_thread (self , thread_id : str ) -> bool :
178
+ """Delete a specific thread by ID."""
179
+ try :
180
+ self .client .agents .threads .delete (thread_id = thread_id )
181
+ return True
182
+ except Exception as e :
183
+ raise Exception (f"Failed to delete thread: { str (e )} " )
184
+
185
+ def list_threads (self , limit : int ) -> ThreadListResponse :
186
+ """List threads for a specific agent."""
187
+ try :
188
+ threads_response = self .client .agents .threads .list (limit = limit )
189
+ threads = []
190
+ for thread in threads_response :
191
+ threads .append (
192
+ ThreadResponse (
193
+ id = thread .id ,
194
+ created_at = thread .created_at .isoformat (),
195
+ )
196
+ )
197
+ return ThreadListResponse (
198
+ threads = threads ,
199
+ total = len (threads ),
200
+ )
201
+ except Exception as e :
202
+ raise Exception (f"Failed to list threads: { str (e )} " )
0 commit comments