GithubHelp home page GithubHelp logo

lambiengcode / flutter-chatgpt-completions Goto Github PK

View Code? Open in Web Editor NEW
15.0 3.0 8.0 12.2 MB

:dove: Dart client for the unofficial ChatGPT API. Support Text Completion and stream response from v1/completions. 非官方 ChatGPT API 的 Dart 客户端。支持文本补全和来自 v1/completions。

Home Page: https://pub.dev/packages/chatgpt_completions

License: MIT License

Java 0.84% Kotlin 0.19% Swift 1.97% Objective-C 0.97% Dart 23.50% CMake 30.45% C++ 34.65% C 3.06% HTML 2.77% Shell 1.62%
chatgpt3 completions dart-package library openai stream-api chatbot dart-packages stream-server-api chatgpt

flutter-chatgpt-completions's Introduction

serverPubfilesLicensePRs Welcome

Dart client for the unofficial ChatGPT API. Support Text Completeion and stream response from v1/completions.

Features

  • Text completions without stream response
  • Text completions with stream response

Getting started

  • Install package
flutter pub add chatgpt_completions

Usage

  • Initialize instance
/// Generate api key from openai console: https://platform.openai.com/account/api-keys
ChatGPTCompletions.instance.initialize(apiKey: "api_key_here");
  • Text completions without stream response (stream: false)
String? responseWithoutStream =
      await ChatGPTCompletions.instance.textCompletions(TextCompletionsParams(
    prompt: "What's Flutter?",
    model: GPTModel.davinci,
    temperature: 0.2,
    topP: 1,
    n: 1,
    stream: false,
    maxTokens: 2048,
));

print("OpenAI: $responseWithoutStream");
  • Text completions with stream response (stream: true)
StreamSubscription? responseSubscription;

await ChatGPTCompletions.instance.textCompletions(
    TextCompletionsParams(
      prompt: "What's Flutter?",
      model: GPTModel.davinci,
      temperature: 0.2,
      topP: 1,
      n: 1,
      stream: true, // --> set this is true
      maxTokens: 2048,
    ),
    onStreamValue: (characters) {
      responseWithStream += characters;
      print(responseWithStream);
    },
    onStreamCreated: (subscription) {
      responseSubscription = subscription;
    },
);
  • Stop generating (with stream)
responseSubscription?.cancel();
  • Using model gpt-3.5-turbo/gpt-4
// Using GPT-3.5-Turbo
await ChatGPTCompletions.instance.textCompletions(
  TextCompletionsParams(
    // using messagesTurbo insteal of prompt
    messagesTurbo: [
      MessageTurbo(
        role: TurboRole.user,
        content: "What's Flutter?",
      ),
    ],
    model: GPTModel.gpt3p5turbo, // --> switch to gpt-3.5-turbo model
  ),
  onStreamValue: (characters) {
    responseWithStream += characters;
    print(responseWithStream);
  },
  onStreamCreated: (subscription) {
    responseSubscription = subscription;
  },
  // Debounce 100ms for receive next value
  debounce: const Duration(milliseconds: 100),
);

License - lambiengcode

MIT License

Copyright (c) 2023 lambiengcode

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

flutter-chatgpt-completions's People

Contributors

chopper985 avatar lambiengcode avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

flutter-chatgpt-completions's Issues

Error on first run

Hi, I get as far as:

Error: FormatException: SyntaxError: Unexpected non-whitespace character after JSON at position 209
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 266:49        throw_
dart-sdk/lib/_internal/js_dev_runtime/patch/convert_patch.dart 39:5                 _parseJson
dart-sdk/lib/convert/json.dart 610:36                                               convert
dart-sdk/lib/convert/json.dart 216:41                                               decode
dart-sdk/lib/convert/json.dart 155:10                                               jsonDecode
packages/chatgpt_completions/src/repository/text_completions_repository.dart 63:34  <fn>
dart-sdk/lib/async/zone.dart 1593:10                                                runUnaryGuarded
dart-sdk/lib/async/stream_impl.dart 339:11                                          [_sendData]
dart-sdk/lib/async/stream_impl.dart 515:13                                          perform
dart-sdk/lib/async/stream_impl.dart 620:10                                          handleNext
dart-sdk/lib/async/stream_impl.dart 591:7                                           callback
dart-sdk/lib/async/schedule_microtask.dart 40:11                                    _microtaskLoop
dart-sdk/lib/async/schedule_microtask.dart 49:5                                     _startMicrotaskLoop
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 166:15                 <fn>

