Retrieval System Generates Answers Instead of Retrieving from Text File -llama2,langchain

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật