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

[BUG] Azure.AI.OpenAI - Chat Completion - ChatCompletionsOptions.Messages is read only but shouldn't be #37195

Closed
ekoostikmartin opened this issue Jun 23, 2023 · 7 comments
Assignees
Labels
Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. issue-addressed The Azure SDK team member assisting with this issue believes it to be addressed and ready to close. OpenAI question The issue doesn't require a change to the product in order to be resolved. Most issues start as that

Comments

@ekoostikmartin
Copy link

ekoostikmartin commented Jun 23, 2023

Library name and version

Azure.AI.OpenAI 1.0.0-beta.5

Describe the bug

When creating a new ChatCompletionsOptions() object, cannot set the Messages property except with an object initializer. This makes it impossible to create an ongoing dynamic conversation which is feeding in the system and user responses to the service dynamically.

The comment in the code even says "Gets or sets" (see below)

    ///     Gets or sets the set of chat messages associated with a chat completions request.
    ///     This is the full set of chat messages for the history of a conversation between an assistant and user.
    ///     Typical usage begins with a chat message for the System role that provides instructions for the
    ///     behavior of the assistant followed by alternating messages between the User role and Assistant role.

    public IList<ChatMessage> Messages { get; }

Expected behavior

The Messages property should not be read only, it should allow "set".

Actual behavior

The Messages property is read only

Reproduction Steps

List chatMessageList = new List();

        chatMessageList.Add(new ChatMessage(ChatRole.System, "blah1"));
        chatMessageList.Add(new ChatMessage(ChatRole.User, "blah2"));

        Response<ChatCompletions> responseWithoutStream = await client.GetChatCompletionsAsync(
            "gpt-4",
            new ChatCompletionsOptions()
            {
                Messages = chatMessageList,
                Temperature = (float)0.7,
                MaxTokens = 800,
                NucleusSamplingFactor = (float)0.95,
                FrequencyPenalty = 0,
                PresencePenalty = 0,
            });

//CS0200 Property or indexer 'ChatCompletionsOptions.Messages' cannot be assigned to -- it is read only

Environment

No response

@github-actions github-actions bot added customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-triage This is a new issue that needs to be triaged to the appropriate team. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Jun 23, 2023
@zhuxingke
Copy link

zhuxingke commented Jun 25, 2023

OpenAIClient client = new OpenAIClient(new Uri(_chatGptOption.Value.Azure.EndPoint), new AzureKeyCredential(_chatGptOption.Value.Azure.ApiKey), new OpenAIClientOptions());
ChatCompletionsOptions completionsOptions = new ChatCompletionsOptions();
foreach (var prompt in input.Prompts)
{
completionsOptions.Messages.Add(new ChatMessage(prompt.role, prompt.Content));
}
completionsOptions.MaxTokens = input.Model.MaxResponseTokens;
completionsOptions.Temperature = 1;
completionsOptions.FrequencyPenalty = 0;
completionsOptions.PresencePenalty = 0;
completionsOptions.NucleusSamplingFactor = 1;
return await client.GetChatCompletionsAsync(input.Model.DeploymentName, completionsOptions);

@jsquire jsquire added Client This issue points to a problem in the data-plane of the library. needs-team-attention This issue needs attention from Azure service team or SDK team CXP Attention OpenAI and removed needs-triage This is a new issue that needs to be triaged to the appropriate team. labels Jun 26, 2023
@github-actions
Copy link

Thank you for your feedback. This has been routed to the support team for assistance.

@PramodValavala-MSFT PramodValavala-MSFT self-assigned this Jun 27, 2023
@PramodValavala-MSFT PramodValavala-MSFT added issue-addressed The Azure SDK team member assisting with this issue believes it to be addressed and ready to close. and removed needs-team-attention This issue needs attention from Azure service team or SDK team labels Jun 27, 2023
@PramodValavala-MSFT
Copy link
Contributor

