### Managing Pinecone Collections in C# Source: https://github.com/neon-sunset/pinecone.net/blob/main/README.md This snippet provides examples for interacting with Pinecone collections using the Pinecone.NET client. It shows how to list all available collections, create a new collection from an existing index, retrieve details about a specific collection, and delete a collection. It assumes an initialized 'PineconeClient' instance. ```C# using Pinecone; // Assuming you have an instance of `PineconeClient` named `pinecone` // List all collections var collections = await pinecone.ListCollections(); // Create a new collection await pinecone.CreateCollection("myCollection", "myIndex"); // Describe a collection var details = await pinecone.DescribeCollection("myCollection"); // Delete a collection await pinecone.DeleteCollection("myCollection"); ``` -------------------------------- ### Performing Vector Operations in Pinecone.NET (C#) Source: https://github.com/neon-sunset/pinecone.net/blob/main/README.md This code illustrates various operations on vectors within a Pinecone index using the Pinecone.NET library. It includes examples for upserting vectors with metadata, fetching vectors by ID, querying for similar vectors (by ID or by vector values), querying with metadata filters, deleting vectors by ID or filter, and deleting all vectors in the index. It assumes an existing 'index' client instance. ```C# // Assuming you have an instance of `index` // Create and upsert vectors var vectors = new[] { new Vector { Id = "vector1", Values = new float[] { 0.1f, 0.2f, 0.3f, ... }, Metadata = new MetadataMap { ["genre"] = "horror", ["duration"] = 120 } } }; await index.Upsert(vectors); // Fetch vectors by IDs var fetched = await index.Fetch(["vector1"]); // Query scored vectors by ID var scored = await index.Query("vector1", topK: 10); // Query scored vectors by a new, previously unseen vector var vector = new[] { 0.1f, 0.2f, 0.3f, ... }; var scored = await index.Query(vector, topK: 10); // Query scored vectors by ID with metadata filter var filter = new MetadataMap { ["genre"] = new MetadataMap { ["$in"] = new[] { "documentary", "action" } } }; var scored = await index.Query("birds", topK: 10, filter); // Delete vectors by vector IDs await index.Delete(new[] { "vector1" }); // Delete vectors by metadata filter await index.Delete(new MetadataMap { ["genre"] = new MetadataMap { ["$in"] = new[] { "documentary", "action" } } }); // Delete all vectors in the index await index.DeleteAll(); ``` -------------------------------- ### Managing Pinecone Indexes in C# Source: https://github.com/neon-sunset/pinecone.net/blob/main/README.md This snippet demonstrates how to initialize the Pinecone.NET client and perform common index management operations. It covers listing existing indexes, creating a new serverless index if needed, retrieving an index client instance, configuring index properties, and deleting an index. The index client is noted as thread-safe. ```C# using Pinecone; // Initialize the client with your API key using var pinecone = new PineconeClient("your-api-key"); // List all indexes var indexes = await pinecone.ListIndexes(); // Create a new index if it doesn't exist var indexName = "myIndex"; if (!indexes.Contains(indexName)) { await pinecone.CreateServerlessIndex(indexName, 1536, Metric.Cosine, "aws", "us-east-1"); } // Get the Pinecone index by name (uses REST by default). // The index client is thread-safe, consider caching and/or // injecting it as a singleton into your DI container. using var index = await pinecone.GetIndex(indexName); // Configure an index await pinecone.ConfigureIndex(indexName, replicas: 2, podType: "p2"); // Delete an index await pinecone.DeleteIndex(indexName); ``` -------------------------------- ### Handling Failures During Batched Parallel Upsert in C# Source: https://github.com/neon-sunset/pinecone.net/blob/main/README.md This snippet demonstrates how to perform a batched parallel upsert operation using the Pinecone .NET client and implement a retry mechanism to recover from ParallelUpsertException. It shows how to filter out vectors that failed in a previous attempt and retry the upsert with the remaining vectors, up to a specified limit. The client automatically handles batching and parallelization. ```csharp // Upsert with recovery from up to three failures on batched parallel upsert. // // The parallelization is done automatically by the client based on the vector // dimension and the number of vectors to upsert. It aims to keep the individual // request size below Pinecone's 2MiB limit with some safety margin for metadata. // This behavior can be further controlled by calling the 'Upsert' overload with // custom values for 'batchSize' and 'parallelism' parameters. // // This is not the most efficient implementation in terms of allocations in // GC pause frequency sensitive scenarios, but is perfectly acceptable // for pretty much all regular back-end applications. // Assuming there is an instance of 'index' available // Generate 25k random vectors var vectors = Enumerable .Range(0, 25_000) .Select(_ => new Vector { Id = Guid.NewGuid().ToString(), Values = Enumerable .Range(0, 1536) .Select(_ => Random.Shared.NextSingle()) .ToArray() }) .ToArray(); // Specify the retry limit we are okay with var retries = 3; while (true) { try { // Perform the upsert await index.Upsert(vectors); // If no exception is thrown, break out of the retry loop break; } catch (ParallelUpsertException e) when (retries-- > 0) { // Create a hash set to efficiently filter out the failed vectors var filter = e.FailedBatchVectorIds.ToHashSet(); // Filter out the failed vectors from the batch and assign them to // the 'vectors' variable consumed by 'Upsert' operation above vectors = vectors.Where(v => filter.Contains(v.Id)).ToArray(); Console.WriteLine($"Retrying upsert due to error: {e.Message}"); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.