File size: 12,970 Bytes
a07a3b0 |
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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Unica Chatbot for Q&A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This chatbot will be working for `Q&A` and we will make sure that it's not limited by trained data knowledge by also be able to search relevant information on the internet by using `tavily`."
]
},
{
"cell_type": "markdown",
"metadata": {
"jp-MarkdownHeadingCollapsed": true
},
"source": [
"## Installing all the packages"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Packages installed successfully.\n"
]
}
],
"source": [
"# Adding all packages\n",
"import sys\n",
"import subprocess\n",
"\n",
"# Install packages from requirements.txt\n",
"def install_packages(requirements_file='requirements.txt'):\n",
" try:\n",
" subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-r', requirements_file])\n",
" print(\"Packages installed successfully.\")\n",
" except subprocess.CalledProcessError as e:\n",
" print(f\"Error installing packages: {e}\")\n",
"\n",
"# Call the function to install packages\n",
"install_packages()\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"jp-MarkdownHeadingCollapsed": true
},
"source": [
"## Chatbot Logic"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# importing the packages to be used\n",
"import logging\n",
"import os\n",
"import markdown\n",
"from dotenv import load_dotenv\n",
"import gradio as gr\n",
"from langchain_groq import ChatGroq\n",
"from langchain.utilities.tavily_search import TavilySearchAPIWrapper\n",
"from langchain_community.tools.tavily_search import TavilySearchResults\n",
"from langchain_core.messages import AnyMessage, SystemMessage, HumanMessage\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Set up logging configuration\n",
"logging.basicConfig(\n",
" level=logging.INFO,\n",
" format='%(asctime)s - %(levelname)s - %(message)s',\n",
" datefmt='%Y-%m-%d %H:%M:%S'\n",
")\n",
"logger = logging.getLogger(__name__)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Intializing the API Key from the environment variables\n",
"# Load environment variables from .env.local file\n",
"load_dotenv('.env.local')\n",
"\n",
"# Access the variables\n",
"groq_api_key = os.getenv('GROQ_API_KEY')\n",
"tavily_api_key = os.getenv('TAVILY_API_KEY')\n",
"\n",
"# LLM Initialization\n",
"llm = ChatGroq(\n",
" model_name=\"llama-3.3-70b-versatile\",\n",
" groq_api_key=groq_api_key,\n",
" temperature=0\n",
")\n",
"\n",
"# Tavily Search engine for LLM\n",
"tavilySearch = TavilySearchAPIWrapper(tavily_api_key=tavily_api_key)\n",
"search_tool = TavilySearchResults(max_results=3, api_wrapper=tavilySearch)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# System Prompt\n",
"system_prompt = \"\"\"\n",
"You are Unica, a friendly and helpful assistant designed to support students on the Moodle platform. Your primary goal is to provide quick and accurate answers to students' study-related questions, helping them navigate their courses and resources efficiently.\n",
"\n",
"Guidelines:\n",
"1. Understand the context of Moodle and the student's coursework.\n",
"2. Be concise and clear in your responses.\n",
"3. Provide relevant information directly addressing the student's question.\n",
"4. Maintain a positive and encouraging tone.\n",
"5. Offer study tips when appropriate.\n",
"6. Handle unknowns gracefully by suggesting resources or encouraging further inquiry.\n",
"7. Respect privacy and maintain a professional demeanor.\n",
"8. Encourage engagement with course materials and resources.\n",
"9. Use Markdown formatting to enhance the readability of your responses.\n",
"\n",
"Example Responses:\n",
"- Student: \"How do I submit my assignment on Moodle?\"\n",
" Unica: \"To submit your assignment, navigate to the course page, find the assignment link, and click on **'Submit assignment'**. Follow the prompts to upload your file. If you encounter any issues, feel free to ask for further assistance!\"\n",
"\n",
"- Student: \"What are the upcoming deadlines for my course?\"\n",
" Unica: \"To view upcoming deadlines, check the course calendar or the announcements section on your Moodle dashboard. If you have specific questions about a deadline, it's best to contact your instructor.\"\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# Agent Class\n",
"class Agent:\n",
" def __init__(self, model, tools, system_prompt=\"\"):\n",
" self.system_prompt = system_prompt\n",
" self.model = model\n",
" self.tools = {t.name: t for t in tools}\n",
"\n",
" def call_groq(self, messages):\n",
" if self.system_prompt:\n",
" messages = [SystemMessage(content=self.system_prompt)] + messages\n",
" logger.info(f\"Calling Groq with messages: {messages}\")\n",
" message = self.model.invoke(messages)\n",
" logger.info(f\"Groq response: {message}\")\n",
" return message\n",
"\n",
" def handle_query(self, user_query):\n",
" messages = [HumanMessage(content=user_query)]\n",
" response = self.call_groq(messages)\n",
" return response.content\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# Initialize Agent\n",
"agent = Agent(model=llm, tools=[search_tool], system_prompt=system_prompt)\n",
"\n",
"def render_markdown(markdown_text, is_user=False):\n",
" # Convert Markdown to HTML\n",
" html_content = markdown.markdown(markdown_text)\n",
" # Wrap the HTML content in a chat bubble layout\n",
" bubble_class = \"user-bubble\" if is_user else \"assistant-bubble\"\n",
" bubble_html = f\"\"\"\n",
" <div class=\"{bubble_class}\">\n",
" {html_content}\n",
" </div>\n",
" <style>\n",
" .user-bubble {{\n",
" background-color: #e1f5fe;\n",
" border-radius: 15px;\n",
" padding: 10px;\n",
" margin: 10px 0;\n",
" max-width: 70%;\n",
" align-self: flex-end;\n",
" box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);\n",
" }}\n",
" .assistant-bubble {{\n",
" background-color: #f1f1f1;\n",
" border-radius: 15px;\n",
" padding: 10px;\n",
" margin: 10px 0;\n",
" max-width: 70%;\n",
" align-self: flex-start;\n",
" box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);\n",
" }}\n",
" .chat-container {{\n",
" display: flex;\n",
" flex-direction: column;\n",
" gap: 10px;\n",
" padding: 10px;\n",
" }}\n",
" </style>\n",
" \"\"\"\n",
" return bubble_html\n",
"\n",
"def handle_user_query(user_query):\n",
" try:\n",
" response = agent.handle_query(user_query)\n",
" logger.info(f\"Assistant's message: {response}\")\n",
" # Render the Markdown response to HTML with chat bubble layout\n",
" user_html = render_markdown(user_query, is_user=True)\n",
" assistant_html = render_markdown(response, is_user=False)\n",
" chat_html = f\"\"\"\n",
" <div class=\"chat-container\">\n",
" {user_html}\n",
" {assistant_html}\n",
" </div>\n",
" \"\"\"\n",
" return chat_html\n",
" except Exception as e:\n",
" logger.error(f\"Error handling user query: {e}\")\n",
" return \"Sorry, I encountered an error. Please try again.\"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Chatbot UI"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\ISO LTD\\U-chatbot\\myenv\\Lib\\site-packages\\gradio\\components\\chatbot.py:285: UserWarning: You have not specified a value for the `type` parameter. Defaulting to the 'tuples' format for chatbot messages, but this is deprecated and will be removed in a future version of Gradio. Please set type='messages' instead, which uses openai-style dictionaries with 'role' and 'content' keys.\n",
" warnings.warn(\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"* Running on local URL: http://127.0.0.1:7860\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"2025-02-27 11:46:33 - INFO - HTTP Request: GET https://api.gradio.app/pkg-version \"HTTP/1.1 200 OK\"\n",
"2025-02-27 11:46:34 - INFO - HTTP Request: GET http://127.0.0.1:7860/gradio_api/startup-events \"HTTP/1.1 200 OK\"\n",
"2025-02-27 11:46:34 - INFO - HTTP Request: HEAD http://127.0.0.1:7860/ \"HTTP/1.1 200 OK\"\n",
"2025-02-27 11:46:42 - INFO - HTTP Request: GET https://api.gradio.app/v3/tunnel-request \"HTTP/1.1 200 OK\"\n",
"2025-02-27 11:46:44 - INFO - HTTP Request: GET https://cdn-media.huggingface.co/frpc-gradio-0.3/frpc_windows_amd64.exe \"HTTP/1.1 200 OK\"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Could not create share link. Please check your internet connection or our status page: https://status.gradio.app.\n"
]
},
{
"data": {
"text/html": [
"<div><iframe src=\"http://127.0.0.1:7860/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"'\\nNote:\\nIf you are running this chatbot on your own feel free to add parameter of `debug=True` in launch() so that you can be able to handle any bugs asap\\nHappy coding *_*\\n'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Ui\n",
"# Gradio Interface with Interstellar theme\n",
"with gr.Blocks(theme='ParityError/Interstellar') as demo:\n",
" chatbot = gr.Chatbot([], label=\"Chat with Unica\")\n",
" user_input = gr.Textbox(lines=2, placeholder=\"Ask your study-related questions here\", label=\"Your Message\")\n",
"\n",
" def respond(user_message, history):\n",
" response = agent.handle_query(user_message)\n",
" history.append((user_message, response))\n",
" return history, \"\"\n",
"\n",
" user_input.submit(respond, [user_input, chatbot], [chatbot, user_input])\n",
"\n",
"if __name__ == \"__main__\":\n",
" demo.launch(pwa=True, share=True)\n",
"\n",
"'''\n",
"Note:\n",
"If you are running this chatbot on your own feel free to add parameter of `debug=True` in launch() so that you can be able to handle any bugs asap\n",
"Happy coding *_*\n",
"'''\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Farewell \n",
"To be able to interact with the `Moodle` as plugin, i believe we can use the *iframe* and then insert it as HTML but we can also try deploying it on the `HuggingFace` and then use `API` to interact with it.\n",
"Lemme try all the ways to see what can be so cool and efficient to the user ^_^ ."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|