momegas commited on
Commit
3c42732
·
1 Parent(s): 2b8bf40
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.document_loaders import DirectoryLoader
2
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
3
+ from langchain.embeddings.openai import OpenAIEmbeddings
4
+ from langchain.vectorstores import Chroma
5
+ from langchain.chat_models import ChatOpenAI
6
+ from langchain.retrievers.multi_query import MultiQueryRetriever
7
+ import dotenv
8
+ from langchain.indexes import VectorstoreIndexCreator
9
+ from langchain.chains.question_answering import load_qa_chain
10
+ from langchain.llms import OpenAI
11
+ from langchain.prompts import PromptTemplate
12
+ from langchain.chat_models import ChatOpenAI
13
+ from langchain.schema import AIMessage, HumanMessage, SystemMessage
14
+ import gradio as gr
15
+
16
+ dotenv.load_dotenv()
17
+
18
+
19
+ system_message = """You are the helpful assistant representing the company ecredit.
20
+ You answers should be in Greek.
21
+ If you don't know the answer, just say that you don't know, don't try to make up an answer.
22
+ Always finish your answer with "για περισσότερες πληροφορίες καλέστε στο: XXXXXXXXXXX.".
23
+ """
24
+
25
+ prompt_template = """Use the following pieces of context to answer the question at the end.
26
+ If you don't know the answer, just say that you don't know, don't try to make up an answer.
27
+ Only answer questions that are related to the context. If it's not in the context say "Δεν γνωρίζω".
28
+
29
+ Context:
30
+ {context}
31
+
32
+ Question: {question}
33
+ Answer in Greek:
34
+ """
35
+ PROMPT = PromptTemplate(
36
+ template=prompt_template, input_variables=["context", "question"]
37
+ )
38
+
39
+ loader = DirectoryLoader("./documents", glob="**/*.pdf", show_progress=True)
40
+ docs = loader.load()
41
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0)
42
+ texts = text_splitter.split_documents(docs)
43
+
44
+ embeddings = OpenAIEmbeddings()
45
+ docsearch = Chroma.from_documents(texts, embeddings).as_retriever()
46
+ chat = ChatOpenAI(temperature=0.1)
47
+
48
+
49
+ with gr.Blocks() as demo:
50
+ chatbot = gr.Chatbot()
51
+ msg = gr.Textbox()
52
+ clear = gr.ClearButton([msg, chatbot])
53
+
54
+ def respond(message, chat_history):
55
+ messages = [
56
+ SystemMessage(content=system_message),
57
+ ]
58
+
59
+ result_docs = docsearch.get_relevant_documents(message)
60
+ human_message = None
61
+ human_message = HumanMessage(
62
+ content=PROMPT.format(context=result_docs[:3], question=message)
63
+ )
64
+ messages.append(human_message)
65
+
66
+ result = chat(messages)
67
+ bot_message = result.content
68
+ chat_history.append((message, bot_message))
69
+ return "", chat_history
70
+
71
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
72
+
73
+
74
+ if __name__ == "__main__":
75
+ demo.launch()