peichao.dong commited on
Commit
405c09f
·
1 Parent(s): cbbcc75

add python code tool

Browse files
Files changed (2) hide show
  1. agents/tools/python_code_tool.py +76 -0
  2. app.py +19 -6
agents/tools/python_code_tool.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from langchain import LLMChain, OpenAI, PromptTemplate
3
+ from langchain.chat_models import ChatOpenAI
4
+ from langchain.agents import tool
5
+ from langchain.utilities import PythonREPL
6
+
7
+
8
+ generate_python_code = """
9
+ 请用python写一段代码实现如下需求:
10
+
11
+ ---
12
+ {input}
13
+ ---
14
+ """
15
+
16
+ generate_python_code_promopt = PromptTemplate(input_variables=["input"], template=generate_python_code,)
17
+
18
+ generate_code_chain = LLMChain(llm = OpenAI(temperature=0.1), prompt=generate_python_code_promopt, output_key="code")
19
+
20
+
21
+ @tool("Generate and Excute Python Code ", return_direct=True)
22
+ def generate_and_excute_python_code(input: str) -> str:
23
+ '''useful for when you need to generate python code and excute it'''
24
+ answer_code = generate_code_chain.run(input)
25
+ print(answer_code)
26
+ start = answer_code.find('python') + len('python')
27
+ end = answer_code.rfind('```')
28
+ code_content = answer_code[start:end].strip()
29
+
30
+ print(code_content)
31
+ python_repl = PythonREPL()
32
+ result = python_repl.run(code_content)
33
+ return f"""
34
+ code:
35
+ ```
36
+ {code_content}
37
+ ```
38
+
39
+ execute result:
40
+ ---
41
+ {result}
42
+ ---
43
+ """
44
+
45
+
46
+
47
+ if __name__ == "__main__":
48
+ input = """
49
+ 我有一个json文件url为: https://artwork-assets-staging-sbux.starbucks.com.cn/accountavatars.json
50
+ 并按照如下Example进行格式转换
51
+ 文件格式为:
52
+ ```
53
+ {
54
+ 'artworks': {
55
+ 'file1.png': {
56
+ 'middle@1x': '***',
57
+ 'middle@2x': '***',
58
+ 'middle@3x': '***'
59
+ },
60
+ 'file2.png': {
61
+ 'middle@1x': '***',
62
+ 'middle@2x': '***',
63
+ 'middle@3x': '***'
64
+ }
65
+ }
66
+ }
67
+ ```
68
+ 输出格式:
69
+ ```
70
+ curl https://active.stg.starbucks.com.cn/accountAvatar/file1.png
71
+ curl https://active.stg.starbucks.com.cn/accountAvatar/file2.png
72
+ ```
73
+ """
74
+
75
+ result = generate_and_excute_python_code(input)
76
+ print(result)
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  from langchain.chains import LLMChain
3
  from langchain.chat_models import ChatOpenAI
4
  from langchain.document_loaders import TextLoader
 
5
  from chains import HumanFeedBackChain, contextRewriteChain
6
  from embedding import CustomEmbedding
7
  from memories import HumenFeedbackBufferMemory
@@ -91,7 +92,10 @@ def rewriteContext(input, chatbot):
91
  chatbot.append((input, response))
92
  return chatbot, response
93
 
94
-
 
 
 
95
 
96
  toolTextBox = []
97
  with gr.Blocks() as demo:
@@ -99,7 +103,7 @@ with gr.Blocks() as demo:
99
  with gr.Tab("Business"):
100
  with gr.Row():
101
  with gr.Column():
102
- chatbot = gr.Chatbot(elem_id="chatbot").style()
103
  with gr.Row():
104
  txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(
105
  container=False)
@@ -123,12 +127,12 @@ with gr.Blocks() as demo:
123
 
124
  with gr.Tab("Tech"):
125
  with gr.Row():
126
- with gr.Column(min_width="50%"):
127
- code_chatbot = gr.Chatbot(elem_id="chatbot").style()
128
  with gr.Row():
