-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathagent.py
More file actions
223 lines (174 loc) · 7.07 KB
/
Copy pathagent.py
File metadata and controls
223 lines (174 loc) · 7.07 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
"""
Example Agent Script with Agentverse Registration and ASI One Integration
"""
import os
import requests
import traceback
from datetime import datetime
from uuid import uuid4
from uagents import Agent, Protocol, Context
from uagents_core.contrib.protocols.chat import (
ChatAcknowledgement,
ChatMessage,
EndSessionContent,
StartSessionContent,
TextContent,
chat_protocol_spec,
)
from uagents_core.utils.registration import (
register_chat_agent,
RegistrationRequestCredentials,
)
from dotenv import load_dotenv
load_dotenv()
UAGENT_NAME = "ExampleAgent"
SEED_PHRASE = os.getenv("AGENT_SEED_PHRASE", "example-agent-seed-phrase-123")
AGENTVERSE_KEY = os.getenv("AGENTVERSE_KEY")
ASI_ONE_API_KEY = os.getenv("ASI_ONE_API_KEY")
AGENT_PORT = int(os.getenv("AGENT_PORT", "8006"))
HOSTING_ENDPOINT = os.getenv("HOSTING_ENDPOINT")
agent = Agent(
name=UAGENT_NAME,
seed=SEED_PHRASE,
port=AGENT_PORT,
mailbox=True
)
chat_proto = Protocol(spec=chat_protocol_spec)
README = """# Example Agent 🤖
An example agent that demonstrates Agentverse registration, chat protocol, and ASI One API integration.
## Features
- **Agentverse Registration**: Automatically registers with Agentverse for discoverability
- **Chat Protocol**: Full chat protocol implementation with session management
- **ASI One Integration**: Uses ASI One API for AI-powered responses
- **Multi-turn conversations**: Supports session-based chat interactions
## Prerequisites
- Python 3.10+
- ASI One API key
- Agentverse API key (optional, for registration)
## Usage
### Run Locally
```bash
python agent.py
```
The agent will start on `http://0.0.0.0:8006`
### Example Queries
Once running, send messages to the agent via Agentverse or direct chat protocol:
```
"What is agentic AI?"
"Explain machine learning"
"Hello, how are you?"
```
## Technology Stack
- **uAgents**: Chat protocol communication and agent-to-agent messaging
- **ASI One API**: AI-powered responses
- **Agentverse**: Agent discovery and registration
"""
def call_asi_one_api(user_message: str) -> str:
url = "https://api.asi1.ai/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {ASI_ONE_API_KEY}"
}
data = {
"model": "asi1-mini",
"messages": [{"role": "user", "content": user_message}]
}
try:
response = requests.post(url, headers=headers, json=data, timeout=30)
response.raise_for_status()
result = response.json()
if "choices" in result and len(result["choices"]) > 0:
return result["choices"][0]["message"]["content"]
return "Sorry, I couldn't generate a response."
except requests.exceptions.RequestException as e:
return f"Error calling ASI One API: {str(e)}"
except Exception as e:
return f"Unexpected error: {str(e)}"
def create_text_chat(text: str, end_session: bool = False) -> ChatMessage:
content = [TextContent(type="text", text=text)]
if end_session:
content.append(EndSessionContent())
return ChatMessage(
timestamp=datetime.utcnow(),
msg_id=uuid4(),
content=content,
)
@chat_proto.on_message(ChatMessage)
async def handle_message(ctx: Context, sender: str, msg: ChatMessage):
ctx.logger.info(f"Received message from {sender}")
await ctx.send(
sender,
ChatAcknowledgement(
timestamp=datetime.utcnow(),
acknowledged_msg_id=msg.msg_id
)
)
for item in msg.content:
if isinstance(item, StartSessionContent):
ctx.logger.info(f"Session started with {sender}")
welcome_message = create_text_chat("Hello! I'm an example agent. How can I help you?")
await ctx.send(sender, welcome_message)
elif isinstance(item, TextContent):
ctx.logger.info(f"Text message from {sender}: {item.text}")
if ASI_ONE_API_KEY:
try:
ai_response = call_asi_one_api(item.text)
response_message = create_text_chat(ai_response)
except Exception as e:
ctx.logger.error(f"Error calling ASI One API: {e}")
response_message = create_text_chat("Sorry, I encountered an error processing your request.")
else:
response_message = create_text_chat(
f"Hello from Agent! You said: {item.text}\n\n"
"Note: ASI_ONE_API_KEY not configured. Set it to enable AI-powered responses."
)
await ctx.send(sender, response_message)
elif isinstance(item, EndSessionContent):
ctx.logger.info(f"Session ended with {sender}")
goodbye_message = create_text_chat("Goodbye! Thanks for chatting.", end_session=True)
await ctx.send(sender, goodbye_message)
else:
ctx.logger.info(f"Received unexpected content type from {sender}")
@chat_proto.on_message(ChatAcknowledgement)
async def handle_acknowledgement(ctx: Context, sender: str, msg: ChatAcknowledgement):
ctx.logger.info(f"Received acknowledgement from {sender} for message {msg.acknowledged_msg_id}")
@agent.on_event("startup")
async def startup_handler(ctx: Context):
ctx.logger.info(f"🚀 Agent starting: {ctx.agent.name} at {ctx.agent.address}")
if AGENTVERSE_KEY and SEED_PHRASE:
try:
endpoint_url = HOSTING_ENDPOINT
if not endpoint_url and hasattr(agent, '_endpoints') and agent._endpoints:
endpoint_url = agent._endpoints[0].url
elif not endpoint_url:
endpoint_url = f"http://localhost:{AGENT_PORT}"
ctx.logger.info(f"Registering with Agentverse using endpoint: {endpoint_url}")
register_chat_agent(
"Example Agent",
endpoint_url,
active=True,
credentials=RegistrationRequestCredentials(
agentverse_api_key=AGENTVERSE_KEY,
agent_seed_phrase=SEED_PHRASE,
),
readme=README,
description="An example agent demonstrating Agentverse registration, chat protocol, and ASI One API integration."
)
ctx.logger.info("✅ Registered with Agentverse")
except Exception as e:
ctx.logger.error(f"❌ Failed to register with Agentverse: {e}")
ctx.logger.error(f"Traceback: {traceback.format_exc()}")
else:
missing = []
if not AGENTVERSE_KEY:
missing.append("AGENTVERSE_KEY")
if not SEED_PHRASE:
missing.append("AGENT_SEED_PHRASE")
ctx.logger.warning(f"⚠️ {', '.join(missing)} not set, skipping Agentverse registration")
if ASI_ONE_API_KEY:
ctx.logger.info("✅ ASI One API key configured")
else:
ctx.logger.warning("⚠️ ASI_ONE_API_KEY not set, AI responses will be disabled")
agent.include(chat_proto, publish_manifest=True)
if __name__ == "__main__":
agent.run()