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

alter prompt

Browse files
Files changed (3) hide show
  1. app.py +11 -14
  2. prompts/qa_sys_prompt.txt +1 -1
  3. prompts/summary_prompt.txt +1 -1
app.py CHANGED
@@ -25,6 +25,7 @@ from langchain.chains.combine_documents.stuff import StuffDocumentsChain
25
  API_URL = "https://api.openai.com/v1/chat/completions"
26
  cohere_key = '5IRbILAbjTI0VcqTsktBfKsr13Lych9iBAFbLpkj'
27
  faiss_store = './output/'
 
28
 
29
 
30
  def process(files, openai_api_key, max_tokens, model, n_sample):
@@ -66,8 +67,7 @@ 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)
@@ -89,18 +89,17 @@ def get_summary(docs, openai_api_key, max_tokens, n_sample=5, verbose=None):
89
  return summary
90
 
91
 
92
- def predict(inputs, openai_api_key, max_tokens, model, chat_counter, chatbot=[], history=[]):
93
- model = model[0]
94
  print(f"chat_counter - {chat_counter}")
95
  print(f'Histroy - {history}') # History: Original Input and Output in flatten list
96
  print(f'chatbot - {chatbot}') # Chat Bot: 上一轮回复的[[user, AI]]
97
 
98
  history.append(inputs)
99
- print(f'loading faiss store from {faiss_store}')
100
- if model == 'openai':
101
  docsearch = FAISS.load_local(faiss_store, OpenAIEmbeddings(openai_api_key=openai_api_key))
102
  else:
103
- docsearch = FAISS.load_local(faiss_store, CohereEmbeddings(cohere_api_key=cohere_key))
104
  # 构建模板
105
  llm = ChatOpenAI(openai_api_key=openai_api_key, max_tokens=max_tokens)
