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