1+ import os
2+ from mistralai import Mistral
3+
4+ from mistralai .extra .run .context import RunContext
5+ import logging
6+ import time
7+ import asyncio
8+
9+
10+ MODEL = "mistral-medium-latest"
11+
12+ USER_MESSAGE = """
13+ Please make the Secret Santa for me
14+ To properly do it you need to:
15+ - Get the friend you were assigned to (using the get_secret_santa_assignment function)
16+ - Read into his gift wishlist what they would like to receive (using the get_gift_wishlist function)
17+ - Buy the gift (using the buy_gift function)
18+ - Find the best website to buy the gift using a web search
19+ - Send it to them (using the send_gift function)
20+ """
21+
22+
23+ async def main ():
24+ api_key = os .environ ["MISTRAL_API_KEY" ]
25+ mistral_agent_id = os .environ ["MISTRAL_AGENT_ID" ]
26+ client = Mistral (
27+ api_key = api_key , debug_logger = logging .getLogger ("mistralai" )
28+ )
29+
30+ async with RunContext (
31+ agent_id = mistral_agent_id
32+ ) as run_context :
33+ run_context .register_func (get_secret_santa_assignment )
34+ run_context .register_func (get_gift_wishlist )
35+ run_context .register_func (buy_gift )
36+ run_context .register_func (send_gift )
37+
38+ await client .beta .conversations .run_async (
39+ run_ctx = run_context ,
40+ inputs = USER_MESSAGE ,
41+ )
42+
43+
44+ def get_secret_santa_assignment ():
45+ """Get the friend you were assigned to"""
46+ time .sleep (2 )
47+ return "John Doe"
48+
49+
50+ def get_gift_wishlist (friend_name : str ):
51+ """Get the gift wishlist of the friend you were assigned to"""
52+ time .sleep (1.5 )
53+ return ["Book" , "Chocolate" , "T-Shirt" ]
54+
55+
56+ def buy_gift (gift_name : str ):
57+ """Buy the gift you want to send to your friend"""
58+ time .sleep (1.1 )
59+ return f"Bought { gift_name } "
60+
61+
62+ def send_gift (friend_name : str , gift_name : str , website : str ):
63+ """Send the gift to your friend"""
64+ time .sleep (2.2 )
65+ return f"Sent { gift_name } to { friend_name } bought on { website } "
66+
67+
68+ if __name__ == "__main__" :
69+ asyncio .run (main ())
0 commit comments