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

网站开发怎么切换多种语言快速排名优化推广手机

网站开发怎么切换多种语言,快速排名优化推广手机,专门做特卖的网站,韩国化妆品网站模板纯文本输出是有用的,但在某些情况下,我们需要 LLM 生成结构化输出,即以机器可读格式(如 JSON、XML 或 CSV)或甚至以编程语言(如 Python 或 JavaScript)生成的输出。当我们打算将该输出传递给其他…

纯文本输出是有用的,但在某些情况下,我们需要 LLM 生成结构化输出,即以机器可读格式(如 JSON、XML 或 CSV)或甚至以编程语言(如 Python 或 JavaScript)生成的输出。当我们打算将该输出传递给其他代码时,这非常有用,使 LLM 可以在更大的应用程序中发挥作用。

调试步骤

import getpass
import osif "GOOGLE_API_KEY" not in os.environ:os.environ["GOOGLE_API_KEY"] = getpass.getpass("Enter your Google AI API key: ")
import os
import requestsos.environ['HTTP_PROXY'] = 'http://127.0.0.1:7890'
os.environ['HTTPS_PROXY'] = 'http://127.0.0.1:7890'r = requests.get("https://www.google.com")
print(r.status_code)  # 能返回 200 就说明代理成功了
from langchain_google_genai import ChatGoogleGenerativeAIllm = ChatGoogleGenerativeAI(model="gemini-2.0-flash-001",  # 或其他可用模型
)print(llm.invoke("你好呀!你现在通了吗?").content)
你好!我一直在线,随时待命。所以,是的,我可以说是“通了”!有什么我可以帮助你的吗?

JSON Output:JSON输出

使用 LLM 生成的最常见格式是 JSON,然后可以将其用于,例如:

  • 将它发送到前端代码

  • 将其保存到数据库中

# openai API
from langchain_openai import ChatOpenAI
from langchain_core.pydantic_v1 import BaseModelclass AnswerWithJustification(BaseModel):'''An answer to the user question along with justification for the answer.'''answer: str'''The answer to the user's question'''justification: str'''Justification for the answer'''llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0)
structured_llm = llm.with_structured_output(AnswerWithJustification)
structured_llm.invoke("What weighs more, a pound of bricks or a pound of feathers")

输出为:

{answer: "They weigh the same", justification: "Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volu"... 42 more characters
}

我们使用gemini API来复现

但是langchain_core.pydantic_v1 是为兼容旧版本 pydantic v1 而设的临时模块,但现在 LangChain 已经全面升级到了 pydantic v2,建议不要再用这个兼容模块了。

from langchain_core.pydantic_v1 import BaseModel出现了红色的提示报错。所以我们改写为from pydantic import BaseModel, 这样就直接使用了最新版的 pydantic,不会再触发警告。

from langchain_google_genai import ChatGoogleGenerativeAI
from pydantic import BaseModelclass AnswerWithJustification(BaseModel):'''An answer to the user question along with justification for the answer.'''answer: str'''The answer to the user's question'''justification: str'''Justification for the answer'''llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash-001",  # 或其他可用模型temperature=0 # 让输出更确定、更稳定(不会随机发挥)
)structured_llm = llm.with_structured_output(AnswerWithJustification)structured_llm.invoke("What weighs more, a pound of bricks or a pound of feathers")
AnswerWithJustification(answer='They weigh the same.', justification='A pound is a unit of weight, so a pound of bricks and a pound of feathers weigh the same amount..')

总体目标:
让大语言模型(LLM)返回结构化的数据(JSON),并且符合你自定义的格式(schema)。

第一步:定义了一个“结构模板”(schema):

class AnswerWithJustification(BaseModel):answer: strjustification: str

这就是你希望模型返回的数据格式 —— 一个包含两个字段的 JSON:

{"answer": "...","justification": "..."
}

第二步:让 LLM “知道” 要用这个格式

structured_llm = llm.with_structured_output(AnswerWithJustification)

第三步:使用这个结构化模型去提问

structured_llm.invoke("What weighs more, a pound of bricks or a pound of feathers?")

这一步就是真正向模型提问。模型在回答前,会参考你定义的 schema,生成符合格式的 JSON 数据。

首先将 schema 转为 JSON Schema,把你的 Python 模板类转成 JSON 格式的规则。然后发给LLM,告诉模型“你输出要符合这个格式”。最后验证输出,模型生成后再校验是否合规,确保你收到的数据符合格式。

你就像是在说:

“AI,你回答我的时候,不能随便写一段文字,必须照着我这张表格来写,字段名和格式都要对上!”

Other Machine-Readable Formats with Output Parsers:其他带有输出解析器的机器可读格式

输出解析器是干嘛的?

输出解析器是帮助大语言模型(LLM)把结果以特定格式输出的一种工具。它有两个主要功能:

  1. 提供格式说明(Providing format instructions)

你可以用解析器给提示(prompt)加上一些额外的说明,比如告诉模型:

“请把结果输出成 XML 格式” 或
“请生成一个 JSON 对象,字段有 name 和 age”

这样模型就知道你想要的输出长什么样。

  1. 验证和解析输出(Validating and parsing output)

LLM 返回结果后,输出解析器还可以:

把普通文本转换成结构化格式(如列表、XML、JSON等);

校验格式是否正确;

修复模型输出中不完整或多余的内容。

这是一个输出解析器的工作示例

from langchain_core.output_parsers import CommaSeparatedListOutputParserparser = CommaSeparatedListOutputParser()items = parser.invoke("apple, banana, cherry")print(items)
['apple', 'banana', 'cherry']

LangChain 为各种用例提供了多种输出解析器,包括 CSV、XML 等。在下一节中,我们将了解如何将输出解析器与模型和提示组合使用。

http://www.dtcms.com/wzjs/72832.html

相关文章:

  • 自己做的网站怎么给别人访问成人职业技能培训班
  • 乐东黎族自治县住房建设局网站竞价排名点击
  • 山西省建设厅网站官网如何建立自己的博客网站
  • 手机网站设计框架怎样有效的做网上宣传
  • 网站建设的id调用怎么操作核心关键词和长尾关键词举例
  • 网站建设需要个体营业执照吗电商培训学校
  • 手机制作网站免费大型集团网站建设公司
  • photoshop做网站广州各区最新动态
  • 做移动端网站软件下载搜狗官网
  • 用java做的网站有哪些内容友情链接交换网
  • 做ui设计工资一般多少深圳关键词优化平台
  • 校园互动网站建设浏览器下载安装2022最新版
  • 域名备案和网站备案是一回事吗软件测试培训机构哪家好
  • wordpress admin-ajax.php 漏洞seo推广优化方案
  • 村政府可以做网站么seo长尾关键词
  • 网络公司制作网站怎么免费制作网站
  • 设计师国外网站郑州网络seo
  • 专业网站建设咨询网站ip查询
  • 怎样制作网站开发的进度表百度怎么发帖做推广
  • 威海建设委员会网站吉林seo外包
  • 亳州网站网站建设北京十大教育培训机构排名
  • 视频主持网站建设编写网站
  • wordpress采集免费版下载seoul是什么国家
  • 备案域名卖出后涉赌怎么办杭州seo排名优化外包
  • 网站建设工期外包项目接单平台
  • wordpress文章自定义常见问题模块seo什么意思简单来说
  • 产品网站建设建议湖南百度推广开户
  • 偃师市住房和城乡建设局网站安徽网站推广
  • 福建外贸网站建设网络推广营销网站建设专家
  • 汕头品牌建设公司南昌seo代理商