Skip to content
Merged
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
30 changes: 15 additions & 15 deletions apps/agentfabric/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import importlib
import os
import random
import shutil
Expand All @@ -11,7 +10,7 @@
from builder_core import (beauty_output, gen_response_and_process,
init_builder_chatbot_agent)
from config_utils import (DEFAULT_AGENT_DIR, Config, get_avatar_image,
get_ci_dir, get_user_cfg_file, get_user_dir,
get_ci_dir, get_user_dir,
is_valid_plugin_configuration, parse_configuration,
save_avatar_image, save_builder_configuration,
save_plugin_configuration)
Expand All @@ -34,10 +33,10 @@ def init_user(uuid_str, state):
state['user_agent'] = user_agent
state['user_memory'] = user_memory
except Exception as e:
logger.error(
logger.query_error(
uuid=uuid_str,
error=str(e),
content={'error_traceback': traceback.format_exc()})
details={'error_traceback': traceback.format_exc()})
return state


Expand All @@ -47,10 +46,10 @@ def init_builder(uuid_str, state):
state['builder_agent'] = builder_agent
state['builder_memory'] = builder_memory
except Exception as e:
logger.error(
logger.query_error(
uuid=uuid_str,
error=str(e),
content={'error_traceback': traceback.format_exc()})
details={'error_traceback': traceback.format_exc()})
return state


Expand All @@ -60,10 +59,10 @@ def update_builder(uuid_str, state):
builder_agent = init_builder_chatbot_agent(uuid_str)
state['builder_agent'] = builder_agent
except Exception as e:
logger.error(
logger.query_error(
uuid=uuid_str,
error=str(e),
content={'error_traceback': traceback.format_exc()})
details={'error_traceback': traceback.format_exc()})

return state

Expand Down Expand Up @@ -292,10 +291,10 @@ def check_uuid(uuid_str):

# 初始化表单
def init_ui_config(uuid_str, _state, builder_cfg, model_cfg, tool_cfg):
logger.info(
logger.query_info(
uuid=uuid_str,
message='builder_cfg',
content={'builder_cfg': str(builder_cfg)})
details={'builder_cfg': str(builder_cfg)})
# available models
models = list(model_cfg.keys())
capabilities = [(tool_cfg[tool_key]['name'], tool_key)
Expand Down Expand Up @@ -408,8 +407,8 @@ def create_send_message(chatbot, input, _state, uuid_str):
llm_result = frame.get('llm_text', '')
exec_result = frame.get('exec_result', '')
step_result = frame.get('step', '')
logger.info(
uuid=uuid_str, message='frame', content={'frame': str(frame)})
logger.query_info(
uuid=uuid_str, message='frame', details={'frame': str(frame)})
if len(exec_result) != 0:
if isinstance(exec_result, dict):
exec_result = exec_result['result']
Expand Down Expand Up @@ -506,10 +505,10 @@ def process_configuration(uuid_str, bot_avatar, name, description,
if is_valid_plugin_configuration(openapi_plugin_cfg):
save_plugin_configuration(openapi_plugin_cfg, uuid_str)
except Exception as e:
logger.error(
logger.query_error(
uuid=uuid_str,
error=str(e),
content={'error_traceback': traceback.format_exc()})
details={'error_traceback': traceback.format_exc()})

save_builder_configuration(builder_cfg, uuid_str)
update_builder(uuid_str, state)
Expand Down Expand Up @@ -585,7 +584,8 @@ def preview_send_message(chatbot, input, _state, uuid_str):
input.text,
history=history,
ref_doc=ref_doc,
append_files=append_files):
append_files=append_files,
uuid_str=uuid_str):
# append_files=new_file_paths):
# important! do not change this
response += frame
Expand Down
4 changes: 2 additions & 2 deletions apps/agentfabric/appBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ def init_user(state):
state['user_agent'] = user_agent
state['user_memory'] = user_memory
except Exception as e:
logger.error(
logger.query_error(
uuid=uuid_str,
error=str(e),
content={'error_traceback': traceback.format_exc()})
details={'error_traceback': traceback.format_exc()})
raise Exception(e)
return state

Expand Down
15 changes: 8 additions & 7 deletions apps/agentfabric/builder_core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# flake8: noqa E501
import os
import re
from http import HTTPStatus

