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

builder support stream #124

Merged
merged 3 commits into from
Nov 28, 2023
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
2 changes: 1 addition & 1 deletion apps/agentfabric/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def create_send_message(chatbot, input, _state, uuid_str):
else:
content = llm_result
frame_text = content
response = f'{response}\n{frame_text}'
response = f'{response}{frame_text}'
chatbot[-1] = (input, response)
yield {
create_chatbot: chatbot,
Expand Down
70 changes: 48 additions & 22 deletions apps/agentfabric/builder_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@

LOGO_TOOL_NAME = 'logo_designer'

ASSISTANT_PROMPT = """Answer: <answer>\nConfig: <config>\nRichConfig: <rich_config>"""
ANSWER = 'Answer'
CONFIG = 'Config'
ASSISTANT_PROMPT = """{}: <answer>\n{}: <config>\nRichConfig: <rich_config>""".format(
ANSWER, CONFIG)


def init_builder_chatbot_agent(uuid_str):
Expand Down Expand Up @@ -113,47 +116,44 @@ def stream_run(self,

llm_result = ''
try:
# no stream yet
llm_result = self.llm.generate(llm_artifacts)
parser_obj = AnswerParser()
for s in self.llm.stream_generate(llm_artifacts=llm_artifacts):
llm_result += s
answer = parser_obj.parse_answer(llm_result)
if answer == '':
continue
yield {'llm_text': answer}

if print_info:
print(f'|LLM output in round {idx}:\n{llm_result}')

re_pattern_answer = re.compile(
pattern=r'Answer:([\s\S]+)\nConfig:')
res = re_pattern_answer.search(llm_result['content'])
llm_text = res.group(1).strip()
self._last_assistant_structured_response[
'answer_str'] = llm_text
yield {'llm_text': llm_text}
except Exception:
except Exception as e:
yield {'error': 'llm result is not valid'}

try:
if self.agent_type == AgentType.Messages:
content = llm_result['content']
else:
content = llm_result

re_pattern_config = re.compile(
pattern=r'Config: ([\s\S]+)\nRichConfig')
res = re_pattern_config.search(llm_result['content'])
res = re_pattern_config.search(llm_result)
config = res.group(1).strip()
self._last_assistant_structured_response['config_str'] = config

rich_config = content[content.rfind('RichConfig:')
+ len('RichConfig:'):].strip()
rich_config = llm_result[llm_result.rfind('RichConfig:')
+ len('RichConfig:'):].strip()
answer = json.loads(rich_config)
self._last_assistant_structured_response[
'rich_config_dict'] = answer
builder_cfg = config_conversion(answer)
yield {'exec_result': {'result': builder_cfg}}
except ValueError as e:
print(e)
yield {'error content=[{}]'.format(content)}
yield {'error content=[{}]'.format(llm_result)}
return

# record the llm_result result
_ = self.prompt_generator.generate(llm_result, '')
_ = self.prompt_generator.generate(
{
'role': 'assistant',
'content': llm_result
}, '')

messages = self.prompt_generator.history
if 'logo_prompt' in answer and len(messages) > 4 and (
Expand Down Expand Up @@ -202,3 +202,29 @@ def update_config_to_history(self, config: Dict):
'<config>', simple_config).replace('<rich_config>',
rich_config)
self.prompt_generator.history[-1]['content'] = new_content


class AnswerParser(object):

def __init__(self):
self._history = ''

def parse_answer(self, llm_result: str):
answer_prompt = ANSWER + ': '

if len(llm_result) >= len(answer_prompt):
start_pos = llm_result.find(answer_prompt)
end_pos = llm_result.find(f'\n{CONFIG}')
if start_pos >= 0:
if end_pos > start_pos:
result = llm_result[start_pos + len(answer_prompt):end_pos]
else:
result = llm_result[start_pos + len(answer_prompt):]
else:
result = llm_result
else:
result = ''

new_result = result[len(self._history):]
self._history = result
return new_result
10 changes: 7 additions & 3 deletions modelscope_agent/llm/dashscope_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,22 @@ def generate(self,
# in the form of text
return llm_result

def stream_generate(self, prompt, functions, **kwargs):
def stream_generate(self,
llm_artifacts: Union[str, dict],
functions=[],
**kwargs):
# print('repr(prompt): ', repr(prompt))
self.generate_cfg['use_raw_prompt'] = False
Copy link
Collaborator

Choose a reason for hiding this comment

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

check if llm_artifacts is str, convert it to messages format

total_response = ''
try:
responses = Generation.call(
model=self.model,
prompt=prompt,
messages=llm_artifacts,
stream=True,
**self.generate_cfg)
except Exception as e:
error = traceback.format_exc()
error_msg = f'LLM error with input {prompt} \n dashscope error: {str(e)} with traceback {error}'
error_msg = f'LLM error with input {llm_artifacts} \n dashscope error: {str(e)} with traceback {error}'
print(error_msg)
raise RuntimeError(error)

Expand Down
Loading