chat-robot 取消性别传入,从用户输入中提取性别

This commit is contained in:
2025-04-23 11:51:53 +08:00
parent f68b5a9f04
commit b8fe29e735
4 changed files with 41 additions and 7 deletions

View File

@@ -2,7 +2,6 @@ from pydantic import BaseModel
class ChatRobotModel(BaseModel): class ChatRobotModel(BaseModel):
gender: str
message: str message: str
session_id: str session_id: str
user_id: int user_id: int

View File

@@ -92,7 +92,6 @@ def chat(post_data):
user_id = post_data.user_id user_id = post_data.user_id
session_id = post_data.session_id session_id = post_data.session_id
input_message = post_data.message input_message = post_data.message
gender = post_data.gender
# final_outputs = agent_executor( # final_outputs = agent_executor(
# {"input": input_message, "gender": gender}, # {"input": input_message, "gender": gender},
@@ -100,7 +99,7 @@ def chat(post_data):
# session_key=f"buffer:{user_id}:{session_id}", # session_key=f"buffer:{user_id}:{session_id}",
# ) # )
final_outputs = CallQWen.call_with_messages(input_message, gender) final_outputs = CallQWen.call_with_messages(input_message)
# api_response = { # api_response = {
# 'user_id': user_id, # 'user_id': user_id,
# 'session_id': session_id, # 'session_id': session_id,

View File

@@ -34,6 +34,39 @@ You may encounter the following types of questions:
Be careful to use the tools, since you are actually a chat bot. Tools can only be used when essential. Be careful to use the tools, since you are actually a chat bot. Tools can only be used when essential.
""" """
FASHION_CHAT_BOT_PREFIX_TEMP = """
You are a fashion design assistant with the following capabilities:
1. Direct conversation: Answer general questions (e.g., greetings, opinions).
2. Tool usage:
- `get_image_from_vector_db`: Retrieve clothing items (requires gender parameter).
- `internet_search`: Fetch real-time fashion trends.
- `tutorial_tool`: Provide styling guides.
Key Rules:
1. Tool Selection:
- Use `get_image_from_vector_db` for clothing queries (e.g., "show men's jackets").
- Use `internet_search` for time-sensitive queries (e.g., "2024 Paris Fashion Week trends").
- Use `tutorial_tool` for educational requests (e.g., "how to layer outfits").
2. Gender Handling (for `get_image_from_vector_db` only):
- Step 1: Check the **current user input** for gender keywords (e.g., "women/men/she/he"). If found, extract and pass as `gender`.
- Step 2: If no gender in current input, scan the **chat history** for the most recent gender reference.
- Step 3: If undetermined, default to `"unisex"`.
3. Output Format:
- Direct replies: Keep responses under 20 words.
- Tool calls:
- Always include required parameters (e.g., `gender` for `get_image_from_vector_db`).
- Auto-fill `gender` using the above rules if unspecified.
Examples:
1. User: "Find red dresses for women"
→ `get_image_from_vector_db(gender="female", query="dress")`
2. User: "show men's jackets"
→ `get_image_from_vector_db(gender="male", query="outwear")`
3. User: "Show casual outfits"
→ `get_image_from_vector_db(gender="unisex", query="casual outfits")`"""
TOOL_SELECT_SUFFIX = """ TOOL_SELECT_SUFFIX = """
Prior to proceeding, it is essential to carefully assess the question and select the appropriate tools or approach accordingly. Prior to proceeding, it is essential to carefully assess the question and select the appropriate tools or approach accordingly.
For database-related questions, use SQL tools to identify relevant tables and query their schemas. For database-related questions, use SQL tools to identify relevant tables and query their schemas.

View File

@@ -9,7 +9,7 @@ from app.core.config import *
from app.service.chat_robot.script.callbacks.qwen_callback_handler import QWenCallbackHandler from app.service.chat_robot.script.callbacks.qwen_callback_handler import QWenCallbackHandler
from app.service.chat_robot.script.database import CustomDatabase from app.service.chat_robot.script.database import CustomDatabase
from app.service.chat_robot.script.prompt import FASHION_CHAT_BOT_PREFIX, TOOLS_FUNCTIONS_SUFFIX, TUTORIAL_TOOL_RETURN, \ from app.service.chat_robot.script.prompt import FASHION_CHAT_BOT_PREFIX, TOOLS_FUNCTIONS_SUFFIX, TUTORIAL_TOOL_RETURN, \
GET_LANGUAGE_PREFIX GET_LANGUAGE_PREFIX, FASHION_CHAT_BOT_PREFIX_TEMP
from app.service.search_image_with_text.service import query from app.service.search_image_with_text.service import query
get_database_table_description = "Input is an empty string, output is a comma separated list of tables in the database." get_database_table_description = "Input is an empty string, output is a comma separated list of tables in the database."
@@ -212,14 +212,15 @@ def get_assistant_response(messages):
return response return response
def call_with_messages(message, gender): def call_with_messages(message):
global tool_info global tool_info
user_input = message user_input = message
print('\n') print('\n')
messages = [ messages = [
{ {
"content": FASHION_CHAT_BOT_PREFIX, # 系统message # "content": FASHION_CHAT_BOT_PREFIX, # 系统message
"content": FASHION_CHAT_BOT_PREFIX_TEMP, # 修改后的系统message
"role": "system" "role": "system"
}, },
{ {
@@ -255,7 +256,7 @@ def call_with_messages(message, gender):
tool_info = {"name": "search_from_internet", "role": "tool"} tool_info = {"name": "search_from_internet", "role": "tool"}
content = json.loads(assistant_output.tool_calls[0]['function']['arguments']) content = json.loads(assistant_output.tool_calls[0]['function']['arguments'])
message = [ message = [
{'role': 'assistant', 'content': content['query']} {'role': 'assistant', 'content': content['query'] if "query" in content.keys() else user_input}
] ]
tool_info['content'] = search_from_internet(message) tool_info['content'] = search_from_internet(message)
flag = False flag = False
@@ -282,6 +283,8 @@ def call_with_messages(message, gender):
result_content = tool_info['content'] result_content = tool_info['content']
elif assistant_output.tool_calls[0]['function']['name'] == 'get_image_from_vector_db': elif assistant_output.tool_calls[0]['function']['name'] == 'get_image_from_vector_db':
content = json.loads(assistant_output.tool_calls[0]['function']['arguments']) content = json.loads(assistant_output.tool_calls[0]['function']['arguments'])
# todo 从历史对话中获取性别目前无法获得性别时默认使用female
gender = content['gender'] if "gender" in content.keys() and content['gender'] != 'unisex' else 'female'
tool_info = {"name": "get_image_from_vector_db", "role": "tool", tool_info = {"name": "get_image_from_vector_db", "role": "tool",
'content': get_image_from_vector_db(gender, content['parameters']['content'] if "parameters" in content.keys() else content['content'])} 'content': get_image_from_vector_db(gender, content['parameters']['content'] if "parameters" in content.keys() else content['content'])}
flag = False flag = False