From a5ceabba047b338a71499896ecac8d47c908ad27 Mon Sep 17 00:00:00 2001 From: zhouchengrong Date: Mon, 2 Dec 2024 20:13:33 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=20translator=20=E5=88=87=E6=8D=A2ollama?= =?UTF-8?q?=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit ea3e6667a051c2003da5055032ea486f0b338b6a) --- .../chatgpt_for_translation.py | 67 +++++++++---------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/app/service/prompt_generation/chatgpt_for_translation.py b/app/service/prompt_generation/chatgpt_for_translation.py index 05d85fb..5d720b9 100644 --- a/app/service/prompt_generation/chatgpt_for_translation.py +++ b/app/service/prompt_generation/chatgpt_for_translation.py @@ -1,6 +1,5 @@ import json - import requests from dashscope import Generation from requests import RequestException @@ -17,35 +16,35 @@ from app.core.config import QWEN_API_KEY # openai_api_key=OPENAI_API_KEY, # temperature=0) -prefix_for_llama = ( - """ - Translate everything within the brackets [] into English. - Never translate or modify any English input. - The input must be fully translated into coherent English sentences. - Please only output the translated result.\n - """ - ) +# prefix_for_llama = ( +# """ +# Translate everything within the brackets [] into English. +# Never translate or modify any English input. +# The input must be fully translated into coherent English sentences. +# Please only output the translated result.\n +# """ +# ) def translate_to_en(text): - template = ( - """You are a translation expert, proficient in various languages. - And can translate various languages into English. - Please translate to grammatically correct English regardless of the input language. - If the input is already in English, or consists of letters or numbers such as "cat", "abc", or "1", - output the input text exactly as it is without any modifications or additions. - If there are grammatical errors, correct them and then output the sentence.""" - ) - - prefix = ( - """ - Translate everything within the brackets [] into English. - Never translate or modify any English input. - The input must be fully translated into coherent English sentences. - Never present the translation results in the format - "The translation of \"Material suave\" into English would be \"Smooth material.\"". Instead, directly output "Smooth material". - """ - ) + # template = ( + # """You are a translation expert, proficient in various languages. + # And can translate various languages into English. + # Please translate to grammatically correct English regardless of the input language. + # If the input is already in English, or consists of letters or numbers such as "cat", "abc", or "1", + # output the input text exactly as it is without any modifications or additions. + # If there are grammatical errors, correct them and then output the sentence.""" + # ) + # + # prefix = ( + # """ + # Translate everything within the brackets [] into English. + # Never translate or modify any English input. + # The input must be fully translated into coherent English sentences. + # Never present the translation results in the format + # "The translation of \"Material suave\" into English would be \"Smooth material.\"". Instead, directly output "Smooth material". + # """ + # ) messages = [ # { # Translate the entire text and ensure the output is a complete and coherent sentence in English. @@ -54,7 +53,7 @@ def translate_to_en(text): # }, { # "content": input('请输入:'), # 用户message - "content": prefix + text, # 用户message + "content": text, # 用户message "role": "user" } ] @@ -74,7 +73,7 @@ def translate_to_en(text): def get_response(messages): response = Generation.call( model='qwen-turbo', - api_key= QWEN_API_KEY, + api_key=QWEN_API_KEY, messages=messages, # seed=random.randint(1, 10000), # 设置随机数种子seed,如果没有设置,则随机数种子默认为1234 result_format='message', # 将输出设置为message形式 @@ -84,15 +83,15 @@ def get_response(messages): def get_translation_from_llama3(text): - url = "http://localhost:11434/api/generate" + url = "http://10.1.1.240:11434/api/generate" # url = "http://10.1.1.240:1143/api/generate" - prompt = f"System: {prefix_for_llama}\nUser:[{text}]" + # prompt = f"System: {prefix_for_llama}\nUser:[{text}]" # 创建请求的负载 payload = { - "model": "llama3.2", - "prompt": prompt, + "model": "translator", + "prompt": f"[{text}]", "stream": False } @@ -114,7 +113,7 @@ def get_translation_from_llama3(text): def main(): """Main function""" - text = translate_to_en("fire") + text = get_translation_from_llama3("[火焰]") print(text)