Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if object is already a DICT before it is passed to JSON5.loads for parsing #401

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions XAgent/agent/base_agent.py

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hello

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert

Hi, sry if I missed anything but i dont see any comments

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 All @@ -147,7 +149,9 @@ def generate(self,
function_call=function_call,
stop=stop,
*args,**kwargs)
message = json5.loads(response["choices"][0]["message"]['content'])
message = response["choices"][0]["message"]['content']
if isinstance(message, (str, bytes)):
message = json5.loads(message)
case _:
raise NotImplementedError(f"Request type {CONFIG.default_request_type} not implemented")

Expand Down
9 changes: 7 additions & 2 deletions XAgent/ai_functions/function_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,20 @@ 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(
messages=messages,
arguments=arguments,
**completions_kwargs
)
returns = json5.loads(response['choices'][0]['message']['content'])['arguments']
content = response['choices'][0]['message']['content']
if isinstance(content, (str, bytes)):
returns = json5.loads(content)
returns = content['arguments']

if return_generation_usage:
return returns, response['usage']
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
12 changes: 10 additions & 2 deletions 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 Expand Up @@ -270,7 +274,11 @@ def plan_refine_mode(self, now_dealing_task: Plan, toolserver_interface, agent_d
additional_insert_index=-1,
)
function_name = new_message["function_call"]["name"]
function_input = json5.loads(new_message["function_call"]["arguments"])
function_input = new_message["function_call"]["arguments"]

if isinstance(function_input, (str, bytes)):
function_input = json5.loads(function_input)


if function_input['operation'] == 'split':
# modify function_input here
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