關(guān)于使用ChatOpenAI調(diào)用阿里云千問(wèn)模型無(wú)法實(shí)現(xiàn)結(jié)構(gòu)化輸出的解決方案
阿里云提供的 “OpenAI 兼容接口”主要是為了支持標(biāo)準(zhǔn) OpenAI API 調(diào)用。
但是并不是完全兼容:尤其是當(dāng)我們使用下面的方式期待模型產(chǎn)生結(jié)構(gòu)化輸出時(shí):
from pydantic import BaseModel, Field
class Movie(BaseModel):
"""A movie with details."""
title: str = Field(..., description="The title of the movie")
year: int = Field(..., description="The year the movie was released")
director: str = Field(..., description="The director of the movie")
rating: float = Field(..., description="The movie's rating out of 10")
model_with_structure = model.with_structured_output(Movie)
response = model_with_structure.invoke("Provide details about the movie Inception")
print(response) # Movie(title="Inception", year=2010, director="Christopher Nolan", rating=8.8)會(huì)出現(xiàn)如下 400 錯(cuò)誤:
BadRequestError: Error code: 400 - {'error': {'code': 'invalid_parameter_error', 'param': None, 'message': "<400> InternalError.Algo.InvalidParameter: 'messages' must contain the word 'json' in some form, to use 'response_format' of type 'json_object'.", 'type': 'invalid_request_error'}, 'id': 'chatcmpl-9b2223da-21bb-4299-b46a-efaf01e38fb3', 'request_id': '9b2223da-21bb-4299-b46a-efaf01e38fb3'}這個(gè)問(wèn)題主要是阿里云限制了用戶(hù)期望模型產(chǎn)生結(jié)構(gòu)化輸出時(shí)要在提示詞中包含 JSON 字樣。
當(dāng)然我們可以安裝 0.2.14 版本的 langchain-openai 來(lái)規(guī)避這種問(wèn)題的產(chǎn)生,但是當(dāng) langchain 更新到1.0版本之后,0.2.14 版本的 langchain-openai 依賴(lài)的 langchain-core 就開(kāi)始與 1.0 版本的langchain不兼容了。
所以為了大家能夠盡快全面升級(jí)到 1.0 版本的 langchain,我試驗(yàn)出了如下方法來(lái)暫時(shí)解決結(jié)構(gòu)化輸出的問(wèn)題,本質(zhì)上還是依賴(lài)于提示詞工程。
from langchain_core.output_parsers import JsonOutputParser
from pydantic import BaseModel, Field
class Movie(BaseModel):
"""A movie with details."""
title: str = Field(..., description="The title of the movie")
year: int = Field(..., description="The year the movie was released")
director: str = Field(..., description="The director of the movie")
rating: float = Field(..., description="The movie's rating out of 10")
structured_output_parser = JsonOutputParser(pydantic_object=Movie)
structured_output_parser_chain = model | structured_output_parser
response = model.invoke(rewrite_question_string_parser.get_format_instructions() + "Provide details about the movie Inception")本文轉(zhuǎn)載自??PyTorch研習(xí)社??,作者:南七無(wú)名式
已于2025-11-6 07:30:23修改
贊
收藏
回復(fù)
分享
微博
QQ
微信
舉報(bào)
回復(fù)
相關(guān)推薦

















