From ef35d6dea28d20ddbaf0dded15fd0f3aa05ca18a Mon Sep 17 00:00:00 2001 From: Mykhailo Bilodid Date: Sun, 22 Oct 2023 22:04:08 +0300 Subject: [PATCH] SA-29 all bugs fixed --- .../Controllers/WeatherForecastController.cs | 93 ------------------- ShoppingAssistantApi.Api/Program.cs | 2 +- .../ShoppingAssistantApi.Api.csproj | 4 + ShoppingAssistantApi.Api/WeatherForecast.cs | 12 --- .../ServicesExtention.cs | 6 +- 5 files changed, 8 insertions(+), 109 deletions(-) delete mode 100644 ShoppingAssistantApi.Api/Controllers/WeatherForecastController.cs delete mode 100644 ShoppingAssistantApi.Api/WeatherForecast.cs diff --git a/ShoppingAssistantApi.Api/Controllers/WeatherForecastController.cs b/ShoppingAssistantApi.Api/Controllers/WeatherForecastController.cs deleted file mode 100644 index 39417a2..0000000 --- a/ShoppingAssistantApi.Api/Controllers/WeatherForecastController.cs +++ /dev/null @@ -1,93 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using ShoppingAssistantApi.Application.IServices; -using ShoppingAssistantApi.Application.Models.OpenAi; -using ShoppingAssistantApi.Domain.Enums; - -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 _logger; - - private readonly IOpenAiService _openAiService; - - public WeatherForecastController(ILogger logger, IOpenAiService openAiService) - { - _openAiService = openAiService; - _logger = logger; - } - - [HttpGet(Name = "GetWeatherForecast")] - public IEnumerable 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(); - } - - [HttpPost("open-ai-test-simple")] - public async Task OpenAiTest(string text) - { - return await _openAiService.GetChatCompletion(new ChatCompletionRequest - { - Messages = new List - { - new OpenAiMessage - { - Role = OpenAiRole.System.RequestConvert(), - Content = "You are a Shopping Assistant that helps people find product recommendations. Ask user additional questions if more context needed.\nYou must return data with one of the prefixes:\n[Question] - return question. Each question must have suggestions.\n[Options] - return semicolon separated suggestion how to answer to a question\n[Message] - return text\n[Products] - return semicolon separated product names" - }, - - new OpenAiMessage - { - Role = OpenAiRole.Assistant.RequestConvert(), - Content = "[Question] What are you looking for?\n[Options] Bicycle, Laptop" - }, - - new OpenAiMessage - { - Role = OpenAiRole.User.RequestConvert(), - Content = text - } - } - }, CancellationToken.None); - } - - [HttpPost("open-ai-test-streamed")] - public IAsyncEnumerable OpenAiTestStrean(string text) - { - return _openAiService.GetChatCompletionStream(new ChatCompletionRequest - { - Messages = new List - { - new OpenAiMessage - { - Role = OpenAiRole.System.RequestConvert(), - Content = "You are a Shopping Assistant that helps people find product recommendations. Ask user additional questions if more context needed.\nYou must return data with one of the prefixes:\n[Question] - return question. Each question must have suggestions.\n[Options] - return semicolon separated suggestion how to answer to a question\n[Message] - return text\n[Products] - return semicolon separated product names" - }, - - new OpenAiMessage - { - Role = OpenAiRole.Assistant.RequestConvert(), - Content = "[Question] What are you looking for?\n[Options] Bicycle, Laptop" - }, - - new OpenAiMessage - { - Role = OpenAiRole.User.RequestConvert(), - Content = text - } - } - }, CancellationToken.None); - } -} \ No newline at end of file diff --git a/ShoppingAssistantApi.Api/Program.cs b/ShoppingAssistantApi.Api/Program.cs index 2d9a9d6..79395dd 100644 --- a/ShoppingAssistantApi.Api/Program.cs +++ b/ShoppingAssistantApi.Api/Program.cs @@ -12,7 +12,7 @@ builder.Services.AddJWTTokenAuthentication(builder.Configuration); builder.Services.AddMapper(); builder.Services.AddInfrastructure(); builder.Services.AddServices(); -builder.Services.AddOpenAiHttpClient(builder.Configuration); +builder.Services.AddHttpClient(builder.Configuration); builder.Services.AddGraphQl(); builder.Services.AddControllers(); diff --git a/ShoppingAssistantApi.Api/ShoppingAssistantApi.Api.csproj b/ShoppingAssistantApi.Api/ShoppingAssistantApi.Api.csproj index 33cec29..50dd57a 100644 --- a/ShoppingAssistantApi.Api/ShoppingAssistantApi.Api.csproj +++ b/ShoppingAssistantApi.Api/ShoppingAssistantApi.Api.csproj @@ -21,4 +21,8 @@ + + + + diff --git a/ShoppingAssistantApi.Api/WeatherForecast.cs b/ShoppingAssistantApi.Api/WeatherForecast.cs deleted file mode 100644 index 360f533..0000000 --- a/ShoppingAssistantApi.Api/WeatherForecast.cs +++ /dev/null @@ -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; } -} diff --git a/ShoppingAssistantApi.Infrastructure/InfrastructureExtentions/ServicesExtention.cs b/ShoppingAssistantApi.Infrastructure/InfrastructureExtentions/ServicesExtention.cs index 028935a..cbc7b65 100644 --- a/ShoppingAssistantApi.Infrastructure/InfrastructureExtentions/ServicesExtention.cs +++ b/ShoppingAssistantApi.Infrastructure/InfrastructureExtentions/ServicesExtention.cs @@ -22,15 +22,15 @@ public static class ServicesExtention return services; } - public static IServiceCollection AddOpenAiHttpClient(this IServiceCollection services, IConfiguration configuration) + public static IServiceCollection AddHttpClient(this IServiceCollection services, IConfiguration configuration) { services.AddHttpClient( "OpenAiHttpClient", client => { - client.BaseAddress = new Uri(configuration.GetValue("OpenAi:OpenAiApiUri")); + client.BaseAddress = new Uri(configuration.GetValue("ApiUri")); client.DefaultRequestHeaders.Authorization = - new AuthenticationHeaderValue("Bearer", configuration.GetValue("OpenAi:OpenAiApiKey")); + new AuthenticationHeaderValue("Bearer", configuration.GetValue("ApiKey")); }); return services;