Updates to handle history
Browse files- __pycache__/app02-chatRag.cpython-310.pyc +0 -0
- app.py → app01-simpleRag.py +0 -4
- app02-chatRag.py +95 -0
- modules.md +2 -0
- test.ipynb +130 -0
__pycache__/app02-chatRag.cpython-310.pyc
ADDED
|
Binary file (2.08 kB). View file
|
|
|
app.py → app01-simpleRag.py
RENAMED
|
@@ -11,16 +11,12 @@ from langchain_community.embeddings import HuggingFaceEmbeddings
|
|
| 11 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 12 |
|
| 13 |
# Pinecone vector database
|
| 14 |
-
# import langchain.vectorstores as vs
|
| 15 |
-
# from langchain_pinecone import Pinecone
|
| 16 |
-
# import pinecone
|
| 17 |
from pinecone import Pinecone, ServerlessSpec
|
| 18 |
from langchain_pinecone import PineconeVectorStore
|
| 19 |
|
| 20 |
|
| 21 |
setid = "global"
|
| 22 |
|
| 23 |
-
#EMBEDDINGS_MODEL = "BAAI/bge-base-en-v1.5" # Ranking 8, 768
|
| 24 |
embeddings = HuggingFaceEmbeddings(model_name=os.getenv("EMBEDDINGS_MODEL"))
|
| 25 |
|
| 26 |
# model = ChatOpenAI(temperature=0.0)
|
|
|
|
| 11 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 12 |
|
| 13 |
# Pinecone vector database
|
|
|
|
|
|
|
|
|
|
| 14 |
from pinecone import Pinecone, ServerlessSpec
|
| 15 |
from langchain_pinecone import PineconeVectorStore
|
| 16 |
|
| 17 |
|
| 18 |
setid = "global"
|
| 19 |
|
|
|
|
| 20 |
embeddings = HuggingFaceEmbeddings(model_name=os.getenv("EMBEDDINGS_MODEL"))
|
| 21 |
|
| 22 |
# model = ChatOpenAI(temperature=0.0)
|
app02-chatRag.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Run with reload mode:
|
| 2 |
+
# gradio app02-chatRag.py
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# Langchain
|
| 8 |
+
from langchain.chains import RetrievalQA
|
| 9 |
+
from langchain.prompts import ChatPromptTemplate
|
| 10 |
+
from langchain_core.runnables import RunnablePassthrough
|
| 11 |
+
|
| 12 |
+
# HuggingFace
|
| 13 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 14 |
+
|
| 15 |
+
# GeminiPro
|
| 16 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 17 |
+
|
| 18 |
+
# Pinecone vector database
|
| 19 |
+
from pinecone import Pinecone, ServerlessSpec
|
| 20 |
+
from langchain_pinecone import PineconeVectorStore
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
setid = "global"
|
| 24 |
+
|
| 25 |
+
embeddings = HuggingFaceEmbeddings(model_name=os.getenv("EMBEDDINGS_MODEL"))
|
| 26 |
+
|
| 27 |
+
# model = ChatOpenAI(temperature=0.0)
|
| 28 |
+
model = ChatGoogleGenerativeAI(
|
| 29 |
+
model="gemini-pro", temperature=0.1, convert_system_message_to_human=True
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
pc = Pinecone(
|
| 33 |
+
api_key=os.getenv("PINECONE_API_KEY")
|
| 34 |
+
)
|
| 35 |
+
index = pc.Index(setid)
|
| 36 |
+
vectorstore = PineconeVectorStore(index, embeddings, "text")
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
template_no_history = """Answer the question based only on the following context:
|
| 40 |
+
{context}
|
| 41 |
+
|
| 42 |
+
Question: {question}
|
| 43 |
+
"""
|
| 44 |
+
PROMPT_NH = ChatPromptTemplate.from_template(template_no_history)
|
| 45 |
+
|
| 46 |
+
template_with_history = """Given the following conversation history, answer the follow up question:
|
| 47 |
+
Chat History:
|
| 48 |
+
{chat_history}
|
| 49 |
+
|
| 50 |
+
Question: {question}
|
| 51 |
+
"""
|
| 52 |
+
PROMPT_WH = ChatPromptTemplate.from_template(template_with_history)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def pipeLog(x):
|
| 56 |
+
print("***", x)
|
| 57 |
+
return x
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def rag_query(question: str, history: list[list[str]]):
|
| 62 |
+
if len(history)==0:
|
| 63 |
+
chain = (
|
| 64 |
+
pipeLog
|
| 65 |
+
| { "context": vectorstore.as_retriever(kwargs={"k":5}), "question": RunnablePassthrough() }
|
| 66 |
+
| PROMPT_NH
|
| 67 |
+
| pipeLog
|
| 68 |
+
| model
|
| 69 |
+
)
|
| 70 |
+
response = chain.invoke(question)
|
| 71 |
+
print(response)
|
| 72 |
+
return response
|
| 73 |
+
else:
|
| 74 |
+
chat_history = ""
|
| 75 |
+
for l in history:
|
| 76 |
+
chat_history += " : ".join(l)
|
| 77 |
+
chain = (
|
| 78 |
+
pipeLog
|
| 79 |
+
| { "chat_history": chat_history, "question": RunnablePassthrough() }
|
| 80 |
+
| PROMPT_WH
|
| 81 |
+
| pipeLog
|
| 82 |
+
| model
|
| 83 |
+
)
|
| 84 |
+
response = chain.invoke(question)
|
| 85 |
+
return response
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
gr.ChatInterface(
|
| 90 |
+
rag_query,
|
| 91 |
+
title="RAG Chatbot demo",
|
| 92 |
+
description="A chatbot doing Retrieval Augmented Generation, backed by a Pinecone vector database"
|
| 93 |
+
).launch()
|
| 94 |
+
|
| 95 |
+
|
modules.md
CHANGED
|
@@ -8,6 +8,8 @@ pip install \
|
|
| 8 |
langchain-pinecone \
|
| 9 |
huggingface_hub
|
| 10 |
|
|
|
|
|
|
|
| 11 |
# python-dotenv \
|
| 12 |
# pinecone-client==2.2.4 \
|
| 13 |
```
|
|
|
|
| 8 |
langchain-pinecone \
|
| 9 |
huggingface_hub
|
| 10 |
|
| 11 |
+
pip install ipykernel IProgress ipywidgets --upgrade
|
| 12 |
+
|
| 13 |
# python-dotenv \
|
| 14 |
# pinecone-client==2.2.4 \
|
| 15 |
```
|
test.ipynb
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 2,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [],
|
| 8 |
+
"source": [
|
| 9 |
+
"import os\n",
|
| 10 |
+
"import gradio as gr\n",
|
| 11 |
+
"\n",
|
| 12 |
+
"# Langchain\n",
|
| 13 |
+
"from langchain.chains import RetrievalQA\n",
|
| 14 |
+
"from langchain.prompts import ChatPromptTemplate\n",
|
| 15 |
+
"from langchain_core.runnables import RunnablePassthrough\n",
|
| 16 |
+
"\n",
|
| 17 |
+
"# HuggingFace\n",
|
| 18 |
+
"from langchain_community.embeddings import HuggingFaceEmbeddings\n",
|
| 19 |
+
"\n",
|
| 20 |
+
"# GeminiPro\n",
|
| 21 |
+
"from langchain_google_genai import ChatGoogleGenerativeAI\n",
|
| 22 |
+
"\n",
|
| 23 |
+
"# Pinecone vector database\n",
|
| 24 |
+
"from pinecone import Pinecone, ServerlessSpec\n",
|
| 25 |
+
"from langchain_pinecone import PineconeVectorStore\n",
|
| 26 |
+
"\n",
|
| 27 |
+
"\n",
|
| 28 |
+
"setid = \"global\"\n",
|
| 29 |
+
"\n",
|
| 30 |
+
"embeddings = HuggingFaceEmbeddings(model_name=os.getenv(\"EMBEDDINGS_MODEL\"))\n",
|
| 31 |
+
"\n",
|
| 32 |
+
"# model = ChatOpenAI(temperature=0.0)\n",
|
| 33 |
+
"model = ChatGoogleGenerativeAI(\n",
|
| 34 |
+
" model=\"gemini-pro\", temperature=0.1, convert_system_message_to_human=True\n",
|
| 35 |
+
")\n",
|
| 36 |
+
"\n",
|
| 37 |
+
"pc = Pinecone(\n",
|
| 38 |
+
" api_key=os.getenv(\"PINECONE_API_KEY\")\n",
|
| 39 |
+
" )\n",
|
| 40 |
+
"index = pc.Index(setid)\n",
|
| 41 |
+
"vectorstore = PineconeVectorStore(index, embeddings, \"text\")\n",
|
| 42 |
+
"\n",
|
| 43 |
+
"\n",
|
| 44 |
+
"template_no_history = \"\"\"Answer the question based only on the following context:\n",
|
| 45 |
+
"{context}\n",
|
| 46 |
+
"\n",
|
| 47 |
+
"Question: {question}\n",
|
| 48 |
+
"\"\"\"\n",
|
| 49 |
+
"PROMPT_NH = ChatPromptTemplate.from_template(template_no_history)\n",
|
| 50 |
+
"\n",
|
| 51 |
+
"template_with_history = \"\"\"Given the following conversation history, answer the follow up question:\n",
|
| 52 |
+
"Chat History:\n",
|
| 53 |
+
"{chat_history}\n",
|
| 54 |
+
"\n",
|
| 55 |
+
"Question: {question}\n",
|
| 56 |
+
"\"\"\"\n",
|
| 57 |
+
"PROMPT_WH = ChatPromptTemplate.from_template(template_with_history)\n",
|
| 58 |
+
"\n",
|
| 59 |
+
"\n",
|
| 60 |
+
"def pipeLog(x):\n",
|
| 61 |
+
" print(\"***\", x)\n",
|
| 62 |
+
" return x\n"
|
| 63 |
+
]
|
| 64 |
+
},
|
| 65 |
+
{
|
| 66 |
+
"cell_type": "code",
|
| 67 |
+
"execution_count": 5,
|
| 68 |
+
"metadata": {},
|
| 69 |
+
"outputs": [
|
| 70 |
+
{
|
| 71 |
+
"name": "stdout",
|
| 72 |
+
"output_type": "stream",
|
| 73 |
+
"text": [
|
| 74 |
+
"content='A blockchain is a distributed ledger technology that enables secure and immutable record-keeping of digital transactions. It comprises a chain of blocks, each containing a list of validated and time-stamped transactions.'\n"
|
| 75 |
+
]
|
| 76 |
+
}
|
| 77 |
+
],
|
| 78 |
+
"source": [
|
| 79 |
+
"question = \"What is a blockchain?\"\n",
|
| 80 |
+
"\n",
|
| 81 |
+
"# chain = (\n",
|
| 82 |
+
"# pipeLog \n",
|
| 83 |
+
"# | { \"context\": vectorstore.as_retriever(kwargs={\"k\":5}), \"question\": RunnablePassthrough() }\n",
|
| 84 |
+
"# | PROMPT_NH \n",
|
| 85 |
+
"# | pipeLog \n",
|
| 86 |
+
"# | model\n",
|
| 87 |
+
"# )\n",
|
| 88 |
+
"\n",
|
| 89 |
+
"\n",
|
| 90 |
+
"chain = (\n",
|
| 91 |
+
" { \"context\": vectorstore.as_retriever(kwargs={\"k\": 5}), \"question\": RunnablePassthrough() }\n",
|
| 92 |
+
" | PROMPT_NH\n",
|
| 93 |
+
" | model\n",
|
| 94 |
+
")\n",
|
| 95 |
+
"\n",
|
| 96 |
+
"\n",
|
| 97 |
+
"response = chain.invoke(question)\n",
|
| 98 |
+
"print(response)\n"
|
| 99 |
+
]
|
| 100 |
+
},
|
| 101 |
+
{
|
| 102 |
+
"cell_type": "code",
|
| 103 |
+
"execution_count": null,
|
| 104 |
+
"metadata": {},
|
| 105 |
+
"outputs": [],
|
| 106 |
+
"source": []
|
| 107 |
+
}
|
| 108 |
+
],
|
| 109 |
+
"metadata": {
|
| 110 |
+
"kernelspec": {
|
| 111 |
+
"display_name": ".venv",
|
| 112 |
+
"language": "python",
|
| 113 |
+
"name": "python3"
|
| 114 |
+
},
|
| 115 |
+
"language_info": {
|
| 116 |
+
"codemirror_mode": {
|
| 117 |
+
"name": "ipython",
|
| 118 |
+
"version": 3
|
| 119 |
+
},
|
| 120 |
+
"file_extension": ".py",
|
| 121 |
+
"mimetype": "text/x-python",
|
| 122 |
+
"name": "python",
|
| 123 |
+
"nbconvert_exporter": "python",
|
| 124 |
+
"pygments_lexer": "ipython3",
|
| 125 |
+
"version": "3.10.12"
|
| 126 |
+
}
|
| 127 |
+
},
|
| 128 |
+
"nbformat": 4,
|
| 129 |
+
"nbformat_minor": 2
|
| 130 |
+
}
|