Files
AiDA_Python/app/service/prompt_generation/chatgpt_for_translation.py

78 lines
3.0 KiB
Python
Raw Normal View History

2024-06-28 13:59:53 +08:00
import logging
2024-05-29 11:12:59 +08:00
from langchain.chains import LLMChain
from langchain.chat_models import ChatOpenAI
2024-06-28 13:59:53 +08:00
from langchain_core.prompts import SystemMessagePromptTemplate, HumanMessagePromptTemplate, ChatPromptTemplate
2024-05-29 11:12:59 +08:00
from app.core.config import OPENAI_MODEL, OPENAI_API_KEY
# os.environ["http_proxy"] = "http://127.0.0.1:7890"
# os.environ["https_proxy"] = "http://127.0.0.1:7890"
llm = ChatOpenAI(model_name=OPENAI_MODEL,
openai_api_key=OPENAI_API_KEY,
temperature=0)
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.
2024-06-28 13:59:53 +08:00
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.
2024-05-29 11:12:59 +08:00
If there are grammatical errors, correct them and then output the sentence."""
)
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
# 待翻译文本由 Human 角色输入
human_template = "User input : {text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(input_variables=["text"], template=human_template)
# 使用 System 和 Human 角色的提示模板构造 ChatPromptTemplate
chat_prompt_template = ChatPromptTemplate.from_messages(
[system_message_prompt, human_message_prompt]
)
translate_chain = LLMChain(llm=llm, prompt=chat_prompt_template)
2024-06-28 13:59:53 +08:00
result = translate_chain.invoke(text)
logging.info("translate result : " + result.get('text'))
# print("translate result : " + result.get('text'))
return result.get('text')
# 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
2024-05-29 11:12:59 +08:00
def main():
"""Main function"""
2024-06-28 13:59:53 +08:00
text = translate_to_en("fire")
print(text)
2024-05-29 11:12:59 +08:00
if __name__ == "__main__":
main()