翻译前置语言判断

This commit is contained in:
2025-01-14 09:40:15 +08:00
parent ff06db7ec1
commit edcd9b68cc
4 changed files with 53 additions and 5 deletions

View File

@@ -26,7 +26,7 @@ def prompt_generation(request_data: PromptGenerationImageModel):
"""
try:
logger.info(f"prompt_generation request item is : @@@@@@:{request_data}")
data = get_translation_from_llama3("[" + request_data.text + "]")
data = get_translation_from_llama3(request_data.text)
logger.info(f"prompt_generation response @@@@@@:{data}")
except Exception as e:
logger.warning(f"prompt_generation Run Exception @@@@@@:{e}")

View File

@@ -69,3 +69,5 @@ TOOLS_FUNCTIONS_SUFFIX = (
)
TUTORIAL_TOOL_RETURN = "Commencing the systematic tutorial guide now."
GET_LANGUAGE_PREFIX = "Please identify the language. Only output the language name"

View File

@@ -8,7 +8,8 @@ from urllib3.exceptions import NewConnectionError
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
from app.service.chat_robot.script.prompt import FASHION_CHAT_BOT_PREFIX, TOOLS_FUNCTIONS_SUFFIX, TUTORIAL_TOOL_RETURN, \
GET_LANGUAGE_PREFIX
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."
@@ -274,7 +275,7 @@ def call_with_messages(message, gender):
flag = False
result_content = tool_info['content']
response_type = "image"
else :
else:
tool_info = {"name": assistant_output.tool_calls[0]['function']['name'], 'content': 'null'}
logging.info(assistant_output.tool_calls[0]['function']['name'] + "(unknown tools)")
flag = False
@@ -300,5 +301,23 @@ def tutorial_tool():
return TUTORIAL_TOOL_RETURN
def get_language(message: str) -> str:
messages = [
{
"content": message, # 用户message
"role": "user"
},
{
"content": GET_LANGUAGE_PREFIX, # ai message
"role": "assistant"
}
]
first_response = get_response(messages)
assistant_output = first_response.output.choices[0].message.content
logging.info(f"大模型输出信息:{first_response}\n判断用户输入的语言为:{assistant_output}")
return assistant_output
if __name__ == '__main__':
call_with_messages()
get_language("")

View File

@@ -8,6 +8,7 @@ from requests import RequestException
from retry import retry
from app.core.config import QWEN_API_KEY
from app.service.chat_robot.script.service.CallQWen import get_language
logger = logging.getLogger(__name__)
@@ -93,7 +94,13 @@ def get_translation_from_llama3(text):
# prompt = f"System: {prefix_for_llama}\nUser:[{text}]"
# 创建请求的负载
# 先获取用户输入文本的语言
language = get_language(text)
if 'English' in language:
return text
# 创建请求的负载 translator是自定义的翻译模型
payload = {
"model": "translator",
"prompt": f"[{text}]",
@@ -117,6 +124,26 @@ def get_translation_from_llama3(text):
print(response.text)
# 在llama3中创建一个翻译模型
# def create_model_with_llama(text):
# url = "http://localhost:11434/api/create"
# # url = "http://10.1.1.240:1143/api/generate"
#
# # prompt = f"System: {prefix_for_llama}\nUser:[{text}]"
#
# # 创建翻译器的配置文件
# payload = {
# "model": "translator",
# "modelfile": "FROM llama3\nSYSTEM Translate everything within the brackets [] into English."
# "Never translate or modify any English input."
# "The input must be fully translated into coherent English sentences."
# }
#
# # 将负载转换为 JSON 格式
# headers = {'Content-Type': 'application/json'}
# response = requests.post(url, data=json.dumps(payload), headers=headers)
def main():
"""Main function"""
text = get_translation_from_llama3("[火焰]")