-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathagent.py
More file actions
48 lines (33 loc) · 1.22 KB
/
agent.py
File metadata and controls
48 lines (33 loc) · 1.22 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
import random
from ai_engine import UAgentResponse, UAgentResponseType
from uagents import Agent, Context, Field, Model, Protocol
class CoinToss(Model):
choice: str = Field(description="The choice. Must be heads or tails.")
# 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!
coin_toss_agent = Agent(
name="coin_toss_agent",
seed=SEED_PHRASE,
mailbox=True
)
# Copy the address shown below
print(f"Your agent's address is: {coin_toss_agent.address}")
coin_toss_protocol = Protocol("CoinToss")
@coin_toss_protocol.on_message(model=CoinToss, replies={UAgentResponse})
async def toss_coin(ctx: Context, sender: str, msg: CoinToss):
random_number = random.randint(0, 1)
if random_number == 0:
coin_tossed = "heads"
else:
coin_tossed = "tails"
if coin_tossed == msg.choice:
message = "You won!"
else:
message = "You lost!"
await ctx.send(
sender, UAgentResponse(message=message, type=UAgentResponseType.FINAL)
)
coin_toss_agent.include(coin_toss_protocol, publish_manifest=True)
if __name__ == "__main__":
coin_toss_agent.run()