diff --git a/app/service/prompt_generation/chatgpt_for_translation.py b/app/service/prompt_generation/chatgpt_for_translation.py index 71d6e4f..4ade635 100644 --- a/app/service/prompt_generation/chatgpt_for_translation.py +++ b/app/service/prompt_generation/chatgpt_for_translation.py @@ -1,9 +1,8 @@ -import os +import logging from langchain.chains import LLMChain from langchain.chat_models import ChatOpenAI -from langchain_core.prompts import SystemMessagePromptTemplate, HumanMessagePromptTemplate, ChatPromptTemplate, \ - PromptTemplate +from langchain_core.prompts import SystemMessagePromptTemplate, HumanMessagePromptTemplate, ChatPromptTemplate from app.core.config import OPENAI_MODEL, OPENAI_API_KEY @@ -21,7 +20,8 @@ def translate_to_en(text): """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 in English or numbers, check for grammatical errors. If there are no errors, output the input directly. + 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.""" ) system_message_prompt = SystemMessagePromptTemplate.from_template(template) @@ -36,34 +36,41 @@ def translate_to_en(text): ) translate_chain = LLMChain(llm=llm, prompt=chat_prompt_template) - template = ( - """ - Input sentence: - {translate} - 1. Based on the input,adjust the input sentence to make it more suitable for prompts for generating images, - ensuring all key nouns or adjectives related to the image are retained. - 2. Simplify complex sentence structures and clarify ambiguous expressions. - 3. Only Output the adjusted English sentence. + result = translate_chain.invoke(text) - Output : - """ - ) - # "Based on the input sentence, extract key adjectives and nouns.Only Output extracted key words." - # 1. Check if the input sentence contains any grammatical errors. If there are errors, please correct them before proceeding. + logging.info("translate result : " + result.get('text')) + # print("translate result : " + result.get('text')) + return result.get('text') - prompt_template = PromptTemplate(input_variables=["translate"], template=template) - prompt_chain = LLMChain(llm=llm, prompt=prompt_template) - - from langchain.chains import SimpleSequentialChain - overall_chain = SimpleSequentialChain(chains=[translate_chain, prompt_chain], verbose=True) - - response = overall_chain.run(text) - return response + # template = ( + # """ + # Input sentence: + # {translate} + # 1. Based on the input,adjust the input sentence to make it more suitable for prompts for generating images, + # ensuring all key nouns or adjectives related to the image are retained. + # 2. Simplify complex sentence structures and clarify ambiguous expressions. + # 3. Only Output the adjusted English sentence. + # + # Output : + # """ + # ) + # # "Based on the input sentence, extract key adjectives and nouns.Only Output extracted key words." + # # 1. Check if the input sentence contains any grammatical errors. If there are errors, please correct them before proceeding. + # + # prompt_template = PromptTemplate(input_variables=["translate"], template=template) + # prompt_chain = LLMChain(llm=llm, prompt=prompt_template) + # + # from langchain.chains import SimpleSequentialChain + # overall_chain = SimpleSequentialChain(chains=[translate_chain, prompt_chain], verbose=True) + # + # response = overall_chain.run(text) + # return response def main(): """Main function""" - translate_to_en("生成一件运动风格的夹克,带有拉链和口袋,适合休闲穿着") + text = translate_to_en("fire") + print(text) if __name__ == "__main__":