赞
踩
大纲:
三个重点:
1.深入理解 Chat Model 和 Chat Prompt Template
2.基于 LangChain 优化 OpenAI-Translator 架构设计
3.OpenAI-Translator v2.0 功能特性研发
知识点:
其他:
https://osschat.io/chat?project=LangChain 用魔法打败魔法,将遗忘的知识点快速学习下。
https://codebeautify.org/python-formatter-beautifier 将打印内容美化
第3和4点对比实现翻译,一个是使用Chat Model 一个是使用LLMChain
from langchain.chat_models import ChatOpenAI chat_model = ChatOpenAI(model_name="gpt-3.5-turbo") from langchain.schema import ( AIMessage, HumanMessage, SystemMessage ) messages = [ SystemMessage(content='You are a helpful assistant.'), HumanMessage(content='Who won the world serirs in 2020?'), AIMessage(content='The Las Angeles Dodgers won the world Series in 2020.'), HumanMessage(content='Where was it played?') ] print(messages) result = chat_model(messages) print(type(result)) print(result.content)
from langchain.schema import AIMessage, HumanMessage, SystemMessage from langchain.prompts.chat import ( ChatPromptTemplate, SystemMessagePromptTemplate, AIMessagePromptTemplate, HumanMessagePromptTemplate ) # 定义system 消息 template = ( """You are a translation expert, proficient in various language. \n Translation {language1} to {language2} """ ) # 将上面的多行提示词传入到 system prompt 中 system_message_prompt = SystemMessagePromptTemplate.from_template(template=template) print(system_message_prompt) # 翻译内容使用 human 来实现 human_template = '{text}' human_message_prompt = HumanMessagePromptTemplate.from_template(human_template) print(human_message_prompt) # 使用 system 和 human 构建翻译提示词 todo:加深对ChatPromptTemplate模块的理解 chat_prompt_template = ChatPromptTemplate.from_messages( [system_message_prompt, human_message_prompt] ) print(chat_prompt_template) # 将提示词中的 format 参数传入提示词 prompt # chat_prompt_template.format_prompt( # language1="English", # language2='Chinese', # text="I Love programming." # )
# 使用 to_messages() 方法将提示词生成可以发送的 messages
chat_prompt = chat_prompt_template.format_prompt(
language1="English",
language2='Chinese',
text="I Love programming."
).to_messages()
# 实现翻译- 使用 Chat Model 实现翻译
from langchain.chat_models import ChatOpenAI
translate_model = ChatOpenAI(model_name='gpt-3.5-turbo', temperature=0)
translate_result = translate_model(chat_prompt)
print(translate_result.content)
# 使用system 和 human 构建翻译提示词 todo:加深对ChatPromptTemplate模块的理解 chat_prompt_template = ChatPromptTemplate.from_messages( [system_message_prompt, human_message_prompt] ) from langchain import LLMChain from langchain.chat_models import ChatOpenAI translate_model = ChatOpenAI(model_name='gpt-3.5-turbo', temperature=0) translation_chain = LLMChain(llm=translate_model, prompt=chat_prompt_template) chain_result = translation_chain.run( { "text": 'hello world!', "language1": "English", "language2": "Chinese", } ) print(chain_result) # chain_result = translation_chain.run( # { # "text": 'hello world!', # "language1": "English", # "language2": "French", # } # ) # print(chain_result)
将 v1.0 版本的model模块大部分内容让langchain实现:
和第1块的第3个比较类似
from langchain.chat_models import ChatOpenAI from langchain.chains import LLMChain from langchain.prompts.chat import ( ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate, AIMessagePromptTemplate ) from utils import LOG class Translation: def __init__(self, model_name:str = "gpt-3.5-turbo", verbose:bool = True): # 翻译任务的提示词始终有system 角色承担 template = ( """You are a translation expert, proficient in various languages. \n Translates {source_language} to {target_language}.""" ) system_message_prompt = SystemMessagePromptTemplate.from_template(template) # 待翻译的内容由 human 角色承担 human_template = ("{text}") human_message_prompt = HumanMessagePromptTemplate.from_template(human_template) # 使用human_prompt 和 system_prompt构建ChatPromptTemplate (messages?) chat_prompt_template = ChatPromptTemplate.from_messages( [system_message_prompt, human_message_prompt] ) # 为了翻译结果的稳定,将temperature 设置为 chat = ChatOpenAI(model_name=model_name, temperature=0, verbose=verbose) self.chain = LLMChain(llm = chat, promppt=chat_prompt_template, verbose=verbose) def run(self, text:str, source_language:str, target_language:str) -> (str, bool): result = "" try: result = self.chain.run({ "text":text, "source_language":source_language, "target_language":target_language }) except Exception as e: LOG.error(f"An error occurred during translation: {e}") return result, False return result, True
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。