Skip to content

.Net: Update BraveTextSearch to use new ITextSearch #11568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions dotnet/src/IntegrationTests/Data/BaseTextSearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ namespace SemanticKernel.IntegrationTests.Data;
/// </summary>
public abstract class BaseTextSearchTests : BaseIntegrationTest
{
[Fact(Skip = "For manual verification only.")]
private const string SkipReason = "For manual verification only.";

[Fact(Skip = SkipReason)]
public virtual async Task CanSearchAsync()
{
// Arrange
Expand All @@ -42,7 +44,7 @@ public virtual async Task CanSearchAsync()
}
}

[Fact(Skip = "For manual verification only.")]
[Fact(Skip = SkipReason)]
public virtual async Task CanGetTextSearchResultsAsync()
{
// Arrange
Expand Down Expand Up @@ -72,7 +74,7 @@ public virtual async Task CanGetTextSearchResultsAsync()
}
}

[Fact(Skip = "For manual verification only.")]
[Fact(Skip = SkipReason)]
public virtual async Task CanGetSearchResultsAsync()
{
// Arrange
Expand All @@ -92,7 +94,7 @@ public virtual async Task CanGetSearchResultsAsync()
Assert.True(this.VerifySearchResults(results, query));
}

[Fact(Skip = "For manual verification only.")]
[Fact(Skip = SkipReason)]
public virtual async Task UsingTextSearchWithAFilterAsync()
{
// Arrange
Expand All @@ -113,7 +115,7 @@ public virtual async Task UsingTextSearchWithAFilterAsync()
Assert.True(this.VerifySearchResults(results, query, filter));
}

[Fact(Skip = "For manual verification only.")]
[Fact(Skip = SkipReason)]
public virtual async Task FunctionCallingUsingCreateWithSearchAsync()
{
// Arrange
Expand Down Expand Up @@ -142,7 +144,7 @@ public virtual async Task FunctionCallingUsingCreateWithSearchAsync()
Assert.NotEmpty(results);
}

[Fact(Skip = "For manual verification only.")]
[Fact(Skip = SkipReason)]
public virtual async Task FunctionCallingUsingCreateWithGetSearchResultsAsync()
{
// Arrange
Expand Down Expand Up @@ -171,7 +173,7 @@ public virtual async Task FunctionCallingUsingCreateWithGetSearchResultsAsync()
Assert.NotEmpty(results);
}