import json
from config_utils import DEFAULT_UUID_HISTORY, parse_configuration
Expand All @@ -24,12 +25,12 @@ def init_builder_chatbot_agent(uuid_str: str, session='default'):
builder_cfg, model_cfg, _, _, _, _ = parse_configuration(uuid_str)

# init agent
logger.info(
logger.query_info(
uuid=uuid_str, message=f'using builder model {builder_cfg.model}')
llm_config = {'model': builder_cfg.model, 'model_server': 'dashscope'}
# function_list = ['image_gen'] # use image_gen to draw logo?

agent = AgentBuilder(llm=llm_config)
agent = AgentBuilder(llm=llm_config, uuid_str=uuid_str)

current_history_path = os.path.join(DEFAULT_UUID_HISTORY, uuid_str,
session + '_builder.json')
Expand All @@ -51,7 +52,7 @@ def gen_response_and_process(agent,
llm_result = ''
llm_result_prefix = ''
try:
response = agent.run(query, history=history)
response = agent.run(query, history=history, uuid_str=uuid_str)
for s in response:
llm_result += s
answer, finish, llm_result_prefix = agent.parse_answer(
Expand All @@ -74,10 +75,10 @@ def gen_response_and_process(agent,
Message(role='assistant', content=llm_result),
])
if print_info:
logger.info(
logger.query_info(
uuid=uuid_str,
message=f'LLM output in round 0',
content={'llm_result': llm_result})
details={'llm_result': llm_result})
except Exception as e:
yield {'error': 'llm result is not valid'}

Expand All @@ -95,14 +96,14 @@ def gen_response_and_process(agent,
try:
answer = json.loads(rich_config)
except Exception:
logger.error(uuid=uuid_str, error='parse RichConfig error')
logger.query_error(uuid=uuid_str, error='parse RichConfig error')
return
agent.last_assistant_structured_response['rich_config_dict'] = answer
builder_cfg = config_conversion(answer, uuid_str=uuid_str)
yield {'exec_result': {'result': builder_cfg}}
yield {'step': CONFIG_UPDATED_STEP}
except ValueError as e:
logger.error(uuid=uuid_str, error=str(e))
logger.query_error(uuid=uuid_str, error=str(e))
yield {'error content=[{}]'.format(llm_result)}
return

Expand Down
2 changes: 1 addition & 1 deletion apps/agentfabric/config_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def parse_configuration(uuid_str=''):
for name, config in config_dict.items():
available_plugin_list.append(name)
except Exception as e:
logger.error(
logger.query_error(
uuid=uuid_str,
error=str(e),
content={
Expand Down
2 changes: 1 addition & 1 deletion apps/agentfabric/help_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def call_wanx(prompt, save_path, uuid_str):
with open(save_path, 'wb+') as f:
f.write(requests.get(result.url).content)
else:
logger.error(
logger.query_error(
uuid=uuid_str,
error='wanx error',
content={
Expand Down
9 changes: 6 additions & 3 deletions apps/agentfabric/user_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ def init_user_chatbot_agent(uuid_str='', session='default'):
model_cfg[builder_cfg.model]['generate_cfg']['stop'] = 'Observation'

# build model
logger.info(
logger.query_info(
uuid=uuid_str,
message=f'using model {builder_cfg.model}',
content={'model_config': model_cfg[builder_cfg.model]})
details={'model_config': model_cfg[builder_cfg.model]})

# update function_list
function_list = parse_tool_cfg(tool_cfg)
Expand All @@ -39,7 +39,10 @@ def init_user_chatbot_agent(uuid_str='', session='default'):
'instruction': builder_cfg.description
}
agent = RolePlay(
function_list=function_list, llm=llm_config, instruction=instruction)
function_list=function_list,
llm=llm_config,
instruction=instruction,
uuid_str=uuid_str)

# build memory
storage_path = get_user_dir(uuid_str)
Expand Down
3 changes: 3 additions & 0 deletions modelscope_agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@ def __init__(self,
self.name = name
self.description = description
self.instruction = instruction
self.uuid_str = kwargs.get('uuid_str', None)

def run(self, *args, **kwargs) -> Union[str, Iterator[str]]:
if 'lang' not in kwargs:
if has_chinese_chars([args, kwargs]):
kwargs['lang'] = 'zh'
else:
kwargs['lang'] = 'en'
if 'uuid_str' not in kwargs and self.uuid_str is not None:
kwargs['uuid_str'] = self.uuid_str
return self._run(*args, **kwargs)

@abstractmethod
Expand Down
2 changes: 1 addition & 1 deletion modelscope_agent/agents/agent_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def _run(self,
# concat the new messages
messages.append({'role': 'user', 'content': user_request})

return self._call_llm(messages=messages)
return self._call_llm(messages=messages, **kwargs)

def parse_answer(self, llm_result_prefix: str, llm_result: str):
"""
Expand Down
2 changes: 1 addition & 1 deletion modelscope_agent/agents/function_calling.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from modelscope_agent import Agent
from modelscope_agent.agents.react import ReAct
from modelscope_agent.agents.react_chat import ReActChat
from modelscope_agent.log import logger
from modelscope_agent.utils.logger import agent_logger as logger


class FunctionCalling(Agent):
Expand Down
2 changes: 1 addition & 1 deletion modelscope_agent/agents/role_play.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def _run(self,
prompt=planning_prompt,
stream=True,
stop=['Observation:', 'Observation:\n'],
)
**kwargs)

llm_result = ''
for s in output:
Expand Down
45 changes: 33 additions & 12 deletions modelscope_agent/llm/dashscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@
from typing import Dict, Iterator, List, Optional

import dashscope
from modelscope_agent.utils.logger import agent_logger as logger

from .base import BaseChatModel, register_llm


def stream_output(response):
def stream_output(response, **kwargs):
last_len = 0
delay_len = 5
in_delay = False
text = ''
for trunk in response:
if trunk.status_code == HTTPStatus.OK:
logger.query_info(
uuid=kwargs.get('uuid_str', ''),
details={
'dashscope.request_id': trunk.request_id,
'dashscope.output': trunk.output
},
message='call dashscope generation api success')
text = trunk.output.choices[0].message.content
if (len(text) - last_len) <= delay_len:
in_delay = True
Expand All @@ -25,6 +33,15 @@ def stream_output(response):
yield now_rsp
last_len = len(real_text)
else:
logger.query_error(
uuid=kwargs.get('uuid_str', ''),
details={
'dashscope.request_id': trunk.request_id,
'dashscope.status_code': trunk.status_code,
'dashscope.code': trunk.code,
'dashscope.message': trunk.message
},
message='call dashscope generation api error')
err = '\nError code: %s. Error message: %s' % (trunk.code,
trunk.message)
if trunk.code == 'DataInspectionFailed':
Expand Down Expand Up @@ -55,20 +72,24 @@ def _chat_stream(self,
stop: Optional[List[str]] = None,
**kwargs) -> Iterator[str]:
stop = stop or []
top_p = kwargs.get('top_p', 0.8)

response = dashscope.Generation.call(
self.model,
messages=messages, # noqa
stop_words=[{
generation_input = {
'model': self.model,
'messages': messages, # noqa
'stop_words': [{
'stop_str': word,
'mode': 'exclude'
} for word in stop],
top_p=top_p,
result_format='message',
stream=True,
)
return stream_output(response)
'top_p': kwargs.get('top_p', 0.8),
'result_format': 'message',
'stream': True,
}

logger.query_info(
uuid=kwargs.get('uuid_str', ''),
details=generation_input,
message='call dashscope generation api')
response = dashscope.Generation.call(**generation_input)
return stream_output(response, **kwargs)

def _chat_no_stream(self,
messages: List[Dict],
Expand Down
17 changes: 0 additions & 17 deletions modelscope_agent/log.py

This file was deleted.

2 changes: 1 addition & 1 deletion modelscope_agent/storage/file_storage.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import hashlib
import os

from modelscope_agent.log import logger
from modelscope_agent.utils.logger import agent_logger as logger
from modelscope_agent.utils.utils import (print_traceback, read_text_from_file,
save_text_to_file)

Expand Down
2 changes: 1 addition & 1 deletion modelscope_agent/tools/doc_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

import json
import json5
from modelscope_agent.log import logger
from modelscope_agent.schemas import Document
from modelscope_agent.storage import BaseStorage, DocumentStorage
from modelscope_agent.tools.base import BaseTool, register_tool
from modelscope_agent.tools.similarity_search import (RefMaterialInput,
RefMaterialInputItem)
from modelscope_agent.utils.logger import agent_logger as logger
from modelscope_agent.utils.parse_doc import (count_tokens, parse_doc,
parse_html_bs)
from modelscope_agent.utils.utils import print_traceback, save_text_to_file
Expand Down
Loading