日本熟妇hd丰满老熟妇,中文字幕一区二区三区在线不卡 ,亚洲成片在线观看,免费女同在线一区二区

基于LlamaIndex構建RAG應用

在Llamaindex中使用百煉提供的檢索增強服務

開始

前提條件

首先,登錄https://bailian.console.aliyun.com/,獲取你的API-KEY,當需要指定業務空間時也要獲取指定“業務空間id”。

然后,安裝DashScopeCloudIndex的安裝包(python>=3.8,<=3.12)

pip install llama-index-core
pip install llama-index-llms-dashscope
pip install llama-index-indices-managed-dashscope

文件解析

準備您的知識庫文件,可以將所有文件放在一個文件夾中也可以是多個獨立的文件。目前支持的文件格式是docx、doc、pdf。準備好之后,使用DashScopeParse進行解析。

import os

from llama_index.readers.dashscope.base import DashScopeParse
from llama_index.readers.dashscope.utils import ResultType

os.environ['DASHSCOPE_API_KEY'] = "<Your API Key>"
os.environ['DASHSCOPE_WORKSPACE_ID'] = "<Your Workspace id, Default workspace is empty.>"

# 多個獨立文件
file = [
    # 需要解析的文件,支持pdf,doc,docx
]
parse = DashScopeParse(result_type=ResultType.DASHSCOPE_DOCMIND)
documents = parse.load_data(file_path=file)

# 使用SimpleDirectoryReader讀取文件夾下所有文件
from llama_index.core import SimpleDirectoryReader
parse = DashScopeParse(result_type=ResultType.DASHSCOPE_DOCMIND)
file_extractor = {".pdf": parse, '.doc': parse, '.docx': parse}
documents = SimpleDirectoryReader(
    "your_folder", file_extractor=file_extractor
).load_data(num_workers=1)

上傳成功后,你可以在百煉管控臺的數據管理中看到上傳后的文檔

image

創建索引

使用得到的documents創建知識庫索引

from llama_index.indices.managed.dashscope import DashScopeCloudIndex

# create a new index
index = DashScopeCloudIndex.from_documents(
    documents,
    "my_first_index",
    verbose=True,
)

創建成功后,您可以在數據應用-知識索引中查看已創建成功的索引

image

讀取索引

您可以使用知識庫名稱在Llamaindex中初始化您創建好的知識索引

index = DashScopeCloudIndex("my_first_index")

獲得retriever

您可以從index對象中快速獲得您的retriever,或者使用知識庫名稱初始化您的DashScopeCloudRetriever

# convert from index
retriever = index.as_retriever()

# initialize from DashScopeCloudRetriever
from llama_index.indices.managed.dashscope.retriever import DashScopeCloudRetriever
retriever = DashScopeCloudRetriever("my_first_index")

nodes = retriever.retrieve("my query")

獲得query engine

from llama_index.llms.dashscope import DashScope, DashScopeGenerationModels

dashscope_llm = DashScope(
  model_name=DashScopeGenerationModels.QWEN_MAX, api_key=os.environ["DASHSCOPE_API_KEY"]
)

query_engine = index.as_query_engine(llm=dashscope_llm)

從索引新增/刪除文檔

# add documents to index
index._insert(documents)
# delete documents from index
index.delete_ref_doc([doc_id])