[Fact(Skip = "For manual verification only.")]
[Fact(Skip = SkipReason)]
public virtual async Task FunctionCallingUsingGetTextSearchResultsAsync()
{
// Arrange
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.SemanticKernel.Data;
using Microsoft.SemanticKernel.Plugins.Web.Brave;
using SemanticKernel.IntegrationTests.Data;
using SemanticKernel.IntegrationTests.TestSettings;
using Xunit;

namespace SemanticKernel.IntegrationTests.Plugins.Web.Brave;

/// <summary>
/// Integration tests for <see cref="BraveTextSearch"/>.
/// </summary>
public class BraveTextSearchTests : BaseTextSearchTests
{
/// <inheritdoc/>
public override Task<ITextSearch> CreateTextSearchAsync()
{
var configuration = this.Configuration.GetSection("Brave").Get<BraveConfiguration>();
Assert.NotNull(configuration);
Assert.NotNull(configuration.ApiKey);

return Task.FromResult<ITextSearch>(new BraveTextSearch(apiKey: configuration.ApiKey));
}

/// <inheritdoc/>
public override string GetQuery() => "What is the Semantic Kernel?";

/// <inheritdoc/>
public override TextSearchFilter GetTextSearchFilter() => new TextSearchFilter().Equality("search_lang", "de");

/// <inheritdoc/>
public override bool VerifySearchResults(object[] results, string query, TextSearchFilter? filter = null)
{
Assert.NotNull(results);
Assert.NotEmpty(results);
Assert.Equal(4, results.Length);
foreach (var result in results)
{
Assert.NotNull(result);
Assert.IsType<BraveWebResult>(result);
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) Microsoft. All rights reserved.
namespace SemanticKernel.IntegrationTests.TestSettings;

#pragma warning disable CA1812 // Configuration classes are instantiated through IConfiguration.
internal sealed class BraveConfiguration(string apiKey)
{
public string ApiKey { get; init; } = apiKey;
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ public async Task SearchReturnsSuccessfullyAsync()
var textSearch = new BraveTextSearch(apiKey: "ApiKey", options: new() { HttpClient = this._httpClient });

// Act
KernelSearchResults<string> result = await textSearch.SearchAsync("What is the Semantic Kernel?", new() { Top = 10, Skip = 0 });
var results = textSearch.SearchAsync("What is the Semantic Kernel?", top: 10, new() { Skip = 0 });

// Assert
Assert.NotNull(result);
Assert.NotNull(result.Results);
var resultList = await result.Results.ToListAsync();
Assert.NotNull(results);
var resultList = await results.ToListAsync();
Assert.NotNull(resultList);
Assert.Equal(10, resultList.Count);
foreach (var stringResult in resultList)
Expand All @@ -74,12 +73,11 @@ public async Task GetTextSearchResultsReturnsSuccessfullyAsync()
var textSearch = new BraveTextSearch(apiKey: "ApiKey", options: new() { HttpClient = this._httpClient });

// Act
KernelSearchResults<TextSearchResult> result = await textSearch.GetTextSearchResultsAsync("What is the Semantic Kernel?", new() { Top = 10, Skip = 0 });
var results = textSearch.GetTextSearchResultsAsync("What is the Semantic Kernel?", top: 10, new() { Skip = 0 });

// Assert
Assert.NotNull(result);
Assert.NotNull(result.Results);
var resultList = await result.Results.ToListAsync();
Assert.NotNull(results);
var resultList = await results.ToListAsync();
Assert.NotNull(resultList);
Assert.Equal(10, resultList.Count);
foreach (var textSearchResult in resultList)
Expand All @@ -100,12 +98,11 @@ public async Task GetSearchResultsReturnsSuccessfullyAsync()
var textSearch = new BraveTextSearch(apiKey: "ApiKey", options: new() { HttpClient = this._httpClient });

// Act
KernelSearchResults<object> result = await textSearch.GetSearchResultsAsync("What is the Semantic Kernel?", new() { Top = 10, Skip = 0 });
var results = textSearch.GetSearchResultsAsync("What is the Semantic Kernel?", top: 10, new() { Skip = 0 });

// Assert
Assert.NotNull(result);
Assert.NotNull(result.Results);
var resultList = await result.Results.ToListAsync();
Assert.NotNull(results);
var resultList = await results.ToListAsync();
Assert.NotNull(resultList);
Assert.Equal(10, resultList.Count);
foreach (BraveWebResult webPage in resultList)
Expand All @@ -126,12 +123,11 @@ public async Task SearchWithCustomStringMapperReturnsSuccessfullyAsync()
var textSearch = new BraveTextSearch(apiKey: "ApiKey", options: new() { HttpClient = this._httpClient, StringMapper = new TestTextSearchStringMapper() });

// Act
KernelSearchResults<string> result = await textSearch.SearchAsync("What is the Semantic Kernel?", new() { Top = 10, Skip = 0 });
var results = textSearch.SearchAsync("What is the Semantic Kernel?", top: 10, new() { Skip = 0 });

// Assert
Assert.NotNull(result);
Assert.NotNull(result.Results);
var resultList = await result.Results.ToListAsync();
Assert.NotNull(results);
var resultList = await results.ToListAsync();
Assert.NotNull(resultList);
Assert.Equal(10, resultList.Count);
foreach (var stringResult in resultList)
Expand All @@ -152,12 +148,11 @@ public async Task GetTextSearchResultsWithCustomResultMapperReturnsSuccessfullyA
var textSearch = new BraveTextSearch(apiKey: "ApiKey", options: new() { HttpClient = this._httpClient, ResultMapper = new TestTextSearchResultMapper() });

// Act
KernelSearchResults<TextSearchResult> result = await textSearch.GetTextSearchResultsAsync("What is the Semantic Kernel?", new() { Top = 10, Skip = 0 });
var results = textSearch.GetTextSearchResultsAsync("What is the Semantic Kernel?", top: 10, new() { Skip = 0 });

// Assert
Assert.NotNull(result);
Assert.NotNull(result.Results);
var resultList = await result.Results.ToListAsync();
Assert.NotNull(results);
var resultList = await results.ToListAsync();
Assert.NotNull(resultList);
Assert.Equal(10, resultList.Count);
foreach (var textSearchResult in resultList)
Expand Down Expand Up @@ -192,8 +187,8 @@ public async Task BuildsCorrectUriForEqualityFilterAsync(string paramName, objec
var textSearch = new BraveTextSearch(apiKey: "ApiKey", options: new() { HttpClient = this._httpClient });

// Act
TextSearchOptions searchOptions = new() { Top = 5, Skip = 0, Filter = new TextSearchFilter().Equality(paramName, paramValue) };
KernelSearchResults<object> result = await textSearch.GetSearchResultsAsync("What is the Semantic Kernel?", searchOptions);
TextSearchOptions searchOptions = new() { Skip = 0, Filter = new TextSearchFilter().Equality(paramName, paramValue) };
var results = await textSearch.GetSearchResultsAsync("What is the Semantic Kernel?", top: 5, searchOptions).ToListAsync();

// Assert
var requestUris = this._messageHandlerStub.RequestUris;
Expand All @@ -207,13 +202,13 @@ public async Task DoesNotBuildsUriForInvalidQueryParameterAsync()
{
// Arrange
this._messageHandlerStub.AddJsonResponse(File.ReadAllText(SiteFilterSkResponseJson));
TextSearchOptions searchOptions = new() { Top = 5, Skip = 0, Filter = new TextSearchFilter().Equality("fooBar", "Baz") };
TextSearchOptions searchOptions = new() { Skip = 0, Filter = new TextSearchFilter().Equality("fooBar", "Baz") };

// Create an ITextSearch instance using Brave search
var textSearch = new BraveTextSearch(apiKey: "ApiKey", options: new() { HttpClient = this._httpClient });

// Act && Assert
var e = await Assert.ThrowsAsync<ArgumentException>(async () => await textSearch.GetSearchResultsAsync("What is the Semantic Kernel?", searchOptions));
var e = await Assert.ThrowsAsync<ArgumentException>(async () => await textSearch.GetSearchResultsAsync("What is the Semantic Kernel?", top: 5, searchOptions).ToListAsync());
Assert.Equal("Unknown equality filter clause field name 'fooBar', must be one of country,search_lang,ui_lang,safesearch,text_decorations,spellcheck,result_filter,units,extra_snippets (Parameter 'searchOptions')", e.Message);
}

Expand All @@ -222,13 +217,13 @@ public async Task DoesNotBuildsUriForQueryParameterNullInputAsync()
{
// Arrange
this._messageHandlerStub.AddJsonResponse(File.ReadAllText(SiteFilterSkResponseJson));
TextSearchOptions searchOptions = new() { Top = 5, Skip = 0, Filter = new TextSearchFilter().Equality("country", null!) };
TextSearchOptions searchOptions = new() { Skip = 0, Filter = new TextSearchFilter().Equality("country", null!) };

// Create an ITextSearch instance using Brave search
var textSearch = new BraveTextSearch(apiKey: "ApiKey", options: new() { HttpClient = this._httpClient });

// Act && Assert
var e = await Assert.ThrowsAsync<ArgumentException>(async () => await textSearch.GetSearchResultsAsync("What is the Semantic Kernel?", searchOptions));
var e = await Assert.ThrowsAsync<ArgumentException>(async () => await textSearch.GetSearchResultsAsync("What is the Semantic Kernel?", top: 5, searchOptions).ToListAsync());
Assert.Equal("Unknown equality filter clause field name 'country', must be one of country,search_lang,ui_lang,safesearch,text_decorations,spellcheck,result_filter,units,extra_snippets (Parameter 'searchOptions')", e.Message);
}

Expand Down
Loading
Loading