-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathagent.py
More file actions
65 lines (47 loc) · 1.97 KB
/
agent.py
File metadata and controls
65 lines (47 loc) · 1.97 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
# Here we demonstrate how we can create a news generating agent that is compatible with DeltaV.
# Import required libraries
from ai_engine import UAgentResponse, UAgentResponseType
from uagents import Agent, Context, Model, Protocol
# Define News Generating Model.
class GenerateNews(Model):
news_type: str
news: str
# First generate a secure seed phrase (e.g. https://pypi.org/project/mnemonic/)
SEED_PHRASE = "put_your_seed_phrase_here"
# Now your agent is ready to join the agentverse!
generate_news_agent = Agent(
name="generate_news_agent",
seed=SEED_PHRASE,
mailbox=True
)
# Copy the address shown below
print(f"Your agent's address is: {generate_news_agent.address}")
# Define Generate news protocol.
generate_news_protocol = Protocol("Generate News")
# Define a handler for the News generation protocol
@generate_news_protocol.on_message(model=GenerateNews, replies=UAgentResponse)
async def on_generate_news_request(ctx: Context, sender: str, msg: GenerateNews):
try:
# Generate news based on the requested category.
ctx.logger.info("Generating News")
ctx.logger.info(f"User have selected {msg.news_type} category")
ctx.logger.info(f"Generate News \n {msg.news}")
message = msg.news
# Send a successful response with the generated news.
await ctx.send(
sender, UAgentResponse(message=message, type=UAgentResponseType.FINAL)
)
# Handle any exceptions that occur during news generation.
except Exception as exc:
ctx.logger.error(f"Error in generating news: {exc}")
# Send an error response with details of the encountered error.
await ctx.send(
sender,
UAgentResponse(
message=f"Error in generating news: {exc}",
type=UAgentResponseType.ERROR,
),
)
# Include the Generate News protocol in your agent.
generate_news_agent.include(generate_news_protocol)
generate_news_agent.run()