Skip to content

Commit

Permalink
test: Fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Apr 9, 2024
1 parent ddd87b7 commit dc3c1cf
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions src/Databases/OpenSearch/test/OpenSearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class OpenSearchTests

#region Query Images

public Task setup_image_tests()
public void setup_image_tests()
{
_indexName = "images-index";
var username = Environment.GetEnvironmentVariable("OPENSEARCH_USERNAME");
Expand All @@ -47,15 +47,13 @@ public Task setup_image_tests()
Dimensions = _options.Dimensions
}
};
_vectorDatabase = new OpenSearchVectorStore(_embeddingModel, _options);

return Task.CompletedTask;
_vectorDatabase = new OpenSearchVectorStore(_options);
}

[Test]
public async Task index_test_images()
{
await setup_image_tests();
setup_image_tests();

string[] extensions = { ".bmp",".gif", ".jpg", ".jpeg", ".png", ".tiff" };
var files = Directory.EnumerateFiles(@"[images directory]", "*.*", SearchOption.AllDirectories)
Expand Down Expand Up @@ -87,13 +85,13 @@ public async Task index_test_images()
documents.Add(document);
}

var pages = await _vectorStore!.AddImagesAsync(documents);
var pages = await _vectorDatabase!.AddDocumentsAsync(_embeddingModel!, documents);
}

[Test]
public async Task can_query_image_against_images()
{
await setup_image_tests();
setup_image_tests();

var path = Path.Combine(Path.GetTempPath(), "test_image.jpg");
var imageData = await File.ReadAllBytesAsync(path);
Expand All @@ -104,22 +102,21 @@ public async Task can_query_image_against_images()
Strings = new List<string>(),
Images = new List<Data> { Data.FromBytes(binaryData.ToArray()) }
};
var embedding = await _embeddings!.CreateEmbeddingsAsync(embeddingRequest)
var embedding = await _embeddingModel!.CreateEmbeddingsAsync(embeddingRequest)
.ConfigureAwait(false);

var floats = embedding.ToSingleArray();
IEnumerable<Document> similaritySearchByVectorAsync = await (_vectorStore?.SimilaritySearchByVectorAsync(floats)!).ConfigureAwait(false);
var similaritySearchByVectorAsync = await _vectorDatabase!.SearchAsync(floats).ConfigureAwait(false);

Console.WriteLine("Count: " + similaritySearchByVectorAsync.Count());
Console.WriteLine("Count: " + similaritySearchByVectorAsync.Items.Count);
}

[Test]
public async Task can_query_text_against_images()
{
await setup_image_tests();
setup_image_tests();

var llm = new Claude3SonnetModel(_provider!);
var index = new VectorStoreIndexWrapper(_vectorStore!);

var promptText =
@"Use the following pieces of context to answer the question at the end. If the answer is not in context then just say that you don't know, don't try to make up an answer. Keep the answer as short as possible.
Expand All @@ -131,7 +128,7 @@ public async Task can_query_text_against_images()

var chain =
Set("tell me about the orange shirt", outputKey: "question") // set the question
| RetrieveDocuments(index, inputKey: "question", outputKey: "documents", amount: 10) // take 5 most similar documents
| RetrieveDocuments(_vectorDatabase!, _embeddingModel!, inputKey: "question", outputKey: "documents", amount: 10) // take 5 most similar documents
| StuffDocuments(inputKey: "documents", outputKey: "context") // combine documents together and put them into context
| Template(promptText) // replace context and question in the prompt with their values
| LLM(llm); // send the result to the language model
Expand All @@ -144,7 +141,7 @@ public async Task can_query_text_against_images()

#region Query Simple Documents

public Task setup_document_tests()
public void setup_document_tests()
{
_indexName = "test-index";
var username = Environment.GetEnvironmentVariable("OPENSEARCH_USERNAME");
Expand Down Expand Up @@ -172,13 +169,10 @@ public Task setup_document_tests()
_vectorDatabase = new OpenSearchVectorStore(_options);
}

return Task.CompletedTask;
}

[Test]
public async Task index_test_documents()
{
await setup_document_tests();
setup_document_tests();

var documents = new[]
{
Expand All @@ -196,10 +190,9 @@ public async Task index_test_documents()
[Test]
public async Task can_query_test_documents()
{
await setup_document_tests();
setup_document_tests();

var llm = new Claude3SonnetModel(_provider!);
var index = new VectorStoreIndexWrapper(_vectorStore!);

const string question = "what color is the car?";

Expand Down Expand Up @@ -229,7 +222,7 @@ public async Task can_query_test_documents()
[Test]
public async Task index_harry_potter_book()
{
await setup_document_tests();
setup_document_tests();

var pdfSource = new PdfPigPdfSource("x:\\Harry-Potter-Book-1.pdf");
var documents = await pdfSource.LoadAsync();
Expand All @@ -241,10 +234,9 @@ public async Task index_harry_potter_book()
[Test]
public async Task can_query_harry_potter_book()
{
await setup_document_tests();
setup_document_tests();

var llm = new Claude3SonnetModel(_provider!);
var index = new VectorStoreIndexWrapper(_vectorStore!);

var promptText =
@"Use the following pieces of context to answer the question at the end. If the answer is not in context then just say that you don't know, don't try to make up an answer. Keep the answer as short as possible.
Expand Down

0 comments on commit dc3c1cf

Please sign in to comment.