Spaces:
Runtime error
Runtime error
wf4403
commited on
Commit
·
545c208
1
Parent(s):
ba7d219
initiate
Browse files- app.py +105 -0
- llama3.py +78 -0
- my_chat_interface.py +750 -0
- output_parser.py +141 -0
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import annotations
|
2 |
+
|
3 |
+
|
4 |
+
|
5 |
+
from utils.llama3 import LlaMa3
|
6 |
+
from utils.my_chat_interface import MyChatInterface
|
7 |
+
|
8 |
+
system_prompt = """You are an AI Agent who is proficient in solve complicated task.
|
9 |
+
Each step you should wirte executable code to fulfill user query. Any Response without code means the task is completed and you do not have another chance to submit code
|
10 |
+
|
11 |
+
You are equipped with a codeinterpreter. You can give the code and get the execution result of your code. You should use the codeinterpreter in the following format:
|
12 |
+
<|execute_start|>
|
13 |
+
```python
|
14 |
+
|
15 |
+
<your code>
|
16 |
+
|
17 |
+
```
|
18 |
+
<|execute_end|>
|
19 |
+
|
20 |
+
|
21 |
+
WARNING:Do not use cv2.waitKey(0) cv2.destroyAllWindows()!!! Or the program will be destoried
|
22 |
+
|
23 |
+
Each round, your answer should ALWAYS use the following format(Each of your response should contain code, until you complete the task):
|
24 |
+
|
25 |
+
|
26 |
+
Analyse:(Analyse the message you received and plan what you should do)
|
27 |
+
|
28 |
+
This Step Todo: One Subtask need to be done at this step
|
29 |
+
|
30 |
+
Code(WARNING:MAKE SURE YOU CODE FOLLOW THE FORMAT AND WRITE CODE OR THE TASK WILL BE FAILED):
|
31 |
+
<|execute_start|>
|
32 |
+
```python
|
33 |
+
|
34 |
+
<your code>
|
35 |
+
|
36 |
+
|
37 |
+
```
|
38 |
+
<|execute_end|>
|
39 |
+
|
40 |
+
|
41 |
+
You will got the result of your code after each step. When the code of previous subtask is excuted successfully, you can write and excuet the code for next subtask
|
42 |
+
When all the code your write are executed and you got the code result that can fulfill the user query, you should summarize the previous analyse process and make a formal response to user, The response should follow this format:
|
43 |
+
WARNING:MAKE SURE YOU GET THE CODE EXECUTED RESULT THAT FULFILLED ALL REQUIREMENT OF USER BEFORE USE "Finished"
|
44 |
+
Finished: <Answer to user query>
|
45 |
+
|
46 |
+
Some notice:
|
47 |
+
1. When you want to draw a plot, use plt.savefig() and print the image path in markdown format instead of plt.show()
|
48 |
+
2. Save anything to current folder
|
49 |
+
3. End the process whenever you complete the task, When you do not have Action(Code), Use: Finished: <summary the analyse process and make response>
|
50 |
+
4. Do not ask for user input in your python code.
|
51 |
+
"""
|
52 |
+
|
53 |
+
|
54 |
+
LLM = LlaMa3(tools=None)
|
55 |
+
|
56 |
+
|
57 |
+
def chat(message, chat_history):
|
58 |
+
print("message:", message)
|
59 |
+
print("history:", chat_history)
|
60 |
+
|
61 |
+
def history_to_messages(message, history):
|
62 |
+
global system_prompt
|
63 |
+
messages = [{"role": "system", "content": system_prompt}]
|
64 |
+
for user_msg, assistant_msg in history:
|
65 |
+
if user_msg is not None:
|
66 |
+
messages.append({"role": "user", "content": user_msg})
|
67 |
+
if assistant_msg is not None:
|
68 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
69 |
+
if isinstance(message, str):
|
70 |
+
messages.append({"role": "user", "content": message})
|
71 |
+
elif isinstance(message, dict):
|
72 |
+
if "files" in message:
|
73 |
+
filepath = ",".join(message["files"])
|
74 |
+
if filepath.strip() != "":
|
75 |
+
messages.append(
|
76 |
+
{
|
77 |
+
"role": "user",
|
78 |
+
"content": f"[INFO]The data is uploaded to {filepath}",
|
79 |
+
}
|
80 |
+
)
|
81 |
+
messages.append({"role": "user", "content": message["text"]})
|
82 |
+
else:
|
83 |
+
messages.append({"role": "user", "content": message["text"]})
|
84 |
+
return messages
|
85 |
+
|
86 |
+
messages = history_to_messages(message, chat_history)
|
87 |
+
print("converted messages:", messages)
|
88 |
+
response = LLM.chat(messages=messages)
|
89 |
+
print("get response:", response)
|
90 |
+
return response
|
91 |
+
|
92 |
+
|
93 |
+
demo = MyChatInterface(
|
94 |
+
fn=chat,
|
95 |
+
examples=[
|
96 |
+
{"text": "draw a cute cat for me"},
|
97 |
+
{"text": "make a qrcode which links to www.modelbest.cn"},
|
98 |
+
{"text": "2的1000次方是多少?"},
|
99 |
+
],
|
100 |
+
title="CPMInterpreter",
|
101 |
+
multimodal=True,
|
102 |
+
)
|
103 |
+
|
104 |
+
if __name__ == "__main__":
|
105 |
+
demo.launch(share=True, allowed_paths=["."])
|
llama3.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from openai import OpenAI
|
2 |
+
# 首先把tool_dict 整合到system message里面
|
3 |
+
#from tooltransform import my_input_format
|
4 |
+
#from tooltransform import message_format
|
5 |
+
class LlaMa3():
|
6 |
+
def __init__(self,tools) -> None:
|
7 |
+
self.client= OpenAI(
|
8 |
+
base_url="http://localhost:8001/v1",
|
9 |
+
api_key="token-abc123",
|
10 |
+
)
|
11 |
+
self.tools=tools
|
12 |
+
self.name="Llama3"
|
13 |
+
|
14 |
+
def chat(self,messages):
|
15 |
+
#result=my_input_format(messages,tools=self.tools,tool_choice=None,output=None)
|
16 |
+
completion = self.client.chat.completions.create(
|
17 |
+
model="/data/zyl7353/models/codeinterpreter_0529-hf",
|
18 |
+
messages=messages,
|
19 |
+
temperature=0.2,
|
20 |
+
)
|
21 |
+
|
22 |
+
#content=completion.choices[0].message['content']['content']
|
23 |
+
#print("test",content)
|
24 |
+
|
25 |
+
return completion.choices[0].message.content
|
26 |
+
#client =
|
27 |
+
'''
|
28 |
+
messages=[
|
29 |
+
{"role": "system", "content": "你是一个AI助手"},
|
30 |
+
{"role":"user","content":"帮我计算1+1"},
|
31 |
+
{"role":"assistant","content":"好的,我会调用excute_python工具\n","tool_calls": [
|
32 |
+
{
|
33 |
+
"name": "excute_python",
|
34 |
+
"arguments": {
|
35 |
+
"code": "print(1+1)"
|
36 |
+
}
|
37 |
+
}
|
38 |
+
]}, # 如果有tool calls,那么拼接 <|tool_call|>
|
39 |
+
{"role":"tool","content":"2"},
|
40 |
+
{"role":"user","content":"帮我计算1+1"}
|
41 |
+
# {"role":"user","content":"请调用excute_python工具,计算1+10"}
|
42 |
+
]
|
43 |
+
#new_messages=[]
|
44 |
+
|
45 |
+
#for msg in messages:
|
46 |
+
# rsp=message_format(msg)
|
47 |
+
# print("rsp",rsp)
|
48 |
+
# new_messages.append(rsp)
|
49 |
+
tools= [
|
50 |
+
{
|
51 |
+
"name": "excute_python",
|
52 |
+
"description": "excute the python code and get result",
|
53 |
+
"parameters": {
|
54 |
+
"type": "object",
|
55 |
+
"properties": {
|
56 |
+
"code": {
|
57 |
+
"type": "string",
|
58 |
+
"description": "The code is going to be excuted"
|
59 |
+
},
|
60 |
+
},
|
61 |
+
"required": [
|
62 |
+
"code"
|
63 |
+
]
|
64 |
+
}
|
65 |
+
}
|
66 |
+
]
|
67 |
+
|
68 |
+
result=my_input_format(messages=messages,tools=tools,tool_choice=None,output=None)
|
69 |
+
print(result)
|
70 |
+
for msg in result:
|
71 |
+
|
72 |
+
print("tool_call_string" in msg.keys())
|
73 |
+
'''
|
74 |
+
|
75 |
+
if __name__=="__main__":
|
76 |
+
GPT=LlaMa3(tools=None)
|
77 |
+
rsp=GPT.chat([{"role":"syetem","content":"You are a helpful assistant"},{"role":"user","content":"Hi?"}])
|
78 |
+
print(rsp)
|
my_chat_interface.py
ADDED
@@ -0,0 +1,750 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
This file defines a useful high-level abstraction to build Gradio chatbots: ChatInterface.
|
3 |
+
"""
|
4 |
+
|
5 |
+
from __future__ import annotations
|
6 |
+
|
7 |
+
import inspect
|
8 |
+
import re
|
9 |
+
import traceback
|
10 |
+
from typing import AsyncGenerator, Callable, Literal, Union, cast
|
11 |
+
|
12 |
+
import anyio
|
13 |
+
from gradio.blocks import Blocks
|
14 |
+
from gradio.components import (
|
15 |
+
Button,
|
16 |
+
Chatbot,
|
17 |
+
Component,
|
18 |
+
Markdown,
|
19 |
+
MultimodalTextbox,
|
20 |
+
State,
|
21 |
+
Textbox,
|
22 |
+
get_component_instance,
|
23 |
+
)
|
24 |
+
from gradio.events import Dependency, on
|
25 |
+
from gradio.helpers import create_examples as Examples # noqa: N812
|
26 |
+
from gradio.helpers import special_args
|
27 |
+
from gradio.layouts import Accordion, Group, Row
|
28 |
+
from gradio.routes import Request
|
29 |
+
from gradio.themes import ThemeClass as Theme
|
30 |
+
from gradio.utils import SyncToAsyncIterator, async_iteration, async_lambda
|
31 |
+
from gradio_client.documentation import document
|
32 |
+
from langchain_experimental.tools.python.tool import PythonREPLTool
|
33 |
+
|
34 |
+
from utils.output_parser import parse_code_action
|
35 |
+
|
36 |
+
|
37 |
+
def replace_image_path(markdown_text):
|
38 |
+
pattern = r"!\[.*?\]\((.*?)\)"
|
39 |
+
# 替换图片链接并添加"file/"前缀
|
40 |
+
modified_text = re.sub(pattern, r"", markdown_text)
|
41 |
+
return modified_text
|
42 |
+
|
43 |
+
|
44 |
+
def execute_code(code_str: str, tool: PythonREPLTool):
|
45 |
+
"""Execute python code and return the execution result.
|
46 |
+
|
47 |
+
Args:
|
48 |
+
code_str (str): The code to be executed.
|
49 |
+
tool (PythonREPLTool): Python AST REPL tool.
|
50 |
+
|
51 |
+
Returns:
|
52 |
+
str: Code execution result.
|
53 |
+
"""
|
54 |
+
try:
|
55 |
+
result = tool.invoke(code_str)
|
56 |
+
result = str(result)
|
57 |
+
return result
|
58 |
+
except:
|
59 |
+
return traceback.format_exc()
|
60 |
+
|
61 |
+
|
62 |
+
def extract_code(response):
|
63 |
+
"""Extract code from the chatbot response.
|
64 |
+
|
65 |
+
Args:
|
66 |
+
response (str): The chatbot response.
|
67 |
+
|
68 |
+
Returns:
|
69 |
+
str: Extracted code.
|
70 |
+
"""
|
71 |
+
try:
|
72 |
+
code = response.split("```")[-2]
|
73 |
+
code = code.replace("python", "")
|
74 |
+
except:
|
75 |
+
code = None
|
76 |
+
return code
|
77 |
+
|
78 |
+
|
79 |
+
@document()
|
80 |
+
class MyChatInterface(Blocks):
|
81 |
+
"""
|
82 |
+
ChatInterface is Gradio's high-level abstraction for creating chatbot UIs, and allows you to create
|
83 |
+
a web-based demo around a chatbot model in a few lines of code. Only one parameter is required: fn, which
|
84 |
+
takes a function that governs the response of the chatbot based on the user input and chat history. Additional
|
85 |
+
parameters can be used to control the appearance and behavior of the demo.
|
86 |
+
|
87 |
+
Example:
|
88 |
+
import gradio as gr
|
89 |
+
|
90 |
+
def echo(message, history):
|
91 |
+
return message
|
92 |
+
|
93 |
+
demo = gr.ChatInterface(fn=echo, examples=["hello", "hola", "merhaba"], title="Echo Bot")
|
94 |
+
demo.launch()
|
95 |
+
Demos: chatinterface_multimodal, chatinterface_random_response, chatinterface_streaming_echo
|
96 |
+
Guides: creating-a-chatbot-fast, sharing-your-app
|
97 |
+
"""
|
98 |
+
|
99 |
+
def __init__(
|
100 |
+
self,
|
101 |
+
fn: Callable,
|
102 |
+
*,
|
103 |
+
multimodal: bool = False,
|
104 |
+
chatbot: Chatbot | None = None,
|
105 |
+
textbox: Textbox | MultimodalTextbox | None = None,
|
106 |
+
additional_inputs: str | Component | list[str | Component] | None = None,
|
107 |
+
additional_inputs_accordion_name: str | None = None,
|
108 |
+
additional_inputs_accordion: str | Accordion | None = None,
|
109 |
+
examples: list[str] | list[dict[str, str | list]] | list[list] | None = None,
|
110 |
+
cache_examples: bool | Literal["lazy"] | None = None,
|
111 |
+
examples_per_page: int = 10,
|
112 |
+
title: str | None = None,
|
113 |
+
description: str | None = None,
|
114 |
+
theme: Theme | str | None = None,
|
115 |
+
css: str | None = None,
|
116 |
+
js: str | None = None,
|
117 |
+
head: str | None = None,
|
118 |
+
analytics_enabled: bool | None = None,
|
119 |
+
submit_btn: str | None | Button = "Submit",
|
120 |
+
stop_btn: str | None | Button = "Stop",
|
121 |
+
retry_btn: str | None | Button = "🔄 Retry",
|
122 |
+
undo_btn: str | None | Button = "↩️ Undo",
|
123 |
+
clear_btn: str | None | Button = "🗑️ Clear",
|
124 |
+
autofocus: bool = True,
|
125 |
+
concurrency_limit: int | None | Literal["default"] = "default",
|
126 |
+
fill_height: bool = True,
|
127 |
+
delete_cache: tuple[int, int] | None = None,
|
128 |
+
mode: str = "prompt",
|
129 |
+
code_start_token: str = "<|execute_start|>\n```python\n",
|
130 |
+
code_end_token: str = "```\n<|execute_end|>",
|
131 |
+
tool_call_token: str = "<|tool_call|>",
|
132 |
+
):
|
133 |
+
"""
|
134 |
+
Parameters:
|
135 |
+
fn: The function to wrap the chat interface around. Should accept two parameters: a string input message and list of two-element lists of the form [[user_message, bot_message], ...] representing the chat history, and return a string response. See the Chatbot documentation for more information on the chat history format.
|
136 |
+
multimodal: If True, the chat interface will use a gr.MultimodalTextbox component for the input, which allows for the uploading of multimedia files. If False, the chat interface will use a gr.Textbox component for the input.
|
137 |
+
chatbot: An instance of the gr.Chatbot component to use for the chat interface, if you would like to customize the chatbot properties. If not provided, a default gr.Chatbot component will be created.
|
138 |
+
textbox: An instance of the gr.Textbox or gr.MultimodalTextbox component to use for the chat interface, if you would like to customize the textbox properties. If not provided, a default gr.Textbox or gr.MultimodalTextbox component will be created.
|
139 |
+
additional_inputs: An instance or list of instances of gradio components (or their string shortcuts) to use as additional inputs to the chatbot. If components are not already rendered in a surrounding Blocks, then the components will be displayed under the chatbot, in an accordion.
|
140 |
+
additional_inputs_accordion_name: Deprecated. Will be removed in a future version of Gradio. Use the `additional_inputs_accordion` parameter instead.
|
141 |
+
additional_inputs_accordion: If a string is provided, this is the label of the `gr.Accordion` to use to contain additional inputs. A `gr.Accordion` object can be provided as well to configure other properties of the container holding the additional inputs. Defaults to a `gr.Accordion(label="Additional Inputs", open=False)`. This parameter is only used if `additional_inputs` is provided.
|
142 |
+
examples: Sample inputs for the function; if provided, appear below the chatbot and can be clicked to populate the chatbot input. Should be a list of strings if `multimodal` is False, and a list of dictionaries (with keys `text` and `files`) if `multimodal` is True.
|
143 |
+
cache_examples: If True, caches examples in the server for fast runtime in examples. The default option in HuggingFace Spaces is True. The default option elsewhere is False.
|
144 |
+
examples_per_page: If examples are provided, how many to display per page.
|
145 |
+
title: a title for the interface; if provided, appears above chatbot in large font. Also used as the tab title when opened in a browser window.
|
146 |
+
description: a description for the interface; if provided, appears above the chatbot and beneath the title in regular font. Accepts Markdown and HTML content.
|
147 |
+
theme: Theme to use, loaded from gradio.themes.
|
148 |
+
css: Custom css as a string or path to a css file. This css will be included in the demo webpage.
|
149 |
+
js: Custom js as a string or path to a js file. The custom js should be in the form of a single js function. This function will automatically be executed when the page loads. For more flexibility, use the head parameter to insert js inside <script> tags.
|
150 |
+
head: Custom html to insert into the head of the demo webpage. This can be used to add custom meta tags, multiple scripts, stylesheets, etc. to the page.
|
151 |
+
analytics_enabled: Whether to allow basic telemetry. If None, will use GRADIO_ANALYTICS_ENABLED environment variable if defined, or default to True.
|
152 |
+
submit_btn: Text to display on the submit button. If None, no button will be displayed. If a Button object, that button will be used.
|
153 |
+
stop_btn: Text to display on the stop button, which replaces the submit_btn when the submit_btn or retry_btn is clicked and response is streaming. Clicking on the stop_btn will halt the chatbot response. If set to None, stop button functionality does not appear in the chatbot. If a Button object, that button will be used as the stop button.
|
154 |
+
retry_btn: Text to display on the retry button. If None, no button will be displayed. If a Button object, that button will be used.
|
155 |
+
undo_btn: Text to display on the delete last button. If None, no button will be displayed. If a Button object, that button will be used.
|
156 |
+
clear_btn: Text to display on the clear button. If None, no button will be displayed. If a Button object, that button will be used.
|
157 |
+
autofocus: If True, autofocuses to the textbox when the page loads.
|
158 |
+
concurrency_limit: If set, this is the maximum number of chatbot submissions that can be running simultaneously. Can be set to None to mean no limit (any number of chatbot submissions can be running simultaneously). Set to "default" to use the default concurrency limit (defined by the `default_concurrency_limit` parameter in `.queue()`, which is 1 by default).
|
159 |
+
fill_height: If True, the chat interface will expand to the height of window.
|
160 |
+
delete_cache: A tuple corresponding [frequency, age] both expressed in number of seconds. Every `frequency` seconds, the temporary files created by this Blocks instance will be deleted if more than `age` seconds have passed since the file was created. For example, setting this to (86400, 86400) will delete temporary files every day. The cache will be deleted entirely when the server restarts. If None, no cache deletion will occur.
|
161 |
+
"""
|
162 |
+
super().__init__(
|
163 |
+
analytics_enabled=analytics_enabled,
|
164 |
+
mode="chat_interface",
|
165 |
+
css=css,
|
166 |
+
title=title or "Gradio",
|
167 |
+
theme=theme,
|
168 |
+
js=js,
|
169 |
+
head=head,
|
170 |
+
fill_height=fill_height,
|
171 |
+
delete_cache=delete_cache,
|
172 |
+
)
|
173 |
+
self.multimodal = multimodal
|
174 |
+
self.concurrency_limit = concurrency_limit
|
175 |
+
self.fn = fn
|
176 |
+
self.is_async = inspect.iscoroutinefunction(
|
177 |
+
self.fn
|
178 |
+
) or inspect.isasyncgenfunction(self.fn)
|
179 |
+
self.is_generator = inspect.isgeneratorfunction(
|
180 |
+
self.fn
|
181 |
+
) or inspect.isasyncgenfunction(self.fn)
|
182 |
+
self.buttons: list[Button | None] = []
|
183 |
+
|
184 |
+
self.examples = examples
|
185 |
+
self.cache_examples = cache_examples
|
186 |
+
|
187 |
+
if additional_inputs:
|
188 |
+
if not isinstance(additional_inputs, list):
|
189 |
+
additional_inputs = [additional_inputs]
|
190 |
+
self.additional_inputs = [
|
191 |
+
get_component_instance(i)
|
192 |
+
for i in additional_inputs # type: ignore
|
193 |
+
]
|
194 |
+
else:
|
195 |
+
self.additional_inputs = []
|
196 |
+
if additional_inputs_accordion_name is not None:
|
197 |
+
print(
|
198 |
+
"The `additional_inputs_accordion_name` parameter is deprecated and will be removed in a future version of Gradio. Use the `additional_inputs_accordion` parameter instead."
|
199 |
+
)
|
200 |
+
self.additional_inputs_accordion_params = {
|
201 |
+
"label": additional_inputs_accordion_name
|
202 |
+
}
|
203 |
+
if additional_inputs_accordion is None:
|
204 |
+
self.additional_inputs_accordion_params = {
|
205 |
+
"label": "Additional Inputs",
|
206 |
+
"open": False,
|
207 |
+
}
|
208 |
+
elif isinstance(additional_inputs_accordion, str):
|
209 |
+
self.additional_inputs_accordion_params = {
|
210 |
+
"label": additional_inputs_accordion
|
211 |
+
}
|
212 |
+
elif isinstance(additional_inputs_accordion, Accordion):
|
213 |
+
self.additional_inputs_accordion_params = (
|
214 |
+
additional_inputs_accordion.recover_kwargs(
|
215 |
+
additional_inputs_accordion.get_config()
|
216 |
+
)
|
217 |
+
)
|
218 |
+
else:
|
219 |
+
raise ValueError(
|
220 |
+
f"The `additional_inputs_accordion` parameter must be a string or gr.Accordion, not {type(additional_inputs_accordion)}"
|
221 |
+
)
|
222 |
+
|
223 |
+
with self:
|
224 |
+
if title:
|
225 |
+
Markdown(
|
226 |
+
f"<h1 style='text-align: center; margin-bottom: 1rem'>{self.title}</h1>"
|
227 |
+
)
|
228 |
+
if description:
|
229 |
+
Markdown(description)
|
230 |
+
|
231 |
+
if chatbot:
|
232 |
+
self.chatbot = chatbot.render()
|
233 |
+
else:
|
234 |
+
self.chatbot = Chatbot(
|
235 |
+
label="Chatbot", scale=1, height=200 if fill_height else None
|
236 |
+
)
|
237 |
+
|
238 |
+
with Row():
|
239 |
+
for btn in [retry_btn, undo_btn, clear_btn]:
|
240 |
+
if btn is not None:
|
241 |
+
if isinstance(btn, Button):
|
242 |
+
btn.render()
|
243 |
+
elif isinstance(btn, str):
|
244 |
+
btn = Button(
|
245 |
+
btn, variant="secondary", size="sm", min_width=60
|
246 |
+
)
|
247 |
+
else:
|
248 |
+
raise ValueError(
|
249 |
+
f"All the _btn parameters must be a gr.Button, string, or None, not {type(btn)}"
|
250 |
+
)
|
251 |
+
self.buttons.append(btn) # type: ignore
|
252 |
+
|
253 |
+
with Group():
|
254 |
+
with Row():
|
255 |
+
if textbox:
|
256 |
+
if self.multimodal:
|
257 |
+
submit_btn = None
|
258 |
+
else:
|
259 |
+
textbox.container = False
|
260 |
+
textbox.show_label = False
|
261 |
+
textbox_ = textbox.render()
|
262 |
+
if not isinstance(textbox_, (Textbox, MultimodalTextbox)):
|
263 |
+
raise TypeError(
|
264 |
+
f"Expected a gr.Textbox or gr.MultimodalTextbox component, but got {type(textbox_)}"
|
265 |
+
)
|
266 |
+
self.textbox = textbox_
|
267 |
+
elif self.multimodal:
|
268 |
+
submit_btn = None
|
269 |
+
self.textbox = MultimodalTextbox(
|
270 |
+
show_label=False,
|
271 |
+
label="Message",
|
272 |
+
placeholder="Type a message...",
|
273 |
+
scale=7,
|
274 |
+
autofocus=autofocus,
|
275 |
+
)
|
276 |
+
else:
|
277 |
+
self.textbox = Textbox(
|
278 |
+
container=False,
|
279 |
+
show_label=False,
|
280 |
+
label="Message",
|
281 |
+
placeholder="Type a message...",
|
282 |
+
scale=7,
|
283 |
+
autofocus=autofocus,
|
284 |
+
)
|
285 |
+
if submit_btn is not None and not multimodal:
|
286 |
+
if isinstance(submit_btn, Button):
|
287 |
+
submit_btn.render()
|
288 |
+
elif isinstance(submit_btn, str):
|
289 |
+
submit_btn = Button(
|
290 |
+
submit_btn,
|
291 |
+
variant="primary",
|
292 |
+
scale=1,
|
293 |
+
min_width=150,
|
294 |
+
)
|
295 |
+
else:
|
296 |
+
raise ValueError(
|
297 |
+
f"The submit_btn parameter must be a gr.Button, string, or None, not {type(submit_btn)}"
|
298 |
+
)
|
299 |
+
if stop_btn is not None:
|
300 |
+
if isinstance(stop_btn, Button):
|
301 |
+
stop_btn.visible = False
|
302 |
+
stop_btn.render()
|
303 |
+
elif isinstance(stop_btn, str):
|
304 |
+
stop_btn = Button(
|
305 |
+
stop_btn,
|
306 |
+
variant="stop",
|
307 |
+
visible=False,
|
308 |
+
scale=1,
|
309 |
+
min_width=150,
|
310 |
+
)
|
311 |
+
else:
|
312 |
+
raise ValueError(
|
313 |
+
f"The stop_btn parameter must be a gr.Button, string, or None, not {type(stop_btn)}"
|
314 |
+
)
|
315 |
+
self.buttons.extend([submit_btn, stop_btn]) # type: ignore
|
316 |
+
|
317 |
+
self.fake_api_btn = Button("Fake API", visible=False)
|
318 |
+
self.fake_response_textbox = Textbox(label="Response", visible=False)
|
319 |
+
(
|
320 |
+
self.retry_btn,
|
321 |
+
self.undo_btn,
|
322 |
+
self.clear_btn,
|
323 |
+
self.submit_btn,
|
324 |
+
self.stop_btn,
|
325 |
+
) = self.buttons
|
326 |
+
|
327 |
+
if examples:
|
328 |
+
if self.is_generator:
|
329 |
+
examples_fn = self._examples_stream_fn
|
330 |
+
else:
|
331 |
+
examples_fn = self._examples_fn
|
332 |
+
|
333 |
+
self.examples_handler = Examples(
|
334 |
+
examples=examples,
|
335 |
+
inputs=[self.textbox] + self.additional_inputs,
|
336 |
+
outputs=self.chatbot,
|
337 |
+
fn=examples_fn,
|
338 |
+
cache_examples=self.cache_examples,
|
339 |
+
_defer_caching=True,
|
340 |
+
examples_per_page=examples_per_page,
|
341 |
+
)
|
342 |
+
|
343 |
+
any_unrendered_inputs = any(
|
344 |
+
not inp.is_rendered for inp in self.additional_inputs
|
345 |
+
)
|
346 |
+
if self.additional_inputs and any_unrendered_inputs:
|
347 |
+
with Accordion(**self.additional_inputs_accordion_params): # type: ignore
|
348 |
+
for input_component in self.additional_inputs:
|
349 |
+
if not input_component.is_rendered:
|
350 |
+
input_component.render()
|
351 |
+
|
352 |
+
# The example caching must happen after the input components have rendered
|
353 |
+
if examples:
|
354 |
+
self.examples_handler._start_caching()
|
355 |
+
|
356 |
+
self.saved_input = State()
|
357 |
+
self.chatbot_state = (
|
358 |
+
State(self.chatbot.value) if self.chatbot.value else State([])
|
359 |
+
)
|
360 |
+
|
361 |
+
self._setup_events()
|
362 |
+
self._setup_api()
|
363 |
+
self.mode = mode
|
364 |
+
self.code_start_token = code_start_token
|
365 |
+
self.code_end_token = code_end_token
|
366 |
+
self.tool_call_token = tool_call_token
|
367 |
+
self.tool = PythonREPLTool()
|
368 |
+
|
369 |
+
def _setup_events(self) -> None:
|
370 |
+
submit_fn = self._stream_fn if self.is_generator else self._submit_fn
|
371 |
+
submit_triggers = (
|
372 |
+
[self.textbox.submit, self.submit_btn.click]
|
373 |
+
if self.submit_btn
|
374 |
+
else [self.textbox.submit]
|
375 |
+
)
|
376 |
+
submit_event = (
|
377 |
+
on(
|
378 |
+
submit_triggers,
|
379 |
+
self._clear_and_save_textbox,
|
380 |
+
[self.textbox],
|
381 |
+
[self.textbox, self.saved_input],
|
382 |
+
show_api=False,
|
383 |
+
queue=False,
|
384 |
+
)
|
385 |
+
.then(
|
386 |
+
self._display_input,
|
387 |
+
[self.saved_input, self.chatbot_state],
|
388 |
+
[self.chatbot, self.chatbot_state],
|
389 |
+
show_api=False,
|
390 |
+
queue=False,
|
391 |
+
)
|
392 |
+
.then(
|
393 |
+
submit_fn,
|
394 |
+
[self.saved_input, self.chatbot_state] + self.additional_inputs,
|
395 |
+
[self.chatbot, self.chatbot_state],
|
396 |
+
show_api=False,
|
397 |
+
concurrency_limit=cast(
|
398 |
+
Union[int, Literal["default"], None], self.concurrency_limit
|
399 |
+
),
|
400 |
+
)
|
401 |
+
)
|
402 |
+
self._setup_stop_events(submit_triggers, submit_event)
|
403 |
+
|
404 |
+
if self.retry_btn:
|
405 |
+
retry_event = (
|
406 |
+
self.retry_btn.click(
|
407 |
+
self._delete_prev_fn,
|
408 |
+
[self.saved_input, self.chatbot_state],
|
409 |
+
[self.chatbot, self.saved_input, self.chatbot_state],
|
410 |
+
show_api=False,
|
411 |
+
queue=False,
|
412 |
+
)
|
413 |
+
.then(
|
414 |
+
self._display_input,
|
415 |
+
[self.saved_input, self.chatbot_state],
|
416 |
+
[self.chatbot, self.chatbot_state],
|
417 |
+
show_api=False,
|
418 |
+
queue=False,
|
419 |
+
)
|
420 |
+
.then(
|
421 |
+
submit_fn,
|
422 |
+
[self.saved_input, self.chatbot_state] + self.additional_inputs,
|
423 |
+
[self.chatbot, self.chatbot_state],
|
424 |
+
show_api=False,
|
425 |
+
concurrency_limit=cast(
|
426 |
+
Union[int, Literal["default"], None], self.concurrency_limit
|
427 |
+
),
|
428 |
+
)
|
429 |
+
)
|
430 |
+
self._setup_stop_events([self.retry_btn.click], retry_event)
|
431 |
+
|
432 |
+
if self.undo_btn:
|
433 |
+
self.undo_btn.click(
|
434 |
+
self._delete_prev_fn,
|
435 |
+
[self.saved_input, self.chatbot_state],
|
436 |
+
[self.chatbot, self.saved_input, self.chatbot_state],
|
437 |
+
show_api=False,
|
438 |
+
queue=False,
|
439 |
+
).then(
|
440 |
+
async_lambda(lambda x: x),
|
441 |
+
[self.saved_input],
|
442 |
+
[self.textbox],
|
443 |
+
show_api=False,
|
444 |
+
queue=False,
|
445 |
+
)
|
446 |
+
|
447 |
+
if self.clear_btn:
|
448 |
+
self.clear_btn.click(
|
449 |
+
async_lambda(lambda: ([], [], None)),
|
450 |
+
None,
|
451 |
+
[self.chatbot, self.chatbot_state, self.saved_input],
|
452 |
+
queue=False,
|
453 |
+
show_api=False,
|
454 |
+
)
|
455 |
+
|
456 |
+
def _setup_stop_events(
|
457 |
+
self, event_triggers: list[Callable], event_to_cancel: Dependency
|
458 |
+
) -> None:
|
459 |
+
if self.stop_btn and self.is_generator:
|
460 |
+
if self.submit_btn:
|
461 |
+
for event_trigger in event_triggers:
|
462 |
+
event_trigger(
|
463 |
+
async_lambda(
|
464 |
+
lambda: (
|
465 |
+
Button(visible=False),
|
466 |
+
Button(visible=True),
|
467 |
+
)
|
468 |
+
),
|
469 |
+
None,
|
470 |
+
[self.submit_btn, self.stop_btn],
|
471 |
+
show_api=False,
|
472 |
+
queue=False,
|
473 |
+
)
|
474 |
+
event_to_cancel.then(
|
475 |
+
async_lambda(lambda: (Button(visible=True), Button(visible=False))),
|
476 |
+
None,
|
477 |
+
[self.submit_btn, self.stop_btn],
|
478 |
+
show_api=False,
|
479 |
+
queue=False,
|
480 |
+
)
|
481 |
+
else:
|
482 |
+
for event_trigger in event_triggers:
|
483 |
+
event_trigger(
|
484 |
+
async_lambda(lambda: Button(visible=True)),
|
485 |
+
None,
|
486 |
+
[self.stop_btn],
|
487 |
+
show_api=False,
|
488 |
+
queue=False,
|
489 |
+
)
|
490 |
+
event_to_cancel.then(
|
491 |
+
async_lambda(lambda: Button(visible=False)),
|
492 |
+
None,
|
493 |
+
[self.stop_btn],
|
494 |
+
show_api=False,
|
495 |
+
queue=False,
|
496 |
+
)
|
497 |
+
self.stop_btn.click(
|
498 |
+
None,
|
499 |
+
None,
|
500 |
+
None,
|
501 |
+
cancels=event_to_cancel,
|
502 |
+
show_api=False,
|
503 |
+
)
|
504 |
+
|
505 |
+
def _setup_api(self) -> None:
|
506 |
+
api_fn = self._api_stream_fn if self.is_generator else self._api_submit_fn
|
507 |
+
|
508 |
+
self.fake_api_btn.click(
|
509 |
+
api_fn,
|
510 |
+
[self.textbox, self.chatbot_state] + self.additional_inputs,
|
511 |
+
[self.textbox, self.chatbot_state],
|
512 |
+
api_name="chat",
|
513 |
+
concurrency_limit=cast(
|
514 |
+
Union[int, Literal["default"], None], self.concurrency_limit
|
515 |
+
),
|
516 |
+
)
|
517 |
+
|
518 |
+
def _clear_and_save_textbox(self, message: str) -> tuple[str | dict, str]:
|
519 |
+
if self.multimodal:
|
520 |
+
return {"text": "", "files": []}, message
|
521 |
+
else:
|
522 |
+
return "", message
|
523 |
+
|
524 |
+
def _append_multimodal_history(
|
525 |
+
self,
|
526 |
+
message: dict[str, list],
|
527 |
+
response: str | None,
|
528 |
+
history: list[list[str | tuple | None]],
|
529 |
+
):
|
530 |
+
for x in message["files"]:
|
531 |
+
history.append([(x,), None])
|
532 |
+
if message["text"] is None or not isinstance(message["text"], str):
|
533 |
+
return
|
534 |
+
elif message["text"] == "" and message["files"] != []:
|
535 |
+
history.append([None, response])
|
536 |
+
else:
|
537 |
+
history.append([message["text"], response])
|
538 |
+
|
539 |
+
async def _display_input(
|
540 |
+
self, message: str | dict[str, list], history: list[list[str | tuple | None]]
|
541 |
+
) -> tuple[list[list[str | tuple | None]], list[list[str | tuple | None]]]:
|
542 |
+
if self.multimodal and isinstance(message, dict):
|
543 |
+
self._append_multimodal_history(message, None, history)
|
544 |
+
elif isinstance(message, str):
|
545 |
+
history.append([message, None])
|
546 |
+
print("history after display input:", history)
|
547 |
+
return history, history
|
548 |
+
|
549 |
+
async def _submit_fn(
|
550 |
+
self,
|
551 |
+
message: str | dict[str, list],
|
552 |
+
history_with_input: list[list[str | tuple | None]],
|
553 |
+
request: Request,
|
554 |
+
*args,
|
555 |
+
) -> tuple[list[list[str | tuple | None]], list[list[str | tuple | None]]]:
|
556 |
+
print("calling custom submit fn")
|
557 |
+
print("message:", message)
|
558 |
+
print("history_with_input:", history_with_input)
|
559 |
+
if self.multimodal and isinstance(message, dict):
|
560 |
+
remove_input = (
|
561 |
+
len(message["files"]) + 1
|
562 |
+
if message["text"] is not None
|
563 |
+
else len(message["files"])
|
564 |
+
)
|
565 |
+
history = history_with_input[:-remove_input]
|
566 |
+
if len(message["files"]) > 0:
|
567 |
+
history.append(
|
568 |
+
(
|
569 |
+
f"[INFO]The data is uploaded to {','.join(message['files'])}",
|
570 |
+
None,
|
571 |
+
)
|
572 |
+
)
|
573 |
+
message = message["text"]
|
574 |
+
else:
|
575 |
+
history = history_with_input[:-1]
|
576 |
+
while True:
|
577 |
+
inputs, _, _ = special_args(
|
578 |
+
self.fn, inputs=[message, history, *args], request=request
|
579 |
+
)
|
580 |
+
print("history:", inputs[1])
|
581 |
+
|
582 |
+
if self.is_async:
|
583 |
+
response = await self.fn(*inputs)
|
584 |
+
else:
|
585 |
+
response = await anyio.to_thread.run_sync(
|
586 |
+
self.fn, *inputs, limiter=self.limiter
|
587 |
+
)
|
588 |
+
response = replace_image_path(response)
|
589 |
+
reasoning, code_script = parse_code_action(
|
590 |
+
response,
|
591 |
+
mode="prompt",
|
592 |
+
code_start_token="```python\n",
|
593 |
+
code_end_token="```",
|
594 |
+
)
|
595 |
+
if code_script is None or code_script.strip() == "":
|
596 |
+
print("no code action")
|
597 |
+
response = reasoning
|
598 |
+
else:
|
599 |
+
print("have code action")
|
600 |
+
response = f"{reasoning}\n{self.code_start_token}\n{code_script}\n{self.code_end_token}"
|
601 |
+
if self.multimodal and isinstance(message, dict):
|
602 |
+
self._append_multimodal_history(message, response, history)
|
603 |
+
else:
|
604 |
+
history.append((message, response))
|
605 |
+
message = None
|
606 |
+
if code_script is None or code_script.strip() == "":
|
607 |
+
break
|
608 |
+
|
609 |
+
code_response = execute_code(code_script, self.tool)
|
610 |
+
history.append((None, code_response))
|
611 |
+
|
612 |
+
return history, history
|
613 |
+
|
614 |
+
async def _stream_fn(
|
615 |
+
self,
|
616 |
+
message: str | dict[str, list],
|
617 |
+
history_with_input: list[list[str | tuple | None]],
|
618 |
+
request: Request,
|
619 |
+
*args,
|
620 |
+
) -> AsyncGenerator:
|
621 |
+
if self.multimodal and isinstance(message, dict):
|
622 |
+
remove_input = (
|
623 |
+
len(message["files"]) + 1
|
624 |
+
if message["text"] is not None
|
625 |
+
else len(message["files"])
|
626 |
+
)
|
627 |
+
history = history_with_input[:-remove_input]
|
628 |
+
else:
|
629 |
+
history = history_with_input[:-1]
|
630 |
+
inputs, _, _ = special_args(
|
631 |
+
self.fn, inputs=[message, history, *args], request=request
|
632 |
+
)
|
633 |
+
|
634 |
+
if self.is_async:
|
635 |
+
generator = self.fn(*inputs)
|
636 |
+
else:
|
637 |
+
generator = await anyio.to_thread.run_sync(
|
638 |
+
self.fn, *inputs, limiter=self.limiter
|
639 |
+
)
|
640 |
+
generator = SyncToAsyncIterator(generator, self.limiter)
|
641 |
+
try:
|
642 |
+
first_response = await async_iteration(generator)
|
643 |
+
if self.multimodal and isinstance(message, dict):
|
644 |
+
for x in message["files"]:
|
645 |
+
history.append([(x,), None])
|
646 |
+
update = history + [[message["text"], first_response]]
|
647 |
+
yield update, update
|
648 |
+
else:
|
649 |
+
update = history + [[message, first_response]]
|
650 |
+
yield update, update
|
651 |
+
except StopIteration:
|
652 |
+
if self.multimodal and isinstance(message, dict):
|
653 |
+
self._append_multimodal_history(message, None, history)
|
654 |
+
yield history, history
|
655 |
+
else:
|
656 |
+
update = history + [[message, None]]
|
657 |
+
yield update, update
|
658 |
+
async for response in generator:
|
659 |
+
if self.multimodal and isinstance(message, dict):
|
660 |
+
update = history + [[message["text"], response]]
|
661 |
+
yield update, update
|
662 |
+
else:
|
663 |
+
update = history + [[message, response]]
|
664 |
+
yield update, update
|
665 |
+
|
666 |
+
async def _api_submit_fn(
|
667 |
+
self, message: str, history: list[list[str | None]], request: Request, *args
|
668 |
+
) -> tuple[str, list[list[str | None]]]:
|
669 |
+
inputs, _, _ = special_args(
|
670 |
+
self.fn, inputs=[message, history, *args], request=request
|
671 |
+
)
|
672 |
+
|
673 |
+
if self.is_async:
|
674 |
+
response = await self.fn(*inputs)
|
675 |
+
else:
|
676 |
+
response = await anyio.to_thread.run_sync(
|
677 |
+
self.fn, *inputs, limiter=self.limiter
|
678 |
+
)
|
679 |
+
history.append([message, response])
|
680 |
+
return response, history
|
681 |
+
|
682 |
+
async def _api_stream_fn(
|
683 |
+
self, message: str, history: list[list[str | None]], request: Request, *args
|
684 |
+
) -> AsyncGenerator:
|
685 |
+
inputs, _, _ = special_args(
|
686 |
+
self.fn, inputs=[message, history, *args], request=request
|
687 |
+
)
|
688 |
+
|
689 |
+
if self.is_async:
|
690 |
+
generator = self.fn(*inputs)
|
691 |
+
else:
|
692 |
+
generator = await anyio.to_thread.run_sync(
|
693 |
+
self.fn, *inputs, limiter=self.limiter
|
694 |
+
)
|
695 |
+
generator = SyncToAsyncIterator(generator, self.limiter)
|
696 |
+
try:
|
697 |
+
first_response = await async_iteration(generator)
|
698 |
+
yield first_response, history + [[message, first_response]]
|
699 |
+
except StopIteration:
|
700 |
+
yield None, history + [[message, None]]
|
701 |
+
async for response in generator:
|
702 |
+
yield response, history + [[message, response]]
|
703 |
+
|
704 |
+
async def _examples_fn(self, message: str, *args) -> list[list[str | None]]:
|
705 |
+
inputs, _, _ = special_args(self.fn, inputs=[message, [], *args], request=None)
|
706 |
+
|
707 |
+
if self.is_async:
|
708 |
+
response = await self.fn(*inputs)
|
709 |
+
else:
|
710 |
+
response = await anyio.to_thread.run_sync(
|
711 |
+
self.fn, *inputs, limiter=self.limiter
|
712 |
+
)
|
713 |
+
return [[message, response]]
|
714 |
+
|
715 |
+
async def _examples_stream_fn(
|
716 |
+
self,
|
717 |
+
message: str,
|
718 |
+
*args,
|
719 |
+
) -> AsyncGenerator:
|
720 |
+
inputs, _, _ = special_args(self.fn, inputs=[message, [], *args], request=None)
|
721 |
+
|
722 |
+
if self.is_async:
|
723 |
+
generator = self.fn(*inputs)
|
724 |
+
else:
|
725 |
+
generator = await anyio.to_thread.run_sync(
|
726 |
+
self.fn, *inputs, limiter=self.limiter
|
727 |
+
)
|
728 |
+
generator = SyncToAsyncIterator(generator, self.limiter)
|
729 |
+
async for response in generator:
|
730 |
+
yield [[message, response]]
|
731 |
+
|
732 |
+
async def _delete_prev_fn(
|
733 |
+
self,
|
734 |
+
message: str | dict[str, list],
|
735 |
+
history: list[list[str | tuple | None]],
|
736 |
+
) -> tuple[
|
737 |
+
list[list[str | tuple | None]],
|
738 |
+
str | dict[str, list],
|
739 |
+
list[list[str | tuple | None]],
|
740 |
+
]:
|
741 |
+
if self.multimodal and isinstance(message, dict):
|
742 |
+
remove_input = (
|
743 |
+
len(message["files"]) + 1
|
744 |
+
if message["text"] is not None
|
745 |
+
else len(message["files"])
|
746 |
+
)
|
747 |
+
history = history[:-remove_input]
|
748 |
+
else:
|
749 |
+
history = history[:-1]
|
750 |
+
return history, message or "", history
|
output_parser.py
ADDED
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""output parser for code interpreter"""
|
2 |
+
|
3 |
+
import ast
|
4 |
+
from typing import Tuple
|
5 |
+
|
6 |
+
|
7 |
+
def parse_code_action(
|
8 |
+
output: str,
|
9 |
+
mode: str = "prompt",
|
10 |
+
code_start_token: str = "```\npython\n",
|
11 |
+
code_end_token: str = "```",
|
12 |
+
tool_call_token: str = "<|tool_call|>",
|
13 |
+
) -> Tuple[str, str]:
|
14 |
+
"""parse output from code interpreter
|
15 |
+
|
16 |
+
Args:
|
17 |
+
output (str): the output from code interpreter
|
18 |
+
mode: the mode of the output, could be prompt, functioncall, assistant
|
19 |
+
code_start_token: the token code script starts with, only used in prompt mode
|
20 |
+
code_end_token: the token code script ends with, only used in prompt mode
|
21 |
+
tool_call_token: the token for tool call, only used in prompt mode
|
22 |
+
|
23 |
+
Returns:
|
24 |
+
Tuple[str, str]: reasoning and code action
|
25 |
+
"""
|
26 |
+
if mode == "prompt":
|
27 |
+
return extract_code(output, code_start_token, code_end_token)
|
28 |
+
elif mode == "functioncall":
|
29 |
+
rsp = fc2dict(output, tool_call_token)
|
30 |
+
if "tool_calls" in rsp and len(rsp["tool_calls"]) > 0:
|
31 |
+
return rsp["content"], rsp["tool_calls"][0]["arguments"]["code"]
|
32 |
+
else:
|
33 |
+
return rsp["content"], ""
|
34 |
+
elif mode == "assistant":
|
35 |
+
raise NotImplementedError("assistant mode is not implemented yet")
|
36 |
+
else:
|
37 |
+
raise ValueError(f"mode {mode} is not supported")
|
38 |
+
|
39 |
+
|
40 |
+
def extract_code(
|
41 |
+
rsp: str, code_start_token: str = "```\npython\n", code_end_token: str = "```"
|
42 |
+
) -> Tuple[str, str]:
|
43 |
+
"""extract code from assistant content
|
44 |
+
|
45 |
+
Args:
|
46 |
+
rsp (str): the response content from assistant
|
47 |
+
code_start_token (str, optional): the token code script starts with. Defaults to "```\npython".
|
48 |
+
code_end_token (str, optional): the token code script ends with. Defaults to "```".
|
49 |
+
|
50 |
+
Returns:
|
51 |
+
Tuple[str, str]: reasoning and code action
|
52 |
+
"""
|
53 |
+
# TODO: implement the code extraction logic using different code_start_token and code_end_token
|
54 |
+
rsp = str(rsp)
|
55 |
+
|
56 |
+
start_index = rsp.find(code_start_token)
|
57 |
+
if start_index == -1:
|
58 |
+
return rsp, ""
|
59 |
+
|
60 |
+
start_index += len(code_start_token)
|
61 |
+
end_index = rsp.find(code_end_token, start_index)
|
62 |
+
if end_index == -1:
|
63 |
+
return rsp, ""
|
64 |
+
|
65 |
+
return rsp[:start_index].replace(code_start_token, "").strip(), rsp[
|
66 |
+
start_index:end_index
|
67 |
+
].strip()
|
68 |
+
|
69 |
+
|
70 |
+
import ast
|
71 |
+
import re
|
72 |
+
import json
|
73 |
+
|
74 |
+
def convert_function_call_to_json(string):
|
75 |
+
try:
|
76 |
+
tool_calls = []
|
77 |
+
x = ast.parse(string)
|
78 |
+
for tool in x.body:
|
79 |
+
function_name = tool.value.func.id
|
80 |
+
function_args = {}
|
81 |
+
for keyword in tool.value.keywords:
|
82 |
+
function_args[keyword.arg] = ast.literal_eval(keyword.value)
|
83 |
+
this_one = {"name": function_name, "arguments": function_args}
|
84 |
+
tool_calls.append(this_one)
|
85 |
+
return tool_calls
|
86 |
+
except Exception:
|
87 |
+
return []
|
88 |
+
import json
|
89 |
+
|
90 |
+
def extract_code_from_arguments(arguments_str):
|
91 |
+
try:
|
92 |
+
arguments_dict = json.loads(arguments_str)
|
93 |
+
return arguments_dict.get("code", "")
|
94 |
+
except json.JSONDecodeError:
|
95 |
+
return ""
|
96 |
+
|
97 |
+
def fc2dict(sequence: str, spliter="<|tool_call|>"):
|
98 |
+
if spliter in sequence:
|
99 |
+
content, tool_call_string = sequence.split(spliter, 1)
|
100 |
+
try:
|
101 |
+
# 找到第一个 { 和最后一个 }
|
102 |
+
start_idx = tool_call_string.find('{')
|
103 |
+
end_idx = tool_call_string.rfind('}')
|
104 |
+
if start_idx != -1 and end_idx != -1:
|
105 |
+
arguments_str = tool_call_string[start_idx:end_idx + 1]
|
106 |
+
print("Arg:",arguments_str)
|
107 |
+
arguments_str=arguments_str.replace("\n","\\n")
|
108 |
+
#code_content = extract_code_from_arguments(arguments_str)
|
109 |
+
tool_call_dict = {
|
110 |
+
"name": "execute_python",
|
111 |
+
"arguments": json.loads(arguments_str)
|
112 |
+
}
|
113 |
+
tool_calls = [tool_call_dict]
|
114 |
+
else:
|
115 |
+
tool_calls = []
|
116 |
+
return {
|
117 |
+
"content": content.strip(),
|
118 |
+
"tool_calls": tool_calls,
|
119 |
+
"role": "assistant",
|
120 |
+
}
|
121 |
+
except Exception as e:
|
122 |
+
print(f"Error: {e}")
|
123 |
+
return {"content": content.strip(), "role": "assistant"}
|
124 |
+
else:
|
125 |
+
return {"content": sequence.strip(), "role": "assistant"}
|
126 |
+
|
127 |
+
# 示例用法
|
128 |
+
sequence = '''To fulfill your request, I will perform the following steps:
|
129 |
+
|
130 |
+
1. Read the dataset from the provided path.
|
131 |
+
2. Extract the necessary data for the radar chart.
|
132 |
+
3. Create a radar chart using the extracted data.
|
133 |
+
|
134 |
+
Let's start by reading the dataset.
|
135 |
+
|
136 |
+
Action:
|
137 |
+
|
138 |
+
|
139 |
+
<|tool_call|>execute_python({"code":"import pandas as pd\n\n# Read the dataset\ndata_path = './data/radar.csv'\ndf = pd.read_csv(data_path)\ndf.head()"})\n'''
|
140 |
+
result = fc2dict(sequence)
|
141 |
+
print(result)
|