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

璧山集团网站建设婚纱外贸soho建哪种网站好

璧山集团网站建设,婚纱外贸soho建哪种网站好,网站建设模板黄页,腾讯网站的品牌建设计划LangChain 的核心构建模块 LLMChain LangChain 应用程序的核心构建模块语言模型 - LLMs提示模板 - Prompt templates输出解析器 - Output Parsers LLMChain 组合 LangChain 应用程序的核心构建模块 LangChain 应用程序的核心构建模块 LLMChain 由三部分组成: 语言…

LangChain 的核心构建模块 LLMChain

  • LangChain 应用程序的核心构建模块
    • 语言模型 - LLMs
    • 提示模板 - Prompt templates
    • 输出解析器 - Output Parsers
  • LLMChain 组合

在这里插入图片描述

LangChain 应用程序的核心构建模块

LangChain 应用程序的核心构建模块 LLMChain 由三部分组成:

  • 语言模型 - LLMs: 语言模型是这里的核心推理引擎。为了使用 LangChain,您需要了解不同类型的语言模型以及如何使用它们。
  • 提示模板 - Prompt templates: 它为语言模型提供指令。它控制着语言模型的输出,因此了解如何构建提示和不同的提示策略至关重要。
  • 输出解析器 - Output Parsers: 它们将 LLM 的原始响应翻译成更易于使用的格式,从而方便下游使用输出。

本部分我将单独介绍这三个组件,然后介绍将所有组件结合在一起的 LLMChain:


语言模型 - LLMs

在 LangChain 中,存在两种语言模型:

  • LLMs: 将字符串作为输入并返回字符串的语言模型;
    LLMs 的输入/输出是简单易懂的字符串。
  • ChatModels: 聊天模型,将信息列表作为输入并返回信息的语言模型;
    ChatModels 的输入是一个 ChatMessage 列表,输出是一个 ChatMessage。ChatMessage 有两个必备组件:
    • content(内容): 这是信息的内容。
    • role(角色): 这是来自该 ChatMessage 的实体的角色。

LangChain 为这两种语言模型提供了一个标准接口,该标准接口有两个方法:

  • predict: 接收一个字符串,返回一个字符串;明显是 LLMs 的方法。
  • predict_messages: 接收信息列表,返回信息;明显是 ChatModels 的方法。

LangChain 提供了多个对象,可以轻松区分不同的角色:

  • HumanMessage(人类信息): 来自人类/用户的 ChatMessage。
  • AIMessage(人工智能助手信息): 来自人工智能/助手的聊天信息。
  • SystemMessage(系统信息): 系统消息来自系统的聊天信息。
  • FunctionMessage(功能消息): 来自函数调用的聊天信息。

初始化 llm 与 chat_model

from langchain.llms import OpenAI
from langchain.chat_models import ChatOpenAIllm = OpenAI(openai_api_key="xxx")
chat_model = ChatOpenAI(openai_api_key="xxx")
# 如果需要 API Key 可在博文下方留言

使用 predict 方法运行字符串输入:

text = "What would be a good company name for a company that makes colorful socks?"print(llm.predict(text))
print(chat_model.predict(text))

使用 predict_message 方法运行信息列表输入:

from langchain.schema import HumanMessagetext = "What would be a good company name for a company that makes colorful socks?"
messages = [HumanMessage(content=text)]print(llm.predict_messages(messages))
print(chat_model.predict_messages(messages))

提示模板 - Prompt templates

  • 提示模板是什么?

    在大语言模型中,开发人员通常不会直接将用户输入传递给语言模型,而是将用户输入添加到一个较大的文本段中,该文本段称为 “提示模板”(Prompt Template)。

  • 提示模板的目的?

    这样做的目的是为了为特定任务提供更多的上下文和指导,从而引导语言模型生成更有针对性的输出。

    这种方法有助于引导语言模型的生成,使其更加专注于特定任务,同时也可以控制生成的文本的风格和内容。通过提供上下文信息,提示模板可以在不同应用场景中引导语言模型的生成,以适应不同的用户需求。

  • 字符串提示模板案例:

    from langchain.prompts import PromptTemplateprompt = PromptTemplate.from_template("What is a good name for a company that makes {product}?")
    prompt.format(product="colorful socks")
    
  • 信息列表提示模板案例:

    from langchain.prompts.chat import (ChatPromptTemplate,SystemMessagePromptTemplate,HumanMessagePromptTemplate,
    )template = "You are a helpful assistant that translates {input_language} to {output_language}."
    system_message_prompt = SystemMessagePromptTemplate.from_template(template)
    human_template = "{text}"
    human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])chat_prompt.format_messages(input_language="English", output_language="French", text="I love programming.")
    