I have not used Flutter before so it may be something obvious. I ran the following in the example directory flutter run -d chrome

run error

E/flutter ( 6185): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Exception: status code: 400, error: [] is too short - 'messages'
E/flutter ( 6185): #0 TextCompletionsRepository._handleListenBodyBytesTurbo (package:chatgpt_completions/src/repository/text_completions_repository.dart:201:9)
E/flutter ( 6185): #1 TextCompletionsRepository.textCompletions.. (package:chatgpt_completions/src/repository/text_completions_repository.dart:77:15)
E/flutter ( 6185): #2 DecoderQueueService.runQueue (package:chatgpt_completions/src/services/decoder_queue.dart:21:19)
E/flutter ( 6185): #3 DecoderQueueService.addQueue (package:chatgpt_completions/src/services/decoder_queue.dart:14:5)
E/flutter ( 6185): #4 TextCompletionsRepository.textCompletions. (package:chatgpt_completions/src/repository/text_completions_repository.dart:75:40)
E/flutter ( 6185): #5 _RootZone.runUnaryGuarded (dart:async/zone.dart:1593:10)
E/flutter ( 6185): #6 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
E/flutter ( 6185): #7 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
E/flutter ( 6185): #8 _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:774:19)
E/flutter ( 6185): #9 _StreamController._add (dart:async/stream_controller.dart:648:7)
E/flutter ( 6185): #10 _StreamController.add (dart:async/stream_controller.dart:596:5)
E/flutter ( 6185): #11 _DoStreamSink.onData (package:rxdart/src/transformers/do.dart:40:10)
E/flutter ( 6185): #12 _RootZone.runUnaryGuarded (dart:async/zone.dart:1593:10)
E/flutter ( 6185): #13 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
E/flutter ( 6185): #14 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
E/flutter ( 6185): #15 _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:774:19)
E/flutter ( 6185): #16 _StreamController._add (dart:async/stream_controller.dart:648:7)
E/flutter ( 6185): #17 _RootZone.runUnaryGuarded (dart:async/zone.dart:1593:10)
E/flutter ( 6185): #18 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
E/flutter ( 6185): #19 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
E/flutter ( 6185): #20 _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:774:19)
E/flutter ( 6185): #21 _StreamController._add (dart:async/stream_controller.dart:648:7)
E/flutter ( 6185): #22 _StreamController.add (dart:async/stream_controller.dart:596:5)
E/flutter ( 6185): #23 TimerStream._buildController.. (package:rxdart/src/streams/timer.dart:51:22)
E/flutter ( 6185): #24 Timer._createTimer. (dart:async-patch/timer_patch.dart:18:15)
E/flutter ( 6185): #25 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:398:19)
E/flutter ( 6185): #26 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:429:5)
E/flutter ( 6185): #27 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:192:26)

Separating GUI from server

I see nice advantages of using Flutter for the frontend/GUI of a chat app. I have not got this project running yet and it is unclear to me if the requests sent to OpenAI are sent from a server running in a separate process (maybe a separate machine) from the frontend. Sorry for my lack of undestanding about Flutter. I imagine having a server process that interfaces between the Flutter frontend and OpenAI would be essential for protecting the OpenAI keys.

how to stop the stream when response is completed - more example code

Hi, this package is great. thanks!
I am not sure how I can stop the stream based on when the response has been completed, like in your video example.

When response is completed, I would like to stop the stream: how can I do it?
If you could also share the example code used in your example video it would be useful.
Thanks!

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.