AI生成式人工智能
生成式人工智能(Generative AI)是人工智能的一个分支,专注于通过学习现有数据中的模式,生成全新的、原创的内容(如文本、图像、音频、视频、代码等)。与传统的分析型AI(用于分类、预测或决策)不同,生成式AI的核心能力是“创造”。

LangChain
什么是Langchain
https://python.langchain.com.cn/docs/modules/agents/
LangChain 是一个用于开发由语言模型驱动的应用程序的框架。它使得应用程序能够:
具有上下文感知能力:将语言模型连接到上下文来源(提示指令,少量的示例,需要回应的内容等)
具有推理能力:依赖语言模型进行推理(根据提供的上下文如何回答,采取什么行动等)
Langchain核心
这个框架由几个部分组成。
LangChain 库:Python 和 JavaScript 库。包含了各种组件的接口和集成,一个基本的运行时,用于将这些组件组合成链和代理,以及现成的链和代理的实现。
LangChain 模板:一系列易于部署的参考架构,用于各种任务。
LangServe:一个用于将 LangChain 链部署为 REST API 的库。
LangSmith:一个开发者平台,让你可以调试、测试、评估和监控基于任何 LLM 框架构建的链,并且与 LangChain 无缝集成

环境设定
1、安装vscode、python,并设置好python的虚拟环境
2、新建.env文件保存环境变量
3、新建requirements.txt(langchain、langchain-openai、python-dotenv、openai)
4、执行pip install -r requirements.txt,安装相关库与依赖
langchain连接LLM大语言模型
import os
from dotenv import load_dotenv,find_dotenv
load_dotenv(find_dotenv(),override=True)
from langchain.chat_models import ChatOpenAI
chat = ChatOpenAI(
api_key=os.environ.get(‘API_KEY’),
base_url=os.environ.get(‘API_ENDPOINT’),
model_name=“aiplat/qwen2.5-vl-72b”)
text = “what is 1+1”
output = chat.invoke(text)
print(output.content)

langchain三大Package
LangChain 库本身由几个不同的包组成。
langchain-core:基础抽象和 LangChain 表达式语言。
langchain-community:第三方集成。
langchain:构成应用程序认知架构的链、代理和检索策略。

Prompt Template提示词模板

from langchain import PromptTemplate
template = “”"/根据以下上下文回答问题。如果无法根据提供的信息回答,请回答:我不知道。
上下文:LLM(Large Language Model,大语言模型)是一种基于深度学习的自然语言处理(NLP)模型,通过海量文本数据训练而成,能够理解、生成和推理人类语言。代表性模型:如GPT-4、PaLM、LLaMA等。
问题: {query}
回答:
“”"prompt_template = PromptTemplate(
input_variables = [“query”],
template=template
)
question = “LLM是什么?”
my_prompt = prompt_template.format(query=question)
print(chat.invoke(my_prompt).content)
Few-shot prompt templates少样本示例的提示模板
from langchain import FewShotPromptTemplate
from langchain import PromptTemplate
examples = [ { “query”: “你好吗?”,
“answer”: “我的感觉不错,你呢?” },
{ “query”: “现在几点?”,
“answer”: “等一下,我要解锁我的iPhone?” }]
example_templete = “”" User:{query} AI:{answer}“”"
example_prompt = PromptTemplate(
input_variables=[“query”,“answer”],
template=example_templete
)
#添加titile,说明部分
prefix = “”“如下是AI助手的对话。这位助手比较幽默,对用户的提问会给出非常有创意和有趣的回应。以下是一些例子:”“”
#结尾部分
suffix = “”“用户: {userQuery}AI:”“”
few_shot_prompt_template = FewShotPromptTemplate(
examples=examples, example_prompt=example_prompt, prefix=prefix, suffix=suffix,
input_variables=[“userQuery”],
#输出的分隔
example_separator=“\n”)
answer = “如何让生活更幸福”
format_template = few_shot_prompt_template.format(userQuery=answer)
print(format_template)
print(chat.invoke(format_template).content)