输出解析器 - Output Parsers

  • 输出解析器的作用?

    输出解析器可将 LLM 的原始输出转换成下游可使用的格式。

  • 输出解析器的类型?

    • 将 LLM 中的文本转换为结构化信息(如 JSON);
    • 将聊天信息转换为字符串;
    • 将调用返回的除信息外的额外信息(如 OpenAI 函数调用)转换为字符串。
    • 等;
  • 案例:

    下案例为编写自己的输出解析器 – 将逗号分隔的列表转换为列表:

    from langchain.schema import BaseOutputParserclass CommaSeparatedListOutputParser(BaseOutputParser):"""Parse the output of an LLM call to a comma-separated list."""def parse(self, text: str):"""Parse the output of an LLM call."""return text.strip().split(", ")CommaSeparatedListOutputParser().parse("hi, bye")
    # >> ['hi', 'bye']
    

LLMChain 组合

现在,我们将所有这些组合成一个链。
该链将接收输入变量,将其传递给提示模板以创建提示,将提示传递给 LLM,然后将输出传递给输出解析器。
这是一种捆绑模块化逻辑的便捷方法。请看测试案例:

from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (ChatPromptTemplate,SystemMessagePromptTemplate,HumanMessagePromptTemplate,
)
from langchain.chains import LLMChain
from langchain.schema import BaseOutputParser# 输出解析器部分
class CommaSeparatedListOutputParser(BaseOutputParser):"""Parse the output of an LLM call to a comma-separated list."""def parse(self, text: str):"""Parse the output of an LLM call."""return text.strip().split(", ")# 信息列表提示模板案例
template = """You are a helpful assistant who generates comma separated lists.
A user will pass in a category, and you should generate 5 objects in that category in a comma separated list.
ONLY return a comma separated list, and nothing more."""
system_message_prompt = SystemMessagePromptTemplate.from_template(template)
human_template = "{text}"
human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])
chain = LLMChain(llm=ChatOpenAI(),prompt=chat_prompt,output_parser=CommaSeparatedListOutputParser()
)
chain.run("colors")
# >> ['red', 'blue', 'green', 'yellow', 'orange']

上一篇博文:【LangChain】P0 LangChain 是什么与准备工作
下一篇博文:

http://www.15wanjia.com/news/166254.html

相关文章:

  • 做网站网站加载内容慢怎么解决编程课哪个培训机构好
  • 学 网站开发长春网站快照优化公司
  • 建设短视频网站河北建设厅录入业绩的网站
  • 哈尔滨教育云平台网站建设手机网站如何做优化
  • 阿里云网站空间购买网站容易做吗
  • 企业网站建设解决方案百度云域名注册
  • 响应式网站建设推广WordPress添加2233娘
  • 网站建设制作包括哪些方面水源logo设计制作网
  • 餐饮类网站模板logo网站设计素材
  • 网站建设人员的岗位职责网站设网页设计
  • 网站 功能需求成全高清免费观看mv
  • 网站开发人员培训福田附近做网站公司
  • 免费视频素材下载的网站东莞做网站首选企业铭
  • 浮梁网站建设广州招聘网网站开发
  • .net网站开发的例子个人相册网站建设报告
  • 大连电力工程招标网seo好seo
  • 湖北智能建站系统价格抖音关键词推广
  • 招聘网站排名wordpress出错500
  • 二级域名绑定网站wordpress项目下载文件
  • 网站的互动功能做旅游网站的关注与回复
  • 建站公司还行吗网站建设 业务培训
  • 做彩票网站收费标准英文网站建设大概多少钱
  • 保定网站开发公司自己做网站视频教学
  • 网站后台生成文章很慢怎么查看网站的空间商
  • 单县网站建设网页设计论文提纲
  • 做网站的图片大全深圳华强北手机报价
  • 贵州软件开发 网站开发做软件的叫什么职业
  • js效果炫酷的网站推荐编辑网站内容有没有批量办法
  • 建设医院网站的目的app制作单位
  • 自己做网站内容读取太慢公司网站建设招标文件范本