Get started with the Milvus integrations
Bu içerik henüz dilinizde mevcut değil.
Milvus is an open-source vector database system that efficiently stores, indexes, and searches large-scale vector data. It’s commonly used in machine learning, artificial intelligence, and data science applications. The Aspire Milvus integration enables you to connect to existing Milvus servers or create new servers from Aspire with the milvusdb/milvus container image.
In this introduction, you’ll see how to install and use the Aspire Milvus integrations in a simple configuration. If you already have this knowledge, see Milvus Hosting integration for full reference details.
Set up hosting integration
Section titled “Set up hosting integration”To begin, install the Aspire Milvus Hosting integration in your Aspire AppHost project. This integration allows you to create and manage Milvus vector database instances from your Aspire hosting projects:
aspire add milvusAspire CLI etkileşimlidir; istendiğinde uygun sonucu seçin:
Select an integration to add:
> milvus (Aspire.Hosting.Milvus)> Other results listed as selectable options...#:package Aspire.Hosting.Milvus@*<PackageReference Include="Aspire.Hosting.Milvus" Version="*" />Next, in the AppHost project, create instances of Milvus server and database resources, then pass the database to the consuming client projects:
var builder = DistributedApplication.CreateBuilder(args);
var milvus = builder.AddMilvus("milvus") .WithLifetime(ContainerLifetime.Persistent);
var milvusdb = milvus.AddDatabase("milvusdb");
var exampleProject = builder.AddProject<Projects.ExampleProject>("exampleProject") .WaitFor(milvusdb) .WithReference(milvusdb);var builder = DistributedApplication.CreateBuilder(args);
var milvus = builder.AddMilvus("milvus") .WithLifetime(ContainerLifetime.Persistent);
var milvusdb = milvus.AddDatabase("milvusdb");
var exampleProject = builder.AddUvicornApp("api", "./api", "main.app") .WithExternalHttpEndpoints() .WaitFor(milvusdb) .WithReference(milvusdb);var builder = DistributedApplication.CreateBuilder(args);
var milvus = builder.AddMilvus("milvus") .WithLifetime(ContainerLifetime.Persistent);
var milvusdb = milvus.AddDatabase("milvusdb");
var api = builder.AddNodeApp("api", "./api", scriptPath: "index.js") .WithExternalHttpEndpoints() .WaitFor(milvusdb) .WithReference(milvusdb);Use the integration in client projects
Section titled “Use the integration in client projects”Now that the hosting integration is ready, the next step is to install and configure the client integration in any projects that need to use it.
Set up client projects
Section titled “Set up client projects”In each of these consuming client projects, install the Aspire Milvus client integration:
dotnet add package Aspire.Milvus.Client#:package Aspire.Milvus.Client@*<PackageReference Include="Aspire.Milvus.Client" Version="*" />In the Program.cs file of your client-consuming project, call the AddMilvusClient extension method on any IHostApplicationBuilder to register a MilvusClient for use via the dependency injection container. The method takes a connection name parameter.
builder.AddMilvusClient(connectionName: "milvusdb");To interact with Milvus vector databases in your Python consuming projects, you need to include a Milvus client library. The official option is pymilvus, which is Milvus’s Python SDK. You can install this library using pip:
pip install pymilvusEnsure that you import the MilvusClient in code files that interact with the vector database. You should also import the os module to access environment variables:
from pymilvus import MilvusClientimport osTo interact with Milvus vector databases in your JavaScript consuming projects, you need to include a Milvus client library. The official option is @zilliz/milvus2-sdk-node, which is Milvus’s Node.js SDK. You can install this library using npm:
npm install @zilliz/milvus2-sdk-nodeEnsure that you import the MilvusClient in code files that interact with the vector database:
const { MilvusClient } = require('@zilliz/milvus2-sdk-node');Use injected Milvus properties
Section titled “Use injected Milvus properties”In the AppHost, when you used the WithReference method to pass a Milvus database resource to a consuming client project, Aspire injects several configuration properties that you can use in the consuming project.
Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Uri property of a resource called milvusdb becomes MILVUSDB_URI.
Use the GetValue() method to obtain these environment variables in consuming projects:
string connectionUri = builder.Configuration.GetValue<string>("ConnectionStrings__milvusdb");string token = builder.Configuration.GetValue<string>("MILVUSDB_TOKEN");Use the os.getenv() method to obtain these environment variables in consuming projects:
connection_uri = os.getenv("ConnectionStrings__milvusdb")host = os.getenv("MILVUSDB_HOST")port = os.getenv("MILVUSDB_PORT")token = os.getenv("MILVUSDB_TOKEN")database_name = os.getenv("MILVUSDB_DATABASENAME")Use the process.env method to obtain these environment variables in consuming projects:
const connectionUri = process.env.ConnectionStrings__milvusdb;const host = process.env.MILVUSDB_HOST;const port = process.env.MILVUSDB_PORT;const token = process.env.MILVUSDB_TOKEN;const databaseName = process.env.MILVUSDB_DATABASENAME;Use Milvus resources in client code
Section titled “Use Milvus resources in client code”Now that you’ve added the MilvusClient to the builder in the consuming project, you can use the Milvus vector database to store and search vector data. Get the MilvusClient instance using dependency injection. For example, to retrieve your client object from an example service, define it as a constructor parameter and ensure the ExampleService class is registered with the dependency injection container:
public class ExampleService(MilvusClient client){ // Use Milvus client...}Having obtained the client, you can work with the Milvus vector database as you would in any other C# application.
Use the information you have obtained about the Milvus resource to connect to the vector database. Here is an example of how to connect using pymilvus:
# Create Milvus client# The token format is "username:password"client = MilvusClient( uri=f"http://{host}:{port}", token=token, db_name=database_name)
# Verify connectionprint(f"Connected to Milvus database: {database_name}")Having obtained the client, you can work with the Milvus vector database as you would in any other Python application.
Use the information you have obtained about the Milvus resource to connect to the vector database. Here is an example of how to connect using @zilliz/milvus2-sdk-node:
// Create Milvus clientconst client = new MilvusClient({ address: `${host}:${port}`, token: token, database: databaseName});
// Verify connectionconsole.log(`Connected to Milvus database: ${databaseName}`);Having obtained the client, you can work with the Milvus vector database as you would in any other JavaScript application.
Next steps
Section titled “Next steps”Now that you have an Aspire app with Milvus integrations up and running, you can use the following reference documents to learn how to configure and interact with the Milvus resources: