llama_index [Bug]: VectorIndexAutoRetriever运行失败,与gpt-4o有关,

8nuwlpux  于 2个月前  发布在  其他
关注(0)|答案(5)|浏览(38)

Bug描述

我发现在使用GPT-4O导出JSON格式的输出文本时,与VectorIndexAutoRetriever的正常运行存在一些差异。
在查看VectorIndexAutoRetriever源代码时,我发现它使用llm来预测query_bundle,以便为VectorIndexAutoRetriever输出必要的JSON结构。
如果你使用另一个OpenAI模型或另一个LLM模型,输出可以解析为JSON格式。但是当使用GPT-4O时,输出将包含"```json",这使得它无法解析为JSON格式。
我认为,这不仅会导致VectorIndexAutoRetriever出现错误,还会导致任何可以使用llm生成JSON结构的llama_index模型出现错误。

版本

0.10.42

重现步骤

from llama_index.core.retrievers import VectorIndexAutoRetriever
from llama_index.core.vector_stores.types import MetadataInfo, VectorStoreInfo

vector_store_info = VectorStoreInfo(
    content_info="brief biography of celebrities",
    metadata_info=[
        MetadataInfo(
            name="category",
            type="str",
            description=(
                "Category of the celebrity, one of [Sports, Entertainment,"
                " Business, Music]"
            ),
        ),
        MetadataInfo(
            name="country",
            type="str",
            description=(
                "Country of the celebrity, one of [United States, Barbados,"
                " Portugal]"
            ),
        ),
    ],
)
retriever = VectorIndexAutoRetriever(
    index, vector_store_info=vector_store_info
)

相关日志/回溯

validation errors for VectorStoreQuerySpec
query
  field required (type=value_error.missing)
filters
  field required (type=value_error.missing)
72qzrwbm

72qzrwbm1#

你好!我在这里帮助你解决任何错误、问题或贡献。

为了解决由于输出中存在 "```json" 分隔符而导致 VectorIndexAutoRetriever 在使用 gpt-4o 模型时失败的问题,你可以修改代码在解析 JSON 之前去除这些分隔符。以下是操作方法:

  1. 修改 generate_retrieval_spec 方法,在解析之前从输出字符串中去除 " json" and " " 分隔符。
  2. 使用 llama_index.legacy.output_parsers.utils 中的 parse_code_markdown 实用函数处理markdown代码块分隔符的去除。

以下是如何实现这些更改的示例:

from llama_index.legacy.output_parsers.utils import parse_code_markdown

def generate_retrieval_spec(
        self, query_bundle: QueryBundle, **kwargs: Any
    ) -> BaseModel:
    # prepare input
    info_str = self._vector_store_info.json(indent=4)
    schema_str = VectorStoreQuerySpec.schema_json(indent=4)
    
    # call LLM
    output = self._llm.predict(
        self._prompt,
        schema_str=schema_str,
        info_str=info_str,
        query_str=query_bundle.query_str,
    )
    
    # Strip the ```json and ``` delimiters
    output = parse_code_markdown(output, only_last=True)[0]

    # parse output
    return self._parse_generated_spec(output, query_bundle)

在这个示例中,parse_code_markdown 函数用于从输出字符串中去除markdown代码块分隔符。这确保了 JSON 可以被正确解析,避免了你遇到的验证错误。
这种方法应该解决了在使用 gpt-4o 模型时出现的 VectorIndexAutoRetriever 问题。

  • 要继续与 Dosu 交流,请提及 @dosu。*
wvmv3b1j

wvmv3b1j2#

@viethoang261 感谢您的投稿,以便改进输出解析。否则我可能会最终完成它。

at0kjp5o

at0kjp5o3#

抱歉,我之前提到的有误。

这不是关于“```json”分隔符的问题。

而是关于gpt-4o无法提取输出。

当我使用gpt3.5-turbo时,输出如下:

"```json
{"query": "dafasd", "filters": [], "top_k": null}
```"

但是当使用gpt-4o时,输出与schema_str值相同:

"```json
{
    "title": "VectorStoreQuerySpec",
    "description": "Schema for a structured request for vector store\n(i.e. to be converted to a VectorStoreQuery).\n\nCurrently only used by VectorIndexAutoRetriever.",
    "type": "object",
    "properties": {
        "query": {
            "title": "Query",
            "type": "string"
        },
        "filters": {
            "title": "Filters",
            "type": "array",
            "items": {
                "$ref": "#/definitions/MetadataFilter"
            }
        },
        "top_k": {
            "title": "Top K",
            "type": "integer"
        }
    },
    "required": [
        "query",
        "filters"
    ],
...
}
```"
brtdzjyr

brtdzjyr4#

啊,是的。我看到过很多关于gpt-4o的报道,说实话,它在结构化输出和函数调用方面真的很糟糕。

drkbr07n

drkbr07n5#

我遇到了相同的错误。有什么解决方法可以绕过这个问题吗?

相关问题