Spaces:
Sleeping
Sleeping
Commit
·
8b78e2a
1
Parent(s):
46dfa3e
updated notebooks: rag_chatbot, chatbot_agentic_prebuilt
Browse files
notebooks/chatbot_agentic_prebuilt.ipynb
CHANGED
|
@@ -17,23 +17,6 @@
|
|
| 17 |
"from langchain_core.tools import tool"
|
| 18 |
]
|
| 19 |
},
|
| 20 |
-
{
|
| 21 |
-
"cell_type": "code",
|
| 22 |
-
"execution_count": null,
|
| 23 |
-
"metadata": {},
|
| 24 |
-
"outputs": [],
|
| 25 |
-
"source": [
|
| 26 |
-
"@tool\n",
|
| 27 |
-
"def tool_mult(a: int, b: int) -> int:\n",
|
| 28 |
-
" 'Multiplies two given numbers'\n",
|
| 29 |
-
" return a * b\n",
|
| 30 |
-
"\n",
|
| 31 |
-
"@tool\n",
|
| 32 |
-
"def tool_add(a:int, b:int) -> int:\n",
|
| 33 |
-
" 'Adds two given numbers'\n",
|
| 34 |
-
" return a + b"
|
| 35 |
-
]
|
| 36 |
-
},
|
| 37 |
{
|
| 38 |
"cell_type": "code",
|
| 39 |
"execution_count": null,
|
|
@@ -49,8 +32,11 @@
|
|
| 49 |
" api_wrapper=WikipediaAPIWrapper(\n",
|
| 50 |
" top_k_results=2,\n",
|
| 51 |
" doc_content_chars_max=500))\n",
|
| 52 |
-
"\n",
|
| 53 |
-
"
|
|
|
|
|
|
|
|
|
|
| 54 |
"agent_executor = create_react_agent(\n",
|
| 55 |
" llm,\n",
|
| 56 |
" tools,\n",
|
|
@@ -66,19 +52,11 @@
|
|
| 66 |
"source": [
|
| 67 |
"# use the agent\n",
|
| 68 |
"config = {'configurable': {'thread_id': '1'}}\n",
|
| 69 |
-
"\n",
|
| 70 |
"user_input = input(\"User: \")\n",
|
| 71 |
-
"\n",
|
| 72 |
"for chunk in agent_executor.stream(\n",
|
| 73 |
" {\"messages\": [HumanMessage(content=user_input)]},\n",
|
| 74 |
" config):\n",
|
| 75 |
-
" print(chunk)
|
| 76 |
-
"\n",
|
| 77 |
-
"# response = agent_executor.invoke(\n",
|
| 78 |
-
"# input={\"messages\": [HumanMessage(content=user_input)]},\n",
|
| 79 |
-
"# config=config)\n",
|
| 80 |
-
"# for chunk in response['messages']:\n",
|
| 81 |
-
"# print(chunk)"
|
| 82 |
]
|
| 83 |
}
|
| 84 |
],
|
|
|
|
| 17 |
"from langchain_core.tools import tool"
|
| 18 |
]
|
| 19 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
{
|
| 21 |
"cell_type": "code",
|
| 22 |
"execution_count": null,
|
|
|
|
| 32 |
" api_wrapper=WikipediaAPIWrapper(\n",
|
| 33 |
" top_k_results=2,\n",
|
| 34 |
" doc_content_chars_max=500))\n",
|
| 35 |
+
"@tool\n",
|
| 36 |
+
"def tool_mult(a: int, b: int) -> int:\n",
|
| 37 |
+
" 'Multiplies two given numbers'\n",
|
| 38 |
+
" return a * b\n",
|
| 39 |
+
"tools = [tool_search, tool_finance, tool_wiki, tool_mult]\n",
|
| 40 |
"agent_executor = create_react_agent(\n",
|
| 41 |
" llm,\n",
|
| 42 |
" tools,\n",
|
|
|
|
| 52 |
"source": [
|
| 53 |
"# use the agent\n",
|
| 54 |
"config = {'configurable': {'thread_id': '1'}}\n",
|
|
|
|
| 55 |
"user_input = input(\"User: \")\n",
|
|
|
|
| 56 |
"for chunk in agent_executor.stream(\n",
|
| 57 |
" {\"messages\": [HumanMessage(content=user_input)]},\n",
|
| 58 |
" config):\n",
|
| 59 |
+
" print(chunk)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
]
|
| 61 |
}
|
| 62 |
],
|
notebooks/rag_chatbot.ipynb
CHANGED
|
@@ -1,19 +1,25 @@
|
|
| 1 |
{
|
| 2 |
"cells": [
|
| 3 |
-
{
|
| 4 |
-
"cell_type": "markdown",
|
| 5 |
-
"metadata": {
|
| 6 |
-
"id": "d0jr_Q-wl6-0"
|
| 7 |
-
},
|
| 8 |
-
"source": [
|
| 9 |
-
"# RAG - Chatbot"
|
| 10 |
-
]
|
| 11 |
-
},
|
| 12 |
{
|
| 13 |
"cell_type": "markdown",
|
| 14 |
"metadata": {},
|
| 15 |
"source": [
|
| 16 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
"\n",
|
| 18 |
"Note: the code snippets below have been copied and simplified from my original code [here](https://github.com/OnurKerimoglu/chat_with_docs/blob/main/src/rag.py), which is in turn deployed to HuggingFace space [here](https://huggingface.co/spaces/OnurKerimoglu/rag_chat), which may well be sleeping due to inactivity (don't hesitate to wake it up!)"
|
| 19 |
]
|
|
@@ -22,11 +28,8 @@
|
|
| 22 |
"cell_type": "markdown",
|
| 23 |
"metadata": {},
|
| 24 |
"source": [
|
| 25 |
-
"
|
| 26 |
-
"
|
| 27 |
-
"- Embeddings: [HF Sentence Transformers all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2)\n",
|
| 28 |
-
"- Vectorstore: [FAISS](https://faiss.ai/index.html)\n",
|
| 29 |
-
"- For glueing them all: [LangChain (v0.3)](https://python.langchain.com/docs/versions/v0_3/)"
|
| 30 |
]
|
| 31 |
},
|
| 32 |
{
|
|
|
|
| 1 |
{
|
| 2 |
"cells": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
{
|
| 4 |
"cell_type": "markdown",
|
| 5 |
"metadata": {},
|
| 6 |
"source": [
|
| 7 |
+
"# RAG - Chatbot\n",
|
| 8 |
+
"\n",
|
| 9 |
+
"## Background\n",
|
| 10 |
+
"\n",
|
| 11 |
+
"In this notebook I will demonstrate how to build the backend of a RAG-Chatbot, that will allow users to interact with uploaded pdf documents and provided URL's. \n",
|
| 12 |
+
"\n",
|
| 13 |
+
"The critical advantage of a RAG-chatbot, in comparison to a standard chatbot is the retrieval of best matching chunks of information provided by the user, and amendment of these information as context to the original question of the user, as illustrated here:\n",
|
| 14 |
+
"\n",
|
| 15 |
+
"\n",
|
| 16 |
+
"This technique therefore provides a feasible alternative to the expensive process of fine-tuning an LLM with information that were not available during the pre-training phase.\n",
|
| 17 |
+
"\n",
|
| 18 |
+
"In this implementation, we will use:\n",
|
| 19 |
+
"- LLM: [zephyr-7b-alpha](https://huggingface.co/HuggingFaceH4/zephyr-7b-alpha) through [Hugging Face serverless Inference API](https://huggingface.co/docs/api-inference/en/index)\n",
|
| 20 |
+
"- Embeddings: [HF Sentence Transformers all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2)\n",
|
| 21 |
+
"- Vectorstore: [FAISS](https://faiss.ai/index.html)\n",
|
| 22 |
+
"- For glueing them all: [LangChain (v0.3)](https://python.langchain.com/docs/versions/v0_3/)\n",
|
| 23 |
"\n",
|
| 24 |
"Note: the code snippets below have been copied and simplified from my original code [here](https://github.com/OnurKerimoglu/chat_with_docs/blob/main/src/rag.py), which is in turn deployed to HuggingFace space [here](https://huggingface.co/spaces/OnurKerimoglu/rag_chat), which may well be sleeping due to inactivity (don't hesitate to wake it up!)"
|
| 25 |
]
|
|
|
|
| 28 |
"cell_type": "markdown",
|
| 29 |
"metadata": {},
|
| 30 |
"source": [
|
| 31 |
+
"## Building the Bot\n",
|
| 32 |
+
"Let's first import all the packages that will be needed. Here, we will use:"
|
|
|
|
|
|
|
|
|
|
| 33 |
]
|
| 34 |
},
|
| 35 |
{
|