mirror of
https://github.com/hwchase17/langchain
synced 2024-10-31 15:20:26 +00:00
add template for hyde (#12390)
This commit is contained in:
parent
4db8d82c55
commit
56ee56736b
21
templates/hyde/LICENSE
Normal file
21
templates/hyde/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.
|
9
templates/hyde/README.md
Normal file
9
templates/hyde/README.md
Normal file
@ -0,0 +1,9 @@
|
||||
# HyDE
|
||||
|
||||
Hypothetical Document Embeddings (HyDE) are a method to improve retrieval.
|
||||
To do this, a hypothetical document is generated for an incoming query.
|
||||
That document is then embedded, and that embedding is used to look up real documents similar to that hypothetical document.
|
||||
The idea behind this is that the hypothetical document may be closer in the embedding space than the query.
|
||||
For a more detailed description, read the full paper [here](https://arxiv.org/abs/2212.10496).
|
||||
|
||||
For this example, we use a simple RAG architecture, although you can easily use this technique in other more complicated architectures.
|
0
templates/hyde/hyde/__init__.py
Normal file
0
templates/hyde/hyde/__init__.py
Normal file
59
templates/hyde/hyde/chain.py
Normal file
59
templates/hyde/hyde/chain.py
Normal file
@ -0,0 +1,59 @@
|
||||
from langchain.prompts import ChatPromptTemplate
|
||||
from langchain.chat_models import ChatOpenAI
|
||||
from langchain.embeddings import OpenAIEmbeddings
|
||||
from langchain.schema.output_parser import StrOutputParser
|
||||
from langchain.schema.runnable import RunnablePassthrough, RunnableParallel
|
||||
from langchain.vectorstores import Chroma
|
||||
from hyde.prompts import hyde_prompt
|
||||
|
||||
# Example for document loading (from url), splitting, and creating vectostore
|
||||
|
||||
'''
|
||||
# Load
|
||||
from langchain.document_loaders import WebBaseLoader
|
||||
loader = WebBaseLoader("https://lilianweng.github.io/posts/2023-06-23-agent/")
|
||||
data = loader.load()
|
||||
|
||||
# Split
|
||||
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
||||
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0)
|
||||
all_splits = text_splitter.split_documents(data)
|
||||
|
||||
# Add to vectorDB
|
||||
vectorstore = Chroma.from_documents(documents=all_splits,
|
||||
collection_name="rag-chroma",
|
||||
embedding=OpenAIEmbeddings(),
|
||||
)
|
||||
retriever = vectorstore.as_retriever()
|
||||
'''
|
||||
|
||||
# Embed a single document as a test
|
||||
vectorstore = Chroma.from_texts(
|
||||
["harrison worked at kensho"],
|
||||
collection_name="rag-chroma",
|
||||
embedding=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)
|
||||
|
||||
# LLM
|
||||
model = ChatOpenAI()
|
||||
|
||||
# RAG chain
|
||||
chain = (
|
||||
RunnableParallel({
|
||||
# Configure the input, pass it the prompt, pass that to the model, and then the result to the retriever
|
||||
"context": {"input": RunnablePassthrough()} | hyde_prompt | model | StrOutputParser() | retriever,
|
||||
"question": RunnablePassthrough()
|
||||
})
|
||||
| prompt
|
||||
| model
|
||||
| StrOutputParser()
|
||||
)
|
19
templates/hyde/hyde/prompts.py
Normal file
19
templates/hyde/hyde/prompts.py
Normal file
@ -0,0 +1,19 @@
|
||||
from langchain.prompts.prompt import PromptTemplate
|
||||
|
||||
# There are a few different templates to choose from
|
||||
# These are just different ways to generate hypothetical documents
|
||||
web_search_template = """Please write a passage to answer the question
|
||||
Question: {input}
|
||||
Passage:"""
|
||||
sci_fact_template = """Please write a scientific paper passage to support/refute the claim
|
||||
Claim: {input}
|
||||
Passage:"""
|
||||
fiqa_template = """Please write a financial article passage to answer the question
|
||||
Question: {input}
|
||||
Passage:"""
|
||||
trec_news_template = """Please write a news passage about the topic.
|
||||
Topic: {input}
|
||||
Passage:"""
|
||||
|
||||
# For the sake of this example we will use the web search template
|
||||
hyde_prompt = PromptTemplate.from_template(web_search_template)
|
32
templates/hyde/pyproject.toml
Normal file
32
templates/hyde/pyproject.toml
Normal file
@ -0,0 +1,32 @@
|
||||
[tool.poetry]
|
||||
name = "hyde"
|
||||
version = "0.0.1"
|
||||
description = ""
|
||||
authors = []
|
||||
readme = "README.md"
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = ">=3.8.1,<4.0"
|
||||
langchain = ">=0.0.313, <0.1"
|
||||
openai = "^0.28.1"
|
||||
|
||||
[tool.poetry.group.dev.dependencies]
|
||||
poethepoet = "^0.24.1"
|
||||
langchain-cli = ">=0.0.4"
|
||||
fastapi = "^0.104.0"
|
||||
sse-starlette = "^1.6.5"
|
||||
|
||||
[tool.langserve]
|
||||
export_module = "hyde.chain"
|
||||
export_attr = "chain"
|
||||
|
||||
[tool.poe.tasks.start]
|
||||
cmd="uvicorn langchain_cli.dev_scripts:create_demo_server --reload --port $port --host $host"
|
||||
args = [
|
||||
{name = "port", help = "port to run on", default = "8000"},
|
||||
{name = "host", help = "host to run on", default = "127.0.0.1"}
|
||||
]
|
||||
|
||||
[build-system]
|
||||
requires = ["poetry-core"]
|
||||
build-backend = "poetry.core.masonry.api"
|
0
templates/hyde/tests/__init__.py
Normal file
0
templates/hyde/tests/__init__.py
Normal file
Loading…
Reference in New Issue
Block a user