From 29fa27ef18571300a60bc02d53e775166a8b10cf Mon Sep 17 00:00:00 2001 From: Bartosz Pijet Date: Tue, 12 Nov 2024 14:49:37 +0100 Subject: [PATCH] Implement web streaming api --- .flutter-plugins-dependencies | 2 +- CHANGELOG.md | 5 +++- .../lib/bloc/openai/openai_bloc.dart | 19 ++++++++------ example_app/openai_app/pubspec.lock | 26 ++++++++++++++++++- lib/src/model/client/http_setup.dart | 4 +++ lib/src/openai.dart | 15 +++++++++++ pubspec.yaml | 4 ++- 7 files changed, 63 insertions(+), 12 deletions(-) diff --git a/.flutter-plugins-dependencies b/.flutter-plugins-dependencies index c0fed59..116d613 100644 --- a/.flutter-plugins-dependencies +++ b/.flutter-plugins-dependencies @@ -1 +1 @@ -{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"path_provider_foundation","path":"/Users/redev.rx/.pub-cache/hosted/pub.dev/path_provider_foundation-2.4.0/","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"android":[{"name":"path_provider_android","path":"/Users/redev.rx/.pub-cache/hosted/pub.dev/path_provider_android-2.2.10/","native_build":true,"dependencies":[]}],"macos":[{"name":"path_provider_foundation","path":"/Users/redev.rx/.pub-cache/hosted/pub.dev/path_provider_foundation-2.4.0/","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"/Users/redev.rx/.pub-cache/hosted/pub.dev/path_provider_linux-2.2.1/","native_build":false,"dependencies":[]}],"windows":[{"name":"path_provider_windows","path":"/Users/redev.rx/.pub-cache/hosted/pub.dev/path_provider_windows-2.3.0/","native_build":false,"dependencies":[]}],"web":[]},"dependencyGraph":[{"name":"path_provider","dependencies":["path_provider_android","path_provider_foundation","path_provider_linux","path_provider_windows"]},{"name":"path_provider_android","dependencies":[]},{"name":"path_provider_foundation","dependencies":[]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_windows","dependencies":[]}],"date_created":"2024-09-25 20:59:28.938845","version":"3.24.3","swift_package_manager_enabled":false} \ No newline at end of file +{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"cupertino_http","path":"/home/bpijet/.pub-cache/hosted/pub.dev/cupertino_http-1.5.1/","native_build":true,"dependencies":[]}],"android":[{"name":"cronet_http","path":"/home/bpijet/.pub-cache/hosted/pub.dev/cronet_http-1.3.2/","native_build":true,"dependencies":["jni"]},{"name":"jni","path":"/home/bpijet/.pub-cache/hosted/pub.dev/jni-0.10.1/","native_build":true,"dependencies":[]}],"macos":[{"name":"cupertino_http","path":"/home/bpijet/.pub-cache/hosted/pub.dev/cupertino_http-1.5.1/","native_build":true,"dependencies":[]}],"linux":[{"name":"jni","path":"/home/bpijet/.pub-cache/hosted/pub.dev/jni-0.10.1/","native_build":true,"dependencies":[]}],"windows":[{"name":"jni","path":"/home/bpijet/.pub-cache/hosted/pub.dev/jni-0.10.1/","native_build":true,"dependencies":[]}],"web":[]},"dependencyGraph":[{"name":"cronet_http","dependencies":["jni"]},{"name":"cupertino_http","dependencies":[]},{"name":"jni","dependencies":[]}],"date_created":"2024-11-12 13:59:27.464677","version":"3.24.3","swift_package_manager_enabled":false} \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index c0b4c6c..097306d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -251,4 +251,7 @@ ## 3.1.2 - Update sdk - - remove path provider \ No newline at end of file + - remove path provider + +## 3.1.3 + - Implement support for streaming responses from OpenAI on web platform \ No newline at end of file diff --git a/example_app/openai_app/lib/bloc/openai/openai_bloc.dart b/example_app/openai_app/lib/bloc/openai/openai_bloc.dart index c428097..bc156a3 100644 --- a/example_app/openai_app/lib/bloc/openai/openai_bloc.dart +++ b/example_app/openai_app/lib/bloc/openai/openai_bloc.dart @@ -3,6 +3,7 @@ import 'dart:io'; import 'package:chat_gpt_sdk/chat_gpt_sdk.dart'; import 'package:flutter/cupertino.dart'; +import 'package:flutter/foundation.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:get_it/get_it.dart'; import 'package:http/http.dart'; @@ -74,14 +75,16 @@ class OpenAIBloc extends Cubit { ///[initOpenAISdk] void initOpenAISdk() async { _openAI = OpenAI.instance.build( - token: getToken(), - apiUrl: - 'https://api.openai.com/v1/', // you can replace with your api url - enableLog: true, - baseOption: HttpSetup( - receiveTimeout: const Duration(seconds: 30), - connectTimeout: const Duration(seconds: 30), - sendTimeout: const Duration(seconds: 30))); + token: getToken(), + apiUrl: 'https://api.openai.com/v1/', // you can replace with your api url + enableLog: true, + baseOption: HttpSetup( + receiveTimeout: const Duration(seconds: 30), + connectTimeout: const Duration(seconds: 30), + sendTimeout: const Duration(seconds: 30), + streamingWebApi: kIsWeb, + ), + ); } void openAIEvent(String event, {required Function() error}) { diff --git a/example_app/openai_app/pubspec.lock b/example_app/openai_app/pubspec.lock index a3dd9ae..eefd4a4 100644 --- a/example_app/openai_app/pubspec.lock +++ b/example_app/openai_app/pubspec.lock @@ -55,7 +55,7 @@ packages: path: "../.." relative: true source: path - version: "3.1.1" + version: "3.1.2" clock: dependency: transitive description: @@ -96,6 +96,14 @@ packages: url: "https://pub.dev" source: hosted version: "5.7.0" + dio_compatibility_layer: + dependency: transitive + description: + name: dio_compatibility_layer + sha256: bb7ea1dd6fe98b8f5e3d90da408802fc3abf14d4485416e882cf1b2c8fb4b209 + url: "https://pub.dev" + source: hosted + version: "0.1.0" dio_web_adapter: dependency: transitive description: @@ -112,6 +120,22 @@ packages: url: "https://pub.dev" source: hosted version: "1.3.1" + fetch_api: + dependency: transitive + description: + name: fetch_api + sha256: "97f46c25b480aad74f7cc2ad7ccba2c5c6f08d008e68f95c1077286ce243d0e6" + url: "https://pub.dev" + source: hosted + version: "2.2.0" + fetch_client: + dependency: transitive + description: + name: fetch_client + sha256: "9666ee14536778474072245ed5cba07db81ae8eb5de3b7bf4a2d1e2c49696092" + url: "https://pub.dev" + source: hosted + version: "1.1.2" ffi: dependency: transitive description: diff --git a/lib/src/model/client/http_setup.dart b/lib/src/model/client/http_setup.dart index 24f7711..9ec2771 100644 --- a/lib/src/model/client/http_setup.dart +++ b/lib/src/model/client/http_setup.dart @@ -4,10 +4,14 @@ class HttpSetup { Duration receiveTimeout; String proxy; + // Experimental streaming web api with fetch as dio adapter + bool streamingWebApi; + HttpSetup({ this.sendTimeout = const Duration(seconds: 6), this.connectTimeout = const Duration(seconds: 6), this.receiveTimeout = const Duration(seconds: 6), this.proxy = '', + this.streamingWebApi = false, }); } diff --git a/lib/src/openai.dart b/lib/src/openai.dart index ddda814..1c1995a 100644 --- a/lib/src/openai.dart +++ b/lib/src/openai.dart @@ -23,6 +23,8 @@ import 'package:chat_gpt_sdk/src/utils/constants.dart'; import 'package:chat_gpt_sdk/src/utils/token_builder.dart'; import 'package:dio/dio.dart'; import 'package:dio/io.dart'; +import 'package:dio_compatibility_layer/dio_compatibility_layer.dart'; +import 'package:fetch_client/fetch_client.dart'; import 'client/interceptor/interceptor_wrapper.dart'; import 'edit.dart'; import 'i_openai.dart'; @@ -76,6 +78,19 @@ class OpenAI implements IOpenAI { connectTimeout: setup.connectTimeout, receiveTimeout: setup.receiveTimeout, )); + + const bool kIsWeb = bool.fromEnvironment('dart.library.js_util'); + + assert(setup.proxy.isEmpty || !setup.streamingWebApi, + 'You can\'t provide both proxy and experimental streaming api support currently'); + assert( + !setup.streamingWebApi || kIsWeb, + 'You can\'t run web specific API on other platforms'); + + if (setup.streamingWebApi) { + dio.httpClientAdapter = + ConversionLayerAdapter(FetchClient(mode: RequestMode.cors)); + } if (setup.proxy.isNotEmpty) { dio.httpClientAdapter = IOHttpClientAdapter(createHttpClient: () { final client = HttpClient(); diff --git a/pubspec.yaml b/pubspec.yaml index 6886282..7c5144f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: chat_gpt_sdk description: create chat bot and other bot with ChatGPT SDK Support GPT-4 , 3.5 and SSE Generate Prompt (Stream) -version: 3.1.2 +version: 3.1.3 homepage: https://www.facebook.com/REDEVRX repository: https://github.com/redevRx/Flutter-ChatGPT @@ -10,6 +10,8 @@ environment: dependencies: dio: ^5.7.0 + dio_compatibility_layer: ^0.1.0 + fetch_client: ^1.1.2 http_parser: ^4.0.0 # path_provider: ^2.1.4