当前位置: 首页 > article >正文

AIGC学习笔记(8)——AI大模型开发工程师

文章目录

  • AI大模型开发工程师
    • 007 LangChain之Model IO模块
      • 1 Model IO核心概念
      • 2 Model IO代码实战
        • 什么是LCEL?
        • Model
          • Model的分类
          • LLMs
          • ChatModel
        • Prompt
          • Prompt templates
          • Example selectors
          • Output parsers

AI大模型开发工程师

007 LangChain之Model IO模块

1 Model IO核心概念

image.png

  • Prompts:提示词
  • Language models:大语言模型
  • Output parsers:输出解析

2 Model IO代码实战

## 导入依赖库
!pip install langchain
什么是LCEL?
  • LangChain于8月1日0.254版本更新,声称采用新的语法来创建带有组合功能的Chain,同时提供一个新的接口,支持批处理、异步和流处理,将这种语法称为LangChain Expression Language(LCEL)
Model
Model的分类
  • LLMs:LangChain 的核心组件。LangChain并不提供自己的LLMs,而是为与许多不同的LLMs(OpenAI、Cohere、Hugging Face等)进行交互提供了一个标准 接口。(类似于Completion)
  • Chat Models:语言模型的一种变体。虽然聊天模型在内部使用了语言模型,但它们提供的接口略有不同。与其暴露一个“输入文本,输出文本”的API不同, 它们提供了一个以“聊天消息”作为输入和输出的接口。(类似于Chat Completion)
LLMs
  • 文档地址:https://python.langchain.com/docs/how_to/#llms
from langchain.llms import OpenAIllm = OpenAI(model_name="gpt-3.5-turbo")llm.invoke("什么是机器学习?")
llm("什么是大模型")

注意:在新版API中,这种方式已过时

ChatModel
  • 文档地址:https://python.langchain.com/docs/how_to/#chat-models
from langchain.chat_models import ChatOpenAIchat_model = ChatOpenAI(model="gpt-3.5-turbo")from langchain.schema import (AIMessage,HumanMessage,SystemMessage
)messages = [SystemMessage(content="你是一个智能助手"),
HumanMessage(content="第二十一届世界杯在哪儿举行的?"),
AIMessage(content="在俄罗斯"), 
HumanMessage(content="冠军是哪个球队")]

image.png

Prompt

一个语言模型的提示是用户提供的一组指令或输入,用于引导模型的响应,帮助它理解上下文并生成相关和连贯的基于语言的输出,例如回答问题、完成句子或进行对话。

  • 提示模板(Prompt Templates):参数化的模型输入
  • 示例选择器(Example Selectors):动态选择要包含在提示中的示例
Prompt templates

use PromptTemplate

## 可以动态传入参数
from langchain_core.prompts import PromptTemplateprompt_template = PromptTemplate.from_template("Tell me a {adjective} joke about {content}."
)prompt_template.format(adjective="funny", content="chickens")## 也可以不传参数
from langchain_core.prompts import PromptTemplateprompt_template = PromptTemplate.from_template("Tell me a joke")
prompt_template.format()chat_model.invoke(prompt_template.format()).content

use ChatPromptTemplate

from langchain_core.prompts import ChatPromptTemplatechat_template = ChatPromptTemplate.from_messages([("system", "You are a helpful AI bot. Your name is {name}."),("human", "Hello, how are you doing?"),("ai", "I'm doing well, thanks!"),("human", "{user_input}"),]
)messages = chat_template.format_messages(name="Bob", user_input="What is your name?")chat_model.invoke(messages)

image.png

Few-shot prompt templates

from langchain.prompts.few_shot import FewShotPromptTemplate
from langchain.prompts.prompt import PromptTemplateexamples  = [
{
"question": "穆罕默德·阿里(Muhammad Ali)和艾伦·图灵(Alan Turing)中哪个活得更长?",
"answer":
"""
这里需要后续问题吗:是。
后续问题:穆罕默德·阿里去世时多少岁?
中间答案:穆罕默德·阿里去世时74岁。
后续问题:艾伦·图灵去世时多少岁?
中间答案:艾伦·图灵去世时41岁。
因此最终答案是:穆罕默德·阿里
"""
},
{
"question": "craigslist的创始人是何时出生的?",
"answer":
"""
这里需要后续问题吗:是。
后续问题:craigslist的创始人是谁?
中间答案:craigslist的创始人是Craig Newmark。
后续问题:Craig Newmark是什么时候出生的?
中间答案:Craig Newmark于1952年12月6日出生。
因此最终答案是:1952年12月6日
"""
},
{
"question": "乔治·华盛顿(George Washington)的母亲的母亲父亲是谁?",
"answer":
"""
这里需要后续问题吗:是。
后续问题:乔治·华盛顿的母亲是谁?
中间答案:乔治·华盛顿的母亲是玛丽·鲍尔·华盛顿(Mary Ball Washington)。
后续问题:玛丽·鲍尔·华盛顿的父亲是谁?
中间答案:玛丽·鲍尔·华盛顿的父亲是约瑟夫·鲍尔(Joseph Ball)。
因此最终答案是:约瑟夫·鲍尔
"""
},
{
"question": "《大白鲨》和《皇家赌场》的导演都来自同一个国家吗?",
"answer":
"""
这里需要后续问题吗:是。
后续问题:《大白鲨》的导演是谁?
中间答案:《大白鲨》的导演是史蒂文·斯皮尔伯格(Steven Spielberg)。
后续问题:史蒂文·斯皮尔伯格来自哪里?
中间答案:美国。
后续问题:《皇家赌场》的导演是谁?
中间答案:《皇家赌场》的导演是马丁·坎贝尔(Martin Campbell)。
后续问题:马丁·坎贝尔来自哪里?
中间答案:新西兰。
因此最终答案是:不是
"""
}
]

