Skip to content

Commit 7470e0b

Browse files
committed
Added new abilities
1 parent 2f2bd16 commit 7470e0b

File tree

11 files changed

+217
-108
lines changed

11 files changed

+217
-108
lines changed

‎logs/logs.txt

Lines changed: 27 additions & 18 deletions
Large diffs are not rendered by default.

‎main.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
from playground.actions_manager import ActionsManager
1010
from playground.assistants_api import api
1111
from playground.assistants_panel import assistants_panel
12-
from playground.assistants_utils import EventHandler
12+
from playground.assistants_utils import EventHandler, get_tools
1313
from playground.environment_manager import EnvironmentManager
1414
from playground.logging import Logger
1515
from playground.semantic_manager import SemanticManager
16-
from playground.constants import ASSISTANTS_WORKING_FOLDER
16+
from playground.global_values import GlobalValues
1717

1818
thread = api.create_thread() # create a new thread everytime this is run
1919
actions_manager = ActionsManager()
@@ -55,17 +55,35 @@ def print_like_dislike(x: gr.LikeData):
5555
print(x.index, x.value, x.liked)
5656

5757

58-
def ask_assistant(history, message):
58+
def ask_assistant(assistant_id, history, message):
59+
assistant = api.retrieve_assistant(assistant_id)
60+
if assistant is None:
61+
history.append((None, "Assistant not found."))
62+
return history, gr.MultimodalTextbox(value=None, interactive=False)
63+
5964
if history is None:
6065
history = []
6166

6267
attachments = []
6368
content = ""
6469
for file in message["files"]:
65-
history.append(((file,), None))
66-
# upload files to the thread
67-
file = api.upload_file(file)
68-
attachments += [{"file_id": file.id, "tools": [{"type": "code_interpreter"}]}]
70+
tools, actions = get_tools(assistant.tools)
71+
if "Code Interpreter" in tools:
72+
# upload files to the thread
73+
file = api.upload_file(file)
74+
attachments += [
75+
{"file_id": file.id, "tools": [{"type": "code_interpreter"}]}
76+
]
77+
else:
78+
with open(file, "r") as f:
79+
file_content = f.read()
80+
file = os.path.basename(file)
81+
file_path = os.path.join(GlobalValues.ASSISTANTS_WORKING_FOLDER, file)
82+
with open(file_path, "w") as file:
83+
file.write(file_content)
84+
attachments = None
85+
history.append((f"file {file}, saved to working folder.", None))
86+
6987
if message["text"] is not None:
7088
history.append((message["text"], None))
7189
content = message["text"]
@@ -97,7 +115,7 @@ def get_file_path(file):
97115
if os.path.isabs(file):
98116
return file
99117

100-
file_path = os.path.join(ASSISTANTS_WORKING_FOLDER, file)
118+
file_path = os.path.join(GlobalValues.ASSISTANTS_WORKING_FOLDER, file)
101119
return file_path
102120

103121

@@ -248,7 +266,9 @@ def stream_worker(assistant_id, thread_id, event_handler):
248266
)
249267

