使用 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
相关推荐
合作小小程序员小小店8 分钟前
旧版本附近停车场推荐系统demo,基于python+flask+协同推荐(基于用户信息推荐),开发语言python,数据库mysql,
人工智能·python·flask·sklearn·推荐算法
大模型教程13 分钟前
开源RAG神器RAGFlow深度解析:融合Agent能力,零门槛搭建企业级AI知识库
程序员·llm·agent
动能小子ohhh18 分钟前
Langchain从零开始到应用落地案例[AI智能助手]【3】---使用Paddle-OCR识别优化可识别图片进行解析回答
人工智能·python·pycharm·langchain·ocr·paddle·1024程序员节
AI大模型31 分钟前
斩获72k Star!谷歌云AI大牛开源LLM应用案例库,拿来即用
程序员·llm·agent
AI大模型32 分钟前
GitHub斩获 19.9k 星!免费!从零开始系统学习大语言模型
程序员·llm·agent
互联网中的一颗神经元32 分钟前
小白python入门 - 9. Python 列表2 ——从基础操作到高级应用
java·开发语言·python
wei_shuo44 分钟前
Llama-2-7b 昇腾 NPU 测评总结:核心性能数据、场景适配建议与硬件选型参考
大模型·llama·昇腾
it&s me1 小时前
EulerOS(NPU)安装llamafactory
ai·1024程序员节·llamafactory
凯子坚持 c1 小时前
Llama-2-7b在昇腾NPU上的六大核心场景性能基准报告
java·开发语言·llama
落798.1 小时前
【在昇腾NPU上部署Llama-2-7B:从环境配置到性能测试的完整实战】
经验分享·llama·1024程序员节