Search code examples
c#openai-apiazure-openaiazure-ai

C#: How to create a mock Azure.AI.OpenAI.ChatCompletions object to test functions using OpenAI?


I have a simple function that takes a prompt, sends it to a model on our Azure OpenAI instance, and returns the content of the LLM's response:

        public async Task<string> GetChatCompletion(string deploymentName, ChatCompletionsOptions conversation)
        {
            // `client` is of type `Azure.AI.OpenAI.OpenAIClient`
            var response = await client.GetChatCompletionsAsync(deploymentName, conversation).ConfigureAwait(false);
            return response.Value.Choices[0].Message.Content;
        }

I'd like to write tests for this function (or similar ones), but I can't figure out how to create a mock object for the return value of client.GetChatCompletionsAsync. It's of type Azure.AI.OpenAI.ChatCompletions, but I don't see any constructor or publicized way to create a mock object of this type. Thanks in advance.

new ChatCompletions() doesn't exist, there doesn't seem to be a clear ChatCompletions.FromX(...) function either. Searching online has given no clear results as everything is populated with tutorials for how to get real data.


Solution

  • Try using AzureOpenAIModelFactory to mock all OpenAIClient response types, see: AzureOpenAIModelFactory.ChatCompletions Method

         var choices = new List<ChatChoice>
        {
            AzureOpenAIModelFactory.ChatChoice(message: new ChatMessage(ChatRole.Assistant, "Foobar"), index: 0, finishReason: CompletionsFinishReason.Stopped),
            AzureOpenAIModelFactory.ChatChoice(message: new ChatMessage(ChatRole.Assistant, "Bazbar"), index: 1, finishReason: CompletionsFinishReason.Stopped)
        };
    
        var chatCompletions = AzureOpenAIModelFactory.ChatCompletions(null, default, choices, null, null);
    

    Hope that helps