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)
相关推荐
施天助1 分钟前
开发ai模型最佳的系统是Ubuntu还是linux?
人工智能·ubuntu
秀儿还能再秀32 分钟前
淘宝母婴购物数据可视化分析(基于脱敏公开数据集)
python·数据分析·学习笔记·数据可视化
邵奈一37 分钟前
运行OpenManus项目(使用Conda)
人工智能·大模型·agent·agi
计算机老学长1 小时前
基于Python的商品销量的数据分析及推荐系统
开发语言·python·数据分析
是理不是里_1 小时前
深度学习与普通神经网络有何区别?
人工智能·深度学习·神经网络
曲幽1 小时前
DeepSeek大语言模型下几个常用术语
人工智能·ai·语言模型·自然语言处理·ollama·deepseek
千益1 小时前
玩转python:系统设计模式在Python项目中的应用
python·设计模式
&白帝&1 小时前
Java @PathVariable获取路径参数
java·开发语言·python
AORO_BEIDOU2 小时前
科普|卫星电话有哪些应用场景?
网络·人工智能·安全·智能手机·信息与通信
dreamczf2 小时前
基于Linux系统的边缘智能终端(RK3568+EtherCAT+PCIe+4G+5G)
linux·人工智能·物联网·5g