Código Fuente Integración FlutterFlow + ChatGPT3
Este código es parte de la configuración realizada para la integración de ChatGPT a una App móvil (para IOS o Android) desarrollada a través de FlutterFlow.
En el ejemplo (link del video abajo), se utiliza en una App sencilla. Se tiene un TextField en donde se escribe la pregunta a ChatGPT, apretando un botón de enviar, se obtiene la repuesta en un Text, ubicado en la parte superior de la App. En ese sentido, se trata de un chat sencillo con ChatGPT pero a través de tu App movil.
Se realiza por medio de una Custom Function > Action Code. En este lugar se aplica el código señalado abajo, y listo! Tu aplicación podrá comunicarse con ChatGPT.
Puedes encontrar la explicación detallada en el siguiente link:
Aquí va el código! :)
FlutterFlow + ChatGPT3 (Custom Action)
Return Value (String)
Arguments
apiKey(string)
prompt (string)
maxToken (integer)
temperature (double) (nullable)
Code
// Automatic FlutterFlow imports
import '../../flutter_flow/flutter_flow_theme.dart';
import '../../flutter_flow/flutter_flow_util.dart';
import '../actions/index.dart'; // Imports other custom actions
import '../../flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
import 'package:http/http.dart' as http;
import 'dart:async';
import 'dart:convert';
Future<String> chatGPT3(
String apiKey,
String prompt,
int maxTokens,
double temperature,
) async {
final data = {
'prompt': prompt,
'max_tokens': maxTokens,
'temperature': temperature,
};
final headers = {
'Authorization': 'Bearer $apiKey',
'Content-Type': 'application/json'
};
final request = http.Request(
'POST',
Uri.parse('https://api.openai.com/v1/engines/text-davinci-003/completions'),
);
request.body = json.encode(data);
request.headers.addAll(headers);
final httpResponse = await request.send();
if (httpResponse.statusCode != 200) {
return 'Failed to get response from the server, status code: ${httpResponse.statusCode}';
}
final jsonResponse = json.decode(await httpResponse.stream.bytesToString());
if (jsonResponse == null) {
return 'Failed to parse response from the server';
}
return jsonResponse['choices'][0]['text'];
}
----------------------------------------------------------------------------------------------------------------------------
Si tienes alguna pregunta, no dudes en dejarla en los comentarios. Y tampoco olvides ver el video explicativo ;)
No hay comentarios.:
Publicar un comentario