Ir al contenido

Get started with the Milvus integrations

Esta página aún no está disponible en tu idioma.

Milvus logo

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.

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 CLI — Añadir paquete Aspire.Hosting.Milvus
aspire add milvus

La CLI de Aspire es interactiva; asegúrate de seleccionar el resultado adecuado cuando se te pida:

Aspire CLI — Ejemplo de salida
Select an integration to add:
> milvus (Aspire.Hosting.Milvus)
> Other results listed as selectable options...

Next, in the AppHost project, create instances of Milvus server and database resources, then pass the database to the consuming client projects:

Select your programming language to get started
C# — AppHost.cs
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);
C# — AppHost.cs
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);
C# — AppHost.cs
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);

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.

In each of these consuming client projects, install the Aspire Milvus client integration:

.NET CLI — Add Aspire.Milvus.Client package
dotnet add package Aspire.Milvus.Client

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.

C# — Program.cs
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:

Terminal window
pip install pymilvus

Ensure 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:

Python - Import pymilvus
from pymilvus import MilvusClient
import os

To 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:

Terminal window
npm install @zilliz/milvus2-sdk-node

Ensure that you import the MilvusClient in code files that interact with the vector database:

JavaScript - Import @zilliz/milvus2-sdk-node
const { MilvusClient } = require('@zilliz/milvus2-sdk-node');

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:

C# - Obtain configuration properties
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:

Python - Obtain configuration properties
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:

JavaScript - Obtain configuration properties
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;

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:

C# — ExampleService.cs
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:

Python - Connect to Milvus
# Create Milvus client
# The token format is "username:password"
client = MilvusClient(
uri=f"http://{host}:{port}",
token=token,
db_name=database_name
)
# Verify connection
print(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:

JavaScript - Connect to Milvus
// Create Milvus client
const client = new MilvusClient({
address: `${host}:${port}`,
token: token,
database: databaseName
});
// Verify connection
console.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.

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: