Last active
September 21, 2024 17:08
-
-
Save pavlovmilen/f94c496bcbffa5bba9c9f68cc4717536 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (completion.FinishReason == ChatFinishReason.ToolCalls) | |
{ | |
try | |
{ | |
var toolResult = string.Empty; | |
foreach (var toolCall in completion.ToolCalls) | |
{ | |
switch (toolCall.FunctionName) | |
{ | |
case "GetImageDataAsync": | |
{ | |
// gets the function argument in this case imageLink variable that is the url from the prompt | |
var arguments = JsonSerializer.Deserialize<GetImageArguments>(toolCall.FunctionArguments); | |
// this will get us the image data consisting of binary data and content type | |
var imageResponse = await _functions.GetImageDataAsync(arguments.imageLink); | |
if (imageResponse == null) | |
{ | |
toolResult = "No image found"; | |
} | |
else | |
{ | |
// the special code here is ChatMessageContentPart.CreateImageMessageContentPart that allows us to add image or another modality to our prompt :) | |
var promptUserMessage = ChatMessageContentPart.CreateTextMessageContentPart($"Given this image and these image requirements: {context} can you tell me if its a valid loft insulation evidence"); | |
var imageUserMessage = ChatMessageContentPart.CreateImageMessageContentPart(imageResponse.BinaryData, imageResponse.ContentType); | |
// Add the imageBytes as a new input message | |
inputMessages.Clear(); | |
inputMessages.Add(new UserChatMessage(promptUserMessage)); | |
inputMessages.Add(new UserChatMessage(imageUserMessage)); | |
// Now, make a final call with the updated input messages | |
ChatCompletion finalCompletion = await chatClient.CompleteChatAsync(inputMessages, chatCompletionOptions); | |
// Process the final response | |
// toolResult will have the final result here that the client will display | |
toolResult = finalCompletion.Content[0].Text; | |
// record token usages for costing purposes | |
var properties = new Dictionary<string, string> | |
{ | |
{ "TotalTokens", finalCompletion.Usage?.TotalTokens.ToString() }, | |
{ "PromptTokens", finalCompletion.Usage?.InputTokens.ToString() }, | |
{ "CompletionTokens", finalCompletion.Usage?.OutputTokens.ToString() }, | |
}; | |
_telemetryClient.TrackMetric("AzureOpenAIApiResponseTotalTokens", finalCompletion?.Usage?.TotalTokens ?? 0, properties); | |
} | |
} | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment