anpigon commited on
Commit
025da86
1 Parent(s): 8ca0d91

refactor(app.py): rename RetrievalQA to RetrievalQAWithSourcesChain to better reflect its functionality

Browse files

refactor(app.py): remove source document references from bot message and add them to a separate message to improve readability and user experience

Files changed (1) hide show
  1. app.py +5 -15
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from langchain.chains import RetrievalQA
3
  from langchain.chat_models import ChatOpenAI
4
  from langchain.embeddings.openai import OpenAIEmbeddings
5
  from langchain.prompts.chat import (
@@ -17,8 +17,9 @@ retriever = vector_store.as_retriever(search_kwargs={"k": 3})
17
 
18
  llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
19
 
20
- system_template = """Your name is Sayno. You act like a successful rich man. Never reveal that it is an AI. Use the following pieces of context to answer the users question shortly.
21
- Given the following summaries of a long document and a question, create a final answer with references ("SOURCES"), use "SOURCES" in capital letters regardless of the number of sources.
 
22
  If you don't know the answer, just say that "I don't know", don't try to make up an answer.
23
  ----------------
24
  {summaries}
@@ -34,7 +35,7 @@ prompt = ChatPromptTemplate.from_messages(messages)
34
 
35
  chain_type_kwargs = {"prompt": prompt}
36
 
37
- chain = RetrievalQA.from_chain_type(
38
  llm=llm,
39
  chain_type="stuff",
40
  retriever=retriever,
@@ -51,17 +52,6 @@ def respond(message, chat_history):
51
 
52
  bot_message = result["answer"]
53
 
54
- for i, doc in enumerate(result["source_documents"]):
55
- bot_message += (
56
- "["
57
- + str(i + 1)
58
- + "] "
59
- + doc.metadata["source"]
60
- + "("
61
- + str(doc.metadata["page"])
62
- + ") "
63
- )
64
-
65
  # 채팅 기록에 사용자의 메시지와 봇의 응답을 추가합니다.
66
  chat_history.append((message, bot_message))
67
 
 
1
  import gradio as gr
2
+ from langchain.chains import RetrievalQAWithSourcesChain
3
  from langchain.chat_models import ChatOpenAI
4
  from langchain.embeddings.openai import OpenAIEmbeddings
5
  from langchain.prompts.chat import (
 
17
 
18
  llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
19
 
20
+ system_template = """Your name is Sayno. You act like a successful rich man.
21
+ Never reveal that it is an AI. Use the following pieces of context to answer the users question shortly.
22
+ Given the following summaries of a long document and a question, create a final answer with references.
23
  If you don't know the answer, just say that "I don't know", don't try to make up an answer.
24
  ----------------
25
  {summaries}
 
35
 
36
  chain_type_kwargs = {"prompt": prompt}
37
 
38
+ chain = RetrievalQAWithSourcesChain.from_chain_type(
39
  llm=llm,
40
  chain_type="stuff",
41
  retriever=retriever,
 
52
 
53
  bot_message = result["answer"]
54
 
 
 
 
 
 
 
 
 
 
 
 
55
  # 채팅 기록에 사용자의 메시지와 봇의 응답을 추가합니다.
56
  chat_history.append((message, bot_message))
57