@ekoostikmartin As shown by @zhuxingke, while the list can't be replaced, it is not read only. Instead, you should add messages or merge list of messages into it.

Hope that helps.

@github-actions
Copy link

Hi @ekoostikmartin. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text "/unresolve" to remove the "issue-addressed" label and continue the conversation.

@zbecknell
Copy link
Contributor

zbecknell commented Jun 28, 2023

It'd be friendlier to at least have a constructor that accepts a list of messages. As it is it's not very intuitive.

Edit to expound: many people may get their starter code through the chat playground, which as of now gives sample code that looks like this:

...
Response<StreamingChatCompletions> response = await client.GetChatCompletionsStreamingAsync(
	deploymentOrModelName: "MyModel",
	new ChatCompletionsOptions()
	{
		Messages =
		{
			new ChatMessage(ChatRole.System, @"You are an AI assistant that helps people find information."),
			new ChatMessage(ChatRole.User, @"Hello"),
			new ChatMessage(ChatRole.Assistant, @"Hi there! How can I assist you today?"),
			new ChatMessage(ChatRole.User, @"May I hear a joke?"),
			new ChatMessage(ChatRole.Assistant, @"Sure! Here's a joke for you: What do you call a fake noodle? An impasta!"),
		},
		...
	});
...

Taking this code as a starting point, you expect to be able to do this:

...
Response<StreamingChatCompletions> response = await client.GetChatCompletionsStreamingAsync(
	deploymentOrModelName: "MyModel",
	new ChatCompletionsOptions()
	{
		Messages = BuildChatMessages(),
                ...
	});
...

If you don't want to give Messages a setter, please consider at least adding a constructor overload where a pre-built messages collection can be passed.

@github-actions
Copy link

github-actions bot commented Jul 5, 2023

Hi @ekoostikmartin, since you haven’t asked that we /unresolve the issue, we’ll close this out. If you believe further discussion is needed, please add a comment /unresolve to reopen the issue.

@github-actions github-actions bot closed this as completed Jul 5, 2023
@ADefWebserver
Copy link

ADefWebserver commented Jul 9, 2023

I have an example of how to make it work here: Creating A Blazor Chat Application With Azure OpenAI

            // Create a new OpenAIClient object
            // with the provided API key and Endpoint
            OpenAIClient client = new OpenAIClient(
                new Uri(Endpoint),
                new AzureKeyCredential(Key));
            // Add the new message to chatMessages
            ChatMessages.Add(new ChatMessage(ChatRole.User, prompt));
            // Create a new ChatCompletionsOptions object
            var chatCompletionsOptions = new ChatCompletionsOptions()
                {
                    Temperature = (float)0.7,
                    MaxTokens = 2000,
                    NucleusSamplingFactor = (float)0.95,
                    FrequencyPenalty = 0,
                    PresencePenalty = 0,
                };
            // Add the prompt to the chatCompletionsOptions object
            foreach (var message in ChatMessages)
            {
                chatCompletionsOptions.Messages.Add(message);
            }
            // Call the GetChatCompletionsAsync method
            Response<ChatCompletions> responseWithoutStream =
            await client.GetChatCompletionsAsync(
                DeploymentOrModelName,
                chatCompletionsOptions);
            // Get the ChatCompletions object from the response
            ChatCompletions result = responseWithoutStream.Value;
            // Create a new Message object with the response and other details
            // and add it to the messages list
            var choice = result.Choices.FirstOrDefault();
            if (choice != null)
            {
                if (choice.Message != null)
                {
                    ChatMessages.Add(choice.Message);
                }
            }
            // Update the total number of tokens used by the API
            TotalTokens = TotalTokens + result.Usage.TotalTokens;

@github-actions github-actions bot locked and limited conversation to collaborators Oct 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. issue-addressed The Azure SDK team member assisting with this issue believes it to be addressed and ready to close. OpenAI question The issue doesn't require a change to the product in order to be resolved. Most issues start as that
Projects
None yet
Development

No branches or pull requests

6 participants