Skip to content
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

.Net: Anthropic - streaming #8560

Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,28 @@
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.Anthropic;
using Microsoft.SemanticKernel.Connectors.Anthropic.Core;
using Microsoft.SemanticKernel.Connectors.Anthropic.Core.Models;
using Microsoft.SemanticKernel.Http;
using SemanticKernel.Connectors.Anthropic.UnitTests.Utils;
using Xunit;

namespace SemanticKernel.Connectors.Anthropic.UnitTests.Core;

/// <summary>
/// Test for <see cref="AnthropicClient"/>
/// </summary>
public sealed class AnthropicClientChatGenerationTests : IDisposable
public sealed class AnthropicChatGenerationTests : IDisposable
{
private readonly HttpClient _httpClient;
private readonly HttpMessageHandlerStub _messageHandlerStub;
private const string ChatTestDataFilePath = "./TestData/chat_one_response.json";

public AnthropicClientChatGenerationTests()
public AnthropicChatGenerationTests()
{
this._messageHandlerStub = new HttpMessageHandlerStub();
this._messageHandlerStub.ResponseToReturn.Content = new StringContent(
Expand Down Expand Up @@ -243,11 +244,13 @@ public async Task ShouldPassSystemMessageToRequestAsync()
}

[Fact]
public async Task ShouldPassVersionToRequestBodyIfCustomHandlerUsedAsync()
public async Task ShouldPassVersionToRequestBodyIfThirdVendorIsUsedAsync()
{
// Arrange
var options = new AnthropicClientOptions();
var client = new AnthropicClient("fake-model", "api-key", options: new(), httpClient: this._httpClient);
var options = new AmazonBedrockAnthropicClientOptions();
var client = new AnthropicClient("fake-model", new Uri("https://fake-uri.com"),
bearerTokenProvider: () => ValueTask.FromResult("fake-token"),
options: options, httpClient: this._httpClient);

var chatHistory = CreateSampleChatHistory();

Expand Down Expand Up @@ -390,7 +393,7 @@ public async Task ItCreatesRequestWithCustomUriAndCustomHeadersAsync(string head
{
// Arrange
Uri uri = new("https://fake-uri.com");
using var httpHandler = new CustomHeadersHandler(headerName, headerValue);
using var httpHandler = new CustomHeadersHandler(headerName, headerValue, ChatTestDataFilePath);
using var httpClient = new HttpClient(httpHandler);
httpClient.BaseAddress = uri;
var client = new AnthropicClient("fake-model", "api-key", options: new(), httpClient: httpClient);
Expand Down Expand Up @@ -439,40 +442,4 @@ public void Dispose()
this._httpClient.Dispose();
this._messageHandlerStub.Dispose();
}

private sealed class CustomHeadersHandler : DelegatingHandler
{
private readonly string _headerName;
private readonly string _headerValue;
public HttpRequestHeaders? RequestHeaders { get; private set; }

public HttpContentHeaders? ContentHeaders { get; private set; }

public byte[]? RequestContent { get; private set; }

public Uri? RequestUri { get; private set; }

public HttpMethod? Method { get; private set; }

public CustomHeadersHandler(string headerName, string headerValue)
{
this.InnerHandler = new HttpMessageHandlerStub
{
ResponseToReturn = { Content = new StringContent(File.ReadAllText(ChatTestDataFilePath)) }
};
this._headerName = headerName;
this._headerValue = headerValue;
}

protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
request.Headers.Add(this._headerName, this._headerValue);
this.Method = request.Method;
this.RequestUri = request.RequestUri;
this.RequestHeaders = request.Headers;
this.RequestContent = request.Content is null ? null : request.Content.ReadAsByteArrayAsync(cancellationToken).Result;

return base.SendAsync(request, cancellationToken);
}
}
}
Loading
Loading