mirror of
https://github.com/Shchoholiev/shopping-assistant-api.git
synced 2025-04-12 01:48:49 +00:00
Merge pull request #9 from Shchoholiev/feature/SA-29-open-ai-service
Feature/sa 29 open ai service
This commit is contained in:
commit
e265257a2a
@ -1,31 +0,0 @@
|
|||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
|
|
||||||
namespace ShoppingAssistantApi.Api.Controllers;
|
|
||||||
[ApiController]
|
|
||||||
[Route("[controller]")]
|
|
||||||
public class WeatherForecastController : ControllerBase
|
|
||||||
{
|
|
||||||
private static readonly string[] Summaries = new[]
|
|
||||||
{
|
|
||||||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
|
||||||
};
|
|
||||||
|
|
||||||
private readonly ILogger<WeatherForecastController> _logger;
|
|
||||||
|
|
||||||
public WeatherForecastController(ILogger<WeatherForecastController> logger)
|
|
||||||
{
|
|
||||||
_logger = logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet(Name = "GetWeatherForecast")]
|
|
||||||
public IEnumerable<WeatherForecast> Get()
|
|
||||||
{
|
|
||||||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
|
|
||||||
{
|
|
||||||
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
|
||||||
TemperatureC = Random.Shared.Next(-20, 55),
|
|
||||||
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
|
|
||||||
})
|
|
||||||
.ToArray();
|
|
||||||
}
|
|
||||||
}
|
|
@ -12,6 +12,7 @@ builder.Services.AddJWTTokenAuthentication(builder.Configuration);
|
|||||||
builder.Services.AddMapper();
|
builder.Services.AddMapper();
|
||||||
builder.Services.AddInfrastructure();
|
builder.Services.AddInfrastructure();
|
||||||
builder.Services.AddServices();
|
builder.Services.AddServices();
|
||||||
|
builder.Services.AddHttpClient(builder.Configuration);
|
||||||
builder.Services.AddGraphQl();
|
builder.Services.AddGraphQl();
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
|
|
||||||
|
@ -21,4 +21,8 @@
|
|||||||
<ProjectReference Include="..\ShoppingAssistantApi.Persistance\ShoppingAssistantApi.Persistance.csproj" />
|
<ProjectReference Include="..\ShoppingAssistantApi.Persistance\ShoppingAssistantApi.Persistance.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="Controllers\" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -1,12 +0,0 @@
|
|||||||
namespace ShoppingAssistantApi.Api;
|
|
||||||
|
|
||||||
public class WeatherForecast
|
|
||||||
{
|
|
||||||
public DateOnly Date { get; set; }
|
|
||||||
|
|
||||||
public int TemperatureC { get; set; }
|
|
||||||
|
|
||||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
|
||||||
|
|
||||||
public string? Summary { get; set; }
|
|
||||||
}
|
|
@ -0,0 +1,12 @@
|
|||||||
|
namespace ShoppingAssistantApi.Application.Models.OpenAi;
|
||||||
|
|
||||||
|
public class OpenAiChoice
|
||||||
|
{
|
||||||
|
public OpenAiMessage Message { get; set; }
|
||||||
|
|
||||||
|
public OpenAiDelta Delta { get; set; }
|
||||||
|
|
||||||
|
public string FinishReason { get; set; }
|
||||||
|
|
||||||
|
public int Index { get; set; }
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
namespace ShoppingAssistantApi.Application.Models.OpenAi;
|
||||||
|
|
||||||
|
public class OpenAiDelta
|
||||||
|
{
|
||||||
|
public string Role { get; set; }
|
||||||
|
|
||||||
|
public string Content { get; set; }
|
||||||
|
}
|
@ -4,7 +4,7 @@ namespace ShoppingAssistantApi.Application.Models.OpenAi;
|
|||||||
|
|
||||||
public class OpenAiMessage
|
public class OpenAiMessage
|
||||||
{
|
{
|
||||||
public OpenAiRole Role { get; set; }
|
public string Role { get; set; }
|
||||||
|
|
||||||
public string Content { get; set; }
|
public string Content { get; set; }
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,16 @@
|
|||||||
|
namespace ShoppingAssistantApi.Application.Models.OpenAi;
|
||||||
|
|
||||||
|
public class OpenAiResponse
|
||||||
|
{
|
||||||
|
public string Id { get; set; }
|
||||||
|
|
||||||
|
public string Object { get; set; }
|
||||||
|
|
||||||
|
public int Created { get; set; }
|
||||||
|
|
||||||
|
public string Model { get; set; }
|
||||||
|
|
||||||
|
public OpenAiUsage Usage { get; set; }
|
||||||
|
|
||||||
|
public List<OpenAiChoice> Choices { get; set; }
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
namespace ShoppingAssistantApi.Application.Models.OpenAi;
|
||||||
|
|
||||||
|
public class OpenAiUsage
|
||||||
|
{
|
||||||
|
public int PromptTokens { get; set; }
|
||||||
|
|
||||||
|
public int CompletionTokens { get; set; }
|
||||||
|
|
||||||
|
public int TotalTokens { get; set; }
|
||||||
|
}
|
@ -6,3 +6,21 @@ public enum OpenAiRole
|
|||||||
User,
|
User,
|
||||||
Assistant
|
Assistant
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class OpenAiRoleExtensions
|
||||||
|
{
|
||||||
|
public static string RequestConvert(this OpenAiRole role)
|
||||||
|
{
|
||||||
|
switch (role)
|
||||||
|
{
|
||||||
|
case OpenAiRole.System:
|
||||||
|
return "system";
|
||||||
|
case OpenAiRole.Assistant:
|
||||||
|
return "assistant";
|
||||||
|
case OpenAiRole.User:
|
||||||
|
return "user";
|
||||||
|
default:
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,10 @@
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using ShoppingAssistantApi.Application.IServices;
|
using ShoppingAssistantApi.Application.IServices;
|
||||||
using ShoppingAssistantApi.Application.IServices.Identity;
|
using ShoppingAssistantApi.Application.IServices.Identity;
|
||||||
using ShoppingAssistantApi.Infrastructure.Services;
|
using ShoppingAssistantApi.Infrastructure.Services;
|
||||||
using ShoppingAssistantApi.Infrastructure.Services.Identity;
|
using ShoppingAssistantApi.Infrastructure.Services.Identity;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
|
||||||
namespace ShoppingAssistantApi.Infrastructure.InfrastructureExtentions;
|
namespace ShoppingAssistantApi.Infrastructure.InfrastructureExtentions;
|
||||||
public static class ServicesExtention
|
public static class ServicesExtention
|
||||||
@ -15,6 +17,21 @@ public static class ServicesExtention
|
|||||||
services.AddScoped<ITokensService, TokensService>();
|
services.AddScoped<ITokensService, TokensService>();
|
||||||
services.AddScoped<IUsersService, UsersService>();
|
services.AddScoped<IUsersService, UsersService>();
|
||||||
services.AddScoped<IWishlistsService, WishlistsService>();
|
services.AddScoped<IWishlistsService, WishlistsService>();
|
||||||
|
services.AddScoped<IOpenAiService, OpenAiService>();
|
||||||
|
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IServiceCollection AddHttpClient(this IServiceCollection services, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
services.AddHttpClient(
|
||||||
|
"OpenAiHttpClient",
|
||||||
|
client =>
|
||||||
|
{
|
||||||
|
client.BaseAddress = new Uri(configuration.GetValue<string>("ApiUri"));
|
||||||
|
client.DefaultRequestHeaders.Authorization =
|
||||||
|
new AuthenticationHeaderValue("Bearer", configuration.GetValue<string>("ApiKey"));
|
||||||
|
});
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,9 @@
|
|||||||
|
using System.IO;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Text;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using Newtonsoft.Json.Serialization;
|
||||||
using ShoppingAssistantApi.Application.IServices;
|
using ShoppingAssistantApi.Application.IServices;
|
||||||
using ShoppingAssistantApi.Application.Models.OpenAi;
|
using ShoppingAssistantApi.Application.Models.OpenAi;
|
||||||
|
|
||||||
@ -5,20 +11,61 @@ namespace ShoppingAssistantApi.Infrastructure.Services;
|
|||||||
|
|
||||||
public class OpenAiService : IOpenAiService
|
public class OpenAiService : IOpenAiService
|
||||||
{
|
{
|
||||||
|
|
||||||
|
private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
|
||||||
|
{
|
||||||
|
ContractResolver = new DefaultContractResolver
|
||||||
|
{
|
||||||
|
NamingStrategy = new SnakeCaseNamingStrategy()
|
||||||
|
},
|
||||||
|
NullValueHandling = NullValueHandling.Ignore,
|
||||||
|
};
|
||||||
|
|
||||||
private readonly HttpClient _httpClient;
|
private readonly HttpClient _httpClient;
|
||||||
|
|
||||||
public OpenAiService(HttpClient client)
|
public OpenAiService(IHttpClientFactory httpClientFactory)
|
||||||
{
|
{
|
||||||
_httpClient = client;
|
_httpClient = httpClientFactory.CreateClient("OpenAiHttpClient");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<OpenAiMessage> GetChatCompletion(ChatCompletionRequest chat, CancellationToken cancellationToken)
|
public async Task<OpenAiMessage> GetChatCompletion(ChatCompletionRequest chat, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
chat.Stream = false;
|
||||||
|
var jsonBody = JsonConvert.SerializeObject(chat, _jsonSettings);
|
||||||
|
var body = new StringContent(jsonBody, Encoding.UTF8, "application/json");
|
||||||
|
|
||||||
|
using var httpResponse = await _httpClient.PostAsync("", body, cancellationToken);
|
||||||
|
|
||||||
|
var responseBody = await httpResponse.Content.ReadAsStringAsync(cancellationToken);
|
||||||
|
|
||||||
|
var data = JsonConvert.DeserializeObject<OpenAiResponse>(responseBody);
|
||||||
|
|
||||||
|
return data.Choices[0].Message;
|
||||||
}
|
}
|
||||||
|
|
||||||
public IAsyncEnumerable<string> GetChatCompletionStream(ChatCompletionRequest chat, CancellationToken cancellationToken)
|
public async IAsyncEnumerable<string> GetChatCompletionStream(ChatCompletionRequest chat, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
chat.Stream = true;
|
||||||
|
var jsonBody = JsonConvert.SerializeObject(chat, _jsonSettings);
|
||||||
|
|
||||||
|
var body = new StringContent(jsonBody, Encoding.UTF8, "application/json");
|
||||||
|
|
||||||
|
using var httpResponse = await _httpClient.PostAsync("", body, cancellationToken);
|
||||||
|
|
||||||
|
using var responseStream = await httpResponse.Content.ReadAsStreamAsync(cancellationToken);
|
||||||
|
using var reader = new StreamReader(responseStream, Encoding.UTF8);
|
||||||
|
|
||||||
|
while (!cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
var jsonChunk = await reader.ReadLineAsync();
|
||||||
|
if (jsonChunk.StartsWith("data: "))
|
||||||
|
{
|
||||||
|
jsonChunk = jsonChunk.Substring("data: ".Length);
|
||||||
|
if (jsonChunk == "[DONE]") break;
|
||||||
|
var data = JsonConvert.DeserializeObject<OpenAiResponse>(jsonChunk);
|
||||||
|
if (data.Choices[0].Delta.Content == "" || data.Choices[0].Delta.Content == null) continue;
|
||||||
|
yield return data.Choices[0].Delta.Content;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -11,6 +11,7 @@
|
|||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
|
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
|
||||||
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.32.0" />
|
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.32.0" />
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.32.0" />
|
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.32.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
155
ShoppingAssistantApi.UnitTests/OpenAiServiceTests.cs
Normal file
155
ShoppingAssistantApi.UnitTests/OpenAiServiceTests.cs
Normal file
@ -0,0 +1,155 @@
|
|||||||
|
using System.Net;
|
||||||
|
using Moq;
|
||||||
|
using Moq.Protected;
|
||||||
|
using ShoppingAssistantApi.Application.IServices;
|
||||||
|
using ShoppingAssistantApi.Application.Models.OpenAi;
|
||||||
|
using ShoppingAssistantApi.Domain.Enums;
|
||||||
|
using ShoppingAssistantApi.Infrastructure.Services;
|
||||||
|
|
||||||
|
namespace ShoppingAssistantApi.UnitTests;
|
||||||
|
|
||||||
|
public class OpenAiServiceTests
|
||||||
|
{
|
||||||
|
private readonly IOpenAiService _openAiService;
|
||||||
|
|
||||||
|
private readonly Mock<HttpMessageHandler> _mockHttpMessageHandler;
|
||||||
|
|
||||||
|
private readonly Mock<IHttpClientFactory> _mockHttpClientFactory;
|
||||||
|
|
||||||
|
public OpenAiServiceTests()
|
||||||
|
{
|
||||||
|
_mockHttpClientFactory = new Mock<IHttpClientFactory>();
|
||||||
|
_mockHttpMessageHandler = new Mock<HttpMessageHandler>();
|
||||||
|
|
||||||
|
var client = new HttpClient(_mockHttpMessageHandler.Object);
|
||||||
|
client.BaseAddress = new Uri("https://www.google.com.ua/");
|
||||||
|
|
||||||
|
_mockHttpClientFactory
|
||||||
|
.Setup(factory => factory.CreateClient(It.IsAny<string>()))
|
||||||
|
.Returns(() =>
|
||||||
|
{
|
||||||
|
return client;
|
||||||
|
});
|
||||||
|
|
||||||
|
_openAiService = new OpenAiService(_mockHttpClientFactory.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[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.RequestConvert(),
|
||||||
|
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 GetChatCompletionStream_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.RequestConvert(),
|
||||||
|
Content = "Return Hello World!"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var newMessage = _openAiService.GetChatCompletionStream(chat, CancellationToken.None);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.NotNull(newMessage);
|
||||||
|
Assert.Equal("Hello World!", newMessage.ToString());
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user