Issue with calling Azure AI Foundry API in React Native Environment

Yoke Huan Shum 40 Reputation points
2024-12-18T07:44:05.96+00:00

Hi there,

I'm working on a mobile application development project that connects to Azure AI Foundry Prompt Flow. It's essentially a knowledge-based AI chatbot app. Currently, I'm facing an issue with calling the API from the Image Prompt Flow.

Screenshot 2024-12-18 092828

As shown in the picture above, this is my simple prompt flow for processing the image input.

Additionally, I have set the 'Type' of question (input) as a List, which should be available for both image and text input. My expected result is to get the extracted text from the uploaded image in the chatbot.

The function testing in the Azure environment is working fine. However, in my own React Native project environment, when I attempt to call the API, it returns errors like this:

Screenshot 2024-12-18 093856

Screenshot 2024-12-18 093948

And the code below there is how I call the API in my React Native environment.

import axios from 'axios';
const apiKey = process.env.IMAGE_API_KEY;
const endpoint = process.env.IMAGE_PROMPT_ENDPOINT;
// Headers for authentication (similar to GPT-4o style)
const headers = {
  'Content-Type': 'application/json',
  'api-key': apiKey, // Updated to Bearer token if required
};
// Function to clean response text
function cleanResponseText(text) {
  return text.replace(/[*#-]/g, '').replace(/\n{2,}/g, '\n\n').trim();
}
// Function to analyze image with a question and chat history
export const analyzeImage = async (question, imageBase64, mimeType, chatHistory = []) => {
  try {
    if (!apiKey || !endpoint) {
      throw new Error('API key or endpoint is missing. Check environment variables.');
    }
    // Construct payload
    const payload = {
      question: question,
      image: imageBase64,
      mimeType: mimeType,
      chat_history: chatHistory,
    };
    console.log('Sending Payload:', payload);
    // Send POST request for image analysis
    const response = await axios.post(endpoint, payload, { headers });
    console.log('Raw Response:', response.data);
    // Clean and return the response
    if (response.data && response.data.analysis) {
      const cleanText = cleanResponseText(response.data.analysis);
      return { analysis: cleanText, metadata: response.data.metadata || {} };
    } else {
      throw new Error('No valid response received from the image analysis API.');
    }
  } catch (error) {
    console.error('Error during image analysis:', error.response?.data || error.message);
    throw new Error(error.response?.data?.message || 'Image analysis failed.');
  }
};

Is the API call failing due to a problem in the code?

Anyone could assist me with this issue? Many thanks in advance!

Azure AI Search
Azure AI Search
An Azure search service with built-in artificial intelligence capabilities that enrich information to help identify and explore relevant content at scale.
1,118 questions
Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
2,999 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.