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)
相关推荐
AEIC学术交流中心9 分钟前
【会议征稿通知 | 广州计算机学会主办 | IEEE出版 | EI 、Scopus稳定检索】
人工智能·信息安全·隐私保护·学术会议
m0_3776182312 分钟前
C# 异步范围Asynchronous Disposal方法 C# await using如何使用
jvm·数据库·python
qq_2837200522 分钟前
Python 模块精讲:platform 获取系统信息,从入门到实战全攻略
python·platform
强盛小灵通专卖员24 分钟前
基于深度学习 的急性阑尾炎CT 影像诊断
人工智能·深度学习·医学影像·ei会议
vb攻城狮32 分钟前
前端害怕被蒸馏 快速入门Python 【demo_03】
python
qq_1898070339 分钟前
SQL多表嵌套查询数据重复怎么办_使用DISTINCT去重优化策略
jvm·数据库·python
m0_747854521 小时前
mysql如何设置数据库连接字符编码_修改default-character
jvm·数据库·python
邦爷的AI架构笔记1 小时前
踩坑3天后,我把公司的AI接口全换成了多模型路由——GPT-6和Claude Opus 4.7同时上线的这周
人工智能·后端
威迪斯特1 小时前
项目解决方案:某连锁餐饮集团AI后厨与运营安全建设解决方案
人工智能·安全·项目解决方案·ai实时分析·智能餐饮管理·ai视频识别·智能视频分析硬件
Wyz201210241 小时前
如何在 React 中正确将父组件函数传递给子组件并触发调用
jvm·数据库·python