File size: 3,364 Bytes
a85c9b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e9a9dc6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from embedchain import App\n",
    "\n",
    "embedchain_docs_bot = App()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c1c24d68",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "All data from https://docs.embedchain.ai/ already exists in the database.\n"
     ]
    }
   ],
   "source": [
    "embedchain_docs_bot.add(\"docs_site\", \"https://docs.embedchain.ai/\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "48cdaecf",
   "metadata": {},
   "outputs": [],
   "source": [
    "answer = embedchain_docs_bot.query(\"Write a flask API for embedchain bot\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0fe18085",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "To write a Flask API for the embedchain bot, you can use the following code snippet:\n",
       "\n",
       "```python\n",
       "from flask import Flask, request, jsonify\n",
       "from embedchain import App\n",
       "\n",
       "app = Flask(__name__)\n",
       "bot = App()\n",
       "\n",
       "# Add datasets to the bot\n",
       "bot.add(\"youtube_video\", \"https://www.youtube.com/watch?v=3qHkcs3kG44\")\n",
       "bot.add(\"pdf_file\", \"https://navalmanack.s3.amazonaws.com/Eric-Jorgenson_The-Almanack-of-Naval-Ravikant_Final.pdf\")\n",
       "\n",
       "@app.route('/query', methods=['POST'])\n",
       "def query():\n",
       "    data = request.get_json()\n",
       "    question = data['question']\n",
       "    response = bot.query(question)\n",
       "    return jsonify({'response': response})\n",
       "\n",
       "if __name__ == '__main__':\n",
       "    app.run()\n",
       "```\n",
       "\n",
       "In this code, we create a Flask app and initialize an instance of the embedchain bot. We then add the desired datasets to the bot using the `add()` function.\n",
       "\n",
       "Next, we define a route `/query` that accepts POST requests. The request body should contain a JSON object with a `question` field. The bot's `query()` function is called with the provided question, and the response is returned as a JSON object.\n",
       "\n",
       "Finally, we run the Flask app using `app.run()`.\n",
       "\n",
       "Note: Make sure to install Flask and embedchain packages before running this code."
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from IPython.display import Markdown\n",
    "# Create a Markdown object and display it\n",
    "markdown_answer = Markdown(answer)\n",
    "display(markdown_answer)"
   ]
  }
 ],
 "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.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}