Use ChatGPT from Flutter / Dart

Overview

Flutter ChatGPT API

This package is a Flutter/Dart API around ChatGPT by OpenAI.

This package requires a valid session token from ChatGPT to access its unofficial REST API.

This version have been updated to use Puppeteer to log in to ChatGPT and extract the Cloudflare cf_clearance cookie and OpenAI session token. ๐Ÿ”ฅ Thanks to Node.js ChatGPT API (unofficial)

โš ๏ธ Be Careful!

  • Your user-agent and IP address must match from the real browser window you're logged in with to the one you're using for ChatGPTAPI.
    • This means that you currently can't log in with your laptop and then run the bot on a server or proxy somewhere.
  • Please check defaultHeaders

Demo

Installation

dependencies:
  flutter_chatgpt_api: ^1.0.0

Usage

import 'package:flutter_chatgpt_api/flutter_chatgpt_api.dart';

 _api = ChatGPTApi(
       sessionToken: SESSION_TOKEN,
       clearanceToken: CLEARANCE_TOKEN,
     );

setState(() {
  _messages.add(
    ChatMessage(
      text: _textController.text,
      chatMessageType: ChatMessageType.user,
    ),
  );
  isLoading = true;
});  

var newMessage = await _api.sendMessage(
  input,
  conversationId: _conversationId,
  parentMessageId: _parentMessageId,
);

setState(() {
  _conversationId = newMessage.conversationId;
  _parentMessageId = newMessage.messageId;
  isLoading = false;
  _messages.add(
    ChatMessage(
      text: newMessage.message,
      chatMessageType: ChatMessageType.bot,
    ),
  );
});

SessionToken

To get a session token:

  1. Go to https://chat.openai.com/chat and log in or sign up.
  2. Open dev tools.
  3. Open Application > Cookies (Storage > Cookies)

image

  1. Create these files and add your session token to run the tests and example respectively:

Copy the value for __Secure-next-auth.session-token and save it to your environment.example/lib/session_token.dart

Copy the value for cf_clearance and save it to your environment. example/lib/clearance_token.dart

Should look something like this:

const SESSION_TOKEN = '__Secure-next-auth.session-token from https://chat.openai.com/chat';
const CLEARANCE_TOKEN = 'cf_clearance token from https://chat.openai.com/chat';

Credit

License

MIT Copyright (c) 2022, Emre CoลŸkunรงay

If you found this project interesting, please consider supporting my open source work by sponsoring me or following me on twitter twitter

You might also like...

Dart Web API Template Using Google Shelf Framework and Postgres Drivers, read readme to learn how to use

Shelf Web Framework Template by Alex Merced of AlexMercedCoder.com Cloning the Template git clone https://github.com/AlexMercedCoder/DartShelfPostgres

Dec 26, 2022

Dart anticaptcha - Easy to use library for solving captcha

The solution of the captcha is carried out at the expense of the resource worker

Jan 12, 2022

A Dart EPUB parser built from the ground up, and designed to support a variety of use cases and custom

A Dart EPUB parser built from the ground up, and designed to support a variety of use cases and custom implementations such as on-device caching and serving content from a server.

Nov 3, 2022

Dart JS interop for Mermaid - The Javascript tool that makes use of a markdown based syntax to render customizable diagrams, charts and visualizations.

Mermaid (Dart) Dart JS interop for Mermaid - Javascript library that makes use of a markdown based syntax to render customizable diagrams, charts and

Dec 12, 2022

A Dart dependency injection library aimed to be flexible, predictable and easy to use.

dino Dino is a Dart dependency injection library with optional code generation. It was inspired by DI in .NET and aimed to be flexible, predictable an

Dec 20, 2022

Mysql.dart - MySQL client for Dart written in Dart

Native MySQL client written in Dart for Dart See example directory for examples

Dec 29, 2022

๐Ÿš— Apple CarPlay for Flutter Apps. Aims to make it safe to use apps made with Flutter in the car by integrating with CarPlay.

๐Ÿš— Apple CarPlay for Flutter Apps. Aims to make it safe to use apps made with Flutter in the car by integrating with CarPlay.

CarPlay with Flutter ๐Ÿš— Flutter Apps now on Apple CarPlay! flutter_carplay aims to make it safe to use iPhone apps made with Flutter in the car by int

