如何返回llama_index的index.query(query, streaming=True)的结果?

问题描述 投票:0回答:2

我正在尝试返回 llama_index 的结果

index.query(query, streaming=True)
.

但不知道该怎么做。

这个显然不行。

index = GPTSimpleVectorIndex.load_from_disk(index_file)

return index.query(query, streaming=True)

错误信息:

TypeError: cannot pickle 'generator' object
.

这个也不是。

def stream_chat(query: str, index):
    for chunk in index.query(query, streaming=True):
        print(chunk)
        content = chunk["response"]
        if content is not None:
            yield content

# in another function
index = GPTSimpleVectorIndex.load_from_disk(index_file)
return StreamingResponse(stream_chat(query, index), media_type="text/html")

错误信息:

TypeError: 'StreamingResponse' object is not iterable
.

谢谢!

python stream artificial-intelligence openai-api llama-index
2个回答
2
投票

好吧,我明白了。

答案是

return StreamingResponse(index.query(query, streaming=True).response_gen)

0
投票

你能给我详细的答案吗

© www.soinside.com 2019 - 2024. All rights reserved.