Milvus Hosting integration reference
Ce contenu n’est pas encore disponible dans votre langue.
To get started with the Aspire Milvus integrations, follow the Get started with Milvus integrations guide.
This article includes full details about the Aspire Milvus Hosting integration, which models Milvus server and database resources as the MilvusServerResource and MilvusDatabaseResource types. To access these types and APIs, you need to install the Milvus Hosting integration in your AppHost project.
Installation
Section titled “Installation”The Aspire Milvus hosting integration models the Milvus vector database server as the following types:
MilvusServerResourceMilvusDatabaseResource
To access these types and APIs for expressing them as resources in your AppHost project, install the 📦 Aspire.Hosting.Milvus NuGet package:
aspire add milvusLa CLI Aspire est interactive ; choisissez le résultat approprié lorsque demandé :
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="*" />Add Milvus server and database resources
Section titled “Add Milvus server and database resources”In the AppHost project, call AddMilvus to add and return a Milvus server resource builder. Chain a call to the returned resource builder to AddDatabase, to add a Milvus database to the server resource:
var builder = DistributedApplication.CreateBuilder(args);
var milvus = builder.AddMilvus("milvus") .WithLifetime(ContainerLifetime.Persistent);
var milvusdb = milvus.AddDatabase("milvusdb");
builder.AddProject<Projects.ExampleProject>() .WithReference(milvusdb) .WaitFor(milvusdb);
// After adding all resources, run the app...When Aspire adds a container image to the AppHost, as shown in the preceding example with the milvusdb/milvus image, it creates a new Milvus server on your local machine. A reference to your Milvus resource builder (the milvus variable) is used to add a database. The database is named milvusdb and then added to the ExampleProject.
The WithReference method configures a connection in the ExampleProject named "milvusdb". For more information, see Container resource lifecycle.
Add Milvus resource with API key parameter
Section titled “Add Milvus resource with API key parameter”The Milvus resource includes default credentials with a username of root and the password Milvus. To change the default password in the container, pass an apiKey parameter:
var builder = DistributedApplication.CreateBuilder(args);
var apiKey = builder.AddParameter("apiKey", secret: true);
var milvus = builder.AddMilvus("milvus", apiKey) .WithLifetime(ContainerLifetime.Persistent);
var milvusdb = milvus.AddDatabase("milvusdb");
var myService = builder.AddProject<Projects.ExampleProject>() .WithReference(milvusdb) .WaitFor(milvusdb);The preceding code gets a parameter to pass to the AddMilvus API, and internally assigns the parameter to the Milvus container configuration. The apiKey parameter is usually specified as a user secret:
{ "Parameters": { "apiKey": "your-secure-password" }}For more information, see External parameters.
Add Milvus resource with data volume
Section titled “Add Milvus resource with data volume”To add a data volume to the Milvus resource, call the WithDataVolume method:
var builder = DistributedApplication.CreateBuilder(args);
var milvus = builder.AddMilvus("milvus") .WithDataVolume() .WithLifetime(ContainerLifetime.Persistent);
var milvusdb = milvus.AddDatabase("milvusdb");
builder.AddProject<Projects.ExampleProject>() .WithReference(milvusdb) .WaitFor(milvusdb);
// After adding all resources, run the app...The data volume is used to persist the Milvus data outside the lifecycle of its container. The data volume is mounted at the /var/lib/milvus path in the Milvus container and when a name parameter isn’t provided, the name is generated at random. For more information on data volumes and details on why they’re preferred over bind mounts, see Docker docs: Volumes.
Add Milvus resource with data bind mount
Section titled “Add Milvus resource with data bind mount”To add a data bind mount to the Milvus resource, call the WithDataBindMount method:
var builder = DistributedApplication.CreateBuilder(args);
var milvus = builder.AddMilvus("milvus") .WithDataBindMount(source: @"C:\Milvus\Data") .WithLifetime(ContainerLifetime.Persistent);
var milvusdb = milvus.AddDatabase("milvusdb");
builder.AddProject<Projects.ExampleProject>() .WithReference(milvusdb) .WaitFor(milvusdb);
// After adding all resources, run the app...Create an Attu resource
Section titled “Create an Attu resource”Attu is a graphical user interface (GUI) and management tool designed to interact with Milvus and its databases. To use Attu, call the WithAttu extension method:
var builder = DistributedApplication.CreateBuilder(args);
var milvus = builder.AddMilvus("milvus") .WithAttu() .WithLifetime(ContainerLifetime.Persistent);
var milvusdb = milvus.AddDatabase("milvusdb");
var myService = builder.AddProject<Projects.ExampleProject>() .WithReference(milvusdb) .WaitFor(milvusdb);When you debug the Aspire solution, you’ll see an Attu container listed in the resources. Select the resource’s endpoint to open the GUI.
Using with non-.NET applications
Section titled “Using with non-.NET applications”When you use the WithReference method to pass a Milvus database resource to a non-.NET application (such as Python or JavaScript), Aspire automatically injects environment variables that describe the connection information.
For example, if you reference a Milvus database resource named milvusdb:
var milvus = builder.AddMilvus("milvus");var milvusdb = milvus.AddDatabase("milvusdb");
var pythonApp = builder.AddUvicornApp("api", "./api", "main.app") .WithReference(milvusdb);The following environment variables are available in the Python application:
ConnectionStrings__milvusdb- The connection string for the Milvus databaseMILVUSDB_HOST- The hostname of the Milvus serverMILVUSDB_PORT- The gRPC port numberMILVUSDB_TOKEN- The authentication token (format:root:{password})MILVUSDB_URI- The gRPC connection URIMILVUSDB_DATABASENAME- The database name
You can access these environment variables in your application code:
from pymilvus import MilvusClientimport os
# Get connection propertieshost = os.getenv("MILVUSDB_HOST")port = os.getenv("MILVUSDB_PORT")token = os.getenv("MILVUSDB_TOKEN")database_name = os.getenv("MILVUSDB_DATABASENAME")
# Create Milvus clientclient = MilvusClient( uri=f"http://{host}:{port}", token=token, db_name=database_name)const { MilvusClient } = require('@zilliz/milvus2-sdk-node');
// Get connection propertiesconst host = process.env.MILVUSDB_HOST;const port = process.env.MILVUSDB_PORT;const token = process.env.MILVUSDB_TOKEN;const databaseName = process.env.MILVUSDB_DATABASENAME;
// Create Milvus clientconst client = new MilvusClient({ address: `${host}:${port}`, token: token, database: databaseName});