指南CookbookIntegration Llama Index Milvus Lite

Cookbook:LlamaIndex 与 Milvus 集成

这是一个简单的 cookbook,演示如何使用 LlamaIndex Litefuse 集成。其中使用 Milvus Lite 来存储文档并进行查询。

Milvus Lite 是 Milvus 的轻量版本,Milvus 是一个开源向量数据库,为基于向量 embedding 与相似性检索的 AI 应用提供支持。

准备工作

⚠️

注意: 本指南使用我们的 Python SDK v2。我们已经基于 OpenTelemetry 推出了全新升级的 SDK,欢迎查看 SDK v3,它更强大也更易用。

%pip install llama-index "langfuse<3.0.0" llama-index-vector-stores-milvus --upgrade

初始化集成。从 Litefuse 项目设置 获取你的 API keys,并把 public_key、secret_key 替换为你自己的值。本示例使用 OpenAI 做 embedding 和聊天补全,因此你也需要在环境变量中设置 OpenAI 的 key。

import os
 
# Get keys for your project from the project settings page
# https://litefuse.cloud
os.environ["LANGFUSE_PUBLIC_KEY"] = ""
os.environ["LANGFUSE_SECRET_KEY"] = ""
os.environ["LANGFUSE_HOST"] = "https://litefuse.cloud"
 
# Your openai key
os.environ["OPENAI_API_KEY"] = ""
from llama_index.core import Settings
from llama_index.core.callbacks import CallbackManager
from langfuse.llama_index import LlamaIndexCallbackHandler
 
langfuse_callback_handler = LlamaIndexCallbackHandler()
Settings.callback_manager = CallbackManager([langfuse_callback_handler])

使用 Milvus Lite 建立索引

from llama_index.core import Document
 
doc1 = Document(text="""
Maxwell "Max" Silverstein, a lauded movie director, screenwriter, and producer, was born on October 25, 1978, in Boston, Massachusetts. A film enthusiast from a young age, his journey began with home movies shot on a Super 8 camera. His passion led him to the University of Southern California (USC), majoring in Film Production. Eventually, he started his career as an assistant director at Paramount Pictures. Silverstein's directorial debut, “Doors Unseen,” a psychological thriller, earned him recognition at the Sundance Film Festival and marked the beginning of a successful directing career.
""")
doc2 = Document(text="""
Throughout his career, Silverstein has been celebrated for his diverse range of filmography and unique narrative technique. He masterfully blends suspense, human emotion, and subtle humor in his storylines. Among his notable works are "Fleeting Echoes," "Halcyon Dusk," and the Academy Award-winning sci-fi epic, "Event Horizon's Brink." His contribution to cinema revolves around examining human nature, the complexity of relationships, and probing reality and perception. Off-camera, he is a dedicated philanthropist living in Los Angeles with his wife and two children.
""")
# Example index construction + LLM query
 
from llama_index.core import VectorStoreIndex
from llama_index.core import StorageContext
from llama_index.vector_stores.milvus import MilvusVectorStore
 
 
vector_store = MilvusVectorStore(
    uri="tmp/milvus_demo.db", dim=1536, overwrite=False
)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
 
index = VectorStoreIndex.from_documents(
    [doc1,doc2], storage_context=storage_context
)

查询

# Query
response = index.as_query_engine().query("What did he do growing up?")
print(response)
# Chat
response = index.as_chat_engine().chat("What did he do growing up?")
print(response)

在 Litefuse 中查看 trace

# As we want to immediately see result in Litefuse, we need to flush the callback handler
langfuse_callback_handler.flush()

完成!✨ 你可以在 Litefuse 项目里看到索引和查询的 trace。

示例 trace(公开链接):

  1. Query
  2. Query (chat)

Litefuse 中的 trace:

Litefuse Traces

想了解更多高级特性?

查看完整的集成文档,了解更多高级特性以及如何使用它们:

  • 与 Langfuse Python SDK 及其他集成的互操作
  • 为 trace 添加自定义元数据和属性
这个页面对你有帮助吗?