Browse Source

Fix: defined a better error handling logic

llamatest 1 month ago
parent
commit
d96cce66e8
1 changed files with 308 additions and 111 deletions
  1. 308 111
      end-to-end-use-cases/technical_blogger/Technical_Blog_Generator.ipynb

+ 308 - 111
end-to-end-use-cases/technical_blogger/Technical_Blog_Generator.ipynb

@@ -19,7 +19,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 18,
+   "execution_count": 60,
    "id": "0930f7de",
    "metadata": {},
    "outputs": [],
@@ -58,7 +58,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": null,
+   "execution_count": 65,
    "id": "a9030dae",
    "metadata": {},
    "outputs": [],
@@ -98,7 +98,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 14,
+   "execution_count": 62,
    "id": "87383cf5",
    "metadata": {},
    "outputs": [],
@@ -169,7 +169,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 15,
+   "execution_count": 63,
    "id": "437395c5",
    "metadata": {},
    "outputs": [],
@@ -259,8 +259,8 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 17,
-   "id": "340f6320",
+   "execution_count": null,
+   "id": "43d9b978",
    "metadata": {},
    "outputs": [
     {
@@ -288,155 +288,171 @@
        "\n",
        "### Introduction\n",
        "\n",
-       "In this blog post, we'll explore the process of building a Llama 3 enabled Messenger chatbot using the Messenger Platform. We'll cover the architectural components, setup instructions, and best practices for integrating Llama 3 with the Messenger Platform.\n",
+       "In this blog post, we'll explore the process of building a Llama 3 enabled Messenger chatbot using the Messenger Platform. We'll cover the architecture, setup instructions, and best practices for integrating Llama 3 with the Messenger Platform.\n",
        "\n",
        "### Overview of the Messenger Platform\n",
        "\n",
-       "The Messenger Platform is a powerful tool that allows businesses to connect with their customers through a Facebook business page. By integrating Llama 3 with the Messenger Platform, businesses can create intelligent and knowledgeable chatbots that provide 24x7 customer support, improving customer experience and reducing costs.\n",
+       "The Messenger Platform is a powerful tool that allows businesses to connect with their customers through a Facebook business page. With the Messenger Platform, businesses can build chatbots that can respond to customer inquiries, provide support, and even offer personalized recommendations.\n",
        "\n",
-       "### Architectural Components\n",
+       "### Architecture of the Llama 3 Enabled Messenger Chatbot\n",
+       "\n",
+       "The diagram below illustrates the components and overall data flow of the Llama 3 enabled Messenger chatbot demo.\n",
        "\n",
-       "The diagram below illustrates the components and overall data flow of the Llama 3 enabled Messenger chatbot demo:\n",
        "```markdown\n",
        "+---------------+\n",
        "|  Facebook    |\n",
        "|  Business Page  |\n",
        "+---------------+\n",
-       "       |\n",
-       "       |\n",
-       "       v\n",
+       "        |\n",
+       "        |  (User Message)\n",
+       "        v\n",
        "+---------------+\n",
        "|  Messenger    |\n",
        "|  Platform      |\n",
        "+---------------+\n",
-       "       |\n",
-       "       |\n",
-       "       v\n",
+       "        |\n",
+       "        |  (Webhook Event)\n",
+       "        v\n",
+       "+---------------+\n",
+       "|  Web Server    |\n",
+       "|  (e.g., Amazon  |\n",
+       "|   EC2 instance)  |\n",
+       "+---------------+\n",
+       "        |\n",
+       "        |  (API Request)\n",
+       "        v\n",
+       "+---------------+\n",
+       "|  Llama 3       |\n",
+       "|  Model          |\n",
+       "+---------------+\n",
+       "        |\n",
+       "        |  (Generated Response)\n",
+       "        v\n",
+       "+---------------+\n",
+       "|  Web Server    |\n",
+       "|  (e.g., Amazon  |\n",
+       "|   EC2 instance)  |\n",
        "+---------------+\n",
-       "|  Webhook       |\n",
-       "|  (Amazon EC2)  |\n",
+       "        |\n",
+       "        |  (API Response)\n",
+       "        v\n",
        "+---------------+\n",
-       "       |\n",
-       "       |\n",
-       "       v\n",
+       "|  Messenger    |\n",
+       "|  Platform      |\n",
        "+---------------+\n",
-       "|  Llama 3      |\n",
-       "|  Chatbot       |\n",
+       "        |\n",
+       "        |  (Bot Response)\n",
+       "        v\n",
+       "+---------------+\n",
+       "|  Facebook    |\n",
+       "|  Business Page  |\n",
        "+---------------+\n",
        "```\n",
-       "The components include:\n",
        "\n",
-       "*   Facebook Business Page: The business page that customers interact with.\n",
-       "*   Messenger Platform: The platform that enables businesses to connect with customers.\n",
-       "*   Webhook (Amazon EC2): The web server that handles incoming requests from the Messenger Platform and sends responses back.\n",
-       "*   Llama 3 Chatbot: The intelligent chatbot powered by Llama 3 that generates responses to customer queries.\n",
+       "The architecture consists of the following components:\n",
        "\n",
-       "### Setting Up the Messenger Chatbot\n",
+       "*   Facebook Business Page: The page where customers interact with the chatbot.\n",
+       "*   Messenger Platform: The platform that handles user messages and sends webhook events to the web server.\n",
+       "*   Web Server: The server that receives webhook events from the Messenger Platform, sends API requests to the Llama 3 model, and returns API responses to the Messenger Platform.\n",
+       "*   Llama 3 Model: The AI model that generates responses to user messages.\n",
        "\n",
-       "To build a Llama 3 enabled Messenger chatbot, follow these steps:\n",
+       "### Setting Up the Messenger Chatbot\n",
        "\n",
-       "#### Step 1: Create a Facebook Business Page\n",
+       "To set up the Messenger chatbot, follow these steps:\n",
        "\n",
-       "1.  Go to the Facebook Business Page creation page and follow the instructions to create a new business page.\n",
-       "2.  Set up the page with the required information, including the page name, category, and description.\n",
+       "1.  **Create a Facebook Business Page**: Create a Facebook business page for your business.\n",
+       "2.  **Create a Facebook Developer Account**: Create a Facebook developer account and register your application.\n",
+       "3.  **Set Up the Messenger Platform**: Set up the Messenger Platform for your application and configure the webhook settings.\n",
+       "4.  **Set Up the Web Server**: Set up a web server (e.g., Amazon EC2 instance) to receive webhook events from the Messenger Platform.\n",
+       "5.  **Integrate with Llama 3**: Integrate the Llama 3 model with your web server to generate responses to user messages.\n",
        "\n",
-       "#### Step 2: Set Up the Messenger Platform\n",
+       "### Configuring the Webhook\n",
        "\n",
-       "1.  Go to the [Messenger Platform](https://developers.facebook.com/docs/messenger-platform/overview) documentation and follow the instructions to set up a new Messenger app.\n",
-       "2.  Create a new app and configure the Messenger settings, including the webhook and messaging permissions.\n",
+       "To configure the webhook, follow these steps:\n",
        "\n",
-       "#### Step 3: Configure the Webhook\n",
+       "1.  Go to the Facebook Developer Dashboard and navigate to the Messenger Platform settings.\n",
+       "2.  Click on \"Webhooks\" and then click on \"Add Subscription\".\n",
+       "3.  Enter the URL of your web server and select the \"messages\" and \"messaging_postbacks\" events.\n",
+       "4.  Verify the webhook by clicking on \"Verify\" and entering the verification token.\n",
        "\n",
-       "1.  Set up an Amazon EC2 instance to host the webhook.\n",
-       "2.  Configure the webhook to receive incoming requests from the Messenger Platform.\n",
-       "3.  Use a secure connection (HTTPS) to ensure the integrity of the data exchanged between the Messenger Platform and the webhook.\n",
+       "### Handling Webhook Events\n",
        "\n",
-       "Here's an example of how to configure the webhook using Node.js and Express:\n",
-       "```javascript\n",
-       "const express = require('express');\n",
-       "const app = express();\n",
+       "To handle webhook events, you'll need to write code that processes the events and sends API requests to the Llama 3 model. Here's an example code snippet in Python:\n",
+       "```python\n",
+       "import os\n",
+       "import json\n",
+       "from flask import Flask, request\n",
+       "import requests\n",
        "\n",
-       "// Verify the webhook\n",
-       "app.get('/webhook', (req, res) => {\n",
-       "  const mode = req.query['hub.mode'];\n",
-       "  const token = req.query['hub.verify_token'];\n",
-       "  const challenge = req.query['hub.challenge'];\n",
+       "app = Flask(__name__)\n",
        "\n",
-       "  if (mode && token) {\n",
-       "    if (mode === 'subscribe' && token === 'YOUR_VERIFY_TOKEN') {\n",
-       "      console.log('WEBHOOK_VERIFIED');\n",
-       "      res.status(200).send(challenge);\n",
-       "    } else {\n",
-       "      res.sendStatus(403);\n",
-       "    }\n",
-       "  }\n",
-       "});\n",
-       "\n",
-       "// Handle incoming requests\n",
-       "app.post('/webhook', (req, res) => {\n",
-       "  const data = req.body;\n",
-       "\n",
-       "  // Process the incoming request\n",
-       "  if (data.object === 'page') {\n",
-       "    data.entry.forEach((entry) => {\n",
-       "      entry.messaging.forEach((event) => {\n",
-       "        if (event.message) {\n",
-       "          // Handle the message\n",
-       "          handleMessage(event);\n",
-       "        }\n",
-       "      });\n",
-       "    });\n",
-       "  }\n",
-       "\n",
-       "  res.status(200).send('EVENT_RECEIVED');\n",
-       "});\n",
-       "```\n",
-       "#### Step 4: Integrate Llama 3 with the Webhook\n",
+       "# Llama 3 API endpoint\n",
+       "LLAMA_API_ENDPOINT = os.environ['LLAMA_API_ENDPOINT']\n",
        "\n",
-       "1.  Use the Llama 3 API to generate responses to customer queries.\n",
-       "2.  Integrate the Llama 3 API with the webhook to send responses back to the Messenger Platform.\n",
+       "# Verify the webhook\n",
+       "@app.route('/webhook', methods=['GET'])\n",
+       "def verify_webhook():\n",
+       "    mode = request.args.get('mode')\n",
+       "    token = request.args.get('token')\n",
+       "    challenge = request.args.get('challenge')\n",
        "\n",
-       "Here's an example of how to integrate Llama 3 with the webhook using Python:\n",
-       "```python\n",
-       "import requests\n",
+       "    if mode == 'subscribe' and token == 'YOUR_VERIFY_TOKEN':\n",
+       "        return challenge\n",
+       "    else:\n",
+       "        return 'Invalid request', 403\n",
        "\n",
-       "def handle_message(event):\n",
-       "    # Get the message text\n",
-       "    message_text = event['message']['text']\n",
+       "# Handle webhook events\n",
+       "@app.route('/webhook', methods=['POST'])\n",
+       "def handle_webhook():\n",
+       "    data = request.get_json()\n",
+       "    if data['object'] == 'page':\n",
+       "        for entry in data['entry']:\n",
+       "            for messaging_event in entry['messaging']:\n",
+       "                if messaging_event.get('message'):\n",
+       "                    # Get the user message\n",
+       "                    user_message = messaging_event['message']['text']\n",
        "\n",
-       "    # Generate a response using Llama 3\n",
-       "    response = generate_response(message_text)\n",
+       "                    # Send API request to Llama 3 model\n",
+       "                    response = requests.post(LLAMA_API_ENDPOINT, json={'prompt': user_message})\n",
        "\n",
-       "    # Send the response back to the Messenger Platform\n",
-       "    send_response(event['sender']['id'], response)\n",
+       "                    # Get the generated response\n",
+       "                    generated_response = response.json()['response']\n",
        "\n",
-       "def generate_response(message_text):\n",
-       "    # Use the Llama 3 API to generate a response\n",
-       "    llama3_api_url = 'https://api.llama3.com/generate'\n",
-       "    headers = {'Authorization': 'Bearer YOUR_LLAMA3_API_KEY'}\n",
-       "    data = {'text': message_text}\n",
+       "                    # Send API response back to Messenger Platform\n",
+       "                    send_response(messaging_event['sender']['id'], generated_response)\n",
        "\n",
-       "    response = requests.post(llama3_api_url, headers=headers, json=data)\n",
-       "    return response.json()['response']\n",
+       "    return 'OK', 200\n",
        "\n",
+       "# Send response back to Messenger Platform\n",
        "def send_response(recipient_id, response):\n",
-       "    # Send the response back to the Messenger Platform\n",
-       "    messenger_api_url = 'https://graph.facebook.com/v13.0/me/messages'\n",
-       "    headers = {'Authorization': 'Bearer YOUR_MESSENGER_API_KEY'}\n",
-       "    data = {'recipient': {'id': recipient_id}, 'message': {'text': response}}\n",
+       "    # Set up the API endpoint and access token\n",
+       "    endpoint = f'https://graph.facebook.com/v13.0/me/messages?access_token={os.environ[\"PAGE_ACCESS_TOKEN\"]}'\n",
+       "\n",
+       "    # Set up the API request payload\n",
+       "    payload = {\n",
+       "        'recipient': {'id': recipient_id},\n",
+       "        'message': {'text': response}\n",
+       "    }\n",
+       "\n",
+       "    # Send the API request\n",
+       "    requests.post(endpoint, json=payload)\n",
        "\n",
-       "    requests.post(messenger_api_url, headers=headers, json=data)\n",
+       "if __name__ == '__main__':\n",
+       "    app.run(debug=True)\n",
        "```\n",
+       "\n",
        "### Best Practices\n",
        "\n",
-       "1.  **Use a secure connection**: Ensure that the webhook uses a secure connection (HTTPS) to protect the data exchanged between the Messenger Platform and the webhook.\n",
-       "2.  **Validate incoming requests**: Validate incoming requests from the Messenger Platform to prevent unauthorized access.\n",
-       "3.  **Handle errors**: Handle errors and exceptions properly to prevent crashes and ensure a smooth user experience.\n",
-       "4.  **Test thoroughly**: Test the chatbot thoroughly to ensure that it works as expected and provides accurate responses.\n",
+       "Here are some best practices to keep in mind when building a Messenger chatbot with Llama 3:\n",
+       "\n",
+       "*   **Test thoroughly**: Test your chatbot thoroughly to ensure that it responds correctly to user messages.\n",
+       "*   **Use a robust web server**: Use a robust web server that can handle a high volume of webhook events.\n",
+       "*   **Implement error handling**: Implement error handling to handle cases where the Llama 3 model fails to generate a response.\n",
+       "*   **Monitor performance**: Monitor the performance of your chatbot to ensure that it's responding quickly to user messages.\n",
        "\n",
        "### Conclusion\n",
        "\n",
-       "Building a Llama 3 enabled Messenger chatbot requires careful planning, setup, and integration with the Messenger Platform. By following the steps outlined in this blog post, businesses can create intelligent and knowledgeable chatbots that provide 24x7 customer support, improving customer experience and reducing costs."
+       "Building a Messenger chatbot with Llama 3 is a powerful way to provide customer support and improve customer experience. By following the steps outlined in this blog post, you can build a chatbot that responds to user messages and provides personalized recommendations. Remember to test thoroughly, use a robust web server, implement error handling, and monitor performance to ensure that your chatbot is successful."
       ],
       "text/plain": [
        "<IPython.core.display.Markdown object>"
@@ -444,13 +460,194 @@
      },
      "metadata": {},
      "output_type": "display_data"
+    },
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "# Building a Messenger Chatbot with Llama 3\n",
+      "\n",
+      "Building a Messenger Chatbot with Llama 3: A Step-by-Step Guide\n",
+      "===========================================================\n",
+      "\n",
+      "### Introduction\n",
+      "\n",
+      "In this blog post, we'll explore the process of building a Llama 3 enabled Messenger chatbot using the Messenger Platform. We'll cover the architecture, setup instructions, and best practices for integrating Llama 3 with the Messenger Platform.\n",
+      "\n",
+      "### Overview of the Messenger Platform\n",
+      "\n",
+      "The Messenger Platform is a powerful tool that allows businesses to connect with their customers through a Facebook business page. With the Messenger Platform, businesses can build chatbots that can respond to customer inquiries, provide support, and even offer personalized recommendations.\n",
+      "\n",
+      "### Architecture of the Llama 3 Enabled Messenger Chatbot\n",
+      "\n",
+      "The diagram below illustrates the components and overall data flow of the Llama 3 enabled Messenger chatbot demo.\n",
+      "\n",
+      "```markdown\n",
+      "+---------------+\n",
+      "|  Facebook    |\n",
+      "|  Business Page  |\n",
+      "+---------------+\n",
+      "        |\n",
+      "        |  (User Message)\n",
+      "        v\n",
+      "+---------------+\n",
+      "|  Messenger    |\n",
+      "|  Platform      |\n",
+      "+---------------+\n",
+      "        |\n",
+      "        |  (Webhook Event)\n",
+      "        v\n",
+      "+---------------+\n",
+      "|  Web Server    |\n",
+      "|  (e.g., Amazon  |\n",
+      "|   EC2 instance)  |\n",
+      "+---------------+\n",
+      "        |\n",
+      "        |  (API Request)\n",
+      "        v\n",
+      "+---------------+\n",
+      "|  Llama 3       |\n",
+      "|  Model          |\n",
+      "+---------------+\n",
+      "        |\n",
+      "        |  (Generated Response)\n",
+      "        v\n",
+      "+---------------+\n",
+      "|  Web Server    |\n",
+      "|  (e.g., Amazon  |\n",
+      "|   EC2 instance)  |\n",
+      "+---------------+\n",
+      "        |\n",
+      "        |  (API Response)\n",
+      "        v\n",
+      "+---------------+\n",
+      "|  Messenger    |\n",
+      "|  Platform      |\n",
+      "+---------------+\n",
+      "        |\n",
+      "        |  (Bot Response)\n",
+      "        v\n",
+      "+---------------+\n",
+      "|  Facebook    |\n",
+      "|  Business Page  |\n",
+      "+---------------+\n",
+      "```\n",
+      "\n",
+      "The architecture consists of the following components:\n",
+      "\n",
+      "*   Facebook Business Page: The page where customers interact with the chatbot.\n",
+      "*   Messenger Platform: The platform that handles user messages and sends webhook events to the web server.\n",
+      "*   Web Server: The server that receives webhook events from the Messenger Platform, sends API requests to the Llama 3 model, and returns API responses to the Messenger Platform.\n",
+      "*   Llama 3 Model: The AI model that generates responses to user messages.\n",
+      "\n",
+      "### Setting Up the Messenger Chatbot\n",
+      "\n",
+      "To set up the Messenger chatbot, follow these steps:\n",
+      "\n",
+      "1.  **Create a Facebook Business Page**: Create a Facebook business page for your business.\n",
+      "2.  **Create a Facebook Developer Account**: Create a Facebook developer account and register your application.\n",
+      "3.  **Set Up the Messenger Platform**: Set up the Messenger Platform for your application and configure the webhook settings.\n",
+      "4.  **Set Up the Web Server**: Set up a web server (e.g., Amazon EC2 instance) to receive webhook events from the Messenger Platform.\n",
+      "5.  **Integrate with Llama 3**: Integrate the Llama 3 model with your web server to generate responses to user messages.\n",
+      "\n",
+      "### Configuring the Webhook\n",
+      "\n",
+      "To configure the webhook, follow these steps:\n",
+      "\n",
+      "1.  Go to the Facebook Developer Dashboard and navigate to the Messenger Platform settings.\n",
+      "2.  Click on \"Webhooks\" and then click on \"Add Subscription\".\n",
+      "3.  Enter the URL of your web server and select the \"messages\" and \"messaging_postbacks\" events.\n",
+      "4.  Verify the webhook by clicking on \"Verify\" and entering the verification token.\n",
+      "\n",
+      "### Handling Webhook Events\n",
+      "\n",
+      "To handle webhook events, you'll need to write code that processes the events and sends API requests to the Llama 3 model. Here's an example code snippet in Python:\n",
+      "```python\n",
+      "import os\n",
+      "import json\n",
+      "from flask import Flask, request\n",
+      "import requests\n",
+      "\n",
+      "app = Flask(__name__)\n",
+      "\n",
+      "# Llama 3 API endpoint\n",
+      "LLAMA_API_ENDPOINT = os.environ['LLAMA_API_ENDPOINT']\n",
+      "\n",
+      "# Verify the webhook\n",
+      "@app.route('/webhook', methods=['GET'])\n",
+      "def verify_webhook():\n",
+      "    mode = request.args.get('mode')\n",
+      "    token = request.args.get('token')\n",
+      "    challenge = request.args.get('challenge')\n",
+      "\n",
+      "    if mode == 'subscribe' and token == 'YOUR_VERIFY_TOKEN':\n",
+      "        return challenge\n",
+      "    else:\n",
+      "        return 'Invalid request', 403\n",
+      "\n",
+      "# Handle webhook events\n",
+      "@app.route('/webhook', methods=['POST'])\n",
+      "def handle_webhook():\n",
+      "    data = request.get_json()\n",
+      "    if data['object'] == 'page':\n",
+      "        for entry in data['entry']:\n",
+      "            for messaging_event in entry['messaging']:\n",
+      "                if messaging_event.get('message'):\n",
+      "                    # Get the user message\n",
+      "                    user_message = messaging_event['message']['text']\n",
+      "\n",
+      "                    # Send API request to Llama 3 model\n",
+      "                    response = requests.post(LLAMA_API_ENDPOINT, json={'prompt': user_message})\n",
+      "\n",
+      "                    # Get the generated response\n",
+      "                    generated_response = response.json()['response']\n",
+      "\n",
+      "                    # Send API response back to Messenger Platform\n",
+      "                    send_response(messaging_event['sender']['id'], generated_response)\n",
+      "\n",
+      "    return 'OK', 200\n",
+      "\n",
+      "# Send response back to Messenger Platform\n",
+      "def send_response(recipient_id, response):\n",
+      "    # Set up the API endpoint and access token\n",
+      "    endpoint = f'https://graph.facebook.com/v13.0/me/messages?access_token={os.environ[\"PAGE_ACCESS_TOKEN\"]}'\n",
+      "\n",
+      "    # Set up the API request payload\n",
+      "    payload = {\n",
+      "        'recipient': {'id': recipient_id},\n",
+      "        'message': {'text': response}\n",
+      "    }\n",
+      "\n",
+      "    # Send the API request\n",
+      "    requests.post(endpoint, json=payload)\n",
+      "\n",
+      "if __name__ == '__main__':\n",
+      "    app.run(debug=True)\n",
+      "```\n",
+      "\n",
+      "### Best Practices\n",
+      "\n",
+      "Here are some best practices to keep in mind when building a Messenger chatbot with Llama 3:\n",
+      "\n",
+      "*   **Test thoroughly**: Test your chatbot thoroughly to ensure that it responds correctly to user messages.\n",
+      "*   **Use a robust web server**: Use a robust web server that can handle a high volume of webhook events.\n",
+      "*   **Implement error handling**: Implement error handling to handle cases where the Llama 3 model fails to generate a response.\n",
+      "*   **Monitor performance**: Monitor the performance of your chatbot to ensure that it's responding quickly to user messages.\n",
+      "\n",
+      "### Conclusion\n",
+      "\n",
+      "Building a Messenger chatbot with Llama 3 is a powerful way to provide customer support and improve customer experience. By following the steps outlined in this blog post, you can build a chatbot that responds to user messages and provides personalized recommendations. Remember to test thoroughly, use a robust web server, implement error handling, and monitor performance to ensure that your chatbot is successful.\n"
+     ]
     }
    ],
    "source": [
+    "# Specify the topic for the blog post\n",
     "topic = \"Building a Messenger Chatbot with Llama 3\"\n",
-    "blog_content = generate_blog(topic)\n",
-    "if isinstance(blog_content, str) and \"Error\" in blog_content:\n",
-    "    print(blog_content)"
+    "try:\n",
+    "    blog_content = generate_blog(topic)\n",
+    "    print(blog_content)\n",
+    "except Exception as e:\n",
+    "    print(e)"
    ]
   }
  ],