|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.ClientModel; |
| 5 | +using System.ClientModel.Primitives; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using System.Net.Http; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Microsoft.SemanticKernel; |
| 11 | +using Microsoft.SemanticKernel.Agents.OpenAI; |
| 12 | +using Microsoft.SemanticKernel.ChatCompletion; |
| 13 | +using OpenAI; |
| 14 | +using OpenAI.Responses; |
| 15 | +using Xunit; |
| 16 | + |
| 17 | +namespace SemanticKernel.Agents.UnitTests.OpenAI; |
| 18 | + |
| 19 | +/// <summary> |
| 20 | +/// Unit testing of <see cref="OpenAIResponsesAgent"/>. |
| 21 | +/// </summary> |
| 22 | +#pragma warning disable CS0419 // Ambiguous reference in cref attribute |
| 23 | +public sealed class OpenAIResponsesAgentTests : IDisposable |
| 24 | +{ |
| 25 | + /// <summary> |
| 26 | + /// Initializes a new instance of objects required to test a <see cref="OpenAIResponsesAgent"/>. |
| 27 | + /// </summary> |
| 28 | + public OpenAIResponsesAgentTests() |
| 29 | + { |
| 30 | + this._messageHandlerStub = new MultipleHttpMessageHandlerStub(); |
| 31 | + this._httpClient = new HttpClient(this._messageHandlerStub, disposeHandler: false); |
| 32 | + this._kernel = new Kernel(); |
| 33 | + |
| 34 | + var options = new OpenAIClientOptions |
| 35 | + { |
| 36 | + Transport = new HttpClientPipelineTransport(this._httpClient) |
| 37 | + }; |
| 38 | + this._client = new(model: "gpt-4o", credential: new ApiKeyCredential("apikey"), options: options); |
| 39 | + } |
| 40 | + |
| 41 | + /// <inheritdoc/> |
| 42 | + public void Dispose() |
| 43 | + { |
| 44 | + this._messageHandlerStub.Dispose(); |
| 45 | + this._httpClient.Dispose(); |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Verify InvokeAsync |
| 50 | + /// </summary> |
| 51 | + [Fact] |
| 52 | + public async Task VerifyInvokeAsync() |
| 53 | + { |
| 54 | + // Arrange |
| 55 | + this._messageHandlerStub.AddJsonResponse(File.ReadAllText(WhatIsTheSKResponseJson)); |
| 56 | + OpenAIResponsesAgent agent = new(this._client) |
| 57 | + { |
| 58 | + Name = "ResponseAgent", |
| 59 | + Instructions = "Answer all queries in English and French.", |
| 60 | + }; |
| 61 | + |
| 62 | + // Act |
| 63 | + var responseItems = agent.InvokeAsync(new ChatMessageContent(AuthorRole.User, "What is the capital of France?")); |
| 64 | + |
| 65 | + // Assert |
| 66 | + Assert.NotNull(responseItems); |
| 67 | + var messages = await responseItems.ToListAsync(); |
| 68 | + Assert.Single(messages); |
| 69 | + Assert.Equal("The capital of France is Paris.\n\nLa capitale de la France est Paris.", messages[0].Message.Content); |
| 70 | + } |
| 71 | + |
| 72 | + #region private |
| 73 | + private const string WhatIsTheSKResponseJson = "./TestData/openai_responses_what_is_the_semantic_kernel.json"; |
| 74 | + |
| 75 | + private readonly MultipleHttpMessageHandlerStub _messageHandlerStub; |
| 76 | + private readonly HttpClient _httpClient; |
| 77 | + private readonly Kernel _kernel; |
| 78 | + private readonly OpenAIResponseClient _client; |
| 79 | + #endregion |
| 80 | +} |
0 commit comments