调用Ollama接口遇404错误:localhost:11434/api/chat未找到
问题排查:Ollama API 404 Not Found错误
执行streamlit run app.py启动应用后,聊天环节触发错误:HTTPStatusError: Client error '404 Not Found' for url 'http://localhost:11434/api/chat'
相关代码片段:
# setup llm & embedding model llm=Ollama(model="llama2") embed_model = HuggingFaceEmbedding( model_name="BAAI/bge-large-en-v1.5", trust_remote_code=True) # Creating an index over loaded data Settings.embed_model = embed_model index = VectorStoreIndex.from_documents(docs, show_progress=True) # Create the query engine, where we use a cohere reranker on the fetched nodes Settings.llm = llm query_engine = index.as_query_engine(streaming=True) streaming_response = query_engine.query(prompt)
错误截图:
错误原因
- Ollama服务未启动或端口异常:默认Ollama使用11434端口,若服务未运行、端口被占用或修改,会导致请求无法找到接口。
- Ollama版本过低:旧版本Ollama可能没有
/api/chat流式聊天接口,该接口为较新版本新增功能。 - llama2模型未正确拉取:模型下载不完整、损坏或不存在时,Ollama无法响应聊天请求。
解决办法
- 检查并启动Ollama服务:
在终端执行ollama serve启动服务,用curl http://localhost:11434/api/tags测试是否能正常返回模型列表,确认服务在11434端口运行。 - 升级Ollama到最新版本:
下载最新版本安装包覆盖安装,重启服务后再测试。 - 重新拉取llama2模型:
执行ollama pull llama2确保模型完整下载,完成后用ollama list确认模型存在。 - 显式指定Ollama API地址(若端口/地址有修改):
修改LLM初始化代码,明确指定API base URL:llm = Ollama(model="llama2", base_url="http://localhost:11434")
内容的提问来源于stack exchange,提问作者Đinh Văn Linh
相关产品推荐
相关产品推荐

