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

株洲新站seowordpress导航栏设置

株洲新站seo,wordpress导航栏设置,东莞常平天气,在线看seo网站之前的文章提到通义千问API无法通过with_structured_output/json schema的方式支持结构化输出,如果就是想使用通义千问大模型做结构化输出,应该怎么办呢?有两种办法 使用Ollama来运行通义千问大模型 从Ollama博客文章 Structured output 中…

之前的文章提到通义千问API无法通过with_structured_output/json schema的方式支持结构化输出,如果就是想使用通义千问大模型做结构化输出,应该怎么办呢?有两种办法

使用Ollama来运行通义千问大模型

从Ollama博客文章 Structured output 中了解到,Ollama已经支持结构化输出了,这个功能是在Ollama 0.5.0版本 引入的。通过Ollama把qwen3大模型在本地运行起来,使用下面的代码就能看到效果。

from langchain.chat_models import init_chat_model  
from langchain_core.prompts import ChatPromptTemplate  
from pydantic import BaseModel, Field  llm = init_chat_model(  model_provider="openai",  model="qwen3:8b",  base_url="http://localhost:11434/v1",  api_key="123456"  
)  tagging_prompt = ChatPromptTemplate.from_template(  """    Extract the desired information from the following passage.  Only extract the properties mentioned in the 'Classification' function.        Passage:        {input}    """)  class Classification(BaseModel):  sentiment: str = Field(description="The sentiment of the text")  aggressiveness: int = Field(description="How aggressive the text is on a scale from 1 to 10")  language: str = Field(description="The language the text is written in")  structured_llm = llm.with_structured_output(Classification)  inp = "Estoy increiblemente contento de haberte conocido! Creo que seremos muy buenos amigos!"  
prompt = tagging_prompt.invoke({"input": inp})  
response = structured_llm.invoke(prompt)  
print(response)

程序能够正常运行并打印出Classification这个结构化的类对象内容(如下)

sentiment='positive' aggressiveness=0 language='Spanish'

如果把代码中llm对象构造时的参数换回通义千问API的值,也即是改为

llm = init_chat_model(  model_provider="openai",  model="qwen-plus-latest",  base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",  api_key=os.environ["QWEN_API_KEY"]
)

会发现之前提到的错误再次出现了,这证明不是通义千问大模型不支持结构化输出,而是通义千问API服务不支持。

如果不想使用Ollama本地运行大模型,而是想使用通义千问API服务支持结构化输出,该怎么办呢?

使用 PydanticOutputParser 自动生成提示词

要使用结构化输出,通义千问文档里提到

您可以在提示词中明确描述所需 JSON 的键值结构和数据类型,并提供标准数据样例,这会帮助大模型达到类似效果。

借助 LangChain 的 PydanticOutputParser 可以帮我们自动生成数据结构的提示词。参考下面的代码

import os  
from langchain_core.output_parsers import PydanticOutputParser  
from langchain_core.prompts import ChatPromptTemplate  
from pydantic import BaseModel, Field  
from langchain.chat_models import init_chat_model  llm = init_chat_model(  model_provider="openai",  model="qwen-plus-latest",  base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",  api_key=os.environ["QWEN_API_KEY"]  
)  class Classification(BaseModel):  sentiment: str = Field(description="The sentiment of the text")  aggressiveness: int = Field(description="How aggressive the text is on a scale from 1 to 10")  language: str = Field(description="The language the text is written in")  parser = PydanticOutputParser(pydantic_object=Classification)  tagging_prompt = ChatPromptTemplate.from_messages([  ("system",  "Extract the desired information into JSON format from the following passage."  "{format_instructions}"     ),  ("human", "{input}")  
]).partial(format_instructions=parser.get_format_instructions())  inp = "Estoy increiblemente contento de haberte conocido! Creo que seremos muy buenos amigos!"  
print(tagging_prompt.invoke({"input": inp}))  chain = tagging_prompt | llm | parser  
response = chain.invoke({"input": inp})  
print(response)

第30行代码会打印出格式化后的提示词,第一行后面的内容都是API根据提供的类结构生成的提示词。

    Extract the desired information into JSON format from the following passage.The output should be formatted as a JSON instance that conforms to the JSON schema below.As an example, for the schema {"properties": {"foo": {"title": "Foo", "description": "a list of strings", "type": "array", "items": {"type": "string"}}}, "required": ["foo"]}the object {"foo": ["bar", "baz"]} is a well-formatted instance of the schema. The object {"properties": {"foo": ["bar", "baz"]}} is not well-formatted.Here is the output schema:{"properties": {"sentiment": {"description": "The sentiment of the text","title": "Sentiment","type": "string"},"aggressiveness": {"description": "How aggressive the text is on a scale from 1 to 10","title": "Aggressiveness","type": "integer"},"language": {"description": "The language the text is written in","title": "Language","type": "string"}},"required": ["sentiment","aggressiveness","language"]}

真是太方便了,这下可以高效的从文档里提取结构化数据啦。

思考

从 Ollama 0.5.0的代码修改记录 来看,Ollama只用了很少的代码修改就支持了结构化输出。那么通义千问API服务什么时候能支持起来呢?

http://www.dtcms.com/a/412664.html

相关文章:

  • 关于做书的网站斌果主题wordpress
  • 坪地网站建设效果网页制作软件属于应用软件吗
  • 基于ILO和PwC研究报告(2025年):AI暴露度与就业增长关系
  • 所有网站都能进的浏览器不需要备案的服务器
  • 网站编程课程设计心得体会天津高端网站建设公司
  • 安徽省建设协会网站湘潭租房网站
  • 网站系统名称htm网站制作
  • daily notes[53]
  • 做资料网站违法系统管理平台
  • 专业 网站建设ic外贸网站建设
  • 【51单片机】【protues仿真】基于51单片机秒表系统(LCD1602多功能、可保持30条记录)
  • 国企网站建设合同文创产品设计网站推荐
  • 福田网站建设团队温州网站建设方案推广
  • Java 日志框架核心:门面 + 实现选型逻辑、Lombok 误区解析与日志用法
  • 室内设计和网站建设哪个前景好学校网站建设解决方案
  • VCS Verdi看波形的时候,有红色的正三角、倒三角,还有虚线,这是什么意思啊
  • 扬州网站建设制作炫酷wordpress主题
  • 做房产的一般用哪个网站好做公众号的公司是什么公司
  • 网站建设 三合一如何在微信上做小程序
  • 青岛网站建设 新视点10个暴利小生意创业
  • I/O详解
  • 如何将域名指向网站学习前端的网站
  • 企业应如何进行网站建设vi设计的基本要素
  • 鞍山制作网站做网站界面尺寸
  • 英文网站建设 潍坊数据库和网站建设的论文
  • 兰州市城乡建设局网站公布的信息网站开发环境windows7的优点
  • 承德网站网络营销方式选择考虑的因素
  • 萧山建设局网站用手机开发app
  • Wordpress标签与分类seo技术是什么意思
  • 秦皇岛学网站建设湖北工业信息化网站备案