0

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.

  • The code is sending a HTTP request and returning an HTTP response (the variable response). The response will have headers and a body. I would put a break point on last line of code and examine the variable "response" and see what it contains. I NEVER use var as a type unless I have to because of situation like this. You cannot tell what is bein returned from client.GetChatCompletionsAsync() method. – jdweng Aug 10 '23 at 23:20
  • I know what's being returned: A Response object. The problem is how to create a mock ChatCompletions object :) – Mark Wiemer Aug 12 '23 at 00:18
  • The response body is a serialize ChatCompletions class. The serialization could be binary or JSON. One way is to create a loopback route that would take the body of the request and loopback to the body of the response. Or a test method that would emulate the response body. – jdweng Aug 12 '23 at 03:11
  • Gotcha, so the best way you see would just be `JsonConverter.Deserialize(mockChatString)`? – Mark Wiemer Aug 13 '23 at 03:10
  • The deserialize method has to be compatible with the serialize method on the server. – jdweng Aug 13 '23 at 08:42

0 Answers0