Register a coin toss agent as a Function
Introduction
This file can be run on any platform supporting Python, with the necessary install permissions. To enable this on DeltaV you will need to create and register a Function. You will need to run this file on a device you are able to open a port on.
Please check out the example code in our examples repo (opens in a new tab) to run this locally.
Guide
Supporting documentation
The agent
Self hosted
agent.pyimport random from uagents import Agent, Context, Model, Field, Protocol from uagents.setup import fund_agent_if_low from ai_engine import UAgentResponse, UAgentResponseType 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 ) fund_agent_if_low(coin_toss_agent.wallet.address()) # 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()