Skip to content

Commit f2f39c9

Browse files
authored
Add integration tests for rerank, embed, and sparse index
1 parent d74973f commit f2f39c9

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System.Text.Json;
2+
using NUnit.Framework;
3+
4+
namespace Pinecone.Test.Integration.Control;
5+
6+
public class SparseIndexTests : BaseTest
7+
{
8+
[Test]
9+
public async Task TestCreateSparseIndexAsync()
10+
{
11+
try
12+
{
13+
// Define the index name and dimensions
14+
var indexName = Helpers.GenerateIndexName("sparse-index-testing");
15+
16+
// Create the sparse index
17+
var index = await Client.CreateIndexAsync(new CreateIndexRequest
18+
{
19+
Name = indexName,
20+
Metric = CreateIndexRequestMetric.Dotproduct,
21+
VectorType = VectorType.Sparse,
22+
Spec = new ServerlessIndexSpec
23+
{
24+
Serverless = new ServerlessSpec
25+
{
26+
Cloud = ServerlessSpecCloud.Aws,
27+
Region = "us-east-1"
28+
}
29+
},
30+
DeletionProtection = DeletionProtection.Enabled
31+
}).ConfigureAwait(false);
32+
33+
// Verify the index was created successfully
34+
Assert.That(index, Is.Not.Null);
35+
Assert.Multiple(() =>
36+
{
37+
Assert.That(index.Name, Is.EqualTo(indexName));
38+
Assert.That(index.Metric, Is.EqualTo(IndexModelMetric.Dotproduct));
39+
Assert.That(index.VectorType, Is.EqualTo(VectorType.Sparse));
40+
});
41+
}
42+
catch (PineconeApiException e)
43+
{
44+
TestContext.WriteLine(e.Message);
45+
TestContext.WriteLine(e.Body is string ? e.Body : JsonSerializer.Serialize(e.Body));
46+
throw;
47+
}
48+
}
49+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using NUnit.Framework;
2+
3+
namespace Pinecone.Test.Integration.Data;
4+
5+
public class InferenceTests : BaseTest
6+
{
7+
[Test]
8+
public async Task TestEmbedAsync()
9+
{
10+
// Prepare input sentences to be embedded
11+
var inputs = new List<EmbedRequestInputsItem>
12+
{
13+
new() { Text = "The quick brown fox jumps over the lazy dog." },
14+
new() { Text = "Lorem ipsum" }
15+
};
16+
17+
// Specify the embedding model and parameters
18+
var embeddingModel = "multilingual-e5-large";
19+
20+
// Generate embeddings for the input data
21+
var embeddings = await Client.Inference.EmbedAsync(new EmbedRequest
22+
{
23+
Model = embeddingModel,
24+
Inputs = inputs,
25+
Parameters = new Dictionary<string, object?>
26+
{
27+
["input_type"] = "query",
28+
["truncate"] = "END"
29+
}
30+
}).ConfigureAwait(false);
31+
32+
// Verify the response contains the expected embeddings
33+
Assert.That(embeddings, Is.Not.Null);
34+
Assert.Multiple(() =>
35+
{
36+
Assert.That(embeddings.Model, Is.EqualTo(embeddingModel));
37+
Assert.That(embeddings.Data, Is.Not.Empty);
38+
});
39+
}
40+
41+
[Test]
42+
public async Task TestRerankAsync()
43+
{
44+
// Prepare the query and documents to be reranked
45+
var query = "The tech company Apple is known for its innovative products like the iPhone.";
46+
var documents = new List<Dictionary<string, object>>
47+
{
48+
new() { ["id"] = "vec1", ["my_field"] = "Apple is a popular fruit known for its sweetness and crisp texture." },
49+
new() { ["id"] = "vec2", ["my_field"] = "Many people enjoy eating apples as a healthy snack." },
50+
new() { ["id"] = "vec3", ["my_field"] = "Apple Inc. has revolutionized the tech industry with its sleek designs and user-friendly interfaces." },
51+
new() { ["id"] = "vec4", ["my_field"] = "An apple a day keeps the doctor away, as the saying goes." }
52+
};
53+
54+
// Specify the reranking model and parameters
55+
var rerankModel = "bge-reranker-v2-m3";
56+
var rankFields = new List<string> { "my_field" };
57+
var topN = 2;
58+
var returnDocuments = true;
59+
var parameters = new Dictionary<string, object>
60+
{
61+
["truncate"] = "END"
62+
};
63+
64+
// Perform the reranking
65+
var rerankResult = await Client.Inference.RerankAsync(new RerankRequest
66+
{
67+
Model = rerankModel,
68+
Query = query,
69+
Documents = documents,
70+
RankFields = rankFields,
71+
TopN = topN,
72+
Parameters = parameters
73+
}).ConfigureAwait(false);
74+
75+
// Verify the response contains the expected reranked data
76+
Assert.That(rerankResult, Is.Not.Null);
77+
Assert.Multiple(() =>
78+
{
79+
Assert.That(rerankResult.Model, Is.EqualTo(rerankModel));
80+
Assert.That(rerankResult.Data, Is.Not.Empty);
81+
});
82+
}
83+
}

0 commit comments

Comments
 (0)