提示词(prompt)

zero-shot:零样本学习,无提示

few-shot:少样本学习,少量示例

检索增强(RAG)

通过构建向量数据库等为大模型提供新的知识库,大模型通过检索这些知识库学习新的知识。

我们可以通过文本嵌入大模型将文本转换成向量,模型会把一段文本拆分成多个维度(抽象语义特征)的数字列表。

进行向量匹配时,主要使用余弦相似度算法来确定两段文本的相似度。

余弦相似度:计算两个向量的夹角。 对于两个 n 维向量 $A = [a_1,a_2,…,a_n]$、$B = [b_1,b_2,…,b_n]$,余弦相似度计算公式为:$$ \cos(\theta) = \frac{\sum_{i=1}^{n} a_i b_i}{\sqrt{\sum_{i=1}^{n} a_i^2} \cdot \sqrt{\sum_{i=1}^{n} b_i^2}} $$

LangChain

三种模型:LLM、聊天模型、

文本转向量:

1
2
3
4
5
6
from langchain_community.embeddings import DashScopeEmbeddings

model = DashScopeEmbeddings()

print(model.embed_query("你是谁"))
print(model.embed_query(["111","222"]))

提示词模板:zero-shot、few-shot

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from langchain_core.prompts import FewShotPromptTemplate, PromptTemplate
from langchain_community.chat_models import ChatTongyi
e_template = PromptTemplate.from_template("喜欢:{like},不喜欢:{dislike}")

e_data = [
{"like": "帅", "dislike": "丑"},
{"like": "甜", "dislike": "苦"}
]

few_shot_template = FewShotPromptTemplate(
examples=e_data,
example_prompt=e_template,
prefix="请根据以下示例,判断用户不喜欢的物品。只输出答案,不要解释。",
suffix="用户喜欢的物品是:{input}",
input_variables=["input"],
)
prompt_text = few_shot_template.format(input="雨天")
model = ChatTongyi(model = "qwen-plus")

print(model.invoke(prompt_text).content)

invoke、format两种构建提示词模板的区别

1
2
3
4
5
6
7
8
9
from langchain_core.prompts import FewShotPromptTemplate, PromptTemplate
from langchain_community.chat_models import ChatTongyi

template = PromptTemplate.from_template("张明的fristname是{firstname},lastname是{lastname}")


print(template.invoke({"firstname": "张", "lastname": "明"}).to_string())

print(template.format(firstname="张", lastname="明"))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_community.chat_models import ChatTongyi

prompt_template = ChatPromptTemplate.from_messages(
[
("system", "你是一个诗人"),
MessagesPlaceholder("history_data"),
("human", "请写一首唐诗,只输出诗句"),
]
)
history_data = [
("human", "写一首唐诗"),
("ai", "窗前明月光,疑是地上霜,举头望明月,低头思故乡"),
("human", "再来一首"),
("ai", "锄禾日当午,汗滴禾下土,谁之盘中餐,粒粒皆辛苦"),
]

prompt_text = prompt_template.invoke({"history_data": history_data}).to_string()


model = ChatTongyi(model="qwen-plus")

print(model.invoke(prompt_text).content)

字符串输出解析器

1
2
3
# 用于将模型输出结果AIMessage转换为字符串
parser = StrOutputParser()
prompt | model | parser | model | parser