I’m using bot framework sdk with C# to call Azure open AI model gpt-4o for image analysis. i receive following response or variance of it. however when i upload same exact image to azure open ai studio it’s able to analyze it. also when i use python it’s able to analyze it. what could be the issue with this c# code?
`/ Generated with EchoBot .NET Template version v4.22.0
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;
using System;
using Azure;
using Azure.AI.OpenAI;
using System.IO; // Required for working with files
using System.Text; // Required for Base64 encoding
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
namespace EchoBot.Bots
{
public class EchoBot : ActivityHandler
{
static readonly HttpClient client2 = new HttpClient();
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
// Check if the activity contains any text
if (!string.IsNullOrEmpty(turnContext.Activity.Text))
{
var replyText = $"Echo: {turnContext.Activity.Text}";
await turnContext.SendActivityAsync(MessageFactory.Text(replyText, replyText), cancellationToken);
OpenAIClient client = new OpenAIClient(new Uri("https://mct-openai01.openai.azure.com/"),
new AzureKeyCredential("xxxxxxxxxxxxxxxxxxxxxxxxx"));
Response<ChatCompletions> responseWithoutStream = await client.GetChatCompletionsAsync("mct-gpt4o-01",
new ChatCompletionsOptions()
{
Messages =
{
new ChatMessage(ChatRole.System, @"You are an AI assistant that helps people find information."),
new ChatMessage(ChatRole.User, @"You are an AI assistant that helps people find information."),
},
Temperature = (float)0.7,
MaxTokens = 800,
NucleusSamplingFactor = (float)0.95,
FrequencyPenalty = 0,
PresencePenalty = 0,
} );
ChatCompletions response = responseWithoutStream.Value;
string completionText = response.Choices[0].Message.Content;
await turnContext.SendActivityAsync(MessageFactory.Text(completionText, completionText), cancellationToken);
}
else if (turnContext.Activity.Attachments.Count > 0)
{
// User sent an attachment
await turnContext.SendActivityAsync(MessageFactory.Text(" I see you sent one!"), cancellationToken);
if (turnContext.Activity.Attachments.Count == 1 && turnContext.Activity.Attachments[0].ContentType.StartsWith("image/"))
{
await turnContext.SendActivityAsync(MessageFactory.Text(" starts with image"), cancellationToken);
// Only process the first attachment if it's an image
var attachment = turnContext.Activity.Attachments[0];
if (attachment.ContentType.Contains("image/"))
{
await turnContext.SendActivityAsync(MessageFactory.Text(" contains"), cancellationToken);
var httpClient = new HttpClient();
var attachmentUrl = attachment.ContentUrl;
try
{
var attachmentResponse = await httpClient.GetAsync(attachmentUrl, cancellationToken);
if (attachmentResponse.IsSuccessStatusCode)
{
using (var memoryStream = new MemoryStream())
{
await attachmentResponse.Content.CopyToAsync(memoryStream);
byte[] imageBytes = memoryStream.ToArray();
// Convert image to Base64 string
string base64Image = Convert.ToBase64String(imageBytes);
// Prepare GPT-4 payload with Base64 image
ChatCompletionsOptions gpt4Options = new ChatCompletionsOptions()
{
Messages =
{
new ChatMessage(ChatRole.System, @"You are an AI assistant that helps people find information."),
new ChatMessage(ChatRole.User, @"Here's an image for you to analyze."),
//new ChatMessage(ChatRole.User, base64Image) // Add the base64 image as a user message
new ChatMessage(ChatRole.User, $"{{"type": "image_url", "image_url": "data:image/png;base64,{base64Image}"}}")
// $"{{"type": "image_url", "image_url": {{"url": "data:image/png;base64,{base64Image}"}}}}")
},
// ... other options like Temperature, MaxTokens, etc.
};
// Send the request with the image data
OpenAIClient client = new OpenAIClient(new Uri("https://mct-openai01.openai.azure.com/"),
new AzureKeyCredential("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
Response<ChatCompletions> gpt4Response = await client.GetChatCompletionsAsync("mct-gpt4o-01", gpt4Options);
ChatCompletions response = gpt4Response.Value;
string completionText = response.Choices[0].Message.Content;
await turnContext.SendActivityAsync(MessageFactory.Text(completionText, completionText), cancellationToken);
}
}
else
{
// Handle download failure (e.g., inform user)
//await turnContext.SendActivityAsync(MessageFactory.Text("else statment"), cancellationToken);
}
}
catch (Exception)
{
// Handle exceptions during download (e.g., logging)
//await turnContext.SendActivityAsync(MessageFactory.Text("catch statment"), cancellationToken);
}
}
}
}
}
protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
var welcomeText = "Hello and welcome!";
foreach (var member in membersAdded)
{
if (member.Id != turnContext.Activity.Recipient.Id)
{
await turnContext.SendActivityAsync(MessageFactory.Text(welcomeText, welcomeText), cancellationToken);
}
}
}
}
}`
did anyone face same issue with c# ?
New contributor
mahelsay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.