chat_gpt_api 1.0.2 copy "chat_gpt_api: ^1.0.2" to clipboard
chat_gpt_api: ^1.0.2 copied to clipboard

Chat GPT wrapper for flutter apps witten in dart languange. Initial release supports Text Completeion, Code Completion and Image Generation

example/lib/main.dart

import 'package:chat_gpt_api/chat_gpt.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final chatGpt = ChatGPT.builder(
    token:
        '<TOKEN>', // generate token from https://beta.openai.com/account/api-keys
  );

  void textCompletion() async {
    const prompt = "Explain Quantum Computing in simple terms";

    Completion? completion = await chatGpt.textCompletion(
      request: CompletionRequest(
        prompt: prompt,
        maxTokens: 256,
      ),
    );

    if (kDebugMode) {
      print(completion?.choices);
    }
  }

  void imageGeneration() async {
    Images? images = await chatGpt.generateImage(
      request: ImageRequest(
        prompt: 'A cute baby sea otter',
      ),
    );

    if (kDebugMode) {
      print(images?.data);
    }
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '0',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: textCompletion,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
32
likes
130
pub points
63%
popularity

Publisher

verified publisherleyo.dev

Chat GPT wrapper for flutter apps witten in dart languange. Initial release supports Text Completeion, Code Completion and Image Generation

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

dio, flutter

More

Packages that depend on chat_gpt_api