使用 Ollama 时遇到的问题

题意:

ImportError: cannot import name 'Ollama' from 'llama_index.llms' (unknown location) - installing dependencies does not solve the problem

Python 无法从 llama_index.llms 模块中导入名为 Ollama 的类或函数

问题背景:

I want to learn LLMs. I run Ollama with the following Docker Compose file - it's running:

我想学习大型语言模型(LLMs)。我使用以下Docker Compose文件运行Ollama,并且它正在运行:

python 复制代码
services:
  ollama:
    image: ollama/ollama:latest
    ports:
      - 11434:11434
    volumes:
      - ollama_data:/root/.ollama
    healthcheck:
      test: ollama list || exit 1
      interval: 10s
      timeout: 30s
      retries: 5
      start_period: 10s
  ollama-models-pull:
    image: curlimages/curl:8.6.0
    command: >-
      http://ollama:11434/api/pull -d '{"name": "mistral"}'
    depends_on:
      ollama:
        condition: service_healthy
volumes:
  ollama_data:

I would like to write a Python app, which will use ollama, and I found this piece of code:

我想编写一个使用Ollama的Python应用程序,我找到了以下代码片段:

python 复制代码
from llama_index.llms import Ollama, ChatMessage

llm = Ollama(model="mistral", base_url="http://127.0.0.1:11434")

messages = [
    ChatMessage(
        role="system", content="you are a multi lingual assistant used for translation and your job is to translate nothing more than that."
    ),
    ChatMessage(
        role="user", content="please translate message in triple tick to french ``` What is standard deviation?```"
    )
]
resp = llm.chat(messages=messages)
print(resp)

I installed all dependencies: 我安装了所有依赖

python 复制代码
python3 -m venv venv
source venv/bin/activate
pip install llama-index  
pip install llama-index-llms-ollama
pip install ollama-python

However, when I run the app, I got: 当我运行app时,得到以下信息:

python 复制代码
Traceback (most recent call last):
  File "/home/user/test.py", line 1, in <module>
    from llama_index.llms import Ollama, ChatMessage
ImportError: cannot import name 'Ollama' from 'llama_index.llms' (unknown location)

where can be the problem? 问题可能在哪里?

问题解决:

The correct way to import Ollama should be: 引入 ollama 的正确方式:

python 复制代码
from llama_index.llms.ollama import Ollama

For ChatMessage it should be: 引入 ChatMessage 的方式:

python 复制代码
from llama_index.core.llms import ChatMessage
相关推荐
用户8356290780512 小时前
Python 操作 PDF 附件:添加、查看与管理指南
后端·python
Darling噜啦啦6 小时前
Tool Use 底层原理:当"缸中大脑"遇上物理世界——LLM 工具调用的认知哲学与技术实现
llm·agent
探索云原生8 小时前
K8s 1.36 这个 GA 特性,把 initContainer 拉模型的 hack 干掉了
ai·云原生·kubernetes
Zy宇8 小时前
从养 OpenClaw 到养社区 AI:一套 Multi-Agent 社区的设计思路
人工智能·ai
宇宙之一粟9 小时前
乐企版式文件生成平台
java·后端·python
不好听6139 小时前
拆解 LLM Tool Use 的完整机制:从缸中大脑到 Agent 觉醒
架构·llm·agent
拾年27510 小时前
我用 30 行代码,搞懂了大模型是怎么"读"中文的
javascript·人工智能·llm
doiito12 小时前
【Agent Harness】Gliding Horse 记忆系统深度剖析:像 CPU 一样思考的 AI 记忆架构
ai·rust·架构设计·系统设计·ai agent
Hyyy1 天前
什么是结构化 Prompt 设计
llm·ai编程
冬奇Lab1 天前
Skill 系列(04):Skill 指标体系——L1/L2/L3 三层监控,让质量下降有据可查
人工智能·开源·llm