image.png

Example selectors

长度选择器

from langchain_core.example_selectors import LengthBasedExampleSelector
from langchain_core.prompts import FewShotPromptTemplate, PromptTemplate# Examples of a pretend task of creating antonyms.
examples = [{"input": "happy", "output": "sad"},{"input": "tall", "output": "short"},{"input": "energetic", "output": "lethargic"},{"input": "sunny", "output": "gloomy"},{"input": "windy", "output": "calm"},
]example_prompt = PromptTemplate(input_variables=["input", "output"],template="Input: {input}\nOutput: {output}",
)
example_selector = LengthBasedExampleSelector(# The examples it has available to choose from.examples=examples,# The PromptTemplate being used to format the examples.example_prompt=example_prompt,# The maximum length that the formatted examples should be.# Length is measured by the get_text_length function below.max_length=25,# The function used to get the length of a string, which is used# to determine which examples to include. It is commented out because# it is provided as a default value if none is specified.# get_text_length: Callable[[str], int] = lambda x: len(re.split("\n| ", x))
)
dynamic_prompt = FewShotPromptTemplate(# We provide an ExampleSelector instead of examples.example_selector=example_selector,example_prompt=example_prompt,prefix="Give the antonym of every input",suffix="Input: {adjective}\nOutput:",input_variables=["adjective"],
)

image.png

  • 官方文档:https://python.langchain.com/docs/how_to/#example-selectors
Output parsers
  • 语言模型输出文本。但是很多时候,你可能希望获得比纯文本更结构化的信息。这就是输出解析器的用处。
  • 输出解析器是帮助结构化语言模型响应的类。一个输出解析器必须实现两个主要方法:
    • 获取格式指令:返回一个包含语言模型输出应如何格式化的字符串的方法。
    • 解析:接受一个字符串(假设是语言模型的响应)并将其解析为某种结构的方法。
  • 然后还有一个可选的方法:
    • 带提示解析:接受一个字符串(假设是语言模型的响应)和一个提示(假设是生成此响应的提示),并将其解析为某种结构。提示主要是在 OutputParser 希望以某种方式重试或修复输出时提供的,它需要来自提示的信息来执行这些操作。

List parser:返回以逗号分隔的列表

from langchain.output_parsers import CommaSeparatedListOutputParser
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAIprompt = PromptTemplate(template="List five {subject}.\n{format_instructions}",input_variables=["subject"],partial_variables={"format_instructions": format_instructions}
)model = OpenAI(model_name="gpt-3.5-turbo")parser = CommaSeparatedListOutputParser()
format_instructions = parser.get_format_instructions()## LCEL
chain = prompt | model | parser
chain.invoke({"subject": "ice cream flavors"})

image.png

  • 官方文档:https://python.langchain.com/docs/how_to/#output-parsers
http://www.lryc.cn/news/2395679.html

相关文章:

  • [蓝桥杯]剪格子
  • 明远智睿SSD2351开发板:语音机器人领域的变革力量
  • Mybtais框架各配置文件主要内容详解(一)
  • Co-IP—验证蛋白互作的不二之选
  • 数据可视化(第4、5、6次课)
  • DAY 18 推断聚类后簇的类型
  • 结合源码分析Redis的内存回收和内存淘汰机制,LRU和LFU是如何进行计算的?
  • ESG体系
  • 基于 KubeKey 3.1.9,快速部署 K8s 1.33.0 高可用集群
  • 华为深度学习面试手撕题:手写nn.Conv2d()函数
  • 归一化相关
  • STM32Cubemx-H7-17-麦克纳姆轮驱动
  • 机器学习算法-逻辑回归
  • Office 2024免费下载 安装包
  • Linux云计算训练营笔记day18(Python)
  • Git深入解析功能逻辑与核心业务场景流程
  • Opencv4 c++ 自用笔记 03 滑动条、相机与视频操作
  • LINUX528 重定向
  • 研华工控机安装Windows10系统,适用UEFI(GPT)格式安装
  • 1、树莓派更换软件下载源
  • 历年中山大学计算机保研上机真题
  • Python----目标检测(《SSD: Single Shot MultiBox Detector》论文和SSD的原理与网络结构)
  • springboot集成websocket给前端推送消息
  • DrissionPage SessionPage模式:轻量级HTTP请求的利器
  • 0527漏洞原理:XSS笔记
  • 智能制造之精读——RPA制造行业常见场景【附全文阅读】
  • spark shuffle的分区支持动态调整,而hive不支持
  • 网络安全十大漏洞
  • 关于uv 工具的使用总结(uv,conda,pip什么关系)
  • 深入剖析 Docker 容器化原理与实战应用,开启技术新征程!