Bootstrap

解决llama_index中使用Ollama出现timed out 问题

现象:

  File "~/anaconda3/envs/leo_py38/lib/python3.8/site-packages/httpx/_transports/default.py", line 86, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.ReadTimeout: timed out

代码:

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.llms.ollama import Ollama

###定义使用的llm,embedding 模型
llm = Ollama(model="yi:34b")
embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-large-zh-v1.5")
Settings.llm = llm
Settings.embed_model = embed_model

## 加载文档
documents = SimpleDirectoryReader("../../data").load_data()
print("documents: ", len(documents))

## 构建index
index = VectorStoreIndex.from_documents(documents, show_progress=True)

## 构建query engine
query_engine = index.as_query_engine()
query = "身长九尺,髯长二尺的人是谁?"

## query
response = query_engine.query(query)
print(f"query:{query}")
print(f"查询结果:{response}")

解决办法是增加timeout时长

llm = Ollama(model="yi:34b", request_timeout=500)
;