Search code examples
node.jsfirebasegoogle-cloud-functionsopenai-api

Using OpenAI API from Firebase Cloud Functions in flutter app


Updated

I can't figure out how to make this work.

This is my cloud function in Javascript. I'm trying a simple code to see if the connection works (other cloud functions not using the openai package do work fine).

The function does work on GCP when I test it, but won't connect to my flutter function for some reason:

error:

flutter: Error calling Firebase Function: internal Response is missing data field.

Cloud function

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const axios = require('axios');
const cors = require('cors')({ origin: true });
admin.initializeApp();

exports.openAiResponse2 = functions.https.onRequest(async (request, response) => {
  const apiKey = 'sk-';
  cors(request, response, async () => {
    try {
      const apiResponse = await axios.post(
        'https://api.openai.com/v1/chat/completions',
        {
          model: 'gpt-3.5-turbo',
          messages: [{ role: 'user', content: 'Say this is a test!' }],
          temperature: 0.7,
        },
        {
          headers: {
            'Authorization': `Bearer ${apiKey}`,
            'Content-Type': 'application/json',
          },
        },
      );

     // response.status(200).json({ message: 'OpenAI API connection successful.', engines: apiResponse.data });
        response.status(200).json({
              message: 'OpenAI API connection successful.',
              completion: apiResponse.data.choices[0].message.content,
            });
    } catch (error) {
      console.error('Error connecting to OpenAI API:', error);
      response.status(500).send('Error connecting to OpenAI API');
    }
  });
});

Flutter:

Future<void> talkToMe2() async {
  try {
    HttpsCallable callable = FirebaseFunctions.instance.httpsCallable('openAiResponse2');
    print(callable);
    final response = await callable.call();
    print('response is $response');
    print('External API response: ${response.data}');
  } on FirebaseFunctionsException catch (e) {
    print('Error calling Firebase Function: ${e.code} ${e.message}');
  } catch (e) {
    print('Error calling Firebase Function: $e');
  }
}

This is my package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "16"
  },
  "main": "index.js",
  "dependencies": {
    "firebase-admin": "^11.5.0",
    "firebase-functions": "^4.2.0",
    "openai": "^3.2.1"
  },
  "devDependencies": {
    "firebase-functions-test": "^3.0.0"
  },
  "private": true
}

And my flutter doctor

[✓] Flutter (Channel stable, 3.7.12, on macOS 13.2 22D49 darwin-arm64, locale en-ES)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc1)
[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.1)
[✓] VS Code (version 1.77.3)
[✓] Connected device (4 available)
[✓] HTTP Host Availability

I can't seem to find a solution. Thanks!


Solution

  • I am not a flutter developer but httpsCallable is for onCall not onRequest.

    https://firebase.google.com/docs/functions/callable?gen=2nd

    https://firebase.google.com/docs/functions/http-events?gen=2nd

    If you look at the flutter sample, you should have something like below for https callable

    const functions = require('firebase-functions');
    
    exports.listFruit = functions.https.onCall((data, context) => { //<<<-- not onRequest
      return ["Apple", "Banana", "Cherry", "Date", "Fig", "Grapes"]
    });