-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexRetriever.py
80 lines (60 loc) · 2.66 KB
/
indexRetriever.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# functions to retrieve the documents and answer the questions
import logging, sys, os
from langchain_openai import ChatOpenAI
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.chains.question_answering import load_qa_chain
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
def question_retrieval(input):
subquestion = []
for i in range(len(input)):
index = input[i].find('?')
if index == -1:
i+1
else:
subquestion.append(input[i])
return subquestion
def question_answer(question, doc):
llm = ChatOpenAI(temperature=0.0, openai_api_key=os.getenv("OPENAI_API_KEY"))
chain = load_qa_chain(llm, chain_type="stuff")
return chain.run(input_documents=doc, question=question)
def answer_retriever():
SYSTEM_TEMPLATE = """As a tutor for the lecture databases and informationssystems, your goal is to provide accurate and helpful infomration about the lecture.
You should answer the user inquiries as best as possible based on the context and chat history provided and avoid making up answers.
If you don't know the answer, simply state that you don'k know. Answer the question in german language.
{context}
Question: {prompt}
"""
llm = ChatOpenAI(model_name="gpt-4", temperature=0.2)
question_answering_prompt = ChatPromptTemplate.from_messages(
[
(
"system",
SYSTEM_TEMPLATE,
),
MessagesPlaceholder(variable_name="messages"),
]
)
document_chain = create_stuff_documents_chain(llm, question_answering_prompt)
return document_chain
def question_generator():
prompt_template = """Als Tutor für die Datenbanken und Informationssysteme hilfst du den Studierenden bei Übungsaufgaben.
Der Student wird die nach einer Übungsaufgabe zu einem speziellen Thema fragen.
Du generierst eine Frage, die sich auf das Thema bezieht. Die Frage sollte in deutscher Sprache sein.
{context}
Input: {prompt}
"""
llm = ChatOpenAI(model_name="gpt-4", temperature=0.2)
question_answering_prompt = ChatPromptTemplate.from_messages(
[
(
"system",
prompt_template,
),
MessagesPlaceholder(variable_name="messages"),
]
)
llm = ChatOpenAI(model_name="gpt-4", temperature=0)
document_chain = create_stuff_documents_chain(llm, question_answering_prompt)
return document_chain