Coder Social home page Coder Social logo

openai-package's Introduction

HNGx OpenAI package for Stage 5.

This library enables stage 5 developers to focus more on coding and care less about the underlying dynamics of connecting to openAI API.

Installing

Inside of your pubspec.yaml add these lines under your dependencies.

hngx_openai:
    git:
      url: https://github.com/hngx-org/openai-package.git
      ref: 51488f5

Importing the package within your project

import 'package:hngx_openai/repository/openai_repository.dart';

Making use of the OpenAI functionalities

  1. Define your query or fetch it from textfield.

    const String userInput = "What is today's date";
  2. Get your session string; cookie (as it is needed for carrying out operations on the package).

    const String cookie =
         "session=487d97a5-3e43-4502-80d4-9315c3d7bf77.24ZfCu95q06BqVuCUFWuJJoLAgM";
  3. Instantiate the OpenAIRepository

    OpenAIRepository openAI = OpenAIRepository();
  4. Make of the getChat() function; by passing userInput and cookie as argument.

    final response = await openAI.getChat(userInput, cookie);

    Or the getChatCompletions which takes list of Strings; history as an additional parameter.

    history = ["What is my name", "How are you today?"];
    final response = await openAI.getChatCompletions(history userInput, cookie);

    This returns a String with prefix "Message" and the content if the operation was successful. Or it returns a String with prefix "Error" and the error message if there's any error.

  5. Finally display the error on the app.

    setState(() {
       _counter = aiResponse;
     });
  • We are returning the String with "Message" or "Error" prefix because we want the developer to use this to filter for when to display the response or display a popup on the app stating the error.

Full Example

import 'package:flutter/material.dart';
import 'package:hngx_openai/repository/openai_repository.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(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  String _counter = "No Chat";

  void _incrementCounter() async {
    const String userInput = "What is today's date";
    const String cookie =
        "session=487d97a5-3e43-4502-80d4-9315c3d7bf77.24ZfCu95q06BqVuCUFWuJJoLAgM";

    // Instantiate OpenAIRepository
    OpenAIRepository openAI = OpenAIRepository();

    // For initiating a new chat
    final aiResponse = await openAI.getChat(userInput, cookie);
    // For getting chat completions
    List<String> history = ["What is my name", "How are you today?"];
    final response =
        await openAI.getChatCompletions(history, userInput, cookie);

    setState(() {
      _counter = aiResponse;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'Hit the plus button to get response from the server',
            ),
            Text(
              _counter,
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

Clicking the floating action button will display the response on the app; be it error or message.

Authors

openai-package's People

Contributors

eokdev avatar boahen123 avatar maverick0x avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.