创建Ollama Embedding时持续触发连接拒绝ValueError求助
解决Ollama Embeddings连接拒绝(Errno 61)的问题
错误详情
Error:ValueError: Error raised by inference endpoint: HTTPConnectionPool(host='localhost', port=11434): Max retries exceeded with url: /api/embeddings (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x29d6fc070>: Failed to establish a new connection: [Errno 61] Connection refused'))
问题代码
#Using OllamaEmbeddings from langchain_community.embeddings import OllamaEmbeddings embedding_function = OllamaEmbeddings() db = Chroma.from_documents(chunks, embedding_function, persist_directory='/Users/raghavjeendgar/Downloads/embeddings/OllamaEmbeddings') db.persist()
解决方案
确认Ollama服务已启动
在终端执行ollama list,若返回模型列表则服务正常;若提示命令不存在,先完成Ollama安装;若提示连接失败,手动启动服务:- macOS/Linux:终端运行
ollama serve - Windows:打开Ollama应用或在命令行执行
ollama serve
- macOS/Linux:终端运行
验证模型是否已拉取
默认OllamaEmbeddings使用llama2模型,若未拉取过,终端执行ollama pull llama2,确保模型本地存在。检查端口占用情况
默认端口11434,若被其他程序占用会导致连接失败:- macOS/Linux:执行
lsof -i :11434查看占用进程,关闭对应程序或更换端口 - Windows:执行
netstat -ano | findstr :11434,找到PID后在任务管理器关闭进程
- macOS/Linux:执行
代码显式配置端点和模型
可以在初始化时明确指定模型和服务地址,避免默认配置异常:embedding_function = OllamaEmbeddings(model="llama2", base_url="http://localhost:11434")排查防火墙权限
确保系统防火墙未阻止Python/终端访问localhost:11434:- macOS:系统设置→网络→防火墙,允许相关程序通过
- Windows:Windows Defender防火墙→入站规则,添加允许11434端口的规则
内容的提问来源于stack exchange,提问作者raghav
相关产品推荐
相关产品推荐

