xl2533 commited on
Commit
d067a6a
·
1 Parent(s): ed98174

change question and summary in chinese

Browse files
app.py CHANGED
@@ -15,9 +15,11 @@ from langchain.prompts.chat import (
15
  SystemMessagePromptTemplate,
16
  HumanMessagePromptTemplate,
17
  )
18
-
19
- from langchain.chains.summarize import load_summarize_chain
 
20
  from langchain.chains import QAGenerationChain
 
21
 
22
  # Streaming endpoint
23
  API_URL = "https://api.openai.com/v1/chat/completions"
@@ -44,19 +46,44 @@ def process(files, openai_api_key, max_tokens, model, n_sample):
44
  def get_question(docs, openai_api_key, max_tokens, n_sample=5):
45
  q_list = []
46
  llm = ChatOpenAI(openai_api_key=openai_api_key, max_tokens=max_tokens, temperature=0)
47
- chain = QAGenerationChain.from_llm(llm)
 
 
 
 
 
 
 
48
  print('Generating Question from template')
49
  for i in range(n_sample):
50
  qa = chain.run(docs[i].page_content)[0]
51
  print(qa)
52
- q_list.append(f"问题{i+1}: {qa['question']}" )
53
  return '\n'.join(q_list)
54
 
55
 
56
- def get_summary(docs, openai_api_key, max_tokens, n_sample=5):
57
  llm = ChatOpenAI(openai_api_key=openai_api_key, max_tokens=max_tokens)
58
- chain = load_summarize_chain(llm, chain_type="map_reduce")
 
59
  print('Generating Summary from tempalte')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  summary = chain.run(docs[:n_sample])
61
  print(summary)
62
  return summary
@@ -127,8 +154,8 @@ with gr.Blocks(css="""#col_container {width: 1000px; margin-left: auto; margin-r
127
  run = gr.Button('研报解读')
128
 
129
  with gr.Column():
130
- summary = gr.Textbox(type='text', label="本文摘要")
131
- question = gr.Textbox(type='text', label='提问问题')
132
 
133
  chatbot = gr.Chatbot(elem_id='chatbot')
134
  inputs = gr.Textbox(placeholder="这篇文档是关于什么的", label="针对文档你有哪些问题?")
@@ -143,8 +170,8 @@ with gr.Blocks(css="""#col_container {width: 1000px; margin-left: auto; margin-r
143
  [inputs, openai_api_key, max_tokens, model, chat_counter, chatbot, state],
144
  [chatbot, state, chat_counter], )
145
  start.click(predict,
146
- [inputs, openai_api_key, max_tokens, model, chat_counter, chatbot, state],
147
- [chatbot, state, chat_counter], )
148
 
149
  # 每次对话结束都重置对话
150
  clear.click(reset_textbox, [], [inputs], queue=False)
 
15
  SystemMessagePromptTemplate,
16
  HumanMessagePromptTemplate,
17
  )
18
+ from langchain.prompts import PromptTemplate
19
+ from langchain.chains.llm import LLMChain
20
+ from langchain.chains.combine_documents.map_reduce import MapReduceDocumentsChain
21
  from langchain.chains import QAGenerationChain
22
+ from langchain.chains.combine_documents.stuff import StuffDocumentsChain
23
 
24
  # Streaming endpoint
25
  API_URL = "https://api.openai.com/v1/chat/completions"
 
46
  def get_question(docs, openai_api_key, max_tokens, n_sample=5):
47
  q_list = []
48
  llm = ChatOpenAI(openai_api_key=openai_api_key, max_tokens=max_tokens, temperature=0)
49
+ # 基于文档进行QA生成
50
+ prompt = ChatPromptTemplate.from_messages(
51
+ [
52
+ SystemMessagePromptTemplate.from_template(MyTemplate['qa_sys_template']),
53
+ HumanMessagePromptTemplate.from_template(MyTemplate['qa_user_template']),
54
+ ]
55
+ )
56
+ chain = QAGenerationChain.from_llm(llm, prompt=prompt)
57
  print('Generating Question from template')
58
  for i in range(n_sample):
59
  qa = chain.run(docs[i].page_content)[0]
60
  print(qa)
61
+ q_list.append(f"问题{i + 1}: {qa['question']}")
62
  return '\n'.join(q_list)
63
 
64
 
65
+ def get_summary(docs, openai_api_key, max_tokens, n_sample=5, verbose=None):
66
  llm = ChatOpenAI(openai_api_key=openai_api_key, max_tokens=max_tokens)
