In the implemented retrieval system,llama generates answers by itself. When debugging it, the prompt does tell it not to make up answers and to follow the context.
Here’s the code:
from langchain.text_splitter import CharacterTextSplitter
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import Chroma
from langchain_community.document_loaders import TextLoader
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
# Equivalent to SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
# text split inot chunks
text_splitter=CharacterTextSplitter(
separator="n",
chunk_size=200,
chunk_overlap=0
)
# get document from loader
loader=TextLoader("facts.txt")
# find file and extract content from it
docs=loader.load_and_split(text_splitter=text_splitter)
# get vector store
db=Chroma.from_documents(
docs, #calc embeddings for these chunks
embedding=embeddings,
persist_directory="emb" #saved inside emb directory
)
results=db.similarity_search_with_score("Give fact about Ostrich",
k=2)
# if i dont want search score, use db.db.similarity_search
for result in results:
# we get four results by default. an array of tuples
print("n")
# the search score
print(result[1])
# the actual doc
print(result[0].page_content) #works correctly
Seperate block of code next->
from langchain.embeddings.base import Embeddings
from langchain.vectorstores import Chroma
from langchain.schema import BaseRetriever
class RedundantFilterRetriever(BaseRetriever):
embeddings:Embeddings
chroma:Chroma
def get_relevant_documents(self,query):
# calc embeddings for query string
emb=self.embeddings.embed_query(query)
print(query,"embedding query",emb)
# take embeddings and feed them into the
# max_marginal_relevance_search_by_vector
# return self.chroma.max_marginal_relevance_search_by_vector(
# embedding=emb,
# lambda_mult=0.8
# )
# Retrieve relevant documents using the embeddings
docs = self.chroma.max_marginal_relevance_search_by_vector(
embedding=emb,
lambda_mult=0.8
)
for doc in docs:
print(f"Retrieved Document: {doc}")
return docs
async def aget_relevant_documents(self):
return []
# from langchain_community.llms import Ollama
from langchain.callbacks.manager import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain_community.vectorstores import Chroma
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import Chroma
from langchain.chains import RetrievalQA
from langchain_community.chat_models import ChatOllama
# from redundant_filter_retriever import RedundantFilterRetriever
from langchain_community.embeddings import OllamaEmbeddings
import langchain
from langchain.llms import HuggingFacePipeline
# langchain.debug=True
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
# Equivalent to SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
# embeddings=OllamaEmbeddings(model="llama2:latest")
db=Chroma(
embedding_function=embeddings,
persist_directory="emb"
)
# get language model
# chat = ChatOllama(model="llama2")
chat = HuggingFacePipeline(pipeline=generate_text)
retriever=RedundantFilterRetriever(
embeddings=embeddings,
chroma=db
)
# old retriver
# retriever=db.as_retriever()
chain=RetrievalQA.from_chain_type(
llm=chat,
retriever=retriever,
chain_type="stuff"
)
# result=chain.invoke("Give me fact about ostrich from txt file only. dont generate yourself or ill murder u")
result=chain.invoke("Give me fact about ostrich")
print(result)
This outputs:
[chain/start] [1:chain:RetrievalQA] Entering Chain run with input:
{
"query": "Give me fact about ostrich"
}
Retrieved Document: page_content='1. "Dreamt" is the only English word that ends with the letters "mt."n2. An ostrich's eye is bigger than its brain.n3. Honey is the only natural food that is made without destroying any kind of life.' metadata={'source': 'facts.txt'}
Retrieved Document: page_content='101. Avocado has more protein than any other fruit.n102. Ostriches can run faster than horses.n103. The Golden Poison Dart Frog’s skin has enough toxins to kill 100 people.' metadata={'source': 'facts.txt'}
Retrieved Document: page_content="112. Saturn's density is low enough that the planet would float in water.n113. Starfish can regenerate their own arms.n114. French Fries originated in Belgium." metadata={'source': 'facts.txt'}
Retrieved Document: page_content='81. The male seahorse carries the eggs until they hatch instead of the female.n82. St. Lucia is the only country in the world named after a woman.' metadata={'source': 'facts.txt'}
[chain/start] [1:chain:RetrievalQA > 3:chain:StuffDocumentsChain] Entering Chain run with input:
[inputs]
[chain/start] [1:chain:RetrievalQA > 3:chain:StuffDocumentsChain > 4:chain:LLMChain] Entering Chain run with input:
{
"question": "Give me fact about ostrich",
"context": "1. "Dreamt" is the only English word that ends with the letters "mt."n2. An ostrich's eye is bigger than its brain.n3. Honey is the only natural food that is made without destroying any kind of life.nn101. Avocado has more protein than any other fruit.n102. Ostriches can run faster than horses.n103. The Golden Poison Dart Frog’s skin has enough toxins to kill 100 people.nn112. Saturn's density is low enough that the planet would float in water.n113. Starfish can regenerate their own arms.n114. French Fries originated in Belgium.nn81. The male seahorse carries the eggs until they hatch instead of the female.n82. St. Lucia is the only country in the world named after a woman."
}
[llm/start] [1:chain:RetrievalQA > 3:chain:StuffDocumentsChain > 4:chain:LLMChain > 5:llm:HuggingFacePipeline] Entering LLM run with input:
{
"prompts": [
"Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.nn1. "Dreamt" is the only English word that ends with the letters "mt."n2. An ostrich's eye is bigger than its brain.n3. Honey is the only natural food that is made without destroying any kind of life.nn101. Avocado has more protein than any other fruit.n102. Ostriches can run faster than horses.n103. The Golden Poison Dart Frog’s skin has enough toxins to kill 100 people.nn112. Saturn's density is low enough that the planet would float in water.n113. Starfish can regenerate their own arms.n114. French Fries originated in Belgium.nn81. The male seahorse carries the eggs until they hatch instead of the female.n82. St. Lucia is the only country in the world named after a woman.nnQuestion: Give me fact about ostrichnHelpful Answer:"
]
}
[llm/end] [1:chain:RetrievalQA > 3:chain:StuffDocumentsChain > 4:chain:LLMChain > 5:llm:HuggingFacePipeline] [5.64s] Exiting LLM run with output:
{
"generations": [
[
{
"text": " Sure! Here's a fun fact about ostriches: They can run faster than 45 km/h (28 mph), making them the fastest birds on land!",
"generation_info": null,
"type": "Generation"
}
]
],
"llm_output": null,
"run": null
}
[chain/end] [1:chain:RetrievalQA > 3:chain:StuffDocumentsChain > 4:chain:LLMChain] [5.65s] Exiting Chain run with output:
{
"text": " Sure! Here's a fun fact about ostriches: They can run faster than 45 km/h (28 mph), making them the fastest birds on land!"
}
[chain/end] [1:chain:RetrievalQA > 3:chain:StuffDocumentsChain] [5.65s] Exiting Chain run with output:
{
"output_text": " Sure! Here's a fun fact about ostriches: They can run faster than 45 km/h (28 mph), making them the fastest birds on land!"
}
[chain/end] [1:chain:RetrievalQA] [5.67s] Exiting Chain run with output:
{
"result": " Sure! Here's a fun fact about ostriches: They can run faster than 45 km/h (28 mph), making them the fastest birds on land!"
}
{'query': 'Give me fact about ostrich', 'result': " Sure! Here's a fun fact about ostriches: They can run faster than 45 km/h (28 mph), making them the fastest birds on land!"}
When i use this, it returns correct answer:
result=chain.invoke(“Give me fact about ostrich from txt file only. dont generate yourself”)
but when i do this, it doesn’t:
result=chain.invoke(“Give me fact about ostrich”)
I am following a tutorial where the second invoke returns the correct response from txt file but it’s not working for me.