forked from themanojdesai/python-a2a
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai.py
More file actions
562 lines (489 loc) · 23.5 KB
/
Copy pathopenai.py
File metadata and controls
562 lines (489 loc) · 23.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
"""
OpenAI-based server implementation for the A2A protocol.
"""
import uuid
import json
import asyncio
from typing import Optional, Dict, Any, List, Union, AsyncGenerator
try:
from openai import OpenAI
from openai import AsyncOpenAI
except ImportError:
OpenAI = None
AsyncOpenAI = None
from ...models.message import Message, MessageRole
from ...models.content import TextContent, FunctionCallContent, FunctionResponseContent, ErrorContent
from ...models.conversation import Conversation
from ...models.task import Task, TaskStatus, TaskState
from ..base import BaseA2AServer
from ...exceptions import A2AImportError, A2AConnectionError, A2AStreamingError
class OpenAIA2AServer(BaseA2AServer):
"""
An A2A server that uses OpenAI's API to process messages.
This server converts incoming A2A messages to OpenAI's format, processes them
using OpenAI's API, and converts the responses back to A2A format.
"""
def __init__(
self,
api_key: str,
model: str = "gpt-4",
temperature: float = 0.7,
system_prompt: Optional[str] = None,
functions: Optional[List[Dict[str, Any]]] = None
):
"""
Initialize the OpenAI A2A server
Args:
api_key: OpenAI API key
model: OpenAI model to use (default: "gpt-4")
temperature: Generation temperature (default: 0.7)
system_prompt: Optional system prompt to use for all conversations
functions: Optional list of function definitions for function calling
Raises:
A2AImportError: If the OpenAI package is not installed
"""
if OpenAI is None:
raise A2AImportError(
"OpenAI package is not installed. "
"Install it with 'pip install openai'"
)
self.api_key = api_key
self.model = model
self.temperature = temperature
self.system_prompt = system_prompt or "You are a helpful AI assistant."
self.functions = functions
self.tools = self._convert_functions_to_tools() if functions else None
self.client = OpenAI(api_key=api_key)
# Create an async client for streaming
if AsyncOpenAI is not None:
self.async_client = AsyncOpenAI(api_key=api_key)
else:
self.async_client = None
# For tracking conversation state
self._conversation_state = {} # conversation_id -> list of messages
def _convert_functions_to_tools(self):
"""Convert functions to the tools format used by newer OpenAI models"""
if not self.functions:
return None
tools = []
for func in self.functions:
tools.append({
"type": "function",
"function": func
})
return tools
def handle_message(self, message: Message) -> Message:
"""
Process an incoming A2A message using OpenAI's API
Args:
message: The incoming A2A message
Returns:
The response as an A2A message
Raises:
A2AConnectionError: If connection to OpenAI fails
"""
try:
# Prepare the OpenAI messages
openai_messages = [{"role": "system", "content": self.system_prompt}]
conversation_id = message.conversation_id
# If this is part of an existing conversation, retrieve history
if conversation_id and conversation_id in self._conversation_state:
# Use the existing conversation history
openai_messages = self._conversation_state[conversation_id].copy()
# Add the user message
if message.content.type == "text":
openai_messages.append({
"role": "user" if message.role == MessageRole.USER else "assistant",
"content": message.content.text
})
elif message.content.type == "function_call":
# Format function call as text for OpenAI
params_str = ", ".join([f"{p.name}={p.value}" for p in message.content.parameters])
text = f"Call function {message.content.name}({params_str})"
openai_messages.append({"role": "user", "content": text})
elif message.content.type == "function_response":
# Format function response in OpenAI's expected format
# This is critical for function calling to work properly
openai_messages.append({
"role": "function",
"name": message.content.name,
"content": json.dumps(message.content.response)
})
else:
# Handle other message types or errors
text = f"Message of type {message.content.type}"
if hasattr(message.content, "message"):
text = message.content.message
openai_messages.append({"role": "user", "content": text})
# Call OpenAI API with appropriate parameters
kwargs = {
"model": self.model,
"messages": openai_messages,
"temperature": self.temperature,
}
# Add tools or functions based on model and availability
if self.tools:
# Newer models use tools
kwargs["tools"] = self.tools
kwargs["tool_choice"] = "auto"
elif self.functions:
# Older models use functions
kwargs["functions"] = self.functions
kwargs["function_call"] = "auto"
response = self.client.chat.completions.create(**kwargs)
# Process the response
choice = response.choices[0]
response_message = choice.message
# If we have a conversation ID, update the conversation state
if conversation_id:
if conversation_id not in self._conversation_state:
self._conversation_state[conversation_id] = [{"role": "system", "content": self.system_prompt}]
# Add the original user message to state
if message.content.type == "text":
self._conversation_state[conversation_id].append({
"role": "user" if message.role == MessageRole.USER else "assistant",
"content": message.content.text
})
elif message.content.type == "function_response":
self._conversation_state[conversation_id].append({
"role": "function",
"name": message.content.name,
"content": json.dumps(message.content.response)
})
# Add the assistant's response to state
if hasattr(response_message, "content") and response_message.content:
self._conversation_state[conversation_id].append({
"role": "assistant",
"content": response_message.content
})
# If it's a tool/function call, add that to state too
tool_calls = getattr(response_message, "tool_calls", None)
if tool_calls:
for tool_call in tool_calls:
if tool_call.type == "function":
self._conversation_state[conversation_id].append({
"role": "assistant",
"tool_calls": [
{
"id": tool_call.id,
"type": "function",
"function": {
"name": tool_call.function.name,
"arguments": tool_call.function.arguments
}
}
]
})
break
elif hasattr(response_message, "function_call") and response_message.function_call:
func_call = response_message.function_call
self._conversation_state[conversation_id].append({
"role": "assistant",
"function_call": {
"name": func_call.name,
"arguments": func_call.arguments
}
})
# Convert the response to A2A format
# Check for function calls via newer tool_calls interface first
tool_calls = getattr(response_message, "tool_calls", None)
if tool_calls:
for tool_call in tool_calls:
if tool_call.type == "function":
# Process function call
try:
# Parse arguments as JSON
args = json.loads(tool_call.function.arguments)
parameters = [
{"name": name, "value": value}
for name, value in args.items()
]
except:
# Fallback parsing for non-JSON arguments
parameters = [{"name": "arguments", "value": tool_call.function.arguments}]
return Message(
content=FunctionCallContent(
name=tool_call.function.name,
parameters=parameters
),
role=MessageRole.AGENT,
parent_message_id=message.message_id,
conversation_id=message.conversation_id
)
# Then check older function_call interface
elif hasattr(response_message, "function_call") and response_message.function_call:
function_call = response_message.function_call
try:
# Parse arguments as JSON
args = json.loads(function_call.arguments)
parameters = [
{"name": name, "value": value}
for name, value in args.items()
]
except:
# Fallback parsing for non-JSON arguments
parameters = [{"name": "arguments", "value": function_call.arguments}]
return Message(
content=FunctionCallContent(
name=function_call.name,
parameters=parameters
),
role=MessageRole.AGENT,
parent_message_id=message.message_id,
conversation_id=message.conversation_id
)
# Regular text response
return Message(
content=TextContent(text=response_message.content or ""),
role=MessageRole.AGENT,
parent_message_id=message.message_id,
conversation_id=message.conversation_id
)
except Exception as e:
raise A2AConnectionError(f"Failed to communicate with OpenAI: {str(e)}")
def handle_task(self, task: Task) -> Task:
"""
Process an incoming A2A task using OpenAI's API
Args:
task: The incoming A2A task
Returns:
The updated task with the response
"""
try:
# Extract the message from the task
message_data = task.message or {}
# Convert to Message object if it's a dict
if isinstance(message_data, dict):
from ...models import Message
message = Message.from_dict(message_data)
else:
message = message_data
# Process the message
response = self.handle_message(message)
# Create artifact based on response content type
if hasattr(response, "content"):
content_type = getattr(response.content, "type", None)
if content_type == "text":
# Handle TextContent
task.artifacts = [{
"parts": [{
"type": "text",
"text": response.content.text
}]
}]
elif content_type == "function_response":
# Handle FunctionResponseContent
task.artifacts = [{
"parts": [{
"type": "function_response",
"name": response.content.name,
"response": response.content.response
}]
}]
elif content_type == "function_call":
# Handle FunctionCallContent
params = []
for param in response.content.parameters:
params.append({
"name": param.name,
"value": param.value
})
task.artifacts = [{
"parts": [{
"type": "function_call",
"name": response.content.name,
"parameters": params
}]
}]
elif content_type == "error":
# Handle ErrorContent
task.artifacts = [{
"parts": [{
"type": "error",
"message": response.content.message
}]
}]
else:
# Handle other content types
task.artifacts = [{
"parts": [{
"type": "text",
"text": str(response.content)
}]
}]
else:
# Handle responses without content
task.artifacts = [{
"parts": [{
"type": "text",
"text": str(response)
}]
}]
# Mark as completed
task.status = TaskStatus(state=TaskState.COMPLETED)
return task
except Exception as e:
# Handle errors
task.artifacts = [{
"parts": [{
"type": "error",
"message": f"Error in OpenAI server: {str(e)}"
}]
}]
task.status = TaskStatus(state=TaskState.FAILED)
return task
def handle_conversation(self, conversation: Conversation) -> Conversation:
"""
Process an incoming A2A conversation using OpenAI's API
This method overrides the default implementation to send the entire
conversation history to OpenAI instead of just the last message.
Args:
conversation: The incoming A2A conversation
Returns:
The updated conversation with the response
"""
if not conversation.messages:
# Empty conversation, create an error
conversation.create_error_message("Empty conversation received")
return conversation
try:
# Store conversation in state
conversation_id = conversation.conversation_id
self._conversation_state[conversation_id] = [{"role": "system", "content": self.system_prompt}]
# Convert all messages to OpenAI format
for msg in conversation.messages:
if msg.content.type == "text":
self._conversation_state[conversation_id].append({
"role": "user" if msg.role == MessageRole.USER else "assistant",
"content": msg.content.text
})
elif msg.content.type == "function_call":
# Format function call for OpenAI
params_str = ", ".join([f"{p.name}={p.value}" for p in msg.content.parameters])
text = f"Call function {msg.content.name}({params_str})"
self._conversation_state[conversation_id].append({
"role": "user" if msg.role == MessageRole.USER else "assistant",
"content": text
})
elif msg.content.type == "function_response":
# Format function response for OpenAI
self._conversation_state[conversation_id].append({
"role": "function",
"name": msg.content.name,
"content": json.dumps(msg.content.response)
})
# Get the last message to process
last_message = conversation.messages[-1]
# Call the handle_message method to process the last message
a2a_response = self.handle_message(last_message)
# Add the response to the conversation
conversation.add_message(a2a_response)
return conversation
except Exception as e:
# Add an error message to the conversation
error_msg = f"Failed to communicate with OpenAI: {str(e)}"
conversation.create_error_message(error_msg, parent_message_id=conversation.messages[-1].message_id)
return conversation
async def stream_response(self, message: Message) -> AsyncGenerator[str, None]:
"""
Stream a response from OpenAI for the given message.
Args:
message: The A2A message to respond to
Yields:
Chunks of the response as they arrive
Raises:
A2AStreamingError: If streaming is not supported or fails
A2AConnectionError: If connection to OpenAI fails
"""
# Check if streaming is supported
if self.async_client is None:
raise A2AStreamingError(
"AsyncOpenAI is not available. Ensure you have the latest "
"openai package installed with 'pip install -U openai'."
)
try:
# Extract message content
query = ""
if hasattr(message.content, "type") and message.content.type == "text":
query = message.content.text
elif hasattr(message.content, "text"):
query = message.content.text
# Prepare message history (similar logic to handle_message)
openai_messages = [{"role": "system", "content": self.system_prompt}]
conversation_id = message.conversation_id
# If this is part of an existing conversation, retrieve history
if conversation_id and conversation_id in self._conversation_state:
# Use the existing conversation history
openai_messages = self._conversation_state[conversation_id].copy()
# Add the user message if not already in the history
if not any(msg.get("role") == "user" and msg.get("content") == query
for msg in openai_messages if "role" in msg and "content" in msg):
openai_messages.append({
"role": "user" if message.role == MessageRole.USER else "assistant",
"content": query
})
# Prepare request arguments
kwargs = {
"model": self.model,
"messages": openai_messages,
"temperature": self.temperature,
"stream": True # Enable streaming
}
# Add tools or functions based on model and availability
if self.tools:
# Newer models use tools
kwargs["tools"] = self.tools
kwargs["tool_choice"] = "auto"
elif self.functions:
# Older models use functions
kwargs["functions"] = self.functions
kwargs["function_call"] = "auto"
# Call OpenAI API with streaming
async for chunk in await self.async_client.chat.completions.create(**kwargs):
if hasattr(chunk.choices[0].delta, 'content') and chunk.choices[0].delta.content:
yield chunk.choices[0].delta.content
# Handle function/tool calls in streaming (if needed in the future)
# This is a placeholder for future implementation
# if hasattr(chunk.choices[0].delta, 'tool_calls') and chunk.choices[0].delta.tool_calls:
# # Process tool calls here if needed
# pass
# Update conversation state after streaming is complete
if conversation_id:
if conversation_id not in self._conversation_state:
self._conversation_state[conversation_id] = [{"role": "system", "content": self.system_prompt}]
# Add the user message
if not any(msg.get("role") == "user" and msg.get("content") == query
for msg in self._conversation_state[conversation_id] if "role" in msg and "content" in msg):
self._conversation_state[conversation_id].append({
"role": "user",
"content": query
})
# Add the assistant's response (aggregate from streaming)
# Future enhancement: This could be improved to capture the full streamed response
self._conversation_state[conversation_id].append({
"role": "assistant",
"content": "[Streamed response]" # Placeholder for now
})
except Exception as e:
# Convert exceptions to A2A-specific exceptions
if isinstance(e, A2AStreamingError):
raise
raise A2AConnectionError(f"Failed to stream from OpenAI: {str(e)}")
def get_metadata(self) -> Dict[str, Any]:
"""
Get metadata about this agent server
Returns:
A dictionary of metadata about this agent
"""
metadata = super().get_metadata()
metadata.update({
"agent_type": "OpenAIA2AServer",
"model": self.model,
})
if self.functions:
metadata["capabilities"].append("function_calling")
metadata["functions"] = [f["name"] for f in self.functions]
# Mark streaming capability based on AsyncOpenAI availability
if self.async_client is not None:
metadata["capabilities"].append("streaming")
return metadata