Skip to content

Commit

Permalink
Created the ChatController
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisIvanov committed Jun 11, 2024
2 parents 5704fba + 2dfadc4 commit c6aea6f
Show file tree
Hide file tree
Showing 12 changed files with 309 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public class Completions
{
public const string Instructions = "You are a helpful assistant that answers questions related to cooking tips, recipes, kitchen tips. \" +" +
public const string AssistantInstructions = "You are a helpful assistant that answers questions related to cooking tips, recipes, kitchen tips. \" +" +
"\r\nYou will receive queries containing different questions on cooking thematic or a list of products that you have to make use of and come up with a recipe for the user.\" +" +
"\r\nYou need to take into account the user's dietary needs and their allergies so that you do not suggest a recipe that includes unhealthy or inappropriate contents. \" +" +
"\r\nHere is a list of the user's allergies:";
Expand Down Expand Up @@ -54,5 +54,7 @@ public class Completions
"\r\nAdjust the seasoning according to your taste preference." +
"\r\nFeel free to add other herbs or spices that you like." +
"\r\nEnjoy your meal!";

public const string TitleGenerationPrompt = "Generate a title from this sentence:";
}
}
7 changes: 6 additions & 1 deletion src/server/CookingApp/Common/ExceptionMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ public static class ExceptionMessages
{
public const string NullOrEmptyInputValues = "The provided input contains either null or an empty value";
public const string SubscriptionCreationFail = "Failed to create a subscription. {0}";
public const string ResponseRequestFailed = "The ChatGPT API failed to respond. Please try again.";

public class ChatGPT
{
public const string ResponseError = "The ChatGPT Service failed to respond. Please try again.";
public const string ConnectionError = "Something went wrong. Follow the log for more information.";
}
}
}
10 changes: 10 additions & 0 deletions src/server/CookingApp/Common/SuccessMessages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace CookingApp.Common
{
public class SuccessMessages
{
public class ChatGPT
{
public const string ResponseSuccess = "Response received successully.";
}
}
}
10 changes: 10 additions & 0 deletions src/server/CookingApp/Common/TaskInformationMessages.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace CookingApp.Common
{
public class TaskInformationMessages
{
public class ChatGPT
{
public const string ConnectionAttempt = "Attempting to establish connection with ChatGPT API.";
}
}
}
72 changes: 72 additions & 0 deletions src/server/CookingApp/Controllers/ChatController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
namespace CookingApp.Controllers
{
using CookingApp.Common;
using CookingApp.Models.DTOs;
using CookingApp.Services.ChatHistory;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

[ApiController]
public class ChatController : ControllerBase
{
private readonly IChatService _chatService;
private readonly ILogger<ChatController> _logger;

public ChatController(IChatService chatService, ILogger<ChatController> logger)
{
_chatService = chatService;
_logger = logger;
}

[HttpGet("chats")]
public async Task<IActionResult> GetChats()
{
//TODO: implement Azure Entra Id functions
//var user = _userService.GetUser();
var chats = await _chatService.GetAllByUserId("user.Id");
return Ok(chats);
}

[HttpPost("chat-request")]
public async Task<IActionResult> SendQuery([FromBody] string message, [FromHeader] string? chatId = null)
{
try
{
if (!ModelState.IsValid)
{
return BadRequest();
}

_logger.LogInformation(TaskInformationMessages.ChatGPT.ConnectionAttempt);

var result =
String.IsNullOrEmpty(chatId)
? await _chatService.CreateChat(message)
: await _chatService.UpdateChat(message, chatId);

if (result == null)
{
_logger.LogError(ExceptionMessages.ChatGPT.ConnectionError);
_logger.LogError(ExceptionMessages.ChatGPT.ResponseError);

return NoContent();
}

_logger.LogInformation(SuccessMessages.ChatGPT.ResponseSuccess);
// To display the message you need to get into result.Choices[0].Message.Content.
// The chat id is also contained inside the result

return Ok(result);
}
catch (Exception e)
{
_logger.LogError(ExceptionMessages.ChatGPT.ConnectionError);
_logger.LogError($"{e.Message}");

return BadRequest();
}

return Ok();
}
}
}
54 changes: 0 additions & 54 deletions src/server/CookingApp/Controllers/ChatGPTController.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
using CookingApp.Services.Stripe;
using Stripe;
using CookingApp.Infrastructure.Configurations.Stripe;
using CookingApp.Services.OpenAI.Completions;
using OpenAI.Extensions;
using OpenAI;
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using CookingApp.Services.ChatHistory;

namespace CookingApp.Infrastructure.Extensions
{
Expand Down Expand Up @@ -148,7 +148,7 @@ public static IHostApplicationBuilder AddOpenAIIntegration(this WebApplicationBu
options.DefaultModelId = builder.Configuration.GetValue<string>("OpenAPIOptions:Model") ?? string.Empty;
});

builder.Services.AddScoped<ICompletionService, CompletionService>();
builder.Services.AddScoped<IChatService, ChatService>();

return builder;
}
Expand Down
18 changes: 18 additions & 0 deletions src/server/CookingApp/Models/DTOs/CreateChatDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace CookingApp.Models.DTOs
{
using MongoDB.Bson.Serialization.Attributes;

public class CreateChatDTO
{
public string Id { get; set; }
public string? Title { get; set; }

public string? UserId { get; set; }

public DateTime CreatedTime { get; set; }

public List<Request> Requests { get; set; } = new List<Request>();

public List<Response> Responses { get; set; } = new List<Response>();
}
}
Loading

0 comments on commit c6aea6f

Please sign in to comment.