Skip to content

Commit

Permalink
Bypass json5 load subtasks in plan_exec.py unless its a string
Browse files Browse the repository at this point in the history
  • Loading branch information
LostQuant committed May 1, 2024
1 parent 3619c25 commit 3c409ff
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
4 changes: 3 additions & 1 deletion XAgent/agent/base_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ def generate(self,
*args,**kwargs)

message = {}
function_call_args:dict = json5.loads(response["choices"][0]["message"]["function_call"]['arguments'])
function_call_args:dict = response["choices"][0]["message"]["function_call"]['arguments']
if isinstance(function_call_args, (str, bytes)):
function_call_args = json5.loads(function_call_args)

if arguments is not None:
message['arguments'] = {
Expand Down
4 changes: 3 additions & 1 deletion XAgent/ai_functions/function_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ def execute(self,function_name:str,return_generation_usage:bool=False,function_c
function_call={'name':function_cfg['function']['name']},
**completions_kwargs
)
returns = json5.loads(response['choices'][0]['message']['function_call']['arguments'])
returns = response['choices'][0]['message']['function_call']['arguments']
if isinstance(returns, (str, bytes)):
returns = json5.loads(returns)
case 'xagent':
arguments = function_cfg['function']['parameters']
response = objgenerator.chatcompletion(
Expand Down
3 changes: 3 additions & 0 deletions XAgent/ai_functions/request/openai.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import openai
import datetime as dt
from XAgent.logs import logger
from XAgent.config import CONFIG, get_apiconfig_by_model, get_model_name

Expand Down Expand Up @@ -60,6 +61,8 @@ def chatcompletion_request(**kwargs):
chatcompletion_kwargs.update({"api_base": api_base})
chatcompletion_kwargs.update(kwargs)

print(f"[{dt.datetime.now()}] chatcompletion:", chatcompletion_kwargs)

try:
response = openai.ChatCompletion.create(**chatcompletion_kwargs)
response = json.loads(str(response))
Expand Down
6 changes: 5 additions & 1 deletion XAgent/workflow/plan_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ def initial_plan_generation(self, agent_dispatcher):
functions=[split_functions],
)

subtasks = json5.loads(new_message["function_call"]["arguments"])
subtasks = new_message["function_call"]["arguments"]

# To preserve the original flow in case `subtasks` is a string or bytes
if isinstance(subtasks, (str, bytes)):
subtasks = json5.loads(subtasks)

for subtask_item in subtasks["subtasks"]:
subplan = plan_function_output_parser(subtask_item)
Expand Down
6 changes: 5 additions & 1 deletion XAgent/workflow/reflection.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def get_posterior_knowledge(all_plan: Plan,
arguments=function_manager.get_function_schema('generate_posterior_knowledge')['parameters']
)

data = json5.loads(new_message["arguments"])
data = new_message["arguments"]

# To preserve the original flow in case `data` is a string or bytes
if isinstance(data, (str, bytes)):
data = json5.loads(data)

return data

0 comments on commit 3c409ff

Please sign in to comment.