Skip to content

Commit

Permalink
[HOTFIX] Fix bugs for Flask (#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
qbc2016 authored Aug 7, 2024
1 parent dcb7c8b commit ab46935
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 46 deletions.
5 changes: 1 addition & 4 deletions scripts/flask_modelscope/setup_ms_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from flask import request

import modelscope
from agentscope.utils.tools import reform_dialogue


def create_timestamp(format_: str = "%Y-%m-%d %H:%M:%S") -> str:
Expand All @@ -23,9 +22,7 @@ def get_response() -> dict:
"""Receive post request and return response"""
json = request.get_json()

inputs = json.pop("inputs")

inputs = reform_dialogue(inputs)
inputs = json.pop("messages")

global model, tokenizer

Expand Down
5 changes: 1 addition & 4 deletions scripts/flask_transformers/setup_hf_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from flask import request

import transformers
from agentscope.utils.tools import reform_dialogue


def create_timestamp(format_: str = "%Y-%m-%d %H:%M:%S") -> str:
Expand All @@ -23,12 +22,10 @@ def get_response() -> dict:
"""Receive post request and return response"""
json = request.get_json()

inputs = json.pop("inputs")
inputs = json.pop("messages")

global model, tokenizer

inputs = reform_dialogue(inputs)

if hasattr(tokenizer, "apply_chat_template"):
prompt = tokenizer.apply_chat_template(
inputs,
Expand Down
4 changes: 2 additions & 2 deletions src/agentscope/models/post_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,14 @@ def format(
)

# OpenAI
if model_name.startswith("gpt-"):
if model_name and model_name.startswith("gpt-"):
return OpenAIChatWrapper.static_format(
*args,
model_name=model_name,
)

# Gemini
elif model_name.startswith("gemini"):
elif model_name and model_name.startswith("gemini"):
return GeminiChatWrapper.format(*args)

# Include DashScope, ZhipuAI, Ollama, the other models supported by
Expand Down
36 changes: 0 additions & 36 deletions src/agentscope/utils/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,42 +315,6 @@ def _convert_to_str(content: Any) -> str:
return str(content)


def reform_dialogue(input_msgs: list[dict]) -> list[dict]:
"""record dialog history as a list of strings"""
messages = []

dialogue = []
for i, unit in enumerate(input_msgs):
if i == 0 and unit["role"] == "system":
# system prompt
messages.append(
{
"role": unit["role"],
"content": _convert_to_str(unit["content"]),
},
)
else:
# Merge all messages into a conversation history prompt
dialogue.append(
f"{unit['name']}: {_convert_to_str(unit['content'])}",
)

dialogue_history = "\n".join(dialogue)

user_content_template = "## Conversation History\n{dialogue_history}"

messages.append(
{
"role": "user",
"content": user_content_template.format(
dialogue_history=dialogue_history,
),
},
)

return messages


def _join_str_with_comma_and(elements: List[str]) -> str:
"""Return the JSON string with comma, and use " and " between the last two
elements."""
Expand Down

0 comments on commit ab46935

Please sign in to comment.