File size: 2,605 Bytes
85eab21
10c5258
 
 
 
 
 
 
85eab21
 
4a83326
85eab21
 
 
 
 
 
 
10c5258
85eab21
 
4a83326
85eab21
 
 
 
 
 
10c5258
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85eab21
 
10c5258
 
 
 
 
 
 
 
 
 
 
eab5ad9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from langchain.prompts.prompt import PromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationalRetrievalChain

#from langchain.chains import ChatVectorDBChain

import langchain
langchain.verbose = False

_template = """Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.
You can assume the question about the most recent response being sent.

Chat History:
{chat_history}
Follow Up Input: {question}
Standalone question:"""
CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)

template_1 = """You are an AI assistant for answering questions about the most recent chat conversation.
You are given the following extracted parts of a long document and a question. Provide a conversational answer.
If you don't know the answer, just say "Hmm, I'm not sure." Don't try to make up an answer.
If the question is not about the most recent files being sent, politely inform them that you are tuned to only answer questions about the files being outside of contents in this chat.
Question: {question}
=========
{context}
=========
Answer in Markdown:"""

template_2 = """You are an AI assistant for answering questions about the most recent chat conversation.
Question: {question}
=========
{context}
=========
Answer in Markdown:"""

qa_template = """
        You are a helpful AI assistant named ICC. The user gives you a file its content is represented by the following pieces of context, use them to answer the question at the end.
        If you don't know the answer, just say you don't know. Do NOT try to make up an answer.
        If the question is not related to the context, politely respond that you are tuned to only answer questions that are related to the context.
        Use as much detail as possible when responding.
	Question: {question}
	=========
	{context}
	=========
	Answer in Markdown:"""

QA_PROMPT = PromptTemplate(template=qa_template, input_variables=["question", "context"])

def get_chain(vectorstore):
    #llm = ChatOpenAI(temperature=0)
    llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
    retriever = vectorstore.as_retriever()
    qa_chain = ConversationalRetrievalChain.from_llm(llm=llm,
            retriever=retriever, verbose=False, return_source_documents=True, max_tokens_limit=4097, combine_docs_chain_kwargs={'prompt':QA_PROMPT})
    #qa_chain = ChatVectorDBChain.from_llm(
    #    llm,
    #    vectorstore,
    #    qa_prompt=QA_PROMPT,
    #    condense_question_prompt=CONDENSE_QUESTION_PROMPT,
    #)
    return qa_chain