|
|
from langchain_community.document_loaders import DirectoryLoader |
|
|
from langchain_community.embeddings import HuggingFaceEmbeddings |
|
|
from langchain_community.vectorstores import FAISS |
|
|
from langchain_community.retrievers import BM25Retriever, EnsembleRetriever |
|
|
from langchain_community.llms import Ollama |
|
|
|
|
|
loader = DirectoryLoader('.', glob="all_dialogues.txt") |
|
|
docs = loader.load() |
|
|
|
|
|
text_splitter = RecursiveCharacterTextSplitter( |
|
|
chunk_size=1000, chunk_overlap=200 |
|
|
) |
|
|
texts = text_splitter.split_documents(docs) |
|
|
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2") |
|
|
|
|
|
|
|
|
db = FAISS.load_local( |
|
|
folder_path="./", |
|
|
embeddings=HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2"), |
|
|
allow_dangerous_deserialization=True |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vector_retriever = db.as_retriever(search_kwargs={"k": 3}) |
|
|
|
|
|
|
|
|
bm25_retriever = BM25Retriever.from_documents(texts) |
|
|
bm25_retriever.k = 2 |
|
|
|
|
|
|
|
|
ensemble_retriever = EnsembleRetriever( |
|
|
retrievers=[vector_retriever, bm25_retriever], |
|
|
weights=[0.6, 0.4] |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from langchain_community.llms import HuggingFaceHub |
|
|
from langchain_core.prompts import ChatPromptTemplate |
|
|
|
|
|
def respond_rag_huggingface( |
|
|
message: str, |
|
|
system_message: str = " you are game of thrones measter answer the given question strictly based on the context provived.if u donot know the answer reply i dont know donot give gibberish answers", |
|
|
num_predict: int = 128, |
|
|
temperature: float = 0.8, |
|
|
): |
|
|
|
|
|
docs = ensemble_retriever.get_relevant_documents(message) |
|
|
context = "\n\n".join(doc.page_content for doc in docs) |
|
|
|
|
|
|
|
|
prompt_template = ChatPromptTemplate.from_messages([ |
|
|
("system", system_message), |
|
|
("human", """Context: {context} |
|
|
|
|
|
Question: {question} |
|
|
|
|
|
Rules: |
|
|
- If the answer isn't in the context, respond with "I don't know" |
|
|
- Keep answers under 5 sentences |
|
|
- Include book/season references when possible""") |
|
|
]) |
|
|
|
|
|
|
|
|
llm = HuggingFaceHub( |
|
|
repo_id="HuggingFaceH4/zephyr-7b-beta", |
|
|
model_kwargs={ |
|
|
"temperature": temperature, |
|
|
"max_new_tokens": num_predict |
|
|
} |
|
|
) |
|
|
|
|
|
|
|
|
chain = prompt_template | llm |
|
|
response = chain.invoke({"context": context, "question": message}) |
|
|
|
|
|
return response.content |
|
|
|
|
|
__all__ = ["respond_rag_huggingface"] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|