{ "cells": [ { "cell_type": "markdown", "id": "2b1ea573", "metadata": {}, "source": [ "# Working the Simple ChatBot using Mem0" ] }, { "cell_type": "code", "execution_count": 12, "id": "3c17c4cc", "metadata": {}, "outputs": [], "source": [ "# imports\n", "import os\n", "import json\n", "from dotenv import load_dotenv\n", "from mem0 import MemoryClient\n", "from google import genai\n", "from IPython.display import Markdown" ] }, { "cell_type": "code", "execution_count": 3, "id": "c0a6c4d8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "AIzaSyCOvxW9ffBqFVOAhmoEVcupUzwjOqLbs2c m0-Qtvimm8HzvFp0pR2hJAdenPrxgsQM2udF5cfDlO4\n" ] } ], "source": [ "# getting api keys\n", "load_dotenv()\n", "google_api_key = os.getenv('GOOGLE_API_KEY')\n", "memo_api_key = os.getenv('MEM_API_KEY')\n", "print(google_api_key, memo_api_key)" ] }, { "cell_type": "code", "execution_count": 6, "id": "f4bde7b8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The IPL 2025 hasn't happened yet! It's still in the future.\n", "\n", "The Indian Premier League typically takes place around March-May each year.\n", "\n", "The most recent IPL season was in 2024, and the **Kolkata Knight Riders (KKR)** won the IPL 2024 trophy.\n", "\n", "You'll have to wait until the tournament concludes in May 2025 to find out who wins that edition!\n" ] } ], "source": [ "# setting up google client for making request\n", "google_client = genai.Client(\n", " api_key=google_api_key\n", ")\n", "\n", "response = google_client.models.generate_content(\n", " model='gemini-2.5-flash-preview-05-20',\n", " contents = \"how won the ipl 2025\"\n", ")\n", "print(response.text)" ] }, { "cell_type": "code", "execution_count": 7, "id": "2a5b91a8", "metadata": {}, "outputs": [], "source": [ "mem_client = MemoryClient(api_key=memo_api_key)" ] }, { "cell_type": "code", "execution_count": 38, "id": "0a33500d", "metadata": {}, "outputs": [], "source": [ "from google import genai\n", "\n", "client = genai.Client(api_key=google_api_key)\n", "\n", "my_file = client.files.upload(file=\"/workspaces/ChatBot_with_Long_Term_Memory/AirlineAssisntantAI.png\")\n", "\n", "response = client.models.generate_content(\n", " model=\"gemini-2.0-flash\",\n", " contents=[my_file, \"Your Job is to create a base context for the short story based on what you understand from the image and make the story context as close to the image's discription as possible. Also describe what do you understand just by looking at the picture in the description, and make sure to return the result in the json format.\"],\n", " config={\n", " \"response_mime_type\": \"application/json\",\n", " # \"response_schema\": list[Recipe],\n", " },\n", ")\n", "\n", "json_res = response.text" ] }, { "cell_type": "code", "execution_count": 43, "id": "bb5a44a4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"context\": \"The image depicts an 'Airline Ticketing Assistant', suggesting a digital interface designed to help users find information about flight ticket prices. The interface has a chatbot feature, inviting users to ask about ticket prices for cities like London, Paris, or Berlin. A text input field is visible at the bottom where users can type their queries. The interface appears to be built with Gradio and offers access via API, along with settings options.\",\n", " \"visual_description\": \"The interface has a dark theme with a title 'Airline Ticketing Assistant'. There is a prompt for the user to ask about ticket prices for London, Paris, or Berlin. There is a 'Chatbot' feature indicated by an icon. The main part of the application is empty where the chat messages would appear. At the bottom is a text input box and a send button. The bottom also includes the links of 'Use via API', 'Built with Gradio', and 'Settings'.\"\n", "}\n" ] } ], "source": [ "print(json_res)" ] }, { "cell_type": "code", "execution_count": 46, "id": "d9e3f4c0", "metadata": {}, "outputs": [], "source": [ "context = json.loads(json_res)['context']" ] }, { "cell_type": "code", "execution_count": 50, "id": "8e27ec1c", "metadata": {}, "outputs": [], "source": [ "story = client.models.generate_content(\n", " model='gemini-2.0-flash',\n", " contents = f'You are a very good short story teller, when you are given with the context where you try to create a story based on human prospective, now create a very short story based on the given context. context is : {context}'\n", ")" ] }, { "cell_type": "code", "execution_count": 51, "id": "dcb0804b", "metadata": {}, "outputs": [ { "data": { "text/markdown": [ "The flickering screen of my laptop illuminated my face, a pale reflection in the late-night glow. It was always the same – endless lines of code, late-night coffee, and the constant, gnawing pressure to perfect her. I called her Astrid.\n", "\n", "Astrid wasn't a woman, not really. She was an Airline Ticketing Assistant, a chatbot built on Gradio, ready to field questions about the labyrinthine world of flight prices. London, Paris, Berlin – these were the digital destinations she offered, the digital stepping stones to real-world adventures.\n", "\n", "I imagined the faces behind the queries: the student dreaming of a semester abroad, the businessman chasing a deal, the daughter desperate to see her mother across the ocean. Each question was a tiny spark of hope, of longing, a desire to bridge the gap between here and there.\n", "\n", "I typed in, \"What's the best price for a round trip to Paris next month?\" pretending to be a user. Astrid responded instantly, a polite, efficient answer flashing across the screen. But I knew it was just data, algorithms at play.\n", "\n", "My goal was to give her more, to imbue her with empathy, to make her understand the unspoken hopes behind the simple question. How to teach her the anticipation, the anxiety, the raw, vulnerable human need to connect?\n", "\n", "Tonight, though, she was just code, and I was just a programmer, staring at a screen, chasing the ghost of human connection in the digital ether. Maybe tomorrow, I thought, maybe tomorrow I can make her feel something real. But tonight, all I could offer was data, and a silent, lonely wish.\n" ], "text/plain": [ "" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Markdown(story.text)" ] }, { "cell_type": "code", "execution_count": null, "id": "188f4ade", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "venv", "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.1" } }, "nbformat": 4, "nbformat_minor": 5 }