当前位置: 首页 > news >正文

mac测试ollama llamaindex

LlamaIndexs 将大语言模型和外部数据连接在一起的工具。大模型prompt有一个长度限制,当外部知识的内容超过这个长度,无法同时将有效信息传递给大模型,因此就诞生了 LlamaIndex。

具体操作就是通过多轮对话的方式不断提纯外部数据,达到在有限的输入长度限制下,传达更多的信息给大模型。

本文在mac平台验证ollama llamaindex,假设ollama已安装,mac安装ollama安装参考

在mac m1基于ollama运行deepseek r1_m1 mac deepseek-r1-CSDN博客

1 llama-index运行环境搭建

环境向量搭建

conda create -n llama-index python=3.12

conda activte llama-index

pip install llama-index

# chromadb依赖

pip install llama-index-llms-ollama

pip install llama-index-embeddings-ollama

pip install llama_index-vector_stores-chroma

# 开源向量存储

pip install chromadb

ollama embedding模型下载

由于mac本地计算能力有限,所以使用qwen3:1.7b小模型。

ollama pull yxl/m3e

ollama pull qwen3:1.7b

ollama list

2 向量本地化 & 自定义查询

以pdf文件"长安的荔枝- 马伯庸.pdf"为例(可以替换为其他PDF书籍),通过llama_index读取为documents,为减少计算量,取前10个子document。

docs/长安的荔枝- 马伯庸.pdf

documents向量本地目录为./chromadb_v0

rm -rf ./chromadb_v0

mkdir -p ./chromadb_v0

向量集合名称为"llama_index_test"

本地向量化代码如下

import chromadb
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, get_response_synthesizer, Settings
from llama_index.llms.ollama import Ollama
from llama_index.embeddings.ollama import OllamaEmbedding
from llama_index.core.node_parser import SentenceSplitter
from llama_index.vector_stores.chroma import ChromaVectorStore
from llama_index.core import StorageContextSettings.embed_model = OllamaEmbedding(model_name="yxl/m3e:latest")
Settings.llm = Ollama(model="qwen3:1.7b", request_timeout=360)documents = SimpleDirectoryReader("docs").load_data()
documents = documents[:10]
print(f"documents: {len(documents)}")db = chromadb.PersistentClient(path="./chromadb_v0")
chroma_collection = db.get_or_create_collection("llama_index_test")
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
storage_context = StorageContext.from_defaults(vector_store=vector_store)index = VectorStoreIndex.from_documents(documents, storage_context=storage_context, transformations=[SentenceSplitter(chunk_size=256)]
)print(index)

然后是自定义查询,prompt=“李善德”


import chromadb
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, get_response_synthesizer, Settings
from llama_index.vector_stores.chroma import ChromaVectorStore
from llama_index.core import StorageContext
from llama_index.llms.ollama import Ollama
from llama_index.embeddings.ollama import OllamaEmbedding
from llama_index.core.retrievers import VectorIndexRetriever
from llama_index.core.query_engine import RetrieverQueryEngineSettings.embed_model = OllamaEmbedding(model_name="yxl/m3e:latest")  
Settings.llm = Ollama(model="qwen3:1.7b", request_timeout=720)  db = chromadb.PersistentClient(path="./chromadb_v1")
chroma_collection = db.get_or_create_collection("llama_index_test")
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
storage_context = StorageContext.from_defaults(vector_store=vector_store)index = VectorStoreIndex.from_vector_store(vector_store, storage_context=storage_context
)retriever = VectorIndexRetriever(index=index,similarity_top_k=5,  # 返回最相似的前 n 个文档片段
)response_synthesizer = get_response_synthesizer()query_engine = RetrieverQueryEngine(retriever=retriever,response_synthesizer=response_synthesizer,    
)response = query_engine.query("李善德")
print(response)  # 输出查询结果

llama_index的回复如下。

<think>
</think>

李善德是《长安的荔枝》一书中的一位重要角色,他因在官场中表现出的忠诚与谨慎,而逐渐被世人所知。他曾在多个衙署任职,包括司农寺和上林署,负责处理各种政务事务。在一次重要的政务活动中,他被圣人指派为荔枝使,负责运输珍贵的荔枝,这一职位对他来说具有极大的意义。他的经历展现了他在官场中的沉稳与担当,也体现了他在复杂的政治环境中所展现出的智慧与忠诚。

可见,llamaindex,借助外部知识库chromadb,和向量检索,找到知识库中最相关内容,然后通过大模型将这些内容提纯为最终答案。

reference

---

ollama - qwen3:1.7b

https://www.ollama.com/library/qwen3:1.7b

Ollama LLM llamaindex

https://docs.llamaindex.ai/en/stable/examples/llm/ollama/

RAG+Agent 实战 llama-index+ollama 本地环境构建rag、agent

https://blog.csdn.net/yierbubu1212/article/details/142718139

http://www.dtcms.com/a/294033.html

相关文章:

  • Log4j CVE-2021-44228 漏洞复现详细教程
  • fastdds:默认qos入门
  • LeetCode|Day23|326. 3 的幂|Python刷题笔记
  • 牛客刷题 -- 二叉树遍历
  • 百度大涨,AIGC视频生成模型蒸汽机将会给百度带来什么?
  • 【Windows命令手册】Windows中的常用命令,并与 Linux 做比较
  • BiLLM:突破大语言模型后训练量化的极限
  • Linux Debian操作系统、Deepin深度操作系统手动分区方案参考
  • 利用红黑树封装实现map,set
  • Keil MDK5 介绍与安装教程
  • Python Day22 - 复习日
  • 【bug】Jetson Orin NX apt更换国内源
  • #Linux权限管理:从“Permission denied“到系统安全大师
  • 如何使用 minio 完成OceanBase社区版的归档和备份
  • [Bug | Cursor] import error: No module named ‘data‘
  • SpringCloud sentinel服务熔断 服务降级
  • 一个没有手动加分号引发的bug
  • python---元组(Tuple)
  • C#简介(从入门到精通)
  • 判断矩形能否放入多边形内——cad c# 二次开发实现
  • 【服务器】 MCTP Over PCIe 的内容、用途、工作原理及硬件设计注意事项
  • 数据结构(2)顺序表算法题
  • C#使用socket报错 System.Net.Sockets.SocketException:“在其上下文中,该请求的地址无效。
  • .net平台的跨平台桌面应用开发的技术方案总结对比
  • 【黑马SpringCloud微服务开发与实战】(六)分布式事务
  • Matlab学习笔记:逻辑基础
  • PyTorch武侠演义 第一卷:初入江湖 第6章:驿站的秘密信鸽
  • Apache JMeter 使用记录踩坑
  • 前端模块化:CommonJS 与 ES Module
  • 性能测试-jmeter实战5