llama_index

目录

安装

[llama_index 搜索引擎](#llama_index 搜索引擎)

[用 DeepSeek API 替换本地 Ollama 模型](#用 DeepSeek API 替换本地 Ollama 模型)

源代码:


安装

pip install llama_index

llama_index 搜索引擎

llama_index框架构建搜索引擎_llamaindex使用正则表达式拆分文档-CSDN博客

用 DeepSeek API 替换本地 Ollama 模型

https://zhuanlan.zhihu.com/p/842132629

源代码:

python 复制代码
# %%
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.vector_stores.chroma import ChromaVectorStore
from llama_index.core import StorageContext
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from IPython.display import Markdown, display

from llama_index.llms.openai import OpenAI
import chromadb

# %%

import openai
openai.api_key = "sk"

openai.api_base = "https://api.deepseek.com/v1"
llm = OpenAI(model='deepseek-chat',api_key=openai.api_key, base_url=openai.base_url)


from llama_index.core import Settings


# llm = OpenAI(api_key=openai.api_key, base_url=openai.base_url)
Settings.llm = OpenAI(model="deepseek-chat",api_key=openai.api_key, base_url=openai.base_url)
# %%
import os

jinaai_api_key = "jina"
os.environ["JINAAI_API_KEY"] = jinaai_api_key

from llama_index.embeddings.jinaai import JinaEmbedding

text_embed_model = JinaEmbedding(
    api_key=jinaai_api_key,
    model="jina-embeddings-v3",
    # choose `retrieval.passage` to get passage embeddings
    task="retrieval.passage",
)

# %%
# create client and a new collection
chroma_client = chromadb.EphemeralClient()
chroma_collection = chroma_client.create_collection("quickstart")

# %%


# define embedding function
embed_model = text_embed_model

# load documents
documents = SimpleDirectoryReader("./data/paul_graham/").load_data()

# save to disk

db = chromadb.PersistentClient(path="./chroma_db")
chroma_collection = db.get_or_create_collection("quickstart")
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, embed_model=embed_model
)

# load from disk
db2 = chromadb.PersistentClient(path="./chroma_db")
chroma_collection = db2.get_or_create_collection("quickstart")
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
index = VectorStoreIndex.from_vector_store(
    vector_store,
    embed_model=embed_model,
)

# Query Data from the persisted index
query_engine = index.as_query_engine()
response = query_engine.query("What did the author do growing up?")
print('response:',response)
相关推荐
仙人掌_lz6 分钟前
Hybrid OCR-LLM框架用于在大量复杂密集企业级文档信息提取
人工智能·ocr·文档解析
酷柚易汛智推官10 分钟前
AI驱动的智能运维知识平台建设:技术实践与未来展望
运维·人工智能·酷柚易汛
lzptouch14 分钟前
多模态生成 Flamingo
人工智能
minhuan16 分钟前
构建AI智能体:八十一、SVD模型压缩的艺术:如何科学选择K值实现最佳性能
人工智能·奇异值分解·svd模型压缩
CILMY2319 分钟前
【一问专栏】Python中is和==的区别详解
开发语言·python·is·==
小龙报28 分钟前
《赋能AI解锁Coze智能体搭建核心技能(2)--- 智能体开发基础》
人工智能·程序人生·面试·职场和发展·创业创新·学习方法·业界资讯
&永恒的星河&33 分钟前
超越传统:大型语言模型在文本分类中的突破与代价
人工智能·自然语言处理·大模型·文本分类·llms
Datawhale43 分钟前
3万字长文!通俗解析大语言模型LLM原理
人工智能·语言模型·自然语言处理
程序员爱钓鱼1 小时前
Python编程实战—面向对象与进阶语法 | 属性与方法
后端·python·ipython
SelectDB技术团队1 小时前
Apache Doris 4.0 版本正式发布:全面升级 AI 与搜索能力,强化离线计算
人工智能·apache