OkGoDoIt / OpenAI-API-dotnet

An unofficial C#/.NET SDK for accessing the OpenAI GPT-3 API
https://www.nuget.org/packages/OpenAI/
Other
1.86k stars 430 forks source link

ChatMessage missing name of the user property, Moderation lacks array support #97

Closed Cerlancism closed 1 year ago

Cerlancism commented 1 year ago

According to the official OpenAI NodeJS library:

https://github.com/openai/openai-node/blob/51b1340e3182c4cb5d43a5b771606ecdd93a60f4/api.ts#L31-L50

On: https://github.com/OkGoDoIt/OpenAI-API-dotnet/blob/6661723d5c908533f0cf1fb4466ef844b3e60cc1/OpenAI_API/Chat/ChatMessage.cs#L21-L30

The optional name property of the user is not implemented. This is useful for group conversations.

I have tried on the NodeJS side, it is able to reference a particular user by the name in the assistant response if necessary. However, the property name must be in the form of ^[a-zA-Z0-9_-]{1,64}$, otherwise will result in a Bad Request (HTTP 400) error.


Similarly for Moderation, missing array input here but supported at NodeJS side: https://github.com/openai/openai-node/blob/51b1340e3182c4cb5d43a5b771606ecdd93a60f4/api.ts#L1168

Though I see the moderation result here is already implemented with array.

Cerlancism commented 1 year ago

Extending the classes for now seems to work.

ChatMessage

    public class ChatMessageWithName : ChatMessage
    {
        [JsonProperty("name")]
        public string Name { get; set; }

        public ChatMessageWithName(ChatMessageRole role, string name, string content): base (role, content)
        {
            Name = name;
        }
    }
    [TestClass()]
    public class ChatMessageWithNameTests
    {
        [TestMethod()]
        public async Task ChatMessageWithNameTest()
        {
            var api = new OpenAIAPI(APIAuthentication.LoadFromEnv());

            var results = await api.Chat.CreateChatCompletionAsync(new ChatRequest
            {
                MaxTokens = 256,
                Temperature = 1,
                Messages = new []
                {
                    new ChatMessage(ChatMessageRole.System, "Join the conversion in this work place chat"),
                    new ChatMessageWithName(ChatMessageRole.User, "John", "Hello everyone"),
                    new ChatMessageWithName(ChatMessageRole.User, "Edward", "Good morning all"),
                    new ChatMessageWithName(ChatMessageRole.User, "Boss", "Is John and Amy in the chat?"),
                }
            });

            var reply = results.Choices[0].Message;
            Console.WriteLine($"{reply.Role}: {reply.Content.Trim()}");
        }
    }

Output:

  Standard Output: 
assistant: I see John is here. However, Amy hasn't joined the chat yet. Maybe she hasn't seen the notification.

ModerationRequest

    public class ModerationRequestWithArray : ModerationRequest
    {
        [JsonProperty("input")]
        public new string[] Input { get; set; }

        public ModerationRequestWithArray(string[] input)
        {
            Model = OpenAI_API.Models.Model.TextModerationLatest;
            Input = input;
        }
    }
    [TestClass()]
    public class ModerationRequestWithArrayTests
    {
        [TestMethod()]
        public async Task ModerationRequestWithArrayTest()
        {
            var api = new OpenAIAPI(APIAuthentication.LoadFromEnv());

            var inputs = new[]
            {
                "Sentence A",
                "Sentence B"
            };

            var result = await api.Moderation.CallModerationAsync(new ModerationRequestWithArray(inputs));

            for (int i = 0; i < result.Results.Count; i++)
            {
                var item = result.Results[i];
                Console.WriteLine($"{inputs[i]} -> {item.Flagged}");
            }
        }
    }

Output

  Standard Output: 
Sentence A -> False
Sentence B -> False
OkGoDoIt commented 1 year ago

Interesting, the name part of chat messages is not in the official API documentation at https://platform.openai.com/docs/api-reference/chat/create and https://platform.openai.com/docs/guides/chat/introduction 🤷‍♂️

Thanks for the catch @Cerlancism!