-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamlit_app.py
146 lines (127 loc) · 4.79 KB
/
streamlit_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import streamlit as st
import openai, json
from e2b_code_interpreter import Sandbox
avatar = {
"user": "🤓",
"assistant": "🤖",
"tool": "🛠️"
}
# Define the tools
tools = [{
"type": "function",
"function": {
"name": "execute_python",
"description": "Execute python code in a Jupyter notebook cell and return result",
"parameters": {
"type": "object",
"properties": {
"code": {
"type": "string",
"description": "The python code to execute in a single cell"
}
},
"required": ["code"]
}
}
}]
if "messages" not in st.session_state:
st.session_state.messages = []
def chatbot(messages,model="gpt-4o-mini",tools=None):
# Generate text with OpenAI
response = openai.chat.completions.create(
model=model,
messages=messages,
tools=tools,
)
response_message = response.choices[0].message
return dict(response_message)
def get_code(response_message):
# Execute the tool if it's called by the model
if response_message['tool_calls']:
for tool_call in response_message['tool_calls']:
if tool_call.function.name == "execute_python":
code = json.loads(tool_call.function.arguments)['code']
return code
@st.cache_resource
def init_sandbox():
sandbox = Sandbox()
return sandbox
def restart_sandbox():
st.cache_resource.clear()
st.session_state.sandbox = init_sandbox()
st.success("Sandbox restarted successfully!")
if 'sandbox' not in st.session_state:
st.session_state.sandbox = init_sandbox()
sandbox = init_sandbox()
st.sidebar.title("E2B Code Interpreter")
# Display the conversation
for msg in st.session_state.messages:
role = msg['role']
with st.chat_message(role, avatar=avatar[role]):
if role == 'assistant':
if msg['tool_calls']:
with st.expander("Code"):
code = get_code(msg)
st.write(f"```python\n{code}\n```")
else:
st.write(msg['content'])
else:
st.write(msg['content'])
if prompt := st.chat_input("Enter a prompt"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user", avatar=avatar["user"]):
st.write(prompt)
response_message = chatbot(st.session_state.messages,tools=tools)
print(response_message)
# Execute the tool if it's called by the model
if response_message['tool_calls']:
# Append the response message to the messages list
st.session_state.messages.append(response_message)
code = get_code(response_message)
with st.chat_message("assistant", avatar=avatar["assistant"]):
with st.expander("Code", expanded=True):
st.write(f"```python\n{code}\n```")
with st.spinner("Executing code..."):
if sandbox.is_running():
execution = sandbox.run_code(code)
result = execution.text
else:
result = 'The sandbox is not running'
with st.chat_message("tool", avatar=avatar["tool"]):
st.write(result)
print(result)
# Send the result back to the model
st.session_state.messages.append({
"role": "tool",
"name": "execute_python",
"content": result,
"tool_call_id": response_message['tool_calls'][0].id,
})
final_response = chatbot(st.session_state.messages)
st.session_state.messages.append(final_response)
with st.chat_message("assistant", avatar=avatar["assistant"]):
st.write(final_response['content'])
print(final_response['content'])
else:
st.session_state.messages.append(response_message)
with st.chat_message("assistant", avatar=avatar["assistant"]):
st.write(response_message['content'])
print(response_message['content'])
with st.sidebar:
# Reset the conversation
if st.button("Reset Conversation"):
st.session_state.messages = []
st.rerun()
# Restart the sandbox
if st.button("Restart Sandbox"):
restart_sandbox()
# Example of a prompt
with st.expander("Example of a prompt"):
st.write("Calculate the 10th element of the Fibonacci sequence.")
# Debug
if st.toggle('Debug', value=True):
sandbox_state = st.session_state.sandbox.is_running()
st.write('Sandbox state:')
st.write("`Running`" if sandbox_state else "`Not running`")
st.write('Session messages:')
st.write(st.session_state.messages)