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)
相关推荐
kyle~2 分钟前
计算机视觉---RealSense深度相机技术
人工智能·数码相机·计算机视觉·机器人·嵌入式·ros·传感器
碣石潇湘无限路38 分钟前
【AI篇】当Transformer模型开始学习《孙子兵法》
人工智能·学习
看到我,请让我去学习1 小时前
OpenCV开发-初始概念
人工智能·opencv·计算机视觉
汀沿河1 小时前
8.1 prefix Tunning与Prompt Tunning模型微调方法
linux·运维·服务器·人工智能
陈敬雷-充电了么-CEO兼CTO1 小时前
大模型技术原理 - 基于Transformer的预训练语言模型
人工智能·深度学习·语言模型·自然语言处理·chatgpt·aigc·transformer
学术 学术 Fun1 小时前
✨ OpenAudio S1:影视级文本转语音与语音克隆Mac整合包
人工智能·语音识别
萧鼎1 小时前
深度探索 Py2neo:用 Python 玩转图数据库 Neo4j
数据库·python·neo4j
华子w9089258592 小时前
基于 Python Django 和 Spark 的电力能耗数据分析系统设计与实现7000字论文实现
python·spark·django
风铃喵游2 小时前
让大模型调用MCP服务变得超级简单
前端·人工智能
Rockson2 小时前
使用Ruby接入实时行情API教程
javascript·python