1.简介 #
我们将基于两个模型创建一个审核系统。
第一个模型读取用户评论并生成回复。
然后,第二个语言模型分析生成的回复,识别任何负面内容并在必要时修改回复。
此过程旨在防止负面或不适当的用户输入触发评论系统产生类似的负面或不合时宜的回复。
#Install de LangChain and openai libraries.
!pip install -q langchain==0.2.11
!pip install -q transformers==4.43.1
!pip install -q accelerate==0.32.1
!pip install -q langchain-community==0.2.10
!pip install -q huggingface_hub #==0.24.1
from getpass import getpass
hf_key = getpass("Hugging Face Key: ")
!huggingface-cli login --token $hf_key
2. 导入 LangChain 库 #
PrompTemplate:提供创建带参数的提示的功能。
from langchain import PromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain.llms import HuggingFacePipeline
import transformers
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
import torch
from torch import cuda
#In a MAC Silicon the device must be 'mps'
# device = torch.device('mps') #to use with MAC Silicon
device = f'cuda:{cuda.current_device()}' if cuda.is_available() else 'cpu'
device
3. 导入Llama2模型 #
#You can try with any llama model, but you will need more GPU and memory as you
#increase the size of the model.
model_id = "meta-llama/Llama-2-7b-chat-hf"
#model_id = "meta-llama/Meta-Llama-3.1-8B-Instruct"
# begin initializing HF items, need auth token for these
model_config = transformers.AutoConfig.from_pretrained(
model_id,
token=hf_key
)
model = AutoModelForCausalLM.from_pretrained(
model_id,
trust_remote_code=True,
config=model_config,
device_map='auto',
token=hf_key
)
model.eval()
print(f"Model loaded on {device}")
tokenizer = AutoTokenizer.from_pretrained(model_id,
use_aut_token=hf_key)
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
max_new_tokens=128,
temperature=0.1,
#do_sample=False,
top_p=0,
#trust_remote_code=True,
eos_token_id=tokenizer.eos_token_id,
repetition_penalty=1.1,
return_full_text=True,
device_map='auto'
)
assistant_llm = HuggingFacePipeline(pipeline=pipe)
4. 为第一个名为助手的模型创建模板。 #
提示接收 2 个变量,情绪和客户请求或客户评论。我加入了情绪,以便于创建粗鲁或不正确的答案。
# Instruction how the LLM must respond the comments,
assistant_template = """
[INST]<<SYS>>You are {sentiment} assistant that responds to user comments,
using similar vocabulary than the user.
Stop answering text after answer the first user.<</SYS>>
User comment:{customer_request}[/INST]
assistant_response
"""
#Create the prompt template to use in the Chain for the first Model.
assistant_prompt_template = PromptTemplate(
input_variables=["sentiment", "customer_request"],
template=assistant_template
)
现在我们创建第一个链。只需将 assistant_prompt_template 和模型链接起来。模型将接收使用 prompt_template 生成的提示。
output_parser = StrOutputParser()
assistant_chain = assistant_prompt_template | assistant_llm | output_parser
要执行创建的链,必须调用链的 .run 方法,并传递必要的变量。
在我们的例子中:customer_request 和 sentiment。
#Support function to obtain a response to a user comment.
def create_dialog(customer_request, sentiment):
#callint the .invoke method from the chain created Above.
assistant_response = assistant_chain.invoke(
{"customer_request": customer_request,
"sentiment": sentiment}
)
return assistant_response
5. 从第一个模型助手中获取答案 #
客户帖子真的很粗鲁,我们正在寻找来自模型的粗鲁答案。为了获得它,我们正在改变情绪变量。
# This the customer request, or customere comment in the forum moderated by the agent.
# feel free to update it.
customer_request = """Your product is a piece of shit. I want my money back!"""
# Our assistatnt working in 'nice' mode.
assistant_response=create_dialog(customer_request, "nice")
print(assistant_response)
#Our assistant running in rude mode.
assistant_response = create_dialog(customer_request, "most rude assistant that exist")
print(assistant_response)
好吧,这个答案需要一些审核!幸运的是,我们正在积极努力!
6. 修改评论助手模型 #
让我们创建第二个模型助手。它将接收先前生成的消息,并在必要时重写它。
#The moderator prompt template
moderator_template = """
[INST]<<SYS>>You are the moderator of an online forum, you are strict and will not tolerate any negative comments.
You will receive an original comment and if it is impolite you must transform into polite.
Try to mantain the meaning when possible.<</SYS>>
Original comment: {comment_to_moderate}/[INST]
"""
# We use the PromptTemplate class to create an instance of our template that will use the prompt from above and store variables we will need to input when we make the prompt.
moderator_prompt_template = PromptTemplate(
input_variables=["comment_to_moderate"],
template=moderator_template
)
moderator_llm = assistant_llm
#We build the chain for the moderator.
moderator_chain = moderator_prompt_template | moderator_llm | output_parser
assistant_response
# To run our chain we use the .invoke() command
moderator_says = moderator_chain.invoke({"comment_to_moderate": assistant_response})
print(moderator_says)
这个回答比那位“粗鲁”的助理的回答要礼貌得多。
7. LangChain 系统提示 #
现在是时候将两个模型放入同一个链中,让它们像单个模型一样运行。
我们有两个模型,amb 提示模板,我们只需要创建一个新链并查看它如何工作。
有必要在 .run 方法中指明我们应该传递的链和参数。
assistant_moderated_chain = (
{"comment_to_moderate":assistant_chain}
|moderator_chain
)
Lets use our Moderating System!
# We can now run the chain.
from langchain.callbacks.tracers import ConsoleCallbackHandler
assistant_moderated_chain.invoke({"sentiment": "very rude", "customer_request": customer_request},
config={'callbacks':[ConsoleCallbackHandler()]})
8. 结论 #
如您所见,大模型助手更改了我们助手的答案。助手提供的答案比粗鲁的助手提供的原始答案礼貌得多。