diff --git a/app/schemas/chat_robot.py b/app/schemas/chat_robot.py index cebf74a..01feeae 100644 --- a/app/schemas/chat_robot.py +++ b/app/schemas/chat_robot.py @@ -2,7 +2,6 @@ from pydantic import BaseModel class ChatRobotModel(BaseModel): - gender: str message: str session_id: str user_id: int diff --git a/app/service/chat_robot/script/main.py b/app/service/chat_robot/script/main.py index 3342a5c..3890a0e 100644 --- a/app/service/chat_robot/script/main.py +++ b/app/service/chat_robot/script/main.py @@ -92,7 +92,6 @@ def chat(post_data): user_id = post_data.user_id session_id = post_data.session_id input_message = post_data.message - gender = post_data.gender # final_outputs = agent_executor( # {"input": input_message, "gender": gender}, @@ -100,7 +99,7 @@ def chat(post_data): # 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 = { # 'user_id': user_id, # 'session_id': session_id, diff --git a/app/service/chat_robot/script/prompt.py b/app/service/chat_robot/script/prompt.py index 121e57d..3cd2fca 100644 --- a/app/service/chat_robot/script/prompt.py +++ b/app/service/chat_robot/script/prompt.py @@ -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. """ +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 = """ 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. diff --git a/app/service/chat_robot/script/service/CallQWen.py b/app/service/chat_robot/script/service/CallQWen.py index d2f28a0..5ab74ba 100644 --- a/app/service/chat_robot/script/service/CallQWen.py +++ b/app/service/chat_robot/script/service/CallQWen.py @@ -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.database import CustomDatabase 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 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 -def call_with_messages(message, gender): +def call_with_messages(message): global tool_info user_input = message print('\n') messages = [ { - "content": FASHION_CHAT_BOT_PREFIX, # 系统message + # "content": FASHION_CHAT_BOT_PREFIX, # 系统message + "content": FASHION_CHAT_BOT_PREFIX_TEMP, # 修改后的系统message "role": "system" }, { @@ -255,7 +256,7 @@ def call_with_messages(message, gender): tool_info = {"name": "search_from_internet", "role": "tool"} content = json.loads(assistant_output.tool_calls[0]['function']['arguments']) 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) flag = False @@ -282,6 +283,8 @@ def call_with_messages(message, gender): result_content = tool_info['content'] elif assistant_output.tool_calls[0]['function']['name'] == 'get_image_from_vector_db': 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", 'content': get_image_from_vector_db(gender, content['parameters']['content'] if "parameters" in content.keys() else content['content'])} flag = False