curry tang commited on
Commit
f6c9b66
·
1 Parent(s): 1528aed
Files changed (2) hide show
  1. app.py +36 -25
  2. prompts.py +27 -0
app.py CHANGED
@@ -5,7 +5,7 @@ from config import settings
5
  import base64
6
  from PIL import Image
7
  import io
8
- from prompts import web_prompt, explain_code_template, optimize_code_template, debug_code_template, function_gen_template, translate_doc_template
9
  from banner import banner_md
10
  from langchain_core.prompts import PromptTemplate
11
 
@@ -27,8 +27,8 @@ def get_default_chat():
27
  return _llm.get_chat_engine()
28
 
29
 
30
- def predict(message, history, chat):
31
- print('!!!!!', message, history, chat)
32
  history_len = len(history)
33
  files_len = len(message.files)
34
  if chat is None:
@@ -40,22 +40,24 @@ def predict(message, history, chat):
40
  history_messages.append(AIMessage(content=assistant))
41
 
42
  if history_len == 0:
43
- history_messages.append(SystemMessage(content=web_prompt))
44
-
45
- history_messages.append(HumanMessage(content=message.text))
46
- # if files_len == 0:
47
- # history_messages.append(HumanMessage(content=message.text))
48
- # else:
49
- # file = message.files[0]
50
- # with Image.open(file.path) as img:
51
- # buffer = io.BytesIO()
52
- # img = img.convert('RGB')
53
- # img.save(buffer, format="JPEG")
54
- # image_data = base64.b64encode(buffer.getvalue()).decode("utf-8")
55
- # history_messages.append(HumanMessage(content=[
56
- # {"type": "text", "text": message.text},
57
- # {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_data}"}}
58
- # ]))
 
 
59
 
60
  response_message = ''
61
  for chunk in chat.stream(history_messages):
@@ -142,8 +144,13 @@ def translate_doc(_language_input, _language_output, _doc, _chat):
142
  yield response_message
143
 
144
 
 
 
 
 
145
  with gr.Blocks() as app:
146
  chat_engine = gr.State(value=None)
 
147
  with gr.Row(variant='panel'):
148
  gr.Markdown(banner_md)
149
  with gr.Accordion('模型参数设置', open=False):
@@ -199,18 +206,22 @@ with gr.Blocks() as app:
199
  with gr.Tab('智能聊天'):
200
  with gr.Row():
201
  with gr.Column(scale=2, min_width=600):
202
- chatbot = gr.ChatInterface(
 
203
  predict,
 
204
  multimodal=True,
205
- chatbot=gr.Chatbot(elem_id="chatbot", height=600, show_share_button=False),
206
  textbox=gr.MultimodalTextbox(interactive=True, file_types=["image"]),
207
- additional_inputs=[chat_engine],
 
 
 
208
  )
209
  with gr.Column(scale=1, min_width=300):
210
  with gr.Accordion("助手类型"):
211
- gr.Radio(["前端助手", "开发助手", "文案助手"], label="类型", info="请选择类型"),
212
- with gr.Accordion("图片"):
213
- gr.ImageEditor()
214
 
215
  with gr.Tab('代码优化'):
216
  with gr.Row():
 
5
  import base64
6
  from PIL import Image
7
  import io
8
+ from prompts import web_prompt, explain_code_template, optimize_code_template, debug_code_template, function_gen_template, translate_doc_template, backend_developer_prompt
9
  from banner import banner_md
10
  from langchain_core.prompts import PromptTemplate
11
 
 
27
  return _llm.get_chat_engine()
28
 
29
 
30
+ def predict(message, history, chat, _current_assistant):
31
+ print('!!!!!', message, history, chat, _current_assistant)
32
  history_len = len(history)
33
  files_len = len(message.files)
34
  if chat is None:
 
40
  history_messages.append(AIMessage(content=assistant))
41
 
42
  if history_len == 0:
43
+ assistant_prompt = web_prompt
44
+ if _current_assistant == '后端开发助手':
45
+ assistant_prompt = backend_developer_prompt
46
+ history_messages.append(SystemMessage(content=assistant_prompt))
47
+
48
+ if files_len == 0:
49
+ history_messages.append(HumanMessage(content=message.text))
50
+ else:
51
+ file = message.files[0]
52
+ with Image.open(file.path) as img:
53
+ buffer = io.BytesIO()
54
+ img = img.convert('RGB')
55
+ img.save(buffer, format="JPEG")
56
+ image_data = base64.b64encode(buffer.getvalue()).decode("utf-8")
57
+ history_messages.append(HumanMessage(content=[
58
+ {"type": "text", "text": message.text},
59
+ {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_data}"}}
60
+ ]))
61
 
62
  response_message = ''
63
  for chunk in chat.stream(history_messages):
 
144
  yield response_message
145
 
146
 
147
+ def assistant_type_update(_assistant_type: str):
148
+ return _assistant_type, [], []
149
+
150
+
151
  with gr.Blocks() as app:
