使用 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
相关推荐
leo·Thomas17 分钟前
OpenClaw多节点一键部署脚本(Ubuntu)
ai·openclaw
dulu~dulu32 分钟前
算法---寻找和为K的子数组
笔记·python·算法·leetcode
编程之升级打怪42 分钟前
用Python语言实现简单的Redis缓冲数据库驱动库
redis·python
电商API&Tina1 小时前
电商数据采集API接口||合规优先、稳定高效、数据精准
java·javascript·数据库·python·json
玲娜贝儿--努力学习买大鸡腿版2 小时前
hot 100 刷题记录(1)
数据结构·python·算法
GISer_Jing2 小时前
Agent开发学习进展总结
ai·前端框架·aigc
兮℡檬,2 小时前
答题卡识别判卷
开发语言·python·计算机视觉
阆遤2 小时前
利用TRAE对nanobot进行安全分析并优化
python·安全·ai·trae·nanobot
雕刻刀3 小时前
ERROR: Failed to build ‘natten‘ when getting requirements to build wheel
开发语言·python
何双新3 小时前
Odoo 技术演进全解析:从 Widget 到 Owl,从 Old API 到声明式 ORM
python