Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Chat Completions] Request payload definition #21 #263

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
38 changes: 38 additions & 0 deletions src/AzureOpenAIProxy.ApiApp/Models/CreateChatCompletionRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using System.Text.Json;

namespace AzureOpenAIProxy.ApiApp.Models
{
public class CreateChatCompletionRequest
{
[JsonPropertyName("messages"), Required]
public List<ChatCompletionRequestMessage>? Messages { get; set; }

}

public class ChatCompletionRequestMessage
{
[JsonPropertyName("role"), Required]
public ChatCompletionRequestMessageRole? Role { get; set; }

[JsonPropertyName("content")]
public string? Content { get; set; }
}

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum ChatCompletionRequestMessageRole
{
[JsonPropertyName("system")]
System,

[JsonPropertyName("user")]
User,

[JsonPropertyName("assistant")]
Assistant,

[JsonPropertyName("tool")]
Tool,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using AzureOpenAIProxy.ApiApp.Models;



using System.Text.Json;


namespace AzureOpenAIProxy.ApiApp.Tests.Models
{
public class CreateChatCompletionRequestTests
{
[Fact]
public void Given_ChatCompletionsJson_When_Deserialized_Then_ShouldReturnCreateChatCompletionRequest()
{
// Arrange
var json = @"
{
""messages"": [
{
""role"": ""system"",
""content"": ""you are a helpful assistant that talks like a pirate""
},
{
""role"": ""user"",
""content"": ""can you tell me how to care for a parrot?""
}
]
}";

// Act
var result = JsonSerializer.Deserialize<CreateChatCompletionRequest>(json);

// Assert
Assert.NotNull(result);
Assert.NotNull(result.Messages); // Ensure Messages is not null
Assert.Equal(2, result.Messages.Count);

var firstMessage = result.Messages[0];
Assert.Equal(ChatCompletionRequestMessageRole.System, firstMessage.Role);
Assert.Equal("you are a helpful assistant that talks like a pirate", firstMessage.Content);

var secondMessage = result.Messages[1];
Assert.Equal(ChatCompletionRequestMessageRole.User, secondMessage.Role);
Assert.Equal("can you tell me how to care for a parrot?", secondMessage.Content);
}
}
}
Loading