Skip to content

Commit

Permalink
Fix self-download for agents
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudotensor committed Oct 29, 2024
1 parent 72a3d92 commit 780d46e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
6 changes: 2 additions & 4 deletions openai_server/autogen_2agent_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import tempfile
import uuid

from openai_server.backend_utils import structure_to_messages
from openai_server.backend_utils import structure_to_messages, run_download_api_all
from openai_server.agent_utils import get_ret_dict_and_handle_files, get_openai_client
from openai_server.agent_prompting import get_full_system_prompt, planning_prompt, planning_final_prompt, \
get_agent_tools
Expand Down Expand Up @@ -92,10 +92,8 @@ def run_autogen_2agent(query=None,
agent_work_dir = tempfile.mkdtemp()

if agent_files:
client = get_openai_client(max_time=autogen_timeout)
# assume list of file_ids for use with File API
from openai_server.openai_client import get_files_from_ids
get_files_from_ids(usage=None, client=client, file_ids=agent_files, work_dir=agent_work_dir)
run_download_api_all(agent_files, authorization, agent_work_dir)

# iostream = IOStream.get_default()
# iostream.print("\033[32m", end="")
Expand Down
27 changes: 27 additions & 0 deletions openai_server/backend_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,33 @@ def run_upload_api(content, filename, purpose, authorization, created_at_orig=No
return response_dict


def run_download_api(file_id, authorization):
user_dir = get_user_dir(authorization)

if not os.path.exists(user_dir):
os.makedirs(user_dir)

file_path = os.path.join(user_dir, file_id)
file_path_meta = os.path.join(user_dir, file_id + meta_ext)

with open(file_path, "rb") as f:
content = f.read()

with open(file_path_meta, "rt") as f:
response_dict = json.dumps(f.read())
assert isinstance(response_dict, dict), "response_dict should be a dict"
return response_dict, content


def run_download_api_all(agent_files, authorization, agent_work_dir):
for file_id in agent_files:
response_dict, content = run_download_api(file_id, authorization)
filename = response_dict['filename']
new_file = os.path.join(agent_work_dir, filename)
with open(new_file, "wb") as f:
f.write(content)


def extract_xml_tags(full_text, tags=['name', 'page']):
results_dict = {k: None for k in tags}
for tag in tags:
Expand Down
6 changes: 3 additions & 3 deletions openai_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,7 @@ async def retrieve_file(request: Request, file_id: str, authorization: str = Hea
file_path = os.path.join(user_dir, file_id)

if not os.path.exists(file_path):
raise HTTPException(status_code=404, detail="File not found")
raise HTTPException(status_code=404, detail=f"retrieve_file: {file_id}: File not found")

file_path_meta = os.path.join(user_dir, file_id + meta_ext)
if os.path.isfile(file_path_meta):
Expand Down Expand Up @@ -1176,7 +1176,7 @@ async def delete_file(request: Request, file_id: str, authorization: str = Heade
file_path = os.path.join(user_dir, file_id)

if not os.path.exists(file_path):
raise HTTPException(status_code=404, detail="File not found")
raise HTTPException(status_code=404, detail=f"delete_file {file_id}: File not found")

try:
os.remove(file_path)
Expand All @@ -1202,7 +1202,7 @@ async def retrieve_file_content(request: Request, file_id: str, stream: bool = Q
file_path = os.path.join(user_dir, file_id)

if not os.path.exists(file_path):
raise HTTPException(status_code=404, detail="File not found")
raise HTTPException(status_code=404, detail=f"retrieve_file_content: {file_id}: File not found")

if stream:
def iter_file():
Expand Down
2 changes: 1 addition & 1 deletion src/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "bc4706f0996163088f1a157f4ce6d4bff3284225"
__version__ = "72a3d924763e873162e922ff45bd36559dfe06ed"

0 comments on commit 780d46e

Please sign in to comment.