mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-04 04:07:54 +00:00
Add Weaviate RAG template (#12460)
This commit is contained in:
parent
df4960a6d8
commit
a476147189
21
templates/rag-weaviate/LICENSE
Normal file
21
templates/rag-weaviate/LICENSE
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2023 LangChain, Inc.
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
16
templates/rag-weaviate/README.md
Normal file
16
templates/rag-weaviate/README.md
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
# RAG Weaviate
|
||||||
|
|
||||||
|
This template performs RAG using Weaviate and OpenAI.
|
||||||
|
|
||||||
|
## Weaviate
|
||||||
|
|
||||||
|
This connects to a hosted Weaviate vectorstore.
|
||||||
|
|
||||||
|
Be sure that you have set a few env variables in `chain.py`:
|
||||||
|
|
||||||
|
* `WEAVIATE_ENVIRONMENT`
|
||||||
|
* `WEAVIATE_API_KEY`
|
||||||
|
|
||||||
|
## LLM
|
||||||
|
|
||||||
|
Be sure that `OPENAI_API_KEY` is set in order to the OpenAI models.
|
24
templates/rag-weaviate/pyproject.toml
Normal file
24
templates/rag-weaviate/pyproject.toml
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
[tool.poetry]
|
||||||
|
name = "rag_weaviate"
|
||||||
|
version = "0.1.0"
|
||||||
|
description = ""
|
||||||
|
authors = ["Erika Cardenas <erika@weaviate.io"]
|
||||||
|
readme = "README.md"
|
||||||
|
|
||||||
|
[tool.poetry.dependencies]
|
||||||
|
python = ">=3.8.1,<4.0"
|
||||||
|
langchain = ">=0.0.313"
|
||||||
|
openai = "^0.28.1"
|
||||||
|
tiktoken = "^0.5.1"
|
||||||
|
weaviate-client = ">=3.24.2"
|
||||||
|
|
||||||
|
[tool.poetry.group.dev.dependencies]
|
||||||
|
python-dotenv = { extras = ["cli"], version = "^1.0.0" }
|
||||||
|
|
||||||
|
[tool.langserve]
|
||||||
|
export_module = "rag_weaviate"
|
||||||
|
export_attr = "chain"
|
||||||
|
|
||||||
|
[build-system]
|
||||||
|
requires = ["poetry-core"]
|
||||||
|
build-backend = "poetry.core.masonry.api"
|
56
templates/rag-weaviate/rag_weaviate.ipynb
Normal file
56
templates/rag-weaviate/rag_weaviate.ipynb
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"attachments": {},
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "8692a430",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Run Template\n",
|
||||||
|
"\n",
|
||||||
|
"In `server.py`, set -\n",
|
||||||
|
"```\n",
|
||||||
|
"add_routes(app, chain_ext, path=\"/rag-weaviate\")\n",
|
||||||
|
"```"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "41db5e30",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from langserve.client import RemoteRunnable\n",
|
||||||
|
"rag_app_weaviate = RemoteRunnable(\"http://localhost:8000/rag-weaviate\")\n",
|
||||||
|
"rag_app_weaviate.invoke(\"How does agent memory work?\")"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3.11.6 64-bit",
|
||||||
|
"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.6"
|
||||||
|
},
|
||||||
|
"vscode": {
|
||||||
|
"interpreter": {
|
||||||
|
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
3
templates/rag-weaviate/rag_weaviate/__init__.py
Normal file
3
templates/rag-weaviate/rag_weaviate/__init__.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
from rag_weaviate.chain import chain
|
||||||
|
|
||||||
|
__all__ = ["chain"]
|
52
templates/rag-weaviate/rag_weaviate/chain.py
Normal file
52
templates/rag-weaviate/rag_weaviate/chain.py
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
from langchain.chat_models import ChatOpenAI
|
||||||
|
from langchain.document_loaders import WebBaseLoader
|
||||||
|
from langchain.embeddings import OpenAIEmbeddings
|
||||||
|
from langchain.prompts import ChatPromptTemplate
|
||||||
|
from langchain.schema.output_parser import StrOutputParser
|
||||||
|
from langchain.schema.runnable import RunnableParallel, RunnablePassthrough
|
||||||
|
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
||||||
|
from langchain.vectorstores import Weaviate
|
||||||
|
|
||||||
|
if os.environ.get("WEAVIATE_API_KEY", None) is None:
|
||||||
|
raise Exception("Missing `WEAVIATE_API_KEY` environment variable.")
|
||||||
|
|
||||||
|
if os.environ.get("WEAVIATE_ENVIRONMENT", None) is None:
|
||||||
|
raise Exception("Missing `WEAVIATE_ENVIRONMENT` environment variable.")
|
||||||
|
|
||||||
|
WEAVIATE_INDEX_NAME = os.environ.get("WEAVIATE_INDEX", "langchain-test")
|
||||||
|
|
||||||
|
### Ingest code - you may need to run this the first time
|
||||||
|
# Load
|
||||||
|
loader = WebBaseLoader("https://lilianweng.github.io/posts/2023-06-23-agent/")
|
||||||
|
data = loader.load()
|
||||||
|
|
||||||
|
# # Split
|
||||||
|
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0)
|
||||||
|
all_splits = text_splitter.split_documents(data)
|
||||||
|
|
||||||
|
# # Add to vectorDB
|
||||||
|
vectorstore = Weaviate.from_documents(
|
||||||
|
documents=all_splits, embedding=OpenAIEmbeddings(), index_name=WEAVIATE_INDEX_NAME
|
||||||
|
)
|
||||||
|
retriever = vectorstore.as_retriever()
|
||||||
|
|
||||||
|
vectorstore = Weaviate.from_existing_index(WEAVIATE_INDEX_NAME, OpenAIEmbeddings())
|
||||||
|
retriever = vectorstore.as_retriever()
|
||||||
|
|
||||||
|
# RAG prompt
|
||||||
|
template = """Answer the question based only on the following context:
|
||||||
|
{context}
|
||||||
|
Question: {question}
|
||||||
|
"""
|
||||||
|
prompt = ChatPromptTemplate.from_template(template)
|
||||||
|
|
||||||
|
# RAG
|
||||||
|
model = ChatOpenAI()
|
||||||
|
chain = (
|
||||||
|
RunnableParallel({"context": retriever, "question": RunnablePassthrough()})
|
||||||
|
| prompt
|
||||||
|
| model
|
||||||
|
| StrOutputParser()
|
||||||
|
)
|
0
templates/rag-weaviate/tests/__init__.py
Normal file
0
templates/rag-weaviate/tests/__init__.py
Normal file
Loading…
Reference in New Issue
Block a user