129
  code = gr.Textbox(show_label=False, label="Code Generate", placeholder="Enter text and press enter").style(
130
  container=False)
131
- with gr.Column(min_width="50%"):
132
  with gr.Row():
133
  code_context = gr.Textbox(show_label=True, label="Context", placeholder="Enter Context").style(
134
  container=False)
@@ -142,12 +146,21 @@ with gr.Blocks() as demo:
142
  sendMessageByMultiPart, [code_chatbot, code_context, relateCode, toolTextBox[index]], [code_chatbot]).then(
143
  generateCodeByMultiPart, [code_context, relateCode, toolTextBox[index], code_chatbot], [code_chatbot, code])
144
  with gr.Tab("FAQ"):
145
- faq_chatbot = gr.Chatbot(elem_id="chatbot").style()
146
  with gr.Row():
147
  faq = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(
148
  container=False)
149
  with gr.Row():
150
  gr.Button("Regenerate embedding").click(generateEmbeddings,[faq_chatbot], [faq_chatbot])
 
 
 
 
 
 
 
 
 
151
 
152
 
153
  txt.submit(sendMessage, [chatbot, txt], [chatbot]).then(
 
2
  from langchain.chains import LLMChain
3
  from langchain.chat_models import ChatOpenAI
4
  from langchain.document_loaders import TextLoader
5
+ from agents.tools.python_util_tool import generate_and_excute_python_code
6
  from chains import HumanFeedBackChain, contextRewriteChain
7
  from embedding import CustomEmbedding
8
  from memories import HumenFeedbackBufferMemory
 
92
  chatbot.append((input, response))
93
  return chatbot, response
94
 
95
+ def generateCodeAndExcute(input, chatbot=[]):
96
+ result = generate_and_excute_python_code.run(input)
97
+ chatbot.append((input, result))
98
+ return chatbot
99
 
100
  toolTextBox = []
101
  with gr.Blocks() as demo:
 
103
  with gr.Tab("Business"):
104
  with gr.Row():
105
  with gr.Column():
106
+ chatbot = gr.Chatbot().style()
107
  with gr.Row():
108
  txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(
109
  container=False)
 
127
 
128
  with gr.Tab("Tech"):
129
  with gr.Row():
130
+ with gr.Column():
131
+ code_chatbot = gr.Chatbot().style()
132
  with gr.Row():
133
  code = gr.Textbox(show_label=False, label="Code Generate", placeholder="Enter text and press enter").style(
134
  container=False)
135
+ with gr.Column():
136
  with gr.Row():
137
  code_context = gr.Textbox(show_label=True, label="Context", placeholder="Enter Context").style(
138
  container=False)
 
146
  sendMessageByMultiPart, [code_chatbot, code_context, relateCode, toolTextBox[index]], [code_chatbot]).then(
147
  generateCodeByMultiPart, [code_context, relateCode, toolTextBox[index], code_chatbot], [code_chatbot, code])
148
  with gr.Tab("FAQ"):
149
+ faq_chatbot = gr.Chatbot().style(max_width="50%")
150
  with gr.Row():
151
  faq = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(
152
  container=False)
153
  with gr.Row():
154
  gr.Button("Regenerate embedding").click(generateEmbeddings,[faq_chatbot], [faq_chatbot])
155
+ with gr.Tab("TOOL"):
156
+ with gr.Row():
157
+ with gr.Column():
158
+ tool_request = gr.Textbox(show_label=False, placeholder="Enter your tool Request").style(
159
+ container=False, show_copy_button=True)
160
+ tool_button = gr.Button("Generate Code and Execute")
161
+ with gr.Column():
162
+ tool_chatbot = gr.Chatbot(elem_id="chatbot").style(container=False)
163
+ tool_button.click(generateCodeAndExcute,[tool_request, tool_chatbot], [tool_chatbot])
164
 
165
 
166
  txt.submit(sendMessage, [chatbot, txt], [chatbot]).then(