CoolFace
Apppublic

RustSa/linux_documentation_support_chatbot

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
qa_system.py64 linesDownload Raw Back to root
1from langchain.chains import ConversationalRetrievalChain2from langchain_openai import ChatOpenAI3from langchain.prompts import (4    ChatPromptTemplate,5    SystemMessagePromptTemplate,6    HumanMessagePromptTemplate7)8 9# System prompt: instruct the model to include citations in the answer text10system_template = """11You are a customer support assistant specialized in Linux documentation.12When you answer, cite each source by including a bracketed reference with the document name and page number, e.g.:13 14"Here is the answer... [source: linux-manual.pdf, page 15]"15 16If you cannot answer based on the provided documentation, simply say "I don’t know.".17 18Company: OpenSource Corp | Email: support@example.com | Phone: 123-456-789019"""20system_prompt = SystemMessagePromptTemplate.from_template(system_template)21 22# Prompt for answering with extracted context23qa_template = """24Use the following passages from Linux documentation to answer the question.25 26{context}27 28Question: {question}29 30Provide a concise answer, and include bracketed citations like [source: filename.pdf, page X] for each fact you use.31If you don't know, say "I don’t know.".32"""33qa_prompt = ChatPromptTemplate.from_messages([34    system_prompt,35    HumanMessagePromptTemplate.from_template(qa_template)36])37 38def create_conversational_chain(vector_store):39    #Set up a conversational retrieval chain with OpenAI chat model.40    retriever = vector_store.as_retriever(search_kwargs={"k": 3})41    llm = ChatOpenAI(temperature=0)42 43    # Build the chain; it will maintain chat history internally44    qa_chain = ConversationalRetrievalChain.from_llm(45        llm=llm,46        retriever=retriever,47        combine_docs_chain_kwargs={"prompt": qa_prompt},48        condense_question_prompt=ChatPromptTemplate.from_messages([49            SystemMessagePromptTemplate.from_template(50                """51                Rephrase the user question to be a standalone query.52 53                Conversation History:54                {chat_history}55 56                Follow-up Input: {question}57 58                Standalone question:"""),59            HumanMessagePromptTemplate.from_template("{question}")60        ]),61        return_source_documents=True62    )63 64    return qa_chain