CoolFace
Apppublic

nataliehamptonn/Knowledge_Graph_RAG

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
Doc_RAG.py81 linesDownload Raw Back to root
1from langchain_openai import ChatOpenAI2from langchain.prompts import ChatPromptTemplate3from langchain_core.output_parsers import StrOutputParser4from langchain_core.messages import AIMessage, HumanMessage5from dotenv import load_dotenv6import os7from sentence_transformers import SentenceTransformer, util8 9 10load_dotenv()11os.environ["LANGCHAIN_TRACING_V2"] = "false"12langchain_api_key = os.environ.get('LANGCHAIN_API_KEY')13openai_api_key = os.environ.get('OPENAI_API_KEY')14langchain_project = os.environ.get('LANGCHAIN_PROJECT')15 16llm = ChatOpenAI(model="gpt-3.5-turbo", 17    temperature=0.7)18 19def retrieval(question, supports):20    """21    Retrives relevant support information22    """23 24    model = SentenceTransformer("all-MiniLM-L6-v2")25    embeddedQuery = model.encode(question)26    embeddedSupports = model.encode(supports)27 28    hits = util.semantic_search(embeddedQuery, embeddedSupports, top_k=1)29    hits = hits[0]30    top_hit = hits[0]31 32    result = supports[top_hit['corpus_id']]33 34    return result35 36 37 38 39def answer(question, relevant_facts, chat_history):40    """41    Takes question and retrieved support information to answer42    """43 44    prompt = ChatPromptTemplate.from_template("""45    You are a chatbot designed to answer students' questions with the aid of provided relevant facts.46    Use the relevant facts and logical reasoning to give a useful, complete answer to the student's question. 47    Do not use any external knowledge - only use the provided facts to answer. Do not include any information 48    not in the provided facts. Answer in as few words as possible.49    Q: {question}50 51    Facts: {facts}52 53    A: """ )54    55    chain = prompt | llm | StrOutputParser()56    result = chain.invoke({"question": question, "facts": relevant_facts, "chat_history": chat_history})57 58    chat_history.extend( 59        [ 60            HumanMessage(content=question), 61            AIMessage(content=result),62        ]63    )64 65    return result66 67 68 69def answer_question_docRAG(question, supports, chat_history): 70 71    context = retrieval(question, supports)72    result = answer(question, context, chat_history)73    return result74 75 76# if __name__ == "__main__":77#     question = "What does antifreeze do to the boiling point of coolant?"78#     supports = ["Flickr: EvelynGiggles, modified by CK-12 Foundation. Antifreeze raises the boiling point of coolant . CC BY 2.0.", "Organic acids such as acetic acid all contain a functional group called a carboxyl group .", "The function of the plasma membrane is to control what goes in and out of the cell. Some molecules can go through the cell membrane to enter and leave the cell, but some cannot. The cell is therefore not completely permeable. \"Permeable\" means that anything can cross a barrier. An open door is completely permeable to anything that wants to enter or exit through the door. The plasma membrane is semipermeable , meaning that some things can enter the cell, and some things cannot."]79#     chat_history = []80 81#     answer_question_docRAG(question, supports, chat_history)