检查其他资源
- 为这个问题添加了一个非常描述性的标题。
- 使用集成搜索在LangChain文档中进行了搜索。
- 使用GitHub搜索查找类似的问题,但没有找到。
- 我确信这是LangChain中的一个bug,而不是我的代码。
- 通过更新到LangChain的最新稳定版本(或特定集成包)无法解决此bug。
示例代码
以下是使用PydanticOutputParser时,由于Langchain无法解析LLM输出而出现的代码:
HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
repo_id = "mistralai/Mistral-7B-Instruct-v0.3"
model_kwargs = {
"max_new_tokens": 60,
"max_length": 200,
"temperature": 0.1,
"timeout": 6000
}
# Using HuggingFaceHub
llm = HuggingFaceHub(
repo_id=repo_id,
huggingfacehub_api_token = HUGGINGFACEHUB_API_TOKEN,
model_kwargs = model_kwargs,
)
# Define your desired data structure.
class Suggestions(BaseModel):
words: List[str] = Field(description="list of substitute words based on context")
# Throw error in case of receiving a numbered-list from API
@field_validator('words')
def not_start_with_number(cls, field):
for item in field:
if item[0].isnumeric():
raise ValueError("The word can not start with numbers!")
return field
parser = PydanticOutputParser(pydantic_object=Suggestions)
prompt_template = """
Offer a list of suggestions to substitute the specified target_word based on the context.
{format_instructions}
target_word={target_word}
context={context}
"""
prompt_input_variables = ["target_word", "context"]
partial_variables = {"format_instructions":parser.get_format_instructions()}
prompt = PromptTemplate(
template=prompt_template,
input_variables=prompt_input_variables,
partial_variables=partial_variables
)
model_input = prompt.format_prompt(
target_word="behaviour",
context="The behaviour of the students in the classroom was disruptive and made it difficult for the teacher to conduct the lesson."
)
output = llm(model_input.to_string())
parser.parse(output)
尝试使用OutputFixingParser修复错误时,又遇到了另一个错误,以下是代码库:
outputfixing_parser = OutputFixingParser.from_llm(parser=parser,llm=llm)
print(outputfixing_parser)
outputfixing_parser.parse(output)
错误信息和堆栈跟踪(如果适用)
---------------------------------------------------------------------------
ValidationError Traceback (most recent call last)
File [~\Desktop\llmai\llm_deep\Lib\site-packages\langchain_core\output_parsers\pydantic.py:33](~/Desktop/llmai/llm_deep/Lib/site-packages/langchain_core/output_parsers/pydantic.py:33), in PydanticOutputParser._parse_obj(self, obj)
[32](~/Desktop/llmai/llm_deep/Lib/site-packages/langchain_core/output_parsers/pydantic.py:32) if issubclass(self.pydantic_object, pydantic.BaseModel):
---> [33](~/Desktop/llmai/llm_deep/Lib/site-packages/langchain_core/output_parsers/pydantic.py:33) return self.pydantic_object.model_validate(obj)
[34](~/Desktop/llmai/llm_deep/Lib/site-packages/langchain_core/output_parsers/pydantic.py:34) elif issubclass(self.pydantic_object, pydantic.v1.BaseModel):
File [~\Desktop\llmai\llm_deep\Lib\site-packages\pydantic\main.py:551](~/Desktop/llmai/llm_deep/Lib/site-packages/pydantic/main.py:551), in BaseModel.model_validate(cls, obj, strict, from_attributes, context)
[550](~/Desktop/llmai/llm_deep/Lib/site-packages/pydantic/main.py:550) __tracebackhide__ = True
--> [551](~/Desktop/llmai/llm_deep/Lib/site-packages/pydantic/main.py:551) return cls.__pydantic_validator__.validate_python(
[552](~/Desktop/llmai/llm_deep/Lib/site-packages/pydantic/main.py:552) obj, strict=strict, from_attributes=from_attributes, context=context
[553](~/Desktop/llmai/llm_deep/Lib/site-packages/pydantic/main.py:553) )
ValidationError: 1 validation error for Suggestions
words
Field required [type=missing, input_value={'properties': {'words': ..., 'required': ['words']}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.7/v/missing
During handling of the above exception, another exception occurred:
OutputParserException Traceback (most recent call last)
Cell In[284], [line 1](vscode-notebook-cell:?execution_count=284&line=1)
----> [1](vscode-notebook-cell:?execution_count=284&line=1) parser.parse(output)
File [~\Desktop\llmai\llm_deep\Lib\site-packages\langchain_core\output_parsers\pydantic.py:64](~/Desktop/llmai/llm_deep/Lib/site-packages/langchain_core/output_parsers/pydantic.py:64), in PydanticOutputParser.parse(self, text)
...
OutputParserException: Failed to parse Suggestions from completion {"properties": {"words": {"description": "list of substitute words based on context", "items": {"type": "string"}, "title": "Words", "type": "array"}}, "required": ["words"]}. Got: 1 validation error for Suggestions
words
Field required [type=missing, input_value={'properties': {'words': ..., 'required': ['words']}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.7/v/missing
使用OutputFixingParser时的错误
---------------------------------------------------------------------------
ValidationError Traceback (most recent call last)
File [~\Desktop\llmai\llm_deep\Lib\site-packages\langchain_core\output_parsers\pydantic.py:33](~\Desktop/llmai/llm_deep/Lib/site-packages/langchain_core/output_parsers/pydantic.py:33), in PydanticOutputParser._parse_obj(self, obj)
[32](~/Desktop/llmai/llm_deep/Lib/site-packages/langchain_core/output_parsers/pydantic.py:32) if issubclass(self.pydantic_object, pydantic.BaseModel):
---> [33](~/Desktop/llmai/llm_deep/Lib/site-packages/langchain_core/output_parsers/pydantic.py:33) return self.pydantic_object.model_validate(obj)
[34](~/Desktop/llmai/llm_deep/Lib/site-packages/langchain_core/output_parsers/pydantic.py:34) elif issubclass(self.pydantic_object, pydantic.v1.BaseModel):
File [~\Desktop\llmai\llm_deep\Lib\site-packages\pydantic\main.py:551](~/Desktop/llmai/llm_deep/Lib/site-packages/pydantic/main.py:551), in BaseModel.model_validate(cls, obj, strict, from_attributes, context)
[550](~/Desktop/llmai/llm_deep/Lib/site-packages/pydantic/main.py:550) __tracebackhide__ = True
--> [551](~/Desktop/llmai/llm_deep/Lib/site-packages/pydantic/main.py:551) return cls.__pydantic_validator__.validate_python(
[552](~/Desktop/llmai/llm_deep/Lib/site-packages/pydantic/main.py:552) obj, strict=strict, from_attributes=from_attributes, context=context
[553](~/Desktop/llmai/llm_deep/Lib/site-packages/pydantic/main.py:553) )
ValidationError: 1 validation error for Suggestions
Input should be a valid dictionary or instance of Suggestions [type=model_type, input_value=None, input_type=NoneType]
For further information visit https://errors.pydantic.dev/2.7/v/model_type
During handling of the above exception, another exception occurred:
OutputParserException Traceback (most recent call last)
Cell In[265], [line 1](vscode-notebook-cell:?execution_count=265&line=1)
----> [1](vscode-notebook-cell:?execution_count=265&line=1) outputfixing_parser.parse(output)
File [~\Desktop\llmai\llm_deep\Lib\site-packages\langchain\output_parsers\fix.py:62](~/Desktop/llmai/llm_deep/Lib/site-packages/langchain/output_parsers/fix.py:62), in OutputFixingParser.parse(self, completion)
[60](~/Desktop/llmai/llm_deep/Lib/site-packages/langchain/output_parsers/fix.py:60) except OutputParserException as e:
...
[44](~/Desktop/llmai/llm_deep/Lib/site-packages/langchain_core/output_parsers/pydantic.py:44) try:
OutputParserException: Failed to parse Suggestions from completion null. Got: 1 validation error for Suggestions
Input should be a valid dictionary or instance of Suggestions [type=model_type, input_value=None, input_type=NoneType]
For further information visit https://errors.pydantic.dev/2.7/v/model_type
描述
输出必须能够解析LLM输出并提取如下所示生成的json;
Suggestions(words=["conduct", "misconduct", "actions", "antics", "performance", "demeanor", "attitude", "behavior", "manner", "pupil actions"])
系统信息
系统信息
操作系统:Windows
操作系统版本:10.0.22631
Python版本:3.11.9 | 由conda-forge打包 | (主版本,2024年4月19日,18:27:10) [MSC v.1938 64位(AMD64)]
软件包信息
langchain_core: 0.2.4
langchain: 0.2.2
langchain_community: 0.2.4
langsmith: 0.1.73
langchain_google_community: 1.0.5
langchain_huggingface: 0.0.3
langchain_text_splitters: 0.2.1
未安装的软件包(不一定是个问题)
以下软件包未找到:
langgraph
langserve
2条答案
按热度按时间9lowa7mx1#
请尝试以下操作:
HuggingFaceEndpoint
,而不是已弃用的HuggingFaceHub
piwo6bdm2#
感谢您的帮助!我使用了您的方法,它确实有效。我注意到原始提示仍然在使用您的方法时有效。
我还注意到任务不需要指定,但仍然有效。我已经将导入模块从 from langchain_community.llms.huggingface_endpoint import HuggingFaceEndpoint 更新为 from langchain_huggingface import HuggingFaceEndpoint,基于以下警告:
我还有一些问题需要解决输出解析问题。
格式化错误的输出类型-1
这涉及到输出中的拼写错误。
错误信息
使用 OutputFixingParser 修复失败
错误信息
格式化错误的输出类型-2
这涉及到输出中缺少关键字。
错误信息
格式化错误的输出类型-2可以通过使用 RetryWithErrorOutputParser 来解决。以下是代码示例:
输出
但是,使用 RetryWithErrorOutputParser 解决格式化错误的输出类型-1时出现了以下错误;
错误代码
问题
在这种情况下,如何解决格式化错误的输出类型-1?