67
+ # chain = load_summarize_chain(llm, chain_type="map_reduce")
68
+ # summary = chain.run(docs[:n_sample])
69
  print('Generating Summary from tempalte')
70
+
71
+ map_prompt = PromptTemplate(template=MyTemplate['summary_template'], input_variables=["text"])
72
+ combine_prompt = PromptTemplate(template=MyTemplate['summary_template'], input_variables=["text"])
73
+ map_chain = LLMChain(llm=llm, prompt=map_prompt, verbose=verbose)
74
+ reduce_chain = LLMChain(llm=llm, prompt=combine_prompt, verbose=verbose)
75
+ combine_document_chain = StuffDocumentsChain(
76
+ llm_chain=reduce_chain,
77
+ document_variable_name='text',
78
+ verbose=verbose,
79
+ )
80
+ chain = MapReduceDocumentsChain(
81
+ llm_chain=map_chain,
82
+ combine_document_chain=combine_document_chain,
83
+ document_variable_name='text',
84
+ collapse_document_chain=None,
85
+ verbose=verbose
86
+ )
87
  summary = chain.run(docs[:n_sample])
88
  print(summary)
89
  return summary
 
154
  run = gr.Button('研报解读')
155
 
156
  with gr.Column():
157
+ summary = gr.Textbox(type='text', label="一眼看尽 - 文档概览")
158
+ question = gr.Textbox(type='text', label='推荐问题 - 问别的也行哟')
159
 
160
  chatbot = gr.Chatbot(elem_id='chatbot')
161
  inputs = gr.Textbox(placeholder="这篇文档是关于什么的", label="针对文档你有哪些问题?")
 
170
  [inputs, openai_api_key, max_tokens, model, chat_counter, chatbot, state],
171
  [chatbot, state, chat_counter], )
172
  start.click(predict,
173
+ [inputs, openai_api_key, max_tokens, model, chat_counter, chatbot, state],
174
+ [chatbot, state, chat_counter], )
175
 
176
  # 每次对话结束都重置对话
177
  clear.click(reset_textbox, [], [inputs], queue=False)
prompts/__init__.py CHANGED
@@ -14,9 +14,23 @@ with open("prompts/chat_reduce_prompt.txt", "r") as f:
14
  chat_reduce_template = f.read()
15
 
16
 
 
 
 
 
 
 
 
 
 
 
 
17
  MyTemplate ={
18
  'chat_reduce_template': chat_reduce_template,
19
  'chat_combine_template': chat_combine_template,
20
  'template_hist': template_hist,
21
- 'template':template
 
 
 
22
  }
 
14
  chat_reduce_template = f.read()
15
 
16
 
17
+ with open("prompts/qa_sys_prompt.txt", "r") as f:
18
+ qa_sys_prompt = f.read()
19
+
20
+ with open("prompts/qa_user_prompt.txt", "r") as f:
21
+ qa_user_prompt = f.read()
22
+
23
+
24
+ with open("prompts/summary_prompt.txt", "r") as f:
25
+ summary_prompt = f.read()
26
+
27
+
28
  MyTemplate ={
29
  'chat_reduce_template': chat_reduce_template,
30
  'chat_combine_template': chat_combine_template,
31
  'template_hist': template_hist,
32
+ 'template':template,
33
+ 'qa_sys_template': qa_sys_prompt,
34
+ 'qa_user_template': qa_user_prompt,
35
+ 'summary_template': summary_prompt
36
  }
prompts/qa_sys_prompt.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ You are a smart assistant designed to help high school teachers come up with reading comprehension questions.
2
+ Given a piece of text, you must come up with a question and answer pair that can be used to test a student's reading comprehension abilities.
3
+ When coming up with this question/answer pair, you must respond in the following format, and always respond in chinese:
4
+ ```
5
+ {{
6
+ "question": "$YOUR_QUESTION_HERE",
7
+ "answer": "$THE_ANSWER_HERE"
8
+ }}
9
+ ```
10
+
11
+ Everything between the ``` must be valid json.
prompts/qa_user_prompt.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ Please come up with a question/answer pair in chinese, in the specified JSON format, for the following text:
2
+ ----------------
3
+ {text}
prompts/summary_prompt.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ Write a concise summary of the following in chinese:
2
+
3
+
4
+ "{text}"
5
+
6
+
7
+ CONCISE SUMMARY: