chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Docker.DotNet;
|
||||
using Docker.DotNet.Models;
|
||||
|
||||
namespace Memory.VectorStoreFixtures;
|
||||
|
||||
/// <summary>
|
||||
/// Helper class that creates and deletes containers for the vector store examples.
|
||||
/// </summary>
|
||||
internal static class VectorStoreInfra
|
||||
{
|
||||
/// <summary>
|
||||
/// Setup the postgres pgvector container by pulling the image and running it.
|
||||
/// </summary>
|
||||
/// <param name="client">The docker client to create the container with.</param>
|
||||
/// <returns>The id of the container.</returns>
|
||||
public static async Task<string> SetupPostgresContainerAsync(DockerClient client)
|
||||
{
|
||||
await client.Images.CreateImageAsync(
|
||||
new ImagesCreateParameters
|
||||
{
|
||||
FromImage = "pgvector/pgvector",
|
||||
Tag = "pg16",
|
||||
},
|
||||
null,
|
||||
new Progress<JSONMessage>());
|
||||
|
||||
var container = await client.Containers.CreateContainerAsync(new CreateContainerParameters()
|
||||
{
|
||||
Image = "pgvector/pgvector:pg16",
|
||||
HostConfig = new HostConfig()
|
||||
{
|
||||
PortBindings = new Dictionary<string, IList<PortBinding>>
|
||||
{
|
||||
{"5432", new List<PortBinding> {new() {HostPort = "5432" } }},
|
||||
},
|
||||
PublishAllPorts = true
|
||||
},
|
||||
ExposedPorts = new Dictionary<string, EmptyStruct>
|
||||
{
|
||||
{ "5432", default },
|
||||
},
|
||||
Env =
|
||||
[
|
||||
"POSTGRES_USER=postgres",
|
||||
"POSTGRES_PASSWORD=example",
|
||||
],
|
||||
});
|
||||
|
||||
await client.Containers.StartContainerAsync(
|
||||
container.ID,
|
||||
new ContainerStartParameters());
|
||||
|
||||
return container.ID;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Setup the qdrant container by pulling the image and running it.
|
||||
/// </summary>
|
||||
/// <param name="client">The docker client to create the container with.</param>
|
||||
/// <returns>The id of the container.</returns>
|
||||
public static async Task<string> SetupQdrantContainerAsync(DockerClient client)
|
||||
{
|
||||
await client.Images.CreateImageAsync(
|
||||
new ImagesCreateParameters
|
||||
{
|
||||
FromImage = "qdrant/qdrant",
|
||||
Tag = "latest",
|
||||
},
|
||||
null,
|
||||
new Progress<JSONMessage>());
|
||||
|
||||
var container = await client.Containers.CreateContainerAsync(new CreateContainerParameters()
|
||||
{
|
||||
Image = "qdrant/qdrant",
|
||||
HostConfig = new HostConfig()
|
||||
{
|
||||
PortBindings = new Dictionary<string, IList<PortBinding>>
|
||||
{
|
||||
{"6333", new List<PortBinding> {new() {HostPort = "6333" } }},
|
||||
{"6334", new List<PortBinding> {new() {HostPort = "6334" } }}
|
||||
},
|
||||
PublishAllPorts = true
|
||||
},
|
||||
ExposedPorts = new Dictionary<string, EmptyStruct>
|
||||
{
|
||||
{ "6333", default },
|
||||
{ "6334", default }
|
||||
},
|
||||
});
|
||||
|
||||
await client.Containers.StartContainerAsync(
|
||||
container.ID,
|
||||
new ContainerStartParameters());
|
||||
|
||||
return container.ID;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Setup the redis container by pulling the image and running it.
|
||||
/// </summary>
|
||||
/// <param name="client">The docker client to create the container with.</param>
|
||||
/// <returns>The id of the container.</returns>
|
||||
public static async Task<string> SetupRedisContainerAsync(DockerClient client)
|
||||
{
|
||||
await client.Images.CreateImageAsync(
|
||||
new ImagesCreateParameters
|
||||
{
|
||||
FromImage = "redis/redis-stack",
|
||||
Tag = "latest",
|
||||
},
|
||||
null,
|
||||
new Progress<JSONMessage>());
|
||||
|
||||
var container = await client.Containers.CreateContainerAsync(new CreateContainerParameters()
|
||||
{
|
||||
Image = "redis/redis-stack",
|
||||
HostConfig = new HostConfig()
|
||||
{
|
||||
PortBindings = new Dictionary<string, IList<PortBinding>>
|
||||
{
|
||||
{"6379", new List<PortBinding> {new() {HostPort = "6379"}}},
|
||||
{"8001", new List<PortBinding> {new() {HostPort = "8001"}}}
|
||||
},
|
||||
PublishAllPorts = true
|
||||
},
|
||||
ExposedPorts = new Dictionary<string, EmptyStruct>
|
||||
{
|
||||
{ "6379", default },
|
||||
{ "8001", default }
|
||||
},
|
||||
});
|
||||
|
||||
await client.Containers.StartContainerAsync(
|
||||
container.ID,
|
||||
new ContainerStartParameters());
|
||||
|
||||
return container.ID;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stop and delete the container with the specified id.
|
||||
/// </summary>
|
||||
/// <param name="client">The docker client to delete the container in.</param>
|
||||
/// <param name="containerId">The id of the container to delete.</param>
|
||||
/// <returns>An async task.</returns>
|
||||
public static async Task DeleteContainerAsync(DockerClient client, string containerId)
|
||||
{
|
||||
await client.Containers.StopContainerAsync(containerId, new ContainerStopParameters());
|
||||
await client.Containers.RemoveContainerAsync(containerId, new ContainerRemoveParameters());
|
||||
}
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Docker.DotNet;
|
||||
using Npgsql;
|
||||
|
||||
namespace Memory.VectorStoreFixtures;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture to use for creating a Postgres container before tests and delete it after tests.
|
||||
/// </summary>
|
||||
public class VectorStorePostgresContainerFixture : IAsyncLifetime
|
||||
{
|
||||
private DockerClient? _dockerClient;
|
||||
private string? _postgresContainerId;
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
}
|
||||
|
||||
public async Task ManualInitializeAsync()
|
||||
{
|
||||
if (this._postgresContainerId == null)
|
||||
{
|
||||
// Connect to docker and start the docker container.
|
||||
using var dockerClientConfiguration = new DockerClientConfiguration();
|
||||
this._dockerClient = dockerClientConfiguration.CreateClient();
|
||||
this._postgresContainerId = await VectorStoreInfra.SetupPostgresContainerAsync(this._dockerClient);
|
||||
|
||||
// Delay until the Postgres server is ready.
|
||||
var connectionString = "Host=localhost;Port=5432;Username=postgres;Password=example;Database=postgres;";
|
||||
var succeeded = false;
|
||||
var attemptCount = 0;
|
||||
while (!succeeded && attemptCount++ < 10)
|
||||
{
|
||||
try
|
||||
{
|
||||
NpgsqlDataSourceBuilder dataSourceBuilder = new(connectionString);
|
||||
dataSourceBuilder.UseVector();
|
||||
using var dataSource = dataSourceBuilder.Build();
|
||||
NpgsqlConnection connection = await dataSource.OpenConnectionAsync().ConfigureAwait(false);
|
||||
|
||||
await using (connection)
|
||||
{
|
||||
// Create extension vector if it doesn't exist
|
||||
await using (NpgsqlCommand command = new("CREATE EXTENSION IF NOT EXISTS vector", connection))
|
||||
{
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
await Task.Delay(1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
if (this._dockerClient != null && this._postgresContainerId != null)
|
||||
{
|
||||
// Delete docker container.
|
||||
await VectorStoreInfra.DeleteContainerAsync(this._dockerClient, this._postgresContainerId);
|
||||
}
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Docker.DotNet;
|
||||
using Qdrant.Client;
|
||||
|
||||
namespace Memory.VectorStoreFixtures;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture to use for creating a Qdrant container before tests and delete it after tests.
|
||||
/// </summary>
|
||||
public class VectorStoreQdrantContainerFixture : IAsyncLifetime
|
||||
{
|
||||
private DockerClient? _dockerClient;
|
||||
private string? _qdrantContainerId;
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
}
|
||||
|
||||
public async Task ManualInitializeAsync()
|
||||
{
|
||||
if (this._qdrantContainerId == null)
|
||||
{
|
||||
// Connect to docker and start the docker container.
|
||||
using var dockerClientConfiguration = new DockerClientConfiguration();
|
||||
this._dockerClient = dockerClientConfiguration.CreateClient();
|
||||
this._qdrantContainerId = await VectorStoreInfra.SetupQdrantContainerAsync(this._dockerClient);
|
||||
|
||||
// Delay until the Qdrant server is ready.
|
||||
var qdrantClient = new QdrantClient("localhost");
|
||||
var succeeded = false;
|
||||
var attemptCount = 0;
|
||||
while (!succeeded && attemptCount++ < 10)
|
||||
{
|
||||
try
|
||||
{
|
||||
await qdrantClient.ListCollectionsAsync();
|
||||
succeeded = true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
await Task.Delay(1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
if (this._dockerClient != null && this._qdrantContainerId != null)
|
||||
{
|
||||
// Delete docker container.
|
||||
await VectorStoreInfra.DeleteContainerAsync(this._dockerClient, this._qdrantContainerId);
|
||||
}
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Docker.DotNet;
|
||||
|
||||
namespace Memory.VectorStoreFixtures;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture to use for creating a Redis container before tests and delete it after tests.
|
||||
/// </summary>
|
||||
public class VectorStoreRedisContainerFixture : IAsyncLifetime
|
||||
{
|
||||
private DockerClient? _dockerClient;
|
||||
private string? _redisContainerId;
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
}
|
||||
|
||||
public async Task ManualInitializeAsync()
|
||||
{
|
||||
if (this._redisContainerId == null)
|
||||
{
|
||||
// Connect to docker and start the docker container.
|
||||
using var dockerClientConfiguration = new DockerClientConfiguration();
|
||||
this._dockerClient = dockerClientConfiguration.CreateClient();
|
||||
this._redisContainerId = await VectorStoreInfra.SetupRedisContainerAsync(this._dockerClient);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
if (this._dockerClient != null && this._redisContainerId != null)
|
||||
{
|
||||
// Delete docker container.
|
||||
await VectorStoreInfra.DeleteContainerAsync(this._dockerClient, this._redisContainerId);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user