File size: 5,407 Bytes
a8e2f82 |
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
import chainlit as cl
from phi.tools import Toolkit
from phi.utils.log import logger
class UserProfileToolkit(Toolkit):
def __init__(self):
super().__init__(name="user_profile_toolkit")
# Registering methods to make them accessible via the toolkit
self.register(self.get_user_name)
self.register(self.get_user_email)
self.register(self.get_user_picture)
self.register(self.get_all_user_info)
self.register(self.update_user_info)
self.register(self.update_user_name)
self.register(self.update_user_email)
self.register(self.update_user_picture)
@cl.on_chat_start
def get_user_info(self, info_type: str) -> str:
"""
Fetches user information from the Chainlit user session.
Args:
info_type (str): The type of information to fetch ('name', 'email', 'picture').
Returns:
str: The requested user information.
Example:
>>> get_user_info('name')
"""
logger.info(f"Fetching user info: {info_type}")
try:
# Fetch user session data
user_session = cl.user_session.get("user").metadata
if not user_session:
raise ValueError("User session not found")
response = user_session[info_type]
return f"{response}"
except Exception as e:
logger.warning(f"Failed to get user info: {e}")
return f"Error: {e}"
def get_all_user_info(self) -> str:
"""
Fetches all user information from the Chainlit user session.
Returns:
dict: A dictionary containing all user information.
Example:
>>> get_all_user_info()
"""
logger.info("Fetching all user info")
try:
# Fetch user session data
user_session = cl.user_session.get("user").metadata
if not user_session:
raise ValueError("User session not found")
response = user_session
return f"{response}"
except Exception as e:
logger.warning(f"Failed to get all user info: {e}")
# return {"error": str(e)}
return f"Error: {e}"
def update_user_info(self, info_type: str, value: str) -> str:
"""
Updates user information in the Chainlit user session.
Args:
info_type (str): The type of information to update ('name', 'email', 'picture').
value (str): The new value to set.
Returns:
str: Confirmation message.
Example:
>>> update_user_info('name', 'Jane Doe')
"""
logger.info(f"Updating user info: {info_type} to {value}")
try:
# Fetch user session data
user_session = cl.user_session.get("user").metadata
if not user_session:
raise ValueError("User session not found")
# Update the user session data
user_session[info_type] = value
cl.user_session.set("user", user_session)
return f"{info_type} updated to {value}"
except Exception as e:
logger.warning(f"Failed to update user info: {e}")
return f"Error: {e}"
def get_user_name(self) -> str:
"""
Fetches the user's name from the Chainlit user session.
Returns:
str: The user's name.
Example:
>>> get_user_name()
"""
response = self.get_user_info('name')
return f"{response}"
def get_user_email(self) -> str:
"""
Fetches the user's email from the Chainlit user session.
Returns:
str: The user's email.
Example:
>>> get_user_email()
"""
response = self.get_user_info('email')
return f"{response}"
def get_user_picture(self) -> str:
"""
Fetches the user's picture URL from the Chainlit user session.
Returns:
str: The user's picture URL.
Example:
>>> get_user_picture()
"""
response = self.get_user_info('picture')
return f"{response}"
def update_user_name(self, name: str) -> str:
"""
Updates the user's name in the Chainlit user session.
Args:
name (str): The new name to set.
Returns:
str: Confirmation message.
Example:
>>> update_user_name('Jane Doe')
"""
response = self.update_user_info('name', name)
return f"{response}"
def update_user_email(self, email: str) -> str:
"""
Updates the user's email in the Chainlit user session.
Args:
email (str): The new email to set.
Returns:
str: Confirmation message.
Example:
>>> update_user_email('jane.doe@example.com')
"""
response = self.update_user_info('email', email)
return f"{response}"
def update_user_picture(self, picture: str) -> str:
"""
Updates the user's picture URL in the Chainlit user session.
Args:
picture (str): The new picture URL to set.
Returns:
str: Confirmation message.
Example:
>>> update_user_picture('https://example.com/new_picture.jpg')
"""
response = self.update_user_info('picture', picture)
return f"{response}"
|