152
  chat_engine = gr.State(value=None)
153
+ current_assistant = gr.State(value='前端开发助手')
154
  with gr.Row(variant='panel'):
155
  gr.Markdown(banner_md)
156
  with gr.Accordion('模型参数设置', open=False):
 
206
  with gr.Tab('智能聊天'):
207
  with gr.Row():
208
  with gr.Column(scale=2, min_width=600):
209
+ chatbot = gr.Chatbot(elem_id="chatbot", height=600, show_share_button=False, type='messages')
210
+ chat_interface = gr.ChatInterface(
211
  predict,
212
+ type="messages",
213
  multimodal=True,
214
+ chatbot=chatbot,
215
  textbox=gr.MultimodalTextbox(interactive=True, file_types=["image"]),
216
+ additional_inputs=[chat_engine, current_assistant],
217
+ clear_btn='🗑️ 清空',
218
+ undo_btn='↩️ 撤销',
219
+ retry_btn='🔄 重试',
220
  )
221
  with gr.Column(scale=1, min_width=300):
222
  with gr.Accordion("助手类型"):
223
+ assistant_type = gr.Radio(["前端开发助手", "后端开发助手", "数据分析师"], label="类型", info="请选择类型", value='前端开发助手')
224
+ assistant_type.change(fn=assistant_type_update, inputs=[assistant_type], outputs=[current_assistant, chat_interface.chatbot_state, chatbot])
 
225
 
226
  with gr.Tab('代码优化'):
227
  with gr.Row():
prompts.py CHANGED
@@ -24,6 +24,33 @@ OUTPUT_CODE
24
  Conduct Security and Operational reviews of PLANNING and OUTPUT, paying particular attention to things that may compromise data or introduce vulnerabilities. For sensitive changes (e.g. Input Handling, Monetary Calculations, Authentication) conduct a thorough review showing your analysis between <SECURITY_REVIEW> tags.
25
  '''
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  explain_code_template = '''
28
  你的任务是获取提供的代码片段,并用简单易懂的语言解释它,假设读者是一个刚刚学习了语言特性和基本语法的初学程序员。
29
  重点解释:
 
24
  Conduct Security and Operational reviews of PLANNING and OUTPUT, paying particular attention to things that may compromise data or introduce vulnerabilities. For sensitive changes (e.g. Input Handling, Monetary Calculations, Authentication) conduct a thorough review showing your analysis between <SECURITY_REVIEW> tags.
25
  '''
26
 
27
+
28
+ backend_developer_prompt = '''
29
+ You are a seasoned WEB backend development expert with mastery in Python, Node.js, Java, PHP, and Go. Your extensive experience spans numerous frameworks, including Django, Flask, FastAPI, Express, NestJS, Laravel, and Symfony. Your expertise extends to databases, where you excel in both relational databases like MySQL and PostgreSQL, as well as NoSQL solutions such as Redis and MongoDB.Don't apologise unnecessarily. Review the conversation history for mistakes and avoid repeating them.
30
+
31
+ During our conversation break things down in to discrete changes, and suggest a small test after each stage to make sure things are on the right track.
32
+
33
+ Only produce code to illustrate examples, or when directed to in the conversation. If you can answer without code, that is preferred, and you will be asked to elaborate if it is required.
34
+
35
+ Request clarification for anything unclear or ambiguous.
36
+
37
+ Before writing or suggesting code, perform a comprehensive code review of the existing code and describe how it works between <CODE_REVIEW> tags.
38
+
39
+ After completing the code review, construct a plan for the change between <PLANNING> tags. Ask for additional source files or documentation that may be relevant. The plan should avoid duplication (DRY principle), and balance maintenance and flexibility. Present trade-offs and implementation choices at this step. Consider available Frameworks and Libraries and suggest their use when relevant. STOP at this step if we have not agreed a plan.
40
+
41
+ Once agreed, produce code between <OUTPUT> tags. Pay attention to Variable Names, Identifiers and String Literals, and check that they are reproduced accurately from the original source files unless otherwise directed. When naming by convention surround in double colons and in ::UPPERCASE:: Maintain existing code style, use language appropriate idioms.
42
+
43
+ Always produce code starting with a new line, and in blocks (```) with the language specified:
44
+
45
+ ```Python
46
+
47
+ OUTPUT_CODE
48
+
49
+ ```
50
+
51
+ Conduct Security and Operational reviews of PLANNING and OUTPUT, paying particular attention to things that may compromise data or introduce vulnerabilities. For sensitive changes (e.g. Input Handling, Monetary Calculations, Authentication) conduct a thorough review showing your analysis between <SECURITY_REVIEW> tags.
52
+ '''
53
+
54
  explain_code_template = '''
55
  你的任务是获取提供的代码片段,并用简单易懂的语言解释它,假设读者是一个刚刚学习了语言特性和基本语法的初学程序员。
56
  重点解释: