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

株洲网站建设报价方案网站建设账户搭建

株洲网站建设报价方案,网站建设账户搭建,网站建设产业pest分析,微商手机网站制作公司之前的文章提到通义千问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/493744.html

相关文章:

  • 成都科技网站建设找哪家西安效果图制作工作室
  • 永城网站建设赣州网站建设渠道
  • 网站积分规则设计网站域名注册空间
  • 网站维护工作内容有什么上饶建网站公司
  • 简单的企业网站源码网站建设常用结构类型
  • 在线网站建设费用是多少做网站江西
  • 微信网站开发简单关键词提取工具
  • 浙江省长兴县建设局网站青岛网站建设哪里好
  • 教人做甜点的网站企业做网站的公司有哪些
  • 网站改域名二手车网站模版售价
  • 河南艾特 网站建设wordpress 5.2 5.3
  • 介绍学校网站怎么做上海知名网站
  • 网站维护与建设ppt岳阳seo快速排名
  • 深圳网站建设运营公司不用php做网站
  • 具有品牌的上海网站建设遵义网约车资格证哪里申请
  • 东莞高端模板建站为什么php做不了大网站
  • 2核4g 1m做网站陕西交通建设集团网站体检
  • 揭阳做网站哪个好网站建设新闻
  • 怎么做网站源码做淘宝客网站服务器
  • 一站式企业服务手游app平台排行榜
  • 广州外贸营销型网站建设公司河南网站推广优化报价
  • 网站建设虚线的代码台州市网站建设
  • 如何用js做网站深圳app开发公司排行
  • 网站建设课程心得wordpress 登陆 443
  • 外链推广网站建网站哪便宜
  • 做网站没有固定电话自己可以设计装修的软件
  • 高端网站设计服务商用网站做宣传的方案
  • 女生做网站前台广告制作公司利润怎么样
  • 企业网站建设的收获拼多多网页设计尺寸
  • dede旅游网站源码 多城市二手东西网站怎么做