mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-21 06:14:37 +00:00
added template to use Vertex Vector Search for q&a (#12622)
added template to use Vertex Vector Search for q&a
This commit is contained in:
parent
944cb552bb
commit
a53cac4508
21
templates/rag-matching-engine/LICENSE
Normal file
21
templates/rag-matching-engine/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.
|
1
templates/rag-matching-engine/README.md
Normal file
1
templates/rag-matching-engine/README.md
Normal file
@ -0,0 +1 @@
|
|||||||
|
# rag-matching-engine
|
24
templates/rag-matching-engine/pyproject.toml
Normal file
24
templates/rag-matching-engine/pyproject.toml
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
[tool.poetry]
|
||||||
|
name = "rag_matching_engine"
|
||||||
|
version = "0.0.1"
|
||||||
|
description = ""
|
||||||
|
authors = []
|
||||||
|
readme = "README.md"
|
||||||
|
|
||||||
|
[tool.poetry.dependencies]
|
||||||
|
python = ">=3.8.1,<4.0"
|
||||||
|
langchain = ">=0.0.313, <0.1"
|
||||||
|
google-cloud-aiplatform = "^1.35.0"
|
||||||
|
|
||||||
|
[tool.poetry.group.dev.dependencies]
|
||||||
|
langchain-cli = ">=0.0.4"
|
||||||
|
fastapi = "^0.104.0"
|
||||||
|
sse-starlette = "^1.6.5"
|
||||||
|
|
||||||
|
[tool.langserve]
|
||||||
|
export_module = "rag_matching_engine.chain"
|
||||||
|
export_attr = "chain"
|
||||||
|
|
||||||
|
[build-system]
|
||||||
|
requires = ["poetry-core"]
|
||||||
|
build-backend = "poetry.core.masonry.api"
|
69
templates/rag-matching-engine/rag_matching_engine/chain.py
Normal file
69
templates/rag-matching-engine/rag_matching_engine/chain.py
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
from langchain.embeddings import VertexAIEmbeddings
|
||||||
|
from langchain.llms import VertexAI
|
||||||
|
from langchain.prompts import PromptTemplate
|
||||||
|
from langchain.schema.output_parser import StrOutputParser
|
||||||
|
from langchain.schema.runnable import RunnableParallel, RunnablePassthrough
|
||||||
|
from langchain.vectorstores import MatchingEngine
|
||||||
|
|
||||||
|
# you need to preate the index first, for example, as described here:
|
||||||
|
# https://github.com/GoogleCloudPlatform/generative-ai/blob/main/language/use-cases/document-qa/question_answering_documents_langchain_matching_engine.ipynb
|
||||||
|
expected_variables = [
|
||||||
|
"project_id",
|
||||||
|
"me_region",
|
||||||
|
"gcs_bucket",
|
||||||
|
"me_index_id",
|
||||||
|
"me_endpoint_id",
|
||||||
|
]
|
||||||
|
variables = []
|
||||||
|
for variable_name in expected_variables:
|
||||||
|
variable = os.environ.get(variable_name.upper())
|
||||||
|
if not variable:
|
||||||
|
raise Exception(f"Missing `{variable_name}` environment variable.")
|
||||||
|
variables.append(variable)
|
||||||
|
|
||||||
|
project_id, me_region, gcs_bucket, me_index_id, me_endpoint_id = variables
|
||||||
|
|
||||||
|
|
||||||
|
vectorstore = MatchingEngine.from_components(
|
||||||
|
project_id=project_id,
|
||||||
|
region=me_region,
|
||||||
|
gcs_bucket_name=gcs_bucket,
|
||||||
|
embedding=VertexAIEmbeddings(),
|
||||||
|
index_id=me_index_id,
|
||||||
|
endpoint_id=me_endpoint_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
model = VertexAI()
|
||||||
|
|
||||||
|
template = (
|
||||||
|
"SYSTEM: You are an intelligent assistant helping the users with their questions"
|
||||||
|
"on research papers.\n\n"
|
||||||
|
"Question: {question}\n\n"
|
||||||
|
"Strictly Use ONLY the following pieces of context to answer the question at the "
|
||||||
|
"end. Think step-by-step and then answer.\n\n"
|
||||||
|
"Do not try to make up an answer:\n"
|
||||||
|
"- If the answer to the question cannot be determined from the context alone, "
|
||||||
|
'say \n"I cannot determine the answer to that."\n'
|
||||||
|
'- If the context is empty, just say "I do not know the answer to that."\n\n'
|
||||||
|
"=============\n{context}\n=============\n\n"
|
||||||
|
"Question: {question}\nHelpful Answer: "
|
||||||
|
)
|
||||||
|
|
||||||
|
prompt = PromptTemplate.from_template(template)
|
||||||
|
|
||||||
|
retriever = vectorstore.as_retriever(
|
||||||
|
search_type="similarity",
|
||||||
|
search_kwargs={
|
||||||
|
"k": 10,
|
||||||
|
"search_distance": 0.6,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
chain = (
|
||||||
|
RunnableParallel({"context": retriever, "question": RunnablePassthrough()})
|
||||||
|
| prompt
|
||||||
|
| model
|
||||||
|
| StrOutputParser()
|
||||||
|
)
|
0
templates/rag-matching-engine/tests/__init__.py
Normal file
0
templates/rag-matching-engine/tests/__init__.py
Normal file
Loading…
Reference in New Issue
Block a user