250268
chat_msg = chat_input.submit(
251-
ask_assistant, [chatbot, chat_input], [chatbot, chat_input]
269+
ask_assistant,
270+
[assistant_id, chatbot, chat_input],
271+
[chatbot, chat_input],
252272
)
253273
bot_msg = chat_msg.then(
254274
run,

‎playground/assistant_actions/excel_actions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import os
44

55
from playground.actions_manager import agent_action
6-
from playground.constants import ASSISTANTS_WORKING_FOLDER
6+
from playground.global_values import GlobalValues
77

88

99
@agent_action
@@ -18,7 +18,7 @@ def excel_to_csv(filename, skiprows):
1818
Returns:
1919
str: The CSV-formatted text string.
2020
"""
21-
file_path = os.path.join(ASSISTANTS_WORKING_FOLDER, filename)
21+
file_path = os.path.join(GlobalValues.ASSISTANTS_WORKING_FOLDER, filename)
2222
# Read the Excel file and skip the specified rows
2323
df = pd.read_excel(file_path, skiprows=skiprows)
2424

@@ -57,7 +57,7 @@ def csv_to_excel(csv_content, start_row, filename):
5757
df = pd.read_csv(buffer)
5858

5959
# Save the DataFrame to an Excel file
60-
file_path = os.path.join(ASSISTANTS_WORKING_FOLDER, filename)
60+
file_path = os.path.join(GlobalValues.ASSISTANTS_WORKING_FOLDER, filename)
6161
df.to_excel(file_path, index=False, startrow=start_row)
6262

6363
# Close the buffer

‎playground/assistant_actions/file_actions.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22

33
from playground.actions_manager import agent_action
4-
from playground.constants import ASSISTANTS_WORKING_FOLDER
4+
from playground.global_values import GlobalValues
55

66

77
@agent_action
@@ -12,7 +12,7 @@ def save_file(filename, content):
1212
:param filename: The name of the file including extension.
1313
:param content: The content to save in the file.
1414
"""
15-
file_path = os.path.join(ASSISTANTS_WORKING_FOLDER, filename)
15+
file_path = os.path.join(GlobalValues.ASSISTANTS_WORKING_FOLDER, filename)
1616
with open(file_path, "w", encoding="utf-8") as file:
1717
file.write(content)
1818
return f"File '{filename}' saved successfully."
@@ -26,7 +26,7 @@ def load_file(filename):
2626
:param filename: The name of the file including extension.
2727
:return: The content of the file.
2828
"""
29-
file_path = os.path.join(ASSISTANTS_WORKING_FOLDER, filename)
29+
file_path = os.path.join(GlobalValues.ASSISTANTS_WORKING_FOLDER, filename)
3030
if not os.path.exists(file_path):
3131
print(f"File '{filename}' does not exist.")
3232
return None
@@ -44,7 +44,7 @@ def delete_file(filename):
4444
4545
:param filename: The name of the file including extension.
4646
"""
47-
file_path = os.path.join(ASSISTANTS_WORKING_FOLDER, filename)
47+
file_path = os.path.join(GlobalValues.ASSISTANTS_WORKING_FOLDER, filename)
4848
if os.path.exists(file_path):
4949
os.remove(file_path)
5050
return f"File '{filename}' deleted successfully."
@@ -59,7 +59,7 @@ def create_folder(foldername):
5959
6060
:param foldername: The name of the folder to create.
6161
"""
62-
folder_path = os.path.join(ASSISTANTS_WORKING_FOLDER, foldername)
62+
folder_path = os.path.join(GlobalValues.ASSISTANTS_WORKING_FOLDER, foldername)
6363
if not os.path.exists(folder_path):
6464
os.makedirs(folder_path)
6565
return f"Folder '{foldername}' created successfully."
@@ -74,10 +74,21 @@ def list_files():
7474
7575
:return: A list of file names.
7676
"""
77-
files = os.listdir(ASSISTANTS_WORKING_FOLDER)
77+
files = os.listdir(GlobalValues.ASSISTANTS_WORKING_FOLDER)
7878
return files
7979

8080

81+
@agent_action
82+
def set_working_folder(foldername):
83+
"""
84+
Set the working folder for file operations.
85+
86+
:param foldername: The name of the folder to set as the working folder.
87+
"""
88+
GlobalValues.set_value("ASSISTANTS_WORKING_FOLDER", foldername)
89+
return f"Working folder set to '{foldername}'."
90+
91+
8192
# # Example usage:
8293
# if __name__ == "__main__":
8394
# # Save a file

‎playground/assistant_actions/image_actions.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import base64
66

77
from playground.actions_manager import agent_action
8-
from playground.constants import ASSISTANTS_WORKING_FOLDER
8+
from playground.global_values import GlobalValues
99

1010
load_dotenv()
1111

@@ -38,7 +38,9 @@ def create_image(prompt, model="dall-e-3", size="1024x1024", quality="standard",
3838
image_response = requests.get(image_url)
3939
if image_response.status_code == 200:
4040
local_filename = f"{prompt.replace(' ', '_').replace(',','')[:50]}.png"
41-
local_path = os.path.join(ASSISTANTS_WORKING_FOLDER, local_filename)
41+
local_path = os.path.join(
42+
GlobalValues.ASSISTANTS_WORKING_FOLDER, local_filename
43+
)
4244
with open(local_path, "wb") as f:
4345
f.write(image_response.content)
4446
return local_filename
@@ -58,7 +60,7 @@ def encode_image(image_filename):
5860
Returns:
5961
str: The base64 encoded string of the image.
6062
"""
61-
local_path = os.path.join(ASSISTANTS_WORKING_FOLDER, image_filename)
63+
local_path = os.path.join(GlobalValues.ASSISTANTS_WORKING_FOLDER, image_filename)
6264
if not os.path.exists(local_path):
6365
return f"File not found: {image_filename}"
6466
with open(local_path, "rb") as image_file:

‎playground/assistant_actions/powerpoint_actions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This file contains the implementation of assistant actions related to PowerPoint presentations. It is still a work in progress.
22

33
from playground.actions_manager import agent_action
4-
from playground.constants import ASSISTANTS_WORKING_FOLDER
4+
from playground.global_values import GlobalValues
55
from pptx import Presentation
66
from pptx.util import Inches
77
import markdown
@@ -23,7 +23,7 @@ def create_new_powerpoint(filename):
2323
os.path.dirname(os.path.abspath(__file__)), "template.pptx"
2424
)
2525
prs = Presentation(template_path)
26-
file_path = os.path.join(ASSISTANTS_WORKING_FOLDER, filename)
26+
file_path = os.path.join(GlobalValues.ASSISTANTS_WORKING_FOLDER, filename)
2727
prs.save(file_path)
2828
return f"Created new PowerPoint presentation: {filename}"
2929

@@ -76,7 +76,7 @@ def add_slide_to_powerpoint(filename, slide_content):
7676
Returns:
7777
None
7878
"""
79-
file_path = os.path.join(ASSISTANTS_WORKING_FOLDER, filename)
79+
file_path = os.path.join(GlobalValues.ASSISTANTS_WORKING_FOLDER, filename)
8080
prs = Presentation(file_path)
8181
slide_type = slide_content.get("slide_type", "Title and Content")
8282

0 commit comments

Comments
 (0)