106
  messages_combine = [
@@ -134,7 +133,7 @@ def reset_textbox():
134
 
135
  with gr.Blocks(css="""#col_container {width: 1000px; margin-left: auto; margin-right: auto;}
136
  #chatbot {height: 520px; overflow: auto;}""") as demo:
137
- gr.HTML("""<h1 align="center">🚀Your Doc Reader🚀</h1>""")
138
  with gr.Column(elem_id="col_container"):
139
  openai_api_key = gr.Textbox(type='password', label="输入 API Key")
140
 
@@ -142,7 +141,6 @@ with gr.Blocks(css="""#col_container {width: 1000px; margin-left: auto; margin-r
142
  with gr.Row():
143
  max_tokens = gr.Slider(minimum=100, maximum=2000, value=1000, step=100, interactive=True,
144
  label="字数")
145
- model = gr.CheckboxGroup(["cohere", "openai"])
146
  chat_counter = gr.Number(value=0, precision=0, label='对话轮数')
147
  n_sample = gr.Slider(minimum=3, maximum=5, value=3, step=1, interactive=True,
148
  label="问题数")
@@ -151,7 +149,7 @@ with gr.Blocks(css="""#col_container {width: 1000px; margin-left: auto; margin-r
151
  with gr.Row():
152
  with gr.Column():
153
  files = gr.File(file_count="multiple", file_types=[".pdf"], label='上传pdf文件')
154
- run = gr.Button('研报解读')
155
 
156
  with gr.Column():
157
  summary = gr.Textbox(type='text', label="一眼看尽 - 文档概览")
@@ -165,16 +163,15 @@ with gr.Blocks(css="""#col_container {width: 1000px; margin-left: auto; margin-r
165
  clear = gr.Button("清空")
166
  start = gr.Button("提问")
167
 
168
- run.click(process, [files, openai_api_key, max_tokens, model, n_sample], [question, summary])
169
  inputs.submit(predict,
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)
178
  inputs.submit(reset_textbox, [], [inputs])
179
-
180
  demo.queue().launch(debug=True)
 
25
  API_URL = "https://api.openai.com/v1/chat/completions"
26
  cohere_key = '5IRbILAbjTI0VcqTsktBfKsr13Lych9iBAFbLpkj'
27
  faiss_store = './output/'
28
+ global doc_search
29
 
30
 
31
  def process(files, openai_api_key, max_tokens, model, n_sample):
 
67
  llm = ChatOpenAI(openai_api_key=openai_api_key, max_tokens=max_tokens)
68
  # chain = load_summarize_chain(llm, chain_type="map_reduce")
69
  # summary = chain.run(docs[:n_sample])
70
+ print('Generating Summary from template')
 
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)
 
89
  return summary
90
 
91
 
92
+ def predict(inputs, openai_api_key, max_tokens, chat_counter, chatbot=[], history=[]):
 
93
  print(f"chat_counter - {chat_counter}")
94
  print(f'Histroy - {history}') # History: Original Input and Output in flatten list
95
  print(f'chatbot - {chatbot}') # Chat Bot: 上一轮回复的[[user, AI]]
96
 
97
  history.append(inputs)
98
+ if doc_search is None:
99
+ print(f'loading faiss store from {faiss_store}')
100
  docsearch = FAISS.load_local(faiss_store, OpenAIEmbeddings(openai_api_key=openai_api_key))
101
  else:
102
+ print('faiss already loaded')
103
  # 构建模板
104
  llm = ChatOpenAI(openai_api_key=openai_api_key, max_tokens=max_tokens)
105
  messages_combine = [
 
133
 
134
  with gr.Blocks(css="""#col_container {width: 1000px; margin-left: auto; margin-right: auto;}
135
  #chatbot {height: 520px; overflow: auto;}""") as demo:
136
+ gr.HTML("""<h1 align="center">🚀Smart Doc Reader🚀</h1>""")
137
  with gr.Column(elem_id="col_container"):
138
  openai_api_key = gr.Textbox(type='password', label="输入 API Key")
139
 
 
141
  with gr.Row():
142
  max_tokens = gr.Slider(minimum=100, maximum=2000, value=1000, step=100, interactive=True,
143
  label="字数")
 
144
  chat_counter = gr.Number(value=0, precision=0, label='对话轮数')
145
  n_sample = gr.Slider(minimum=3, maximum=5, value=3, step=1, interactive=True,
146
  label="问题数")
 
149
  with gr.Row():
150
  with gr.Column():
151
  files = gr.File(file_count="multiple", file_types=[".pdf"], label='上传pdf文件')
152
+ run = gr.Button('文档内容解读')
153
 
154
  with gr.Column():
155
  summary = gr.Textbox(type='text', label="一眼看尽 - 文档概览")
 
163
  clear = gr.Button("清空")
164
  start = gr.Button("提问")
165
 
166
+ run.click(process, [files, openai_api_key, max_tokens, n_sample], [question, summary])
167
  inputs.submit(predict,
168
+ [inputs, openai_api_key, max_tokens, chat_counter, chatbot, state],
169
  [chatbot, state, chat_counter], )
170
  start.click(predict,
171
+ [inputs, openai_api_key, max_tokens, chat_counter, chatbot, state],
172
  [chatbot, state, chat_counter], )
173
 
174
  # 每次对话结束都重置对话
175
  clear.click(reset_textbox, [], [inputs], queue=False)
176
  inputs.submit(reset_textbox, [], [inputs])
 
177
  demo.queue().launch(debug=True)
prompts/qa_sys_prompt.txt CHANGED
@@ -1,5 +1,5 @@
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
  {{
 
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 irrelevant to picture, relevant to the core concept of the text, 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
  {{
prompts/summary_prompt.txt CHANGED
@@ -1,4 +1,4 @@
1
- Write a concise summary of the following in chinese:
2
 
3
 
4
  "{text}"
 
1
+ Write a concise summary of the following in chinese, ignore the content in footnote, appendix or sidebar:
2
 
3
 
4
  "{text}"