Skip to content

Commit

Permalink
Rename function to be clear, and get correct signature
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudotensor committed Sep 9, 2024
1 parent 84bbf75 commit 06285c2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 35 deletions.
19 changes: 16 additions & 3 deletions openai_server/agent_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
import inspect
import os
import shutil
Expand Down Expand Up @@ -51,9 +52,20 @@ def in_pycharm():
return os.getenv("PYCHARM_HOSTED") is not None


def get_inner_function_signature(func):
# Check if the function is a functools.partial object
if isinstance(func, functools.partial):
# Get the original function
assert func.keywords is not None and func.keywords, "The function must have keyword arguments."
func = func.keywords['run_agent_func']
return inspect.signature(func)
else:
return inspect.signature(func)


def filter_kwargs(func, kwargs):
# Get the parameter list of the function
sig = inspect.signature(func)
sig = get_inner_function_signature(func)
valid_kwargs = {k: v for k, v in kwargs.items() if k in sig.parameters}
return valid_kwargs

Expand Down Expand Up @@ -92,12 +104,13 @@ def current_datetime():
return "For current user query: Current Date, Time, and Local Time Zone: %s. Note some APIs may have data from different time zones, so may reflect a different date." % formatted_date_time


def run_agent(run_agent_func,
query,
def run_agent(run_agent_func=None,
query=None,
**kwargs,
) -> dict:
ret_dict = {}
try:
assert run_agent_func is not None, "run_agent_func must be provided."
ret_dict = run_agent_func(query, **kwargs)
finally:
if kwargs.get('agent_venv_dir') is None and 'agent_venv_dir' in ret_dict and ret_dict['agent_venv_dir']:
Expand Down
46 changes: 23 additions & 23 deletions openai_server/autogen_2agent_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@
from openai_server.autogen_utils import terminate_message_func, H2OConversableAgent


def run_autogen(query=None,
visible_models=None,
stream_output=None,
max_new_tokens=None,
authorization=None,
chat_conversation=None,
text_context_list=None,
system_prompt=None,
image_file=None,
# autogen/agent specific parameters
agent_type=None,
autogen_stop_docker_executor=None,
autogen_run_code_in_docker=None,
autogen_max_consecutive_auto_reply=None,
autogen_max_turns=None,
autogen_timeout=None,
autogen_cache_seed=None,
agent_venv_dir=None,
agent_code_writer_system_message=None,
agent_system_site_packages=None,
autogen_code_restrictions_level=None,
autogen_silent_exchange=None,
agent_verbose=None) -> dict:
def run_autogen_2agent(query=None,
visible_models=None,
stream_output=None,
max_new_tokens=None,
authorization=None,
chat_conversation=None,
text_context_list=None,
system_prompt=None,
image_file=None,
# autogen/agent specific parameters
agent_type=None,
autogen_stop_docker_executor=None,
autogen_run_code_in_docker=None,
autogen_max_consecutive_auto_reply=None,
autogen_max_turns=None,
autogen_timeout=None,
autogen_cache_seed=None,
agent_venv_dir=None,
agent_code_writer_system_message=None,
agent_system_site_packages=None,
autogen_code_restrictions_level=None,
autogen_silent_exchange=None,
agent_verbose=None) -> dict:
assert agent_type in ['autogen_2agent', 'auto'], "Invalid agent_type: %s" % agent_type
# raise openai.BadRequestError("Testing Error Handling")
# raise ValueError("Testing Error Handling")
Expand Down
15 changes: 7 additions & 8 deletions openai_server/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,15 +363,14 @@ def split_concatenated_dicts(concatenated_dicts: str):
def get_generator(instruction, gen_kwargs, use_agent=False, stream_output=False):
if use_agent:
agent_type = gen_kwargs.get('agent_type', 'auto')
if agent_type in ['auto', 'autogen_2agent']:
if agent_type == 'auto':
agent_type = 'autogen_2agent'
if agent_type in ['autogen_2agent']:
from openai_server.agent_utils import set_dummy_term, run_agent
set_dummy_term() # before autogen imported
from openai_server.autogen_2agent_backend import run_autogen_2agent
run_agent_func = functools.partial(run_agent, run_agent_func=run_autogen_2agent)
from openai_server.autogen_utils import get_autogen_response
if agent_type in ['auto', 'autogen_2agent']:
from openai_server.agent_utils import set_dummy_term, run_agent
set_dummy_term() # before autogen imported
from openai_server.autogen_2agent_backend import run_autogen
run_agent_func = functools.partial(run_agent, run_autogen_func=run_autogen)
else:
raise ValueError("No such agent_type %s" % agent_type)
generator = get_autogen_response(run_agent_func,
instruction, gen_kwargs, chunk_response=stream_output,
stream_output=stream_output)
Expand Down
2 changes: 1 addition & 1 deletion src/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "6c42e2b02827be9ef5f8605fe942a3b0846d9beb"
__version__ = "84bbf7517ce41c5bd3dbebfb50f7ac6545146ed1"

0 comments on commit 06285c2

Please sign in to comment.