使用 Milvus 的 Llama Stack 建立 RAG
Llama Stack是一種以服務為導向、API 為先的方法,可用於建立生產型 AI 應用程式。它提供了一個通用的堆疊,讓開發人員可以隨處開發、隨處部署,並利用生產就緒的構建區塊,真正獨立於提供商。Llama Stack 著重於 Meta 的 Llama 模型、可複合性、生產就绪性以及合作生態系統。
在本教程中,我們將介紹如何使用 Milvus 建立 Llama Stack 伺服器,讓您能夠匯入私人資料作為您的知識庫。然後,我們將在伺服器上執行查詢,建立完整的 RAG 應用程式。
準備環境
有許多方式可以啟動 Llama Stack 伺服器,例如作為一個函式庫,建立一個發行版等等。對於 Llama Stack 中的每個元件,也可以選擇各種提供者。因此,有許多啟動 Llama Stack 伺服器的方式。
本教學使用下列配置作為啟動服務的範例。如果您希望以其他方式啟動,請參閱啟動Llama Stack 伺服器。
- 我們使用 Conda 以 Milvus 組態建立自訂的發行版。
- 我們使用Together AI作為 LLM 提供者。
- 我們使用預設的
all-MiniLM-L6-v2作為嵌入模型。
本教學主要參考Llama Stack 文件的官方安裝指南。如果您在本教學中發現任何過時的部分,您可以優先遵循官方指南,並為我們建立一個問題。
啟動 Llama Stack 伺服器
準備環境
由於我們需要使用 Together AI 作為 LLM 服務,因此必須先登入官方網站申請API key,並將 API keyTOGETHER_API_KEY 設定為環境變數。
克隆 Llama Stack 原始碼
$ git clone https://github.com/meta-llama/llama-stack.git
$ cd llama-stack
建立 conda 環境並安裝相依性
$ conda create -n stack python=3.10
$ conda activate stack
$ pip install -e .
修改llama_stack/llama_stack/template/together/run.yaml 中的內容,將 vector_io 部分改成相關的 Milvus 設定。例如,新增
vector_io:
- provider_id: milvus
provider_type: inline::milvus
config:
db_path: ~/.llama/distributions/together/milvus_store.db
# - provider_id: milvus
# provider_type: remote::milvus
# config:
# uri: http://localhost:19530
# token: root:Milvus
在 Llama Stack 中,Milvus 有兩種配置方式:本地配置,即inline::milvus ;遠端配置,即remote::milvus 。
最簡單的方法是本地配置,需要設定
db_path,這是本地儲存Milvus-Lite檔案的路徑。遠端設定適合大量資料儲存。
- 如果您有大量的資料,您可以在Docker 或 Kubernetes 上架設效能優異的 Milvus 伺服器。在此設定中,請使用伺服器 URI,例如
http://localhost:19530,作為您的uri。預設的token是root:Milvus。 - 如果您想使用Zilliz Cloud(Milvus 的完全管理雲端服務),請調整
uri和token,它們對應於 Zilliz Cloud 中的Public Endpoint 和 API key。
- 如果您有大量的資料,您可以在Docker 或 Kubernetes 上架設效能優異的 Milvus 伺服器。在此設定中,請使用伺服器 URI,例如
從模板建立發行版
執行下列指令建立發行版:
$ llama stack build --template together --image-type conda
將產生檔案~/.llama/distributions/together/together-run.yaml 。然後,執行此指令啟動伺服器:
$ llama stack run --image-type conda ~/.llama/distributions/together/together-run.yaml
如果一切順利,您應該會看到 Llama Stack 伺服器在 8321 連接埠成功執行。
從用戶端執行 RAG
啟動伺服器後,您就可以寫用戶端程式碼來存取伺服器。以下是範例程式碼:
import uuid
from llama_stack_client.types import Document
from llama_stack_client.lib.agents.agent import Agent
from llama_stack_client.types.agent_create_params import AgentConfig
# See https://www.together.ai/models for all available models
INFERENCE_MODEL = "meta-llama/Llama-3.3-70B-Instruct-Turbo"
LLAMA_STACK_PORT = 8321
def create_http_client():
from llama_stack_client import LlamaStackClient
return LlamaStackClient(
base_url=f"http://localhost:{LLAMA_STACK_PORT}" # Your Llama Stack Server URL
)
client = create_http_client()
# Documents to be used for RAG
urls = ["chat.rst", "llama3.rst", "memory_optimizations.rst", "lora_finetune.rst"]
documents = [
Document(
document_id=f"num-{i}",
content=f"https://raw.githubusercontent.com/pytorch/torchtune/main/docs/source/tutorials/{url}",
mime_type="text/plain",
metadata={},
)
for i, url in enumerate(urls)
]
# Register a vector database
vector_db_id = f"test-vector-db-{uuid.uuid4().hex}"
client.vector_dbs.register(
vector_db_id=vector_db_id,
embedding_model="all-MiniLM-L6-v2",
embedding_dimension=384,
provider_id="milvus",
)
print("inserting...")
# Insert the documents into the vector database
client.tool_runtime.rag_tool.insert(
documents=documents, vector_db_id=vector_db_id, chunk_size_in_tokens=1024,
)
agent_config = AgentConfig(
model=INFERENCE_MODEL,
# Define instructions for the agent ( aka system prompt)
instructions="You are a helpful assistant",
enable_session_persistence=False,
# Define tools available to the agent
toolgroups=[{"name": "builtin::rag", "args": {"vector_db_ids": [vector_db_id]}}],
)
rag_agent = Agent(client, agent_config)
session_id = rag_agent.create_session("test-session")
print("finish init agent...")
user_prompt = (
"What are the top 5 topics that were explained? Only list succinct bullet points."
)
# Get the final answer from the agent
response = rag_agent.create_turn(
messages=[{"role": "user", "content": user_prompt}],
session_id=session_id,
stream=False,
)
print(f"Response: ")
print(response.output_message.content)
執行此程式碼來執行 RAG 查詢,如果一切運作正常,輸出應該是這樣的:
inserting...
finish init agent...
Response:
* Fine-Tuning Llama3 with Chat Data
* Evaluating fine-tuned Llama3-8B models with EleutherAI's Eval Harness
* Generating text with our fine-tuned Llama3 model
* Faster generation via quantization
* Fine-tuning on a custom chat dataset