Dec 26, 2022

This is a Flutter app which shows how to use the PageView Class in your Flutter App

page_view_class A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started

Oct 25, 2021

This is an easy to use Flutter Package for adding Fancy Foldable Sidebar to your Flutter Applications.

This is an easy to use Flutter Package for adding Fancy Foldable Sidebar to your Flutter Applications.

Foldable Sidebar An easy to implement Foldable Sidebar Navigation Drawer for Flutter Applications. Current Features Initial Release for Foldable Navig

Nov 26, 2022
Comments
  • "Checking if the site connection is secure" Challenge

    I'm getting exception "ChatGPT failed to refresh auth token: Exception: Failed to refresh access token".

    It looks like the page has an anti-bot challenge for accessing the site. Even after adding the clearance token (reference) the response body from the refresh token is the challenge html.

    opened by 13lueDavis 3
  • Failed to refresh access token, token in response is null

    Failed to refresh access token, token in response is null

    Hi, thanks for the package! I did as tutorial told, but got this error: Exception: ChatGPT failed to refresh auth token: Exception: Failed to refresh access token, token in response is null

    could you please let me know which part is wrong? I copied my token and created two dart file in lib and test. Thanks again!

    opened by peterbullmatti 3
  • Throwing exception

    Throwing exception

    [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Exception: ChatGPT failed to refresh auth token: Exception: Failed to refresh access token

    #0 ChatGPTApi._refreshAccessToken (package:flutter_chatgpt_api/flutter_chatgpt_api.dart:152:7)

    https://github.com/coskuncay/flutter_chatgpt_api/issues/1 ChatGPTApi.sendMessage (package:flutter_chatgpt_api/flutter_chatgpt_api.dart:50:25)

    https://github.com/coskuncay/flutter_chatgpt_api/issues/2 _ChatScreenState._buildSubmit. (package:neural_ai/screens/chat.screen.dart:110:30)

    opened by itsalfredakku 5
  • Not working on web

    Not working on web

    Hi, thanks for the package!

    It seems that the package currently is not working on the web.

    In the web console I get the following errors: Refused to set unsafe header "cookie" and Refused to set unsafe header "user-agent"

    The package itself throws the following error: ChatGPT failed to refresh auth token: Exception: Failed to refresh access token, token in response is null

    good first issue 
    opened by rienkkk 2
Owner
emre
VueJS Developer | Flutter Lover
emre
A TabBarController that is easy to use for flutter developers. ๐Ÿฅฐ It supports various styles of page navigation, and you can also use it to customize your favorite styles. ๐Ÿป๐Ÿป

easy_tab_controller A user-friendly TabBarController widget for flutter developer. Getting Started This project is a starting point for a Flutter plug

ๅœ†ๅทๆœฌๆ˜Š 3 May 26, 2022
A Dart client for the NATS messaging system. Design to use with Dart and Flutter.

Dart-NATS A Dart client for the NATS messaging system. Design to use with Dart and flutter. Flutter Web Support by WebSocket client.connect(Uri.parse(

Chart Chongcharoen 32 Nov 18, 2022
Dart-dependency-injection - A simple example of how to use dependency injection with dart

This is a simple example of how to use dependency injection with dart. In this e

Rafael Alves 0 Feb 3, 2022
Learn how to use Dart List Utility Methods in Flutter

Flutter Tutorial - List Utility Methods Learn how to use Dart List Utility Metho

Behruz Hurramov 0 Dec 29, 2021
Hyakunin Isshu 1 Jan 11, 2022
A non-official package to use QOSIC on your Dart and Flutter app

Qosic Dart A Very Good Project created by Very Good CLI. About โ“ This package is a simple way to handle Qosic's USSD payment. It allows you to integra

Elikem (Junior) Medehou 4 Dec 8, 2022
This repository was created to provide the basics of the Dart programming language and Its use cases.

dart-exercises A collection of source code of the Dart Programming language. How does this repository works? Clone / Fork this repository to make your

Technosoft Labs 2 Oct 28, 2021
Simple Dart calculator for use in a CLI

Dart-calculator Simple Dart calculator for use in a CLI Related to the Medium article: Building a Simple CLI Calculator App in Dart Overview This appl

Jean Luc Kabulu 6 Sep 13, 2022