Download connection_of_memory_llm.py from A1ee/mediAI: direct link, hf CLI and curl.
- Browser
- Download file 2.01 kB
-
https://huggingface.co/spaces/A1ee/mediAI/resolve/main/connection_of_memory_llm.py
- Command line
-
hf download hf://spaces/A1ee/mediAI/connection_of_memory_llm.py
-
curl -L -o connection_of_memory_llm.py https://huggingface.co/spaces/A1ee/mediAI/resolve/main/connection_of_memory_llm.py
2.01 kB
| import os | |
| from langchain_huggingface import HuggingFaceEndpoint | |
| from langchain_core.prompts import PromptTemplate | |
| from langchain.chains import RetrievalQA | |
| from langchain_huggingface import HuggingFaceEmbeddings | |
| from langchain_community.vectorstores import FAISS | |
| # Step 1: Setup LLM (Mistral with HuggingFace) | |
| HF_TOKEN=os.environ.get("HF_TOKEN") | |
| HUGGINGFACE_REPO_ID="mistralai/Mistral-7B-Instruct-v0.3" | |
| def load_llm(huggingface_repo_id): | |
| llm = HuggingFaceEndpoint( | |
| repo_id=huggingface_repo_id, | |
| temperature=0.5, | |
| max_new_tokens=512, # ✅ use max_new_tokens instead of max_length | |
| huggingfacehub_api_token=HF_TOKEN | |
| ) | |
| return llm | |
| # Step 2: Connect LLM with FAISS and Create chain | |
| CUSTOM_PROMPT_TEMPLATE = """ | |
| Use the pieces of information provided in the context to answer user's question. | |
| If you dont know the answer, just say that you dont know, dont try to make up an answer. | |
| Dont provide anything out of the given context | |
| Context: {context} | |
| Question: {question} | |
| Begin your answer directly, infusing your response with a touch of romance. | |
| """ | |
| def set_custom_prompt(custom_prompt_template): | |
| prompt=PromptTemplate(template=CUSTOM_PROMPT_TEMPLATE, input_variables=["context", "question"]) | |
| return prompt | |
| # Load Database | |
| DB_FAISS_PATH="vectorestore/db_faiss" | |
| embedding_model=HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2") | |
| db= FAISS.load_local(DB_FAISS_PATH, embedding_model, allow_dangerous_deserialization=True) | |
| # Create QA chain | |
| qa_chain=RetrievalQA.from_chain_type( | |
| llm=load_llm(HUGGINGFACE_REPO_ID), | |
| chain_type="stuff", | |
| retriever=db.as_retriever(search_kwargs={'k':3}), | |
| return_source_documents=True, | |
| chain_type_kwargs={'prompt':set_custom_prompt(CUSTOM_PROMPT_TEMPLATE)} | |
| ) | |
| # Now invoke with a single query | |
| user_query=input("Write Query Here: ") | |
| response=qa_chain.invoke({'query': user_query}) | |
| print("RESULT: ", response["result"]) | |
| print("SOURCE DOCUMENTS: ", response["source_documents"]) | |