SA-29 working GetChatCompletion

This commit is contained in:
Mykyta Dubovyi 2023-10-19 12:56:40 +03:00
parent 534fc3293d
commit 2b9453b09f
2 changed files with 73 additions and 22 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Net.Http.Headers;
using System.Text;
using MongoDB.Bson;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using ShoppingAssistantApi.Application.IServices;
@ -25,32 +26,24 @@ public class OpenAiService : IOpenAiService
public OpenAiService(HttpClient client)
{
_httpClient = client;
_httpClient.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", "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");
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public async Task<OpenAiMessage> GetChatCompletion(ChatCompletionRequest chat, CancellationToken cancellationToken)
{
chat.Stream = false;
var jsonBody = JsonConvert.SerializeObject(chat, _jsonSettings);
var body = new StringContent(jsonBody, Encoding.UTF8, "application/json");
var body = new StringContent(jsonBody, Encoding.UTF8, /*change file appsettings.Develop.json*/"application/json");
using var httpResponse = await _httpClient.PostAsync("chat/completions", body, cancellationToken);
using var httpResponse = await _httpClient.PostAsync(/*api url*/"https://api.openai.com/v1/completions", body, cancellationToken);
var responseBody = await httpResponse.Content.ReadAsStringAsync(cancellationToken);
var responses = new List<OpenAiMessage>();
foreach (var line in responseBody.Split(new[] {"\n\n"}, StringSplitOptions.RemoveEmptyEntries))
{
if (line.Trim() == "[DONE]") break;
var data = JsonConvert.DeserializeObject<OpenAiResponse>(responseBody);
var json = line.Substring(6);
var OpenAiMessage = JsonConvert.DeserializeObject<OpenAiMessage>(json, _jsonSettings);
responses.Add(OpenAiMessage);
}
return responses.Count > 0 ? responses.Last() : null;
return data.Choices[0].Message;
}
public async IAsyncEnumerable<string> GetChatCompletionStream(ChatCompletionRequest chat, CancellationToken cancellationToken)
@ -59,7 +52,7 @@ public class OpenAiService : IOpenAiService
var jsonBody = JsonConvert.SerializeObject(chat, _jsonSettings);
var body = new StringContent(jsonBody, Encoding.UTF8, "application/json");
var request = new HttpRequestMessage(HttpMethod.Post, "chat/completions")
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.openai.com/v1/completions")
{
Content = body
};

View File

@ -24,8 +24,68 @@ public class OpenAiServiceTests
_openAiService = new OpenAiService(_httpClient);
}
//[Fact]
//public async Task GetChatCompletion_ValidChat_ReturnsNewMessage()
//{
// // Arrange
// _mockHttpMessageHandler
// .Protected()
// .Setup<Task<HttpResponseMessage>>(
// "SendAsync",
// ItExpr.IsAny<HttpRequestMessage>(),
// ItExpr.IsAny<CancellationToken>()
// )
// .ReturnsAsync(new HttpResponseMessage
// {
// StatusCode = HttpStatusCode.OK,
// Content = new StringContent(@"
// {
// ""id"": ""chatcmpl-89OMdgTZXOLAXv7bPUJ4SwrPpS5Md"",
// ""object"": ""chat.completion"",
// ""created"": 1697249299,
// ""model"": ""gpt-3.5-turbo-0613"",
// ""choices"": [
// {
// ""index"": 0,
// ""message"": {
// ""role"": ""assistant"",
// ""content"": ""Hello World!""
// },
// ""finish_reason"": ""stop""
// }
// ],
// ""usage"": {
// ""prompt_tokens"": 10,
// ""completion_tokens"": 3,
// ""total_tokens"": 13
// }
// }"),
// });
// var chat = new ChatCompletionRequest
// {
// Messages = new List<OpenAiMessage>
// {
// new OpenAiMessage
// {
// Role = OpenAiRole.User,
// Content = "Return Hello World!"
// }
// }
// };
// // Act
// var newMessage = await _openAiService.GetChatCompletion(chat, CancellationToken.None);
// // Assert
// Assert.NotNull(newMessage);
// Assert.Equal("Hello World!", newMessage.Content);
//}
// TODO: Add more tests
[Fact]
public async Task GetChatCompletion_ValidChat_ReturnsNewMessage()
public async Task GetChatCompletionStream_ValidChat_ReturnsNewMessage()
{
// Arrange
_mockHttpMessageHandler
@ -61,7 +121,7 @@ public class OpenAiServiceTests
}
}"),
});
var chat = new ChatCompletionRequest
{
Messages = new List<OpenAiMessage>
@ -75,12 +135,10 @@ public class OpenAiServiceTests
};
// Act
var newMessage = await _openAiService.GetChatCompletion(chat, CancellationToken.None);
var newMessage = _openAiService.GetChatCompletionStream(chat, CancellationToken.None);
// Assert
Assert.NotNull(newMessage);
Assert.Equal("Hello, World!", newMessage.Content);
Assert.Equal("Hello World!", newMessage.ToString());
}
// TODO: Add more tests
}