|
| 1 | + |
| 2 | +import os |
| 3 | +from typing import Optional, List |
| 4 | +from phi.llm.openai.like import OpenAILike |
| 5 | +from phi.assistant import Assistant |
| 6 | +from phi.knowledge.json import JSONKnowledgeBase |
| 7 | +from phi.vectordb.pgvector import PgVector2 |
| 8 | +from phi.storage.assistant.postgres import PgAssistantStorage |
| 9 | +from phi.tools import Toolkit |
| 10 | +from phi.tools.email import EmailTools |
| 11 | +from phi.utils.log import logger |
| 12 | +from phi.tools.email import EmailTools |
| 13 | +from resources import vector_db |
| 14 | +from rich.prompt import Prompt |
| 15 | +from dotenv import load_dotenv |
| 16 | +load_dotenv() |
| 17 | + |
| 18 | +# To run this example, first make sure to follow the instructions below: |
| 19 | +# 1. Install the phidata: pip install phidata |
| 20 | +# 2. Run the following command to start a docker, with pgvector db running: phi start resources.py |
| 21 | + |
| 22 | +class CinemaTools(Toolkit): |
| 23 | + def __init__( |
| 24 | + self, |
| 25 | + email_tools: Optional["EmailTools"] = None, |
| 26 | + ): |
| 27 | + super().__init__(name="cinema_tools") |
| 28 | + self.email_tools = email_tools |
| 29 | + self.register(self.book_cinema_ticket) |
| 30 | + |
| 31 | + def book_cinema_ticket(self, movie_name: str, date: str, time: str, user_email: Optional[str] = None) -> str: |
| 32 | + """Books a cinema ticket for the given movie, date, and time, and sends an email to the user. |
| 33 | +
|
| 34 | + :param movie_name: The name of the movie. |
| 35 | + :param date: The date of the movie (e.g., "2023-06-15"). |
| 36 | + :param time: The time of the movie (e.g., "19:30"). |
| 37 | + :param user_email: The email address of the user. |
| 38 | + :return: "success" if the ticket was booked and email sent successfully, "error: [error message]" otherwise. |
| 39 | + """ |
| 40 | + # Simulate booking the ticket |
| 41 | + ticket_number = self._generate_ticket_number() |
| 42 | + logger.info(f"Booking ticket for {movie_name} on {date} at {time}") |
| 43 | + |
| 44 | + # Prepare the email subject and body |
| 45 | + subject = f"Your ticket for {movie_name}" |
| 46 | + body = f"Dear user,\n\nYour ticket for {movie_name} on {date} at {time} has been booked.\n\n" \ |
| 47 | + f"Your ticket number is: {ticket_number}\n\nEnjoy the movie!\n\nBest regards,\nThe Cinema Team" |
| 48 | + |
| 49 | + # Send the email using the EmailTools |
| 50 | + if not self.email_tools: |
| 51 | + return "error: No email tools provided" |
| 52 | + self.email_tools.receiver_email = user_email |
| 53 | + result = self.email_tools.email_user(subject, body) |
| 54 | + |
| 55 | + if result.startswith("error"): |
| 56 | + logger.error(f"Error booking ticket: {result}") |
| 57 | + return result |
| 58 | + return "success" |
| 59 | + |
| 60 | + def _generate_ticket_number(self) -> str: |
| 61 | + """Generates a dummy ticket number.""" |
| 62 | + import random |
| 63 | + import string |
| 64 | + return "".join(random.choices(string.ascii_uppercase + string.digits, k=10)) |
| 65 | + |
| 66 | +kb = JSONKnowledgeBase( |
| 67 | + path="cinemax.json", |
| 68 | + vector_db=PgVector2(collection="cinemax", db_url=vector_db.get_db_connection_local()), |
| 69 | +) |
| 70 | +storage = PgAssistantStorage( |
| 71 | + table_name="cinemax_assistant_storage", |
| 72 | + db_url=vector_db.get_db_connection_local(), |
| 73 | +) |
| 74 | + |
| 75 | +my_groq = OpenAILike( |
| 76 | + model="mixtral-8x7b-32768", |
| 77 | + api_key=os.environ["GROQ_API_KEY"], |
| 78 | + base_url="http://localhost:8000/proxy/groq/v1" |
| 79 | + # base_url="http://groqcall.ai/proxy/groq/v1" |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +def cinemax_assistant(new: bool = False, user: str = "user"): |
| 84 | + run_id: Optional[str] = None |
| 85 | + # new = False |
| 86 | + # new = True |
| 87 | + user_id = user |
| 88 | + |
| 89 | + if not new: |
| 90 | + existing_run_ids: List[str] = storage.get_all_run_ids(user_id) |
| 91 | + if len(existing_run_ids) > 0: |
| 92 | + run_id = existing_run_ids[0] |
| 93 | + |
| 94 | + assistant = Assistant( |
| 95 | + run_id=run_id, |
| 96 | + user_id="test_user", |
| 97 | + llm=my_groq, |
| 98 | + knowledge_base=kb, |
| 99 | + storage=storage, |
| 100 | + use_tools=True, |
| 101 | + add_chat_history_to_messages=True, |
| 102 | + tools=[CinemaTools(EmailTools("YOUR_EMAIL_ADDRESS", "SENDER_NAME", "SENDER_EMAIL", os.environ['email_pass_key'] ))], show_tool_calls=True, markdown=True |
| 103 | + # add_references_to_prompt=True, |
| 104 | + ) |
| 105 | + assistant.knowledge_base.load(recreate=False) |
| 106 | + |
| 107 | + if run_id is None: |
| 108 | + run_id = assistant.run_id |
| 109 | + print(f"Started Run: {run_id}\n") |
| 110 | + else: |
| 111 | + print(f"Continuing Run: {run_id}\n") |
| 112 | + |
| 113 | + while True: |
| 114 | + message = Prompt.ask(f"[bold] :sunglasses: {user} [/bold]") |
| 115 | + if message in ("exit", "bye"): |
| 116 | + break |
| 117 | + assistant.print_response(message, markdown=True, stream=False) |
| 118 | + # assistant.run(message, markdown=True, stream=False) |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + cinemax_assistant(user="Tom") |
| 122 | + |
| 123 | + |
0 commit comments