Chain链条基本单元与工具链条
链允许我们将多个组件组合在一起创建一个单一的、连贯的应用。例如,我们可以创建一个链,该链接收用户输入,使用PromptTemplate对其进行格式化,然后将格式化后的响应传递给LLM。我们可以通过将多个链组合在一起或将链与其他组件组合来构建更复杂的链。

from langchain.chains import LLMMathChain
math_chain = LLMMathChain( llm=chat,
verbose=True)
#打印temple看格式是啥样
#print(math_chain.prompt.template)
print(math_chain.invoke(“18的0.6352次方是多少?”))

通用链条Sequential顺序链
通用的转换链:
将多个空格转换成一个空格

template = “”“转换如下文字:{tr_output}新形式{cvr_style}转换:”“”
prompt = PromptTemplate(
# input_variables=[“tr_output”,“cvr_style”],
input_variables=[“cvr_style”],
template=template)
convert_chain = LLMChain( llm=chat, prompt=prompt,
output_key=“new_output”, verbose=True)
output = convert_chain.invoke({“tr_output”:“你好”,“cvr_style”:“变英文”})
print(output[“new_output”])
#链接两个chain
from langchain.chains import SequentialChain
sequential_chain = SequentialChain(
chains=[clean_spaces_chain,convert_chain],
input_variables=[“tr_text”,“cvr_style”],
output_variables=[“new_output”],
verbose=True)
input_text = “”“你好, 科技”“”
output = sequential_chain.invoke({“tr_text”: input_text,“cvr_style”: “变英文”})
print(output[“new_output”])
如何为LLM添加记忆
from langchain.chains import ConversationChain
#加载对话的内容
#print(conversation_chain.prompt.template)
from langchain.memory import ConversationBufferMemory
conversation_chain = ConversationChain(
llm=chat,
memory=ConversationBufferMemory()
)
response = conversation_chain.invoke({“input”:“你知道什么是百度吗?”})
print(response)
print(conversation_chain.memory.buffer)
response = conversation_chain.invoke({“input”:“你知道什么是抖音吗?”})
print(response)
print(conversation_chain.memory.buffer)

把所有的返回信息存放到buffer,下次再提问则把buffer中所有的信息给llm,这样prompt会比较大,很费token

将原先问答的信息总结一下再给LLM
from langchain.memory import ConversationSummaryMemory
conversation_summary_memory = ConversationChain(
llm=chat,
memory=ConversationSummaryMemory(llm=chat)
)
#每次把之前的内容都做总结
#print(conversation_summary_memory.memory.prompt.template)
conversation_summary_memory.invoke({“input”:“你知道什么是抖音吗?”})
print(conversation_summary_memory.memory.buffer)
conversation_summary_memory.invoke({“input”:“你知道什么是百度吗?”})
print(conversation_summary_memory.memory.buffer)

指定记忆之前几次聊天信息。k=1则只记录上次的信息
from langchain.memory import ConversationBufferWindowMemory
conversation_window = ConversationChain(
llm=chat,
memory=ConversationBufferWindowMemory(k=1)
)
conversation_window.invoke({“input”:“你知道百度吗?”})
conversation_window.invoke({“input”:“你知道抖音吗?”})
conversation_window.invoke({“input”:“你知道顺丰科技吗?”})

如何制作AI Agent智能体 & AI AGENT团队
如何使用LCEL
LangChain表达式语言,或者LCEL,是一种声明式的方式,可以轻松地将链条组合在一起。

创建AI智能体
Agents代理的核心思想是使用LLM来选择要采取的一系列动作。 在链式结构中,一系列动作是硬编码的(在代码中)。 在代理中,使用语言模型作为推理引擎来确定要采取的动作及其顺序。


行动代理
代理使用 LLM 确定采取哪些行动以及顺序。 行动可以是使用工具并观察其输出,或向用户返回响应。常用的是零-shot ReAct

3863




被折叠的 条评论
为什么被折叠?



