GithubHelp home page GithubHelp logo

anasfik / openai Goto Github PK

View Code? Open in Web Editor NEW
501.0 501.0 137.0 1.59 MB

Dart/Flutter SDK for ChatGPT and all OpenAI APIs (GPT, Dall-e..)

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

License: MIT License

Dart 100.00%
ai dall-e dall-e-api dart dart-library dart-package dartlang flutter gpt-3 gpt3 openai openai-api

openai's People

Contributors

alejandroarciniegas avatar ali-aldhamen avatar alpha-se avatar anasfik avatar badboy-tian avatar baifengheixi avatar brzhang666 avatar davidmigloz avatar hiroyuki-nakayama8 avatar k9i-0 avatar leoafarias avatar lijy91 avatar lollipopkit avatar masa8 avatar micocube avatar mihirshahxenlabs avatar misteroak avatar mny459 avatar panw3i avatar pr-mais avatar rxlabz avatar signmotion avatar theseann avatar thomasdf avatar toshi17 avatar wqjuser avatar yang-chao avatar yczabcxyz avatar yush1ga avatar zmhu avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

openai's Issues

How to modify the baseURL of OpenAI?

Suggest adding an interface to modify the OpenAIConfig.baseUrl
Because some regions' inability to directly access the official API endpoint of OpenAI

Error: FormatException: SyntaxError: Expected property name or '}' in JSON at position 1.

I encountered this error when I set the maxTokens property as 4000, and upon investigation I managed to find and resolve the issue. The issue occurs in the file packages/dart_openai/src/core/networking/client.dart line 225 which resembles to final error = jsonDecode(data)['error'];. In my case setting maxTokens to 4000 fired an error: This model's maximum context length is 4097 tokens. However, you requested 4097 tokens (97 in the messages, 4000 in the completion). Please reduce the length of the messages or completion., and trying to decode this fired the error that's mentioned in the title instead.

when use web, xml format error

OpenAIChatCompletionModel chatCompletion = await OpenAI.instance.chat.create(
      model: "gpt-3.5-turbo-0301",
      messages: [
        OpenAIChatCompletionChoiceMessageModel(content: _textEditingController.text, role: "user"),
      ],
    );

error:

[OpenAI] starting request to https://api.openai.com/v1/chat/completions
Error: XMLHttpRequest error.
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 299:10  createErrorWithStack
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/core_patch.dart 355:28            _throw
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/core/errors.dart 120:5                                           throwWithStackTrace
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/zone.dart 1385:11                                          callback
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/schedule_microtask.dart 40:11                              _microtaskLoop
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/async/schedule_microtask.dart 49:5                               _startMicrotaskLoop
C:/b/s/w/ir/cache/builder/src/out/host_debug/dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 166:15           <fn>

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.7.7, on Microsoft Windows [版本 10.0.22621.1413], locale zh-CN)
[X] Windows Version (Unable to confirm if installed Windows version is 10 or greater)
[√] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[√] Chrome - develop for the web
[√] Visual Studio - develop for Windows (Visual Studio 生成工具 2019 16.11.5)
[√] Android Studio (version 2022.1)
[√] IntelliJ IDEA Community Edition (version 2021.2)
[√] Proxy Configuration
[√] Connected device (3 available)
    ! Device 192.168.31.201:5555 is offline.
[√] HTTP Host Availability

How to cancel a stream?

When using chat completion with streaming, how can we stop the server from further streaming? And will the onDone code be executed? Thanks!

Stream response from `/v1/chat/completions` endpoint is missing the first token

I am successfully streaming a response from this endpoint using the 'gpt-4' model, so I feel the implementation is right, but I am consistently missing the very first token. Is anybody else seeing this?
My code snippet below in Flutter:

Stream<OpenAIStreamChatCompletionModel> chatStream =
        await OpenAI.instance.chat.createStream(
      model: "gpt-4",
      maxTokens: isFilesChat ? 500 : 200,
      temperature: 0.7,
      messages: messages,
    );
    chatStream.listen(
      (chatStreamEvent) async {
        chatStreamSubject.add(chatStreamEvent);
        chatStreamSubscription = chatStreamSubject.stream.listen(
              (OpenAIStreamChatCompletionModel chatStreamEvent) async {
              String? _chunkResponse = chatStreamEvent.choices[0].delta.content;
              if (_chunkResponse != null) {

             String time = DateTime.now().millisecondsSinceEpoch.toString();
              await setMessageBeforeFirebaseResponse(
                context: context,
                answer: _chunkResponse,
                userQuery: userQuery,
                isLoaded: false,
                time: time,
              );
            }
          },
        );
        return Future.value('Empty');
      },
      onDone: () async {
        StreamResponseProvider responseMessage =
            Provider.of<StreamResponseProvider>(context, listen: false);
        responseMessage.isStreamDone = true;
        chatStreamSubscription?.cancel();
        chatStreamSubject.close();

      },
    );

Chat completions stream not working on flutter web

Well I've been running circles around this issue all day. Perhaps someone here can help me. I'm trying to make a chatbot demo with the package. It works wonderfully until we test on flutter web. Instead of receiving the response word by word the browser seems to be buffering the data and then sending the whole response.

Is there a workaround? I've found that using dart:html could be a possible fix but I'm not sure.

The behavior I expect is to be able to get the ChatGPT response word by word.
image

Stream functionality breaks on Web after `1.9.7`

Issue

The chat completion stream on Flutter Web platforms returns all chat at the end. Not streaming.

Using version dart_openai: ^2.0.1

See video:

flutter_chatgpt_notok.webm

Note: Works ok on other platforms (Tested on Windows)

Expected

After downgrading & pinned the package version: dart_openai: 1.9.7. The stream completion on web works normally again.

See video:

flutter_chatgpt_ok.webm

Code snippet

Stream<OpenAIStreamChatCompletionModel> chatStream =
    OpenAI.instance.chat.createStream(
  model: "gpt-3.5-turbo",
  messages: _aiMessages,
);

chatStream.listen((chatStreamEvent) {
  debugPrint(chatStreamEvent.toString());
  // existing id: just update to the same text bubble
  if (chatResponseId == chatStreamEvent.id) {
    chatResponseContent +=
        chatStreamEvent.choices.first.delta.content ?? '';

    _addMessageStream(chatResponseContent);

    if (chatStreamEvent.choices.first.finishReason == "stop") {
      isAiTyping = false;
      _aiMessages.add(OpenAIChatCompletionChoiceMessageModel(
        content: chatResponseContent,
        role: OpenAIChatMessageRole.assistant,
      ));
      chatResponseId = '';
      chatResponseContent = '';
    }
  } else {
    // new id: create new text bubble
    chatResponseId = chatStreamEvent.id;
    chatResponseContent = chatStreamEvent.choices.first.delta.content ?? '';
    onMessageReceived(id: chatResponseId, message: chatResponseContent);
    isAiTyping = true;
  }
});

Full code: https://github.com/iqfareez/flutter_chatgpt/blob/main/lib/screens/chat_page.dart

Help needed with file uploading/content retrieving

Hi, I am trying to upload file and getting file content. File can be uploaded successfully but i am not able to retrieve its content and get format exception. If i use your package, then i get issue of file is not in proper format which is jsonLines issue. so i have created method to create jsonLines from files. Please check and let me know what is your procedure to upload files and get content. My current method for retrieving content returns the same content that was uploaded to that can't be good approach. I am new to it. So i am looking your suggestions and help. My purpose behind this code is to upload any file to openai and then ask questions related to file only. Kind regards.

class UpLoadFiles {
  static late FileUploadingProvider fileProvider;

  static List<dynamic> fileIds = [];

  static Future<List<dynamic>> getFileIds() async {
    try {
      final url = Uri.parse('${ApiUrls.gptBaseUrl}${ApiUrls.files}');
      List<dynamic> ids = [];
      final response = await http.get(
        url,
        headers: {'Authorization': 'Bearer ${ApiUrls.gptApi}'},
      );
      if (response.statusCode == 200) {
        final data = await jsonDecode(response.body);
        for (var i in data['data']) {
          ids.add(i['id']);
        }
        return ids;
      } else {
        log('got error');
        showToast(msg: 'Something went wrong');
        log(ids.toString());
        return [];
      }
    } catch (e) {
      showToast(msg: 'Something went wrong');
      return [];
    }
  }

  static Future<String> getFileContent({required String fileId}) async {
    try {
      final url = Uri.parse(
          '${ApiUrls.gptBaseUrl}${ApiUrls.files}/$fileId${ApiUrls.fileContent}');
      final response = await http.get(
        url,
        headers: {'Authorization': 'Bearer ${ApiUrls.gptApi}'},
      );
      if (response.statusCode == ApiConstants.success) {
        String jsonString = response.body;
        // String actualString = removeWhitespace(jsonString);
        log('.....................Actual String:.........${jsonString}');
        Map<String, dynamic> data = jsonDecode(response.body);
        return data['prompt'];
      } else {
        log('got error');
        showToast(msg: 'Something went wrong');
        return 'No content provided';
      }
    } catch (e) {
      showToast(msg: 'Something went wrong $e');
      log('................getFileContentError.........${e.toString()}');
      return 'No content provided';
    }
  }

  static Future<void> deleteFileId() async {
    if (fileIds.isNotEmpty) {
      log(fileIds.toString());
      for (int i = 0; i < fileIds.length; i++) {
        log('${fileIds.length}');
        String fileId = fileIds[i];
        final Uri url =
            Uri.parse('${ApiUrls.gptBaseUrl}${ApiUrls.files}/$fileId');
        final response = await http.delete(
          url,
          headers: {'Authorization': 'Bearer ${ApiUrls.gptApi}'},
        );
        log('$response');
        log(response.body.toString());
        if (response.statusCode == 200) {
          // final data = await jsonDecode(response.body);
          // logg.log('$data');
          log("File $i: deleted");
          showToast(msg: 'file deleted');
        } else {
          log('got error deleting fiel: $i');
          showToast(msg: 'Couldn\'t delete file');
        }
      }
    }
  }

  upLoadFile(BuildContext context) async {
    final result = await DBServices.pickFile();
    String filePath = result!.files.first.path!;

    File? file = File(filePath);
    String? fileType = await detectFileType(file);
    log('.................Extension: ${fileType}');
    if (filePath.isEmpty) {
      showToast(msg: 'file not picked', toastLength: Toast.LENGTH_LONG);
    } else {
      fileProvider = Provider.of<FileUploadingProvider>(context, listen: false);
      fileProvider.setFileStatus = 'upLoading';

      await fileToJsonLines(file: file, fileType: fileType!)
          .then((value) async {
        await uploadFile(context, value);
      });
    }
  }

  Future<String> fileToJsonLines(
      {required File file, required String fileType}) async {
    String read = '';
    String jsonLinesStr = '';
    String filePath = await removeWhitespace(file.path);
    log('File : ${filePath}');

    try {
      if (file.path.endsWith('.txt') || fileType.contains('text/plain')) {
        read = await file.readAsString();
      } else if (file.path.endsWith('.pdf') ||
          fileType.contains('application/pdf')) {
        Uint8List pdfBytes = file.readAsBytesSync();
        PdfDocument document = PdfDocument(inputBytes: pdfBytes);
        PdfTextExtractor extractor = PdfTextExtractor(document);

        read = extractor.extractText();
        //log('read of .pdf: ${read.length}');
        //log('read of .pdf: \n $read');
      }

      List<String> paragraphs = read.split('\n');
      List<String> jsonLines = [];

      for (var paragraph in paragraphs) {
        while (paragraph.isNotEmpty) {
          String prompt = paragraph;
          paragraph = ''; // Remove the processed portion

          Map<String, dynamic> jsonData = {
            'prompt': prompt,
            'completion': '',
          };
          String json = jsonEncode(jsonData);
          jsonLines.add(json);
        }
      }

      jsonLinesStr = jsonLines.join('\n');
      log('final jsonLines: \n $jsonLinesStr');
    } catch (error) {
      log('.....................fileToJsonLinesError........${error.toString()}');
    }

    return jsonLinesStr;
  }

  Future<String> uploadFile(BuildContext context, var value) async {
    String fileContent = '';

    final url = Uri.parse('${ApiUrls.gptBaseUrl}${ApiUrls.files}');
    final request = http.MultipartRequest('POST', url);
    request.headers['Authorization'] = 'Bearer ${ApiUrls.gptApi}';
    request.fields['purpose'] = 'fine-tune';
    request.files.add(http.MultipartFile.fromString(
      'file',
      value,
      filename: 'output.jsonl', // Always use this name for the uploaded file
      contentType: MediaType('application', 'jsonl'),
    ));
    final streamedResponse = await request.send();
    final response = await http.Response.fromStream(streamedResponse);

    log(response.statusCode.toString());
    log(response.body.toString());
    if (response.statusCode == ApiConstants.success) {
      fileProvider.setFileStatus = 'upLoaded';
      await getFileIds().then((value) async {
        fileIds = value;
        //log('file id : ${value[0]}');
        await getFileContent(fileId: value[0]).then((value) {
          fileProvider.setFileContent = value;
          fileContent = value;
        });
        return fileContent;
      });
    } else {
      //log('.................Uploading Error:${response.body}');
      showToast(msg: 'Error uploading file: ${response.body}');
      return '';
    }
    return fileContent;
  }
}

Flutter's macos desktop app times out

In China.

[OpenAI] accessing endpoint: /models
[OpenAI] starting request to https://api.openai.com/v1/models

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Operation timed out
#0 IOClient.send
io_client.dart:88

#1 BaseClient._sendUnstreamed
base_client.dart:93

#2 _withClient
http.dart:164

#3 OpenAINetworkingClient.get
client.dart:21

#4 OpenAIModel.list

UnHandled exception _ClientSocketException

When I try to get chat completion stream and there is no internet connection this error happens, and I cant use a 'try-catch' since the package returns the stream before the connection is done.
my code :

Future<Stream<ItemBodyCompletion>> itemBodyCompletionStream(
   String system,
   String user,
   String assistant,
 ) async {
   final bodyCompletion = openAI.chat.createStream(
     model: "gpt-3.5-turbo",
     messages: [
       OpenAIChatCompletionChoiceMessageModel(
         role: OpenAIChatMessageRole.system,
         content: system,
       ),
       OpenAIChatCompletionChoiceMessageModel(
         role: OpenAIChatMessageRole.user,
         content: user,
       ),
       OpenAIChatCompletionChoiceMessageModel(
         role: OpenAIChatMessageRole.assistant,
         content: assistant,
       ),
     ],
   );
   final stream = StreamController<ItemBodyCompletion>();
   bodyCompletion.listen((event) {
     return stream
         .add(ItemBodyCompletion(body: event.choices[0].delta.content ?? ""));
   }, onDone: () {
     stream.close();
   }, onError: (e) {
     stream.addError(e);
   });
   return stream.stream;
 }

I am already using a try-catch statement when calling this function but the error is not caught or passed to stream's onError.
Exception is happening in io_client.dart file line 88 like this :
_ClientSocketException (Failed host lookup: 'api.openai.com')

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Null check operator used on a null value

Hi there!
Just upgraded my project to Flutter 3.10.2 / Dart 3.0.2 and i fear something is broken now.
I have dart_openai: ^1.9.93 and http: ^0.13.6 in my pubspec.yaml (nothing changes even reverting to some minor versions such as dart_openai 1.9.91 or http 0.13.5 which were working great before my flutter upgrade).

My android app listens to a Stream to update a list of strings and return some info on the screen but now, when the stream response is done, nothing is displayed and i get the following lines in the console:

[OpenAI] stream response is done
E/flutter (11371): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Null check operator used on a null value
E/flutter (11371): #0 OpenAINetworkingClient.postStream.close
client.dart:264
E/flutter (11371): #1 OpenAINetworkingClient.postStream..
client.dart:317
E/flutter (11371): #2 _RootZone.runGuarded (dart:async/zone.dart:1582:10)
E/flutter (11371): #3 _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:392:13)
E/flutter (11371): #4 _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:402:7)
E/flutter (11371): #5 _BufferingStreamSubscription._close (dart:async/stream_impl.dart:291:7)
E/flutter (11371): #6 _SinkTransformerStreamSubscription._close (dart:async/stream_transformers.dart:87:11)
E/flutter (11371): #7 _EventSinkWrapper.close (dart:async/stream_transformers.dart:21:11)
E/flutter (11371): #8 _StringAdapterSink.close (dart:convert/string_conversion.dart:241:11)
E/flutter (11371): #9 _LineSplitterSink.close (dart:convert/line_splitter.dart:141:11)
E/flutter (11371): #10 _SinkTransformerStreamSubscription._handleDone (dart:async/stream_transformers.dart:132:24)
E/flutter (11371): #11 _RootZone.runGuarded (dart:async/zone.dart:1582:10)
E/flutter (11371): #12 _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:392:13)
E/flutter (11371): #13 _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:402:7)
E/flutter (11371): #14 _BufferingStreamSubscription._close (dart:async/stream_impl.dart:291:7)
E/flutter (11371): #15 _SinkTransformerStreamSubscription._close (dart:async/stream_transformers.dart:87:11)
E/flutter (11371): #16 _EventSinkWrapper.close (dart:async/stream_transformers.dart:21:11)
E/flutter (11371): #17 _StringAdapterSink.close (dart:convert/string_conversion.dart:241:11)
E/flutter (11371): #18 _Utf8ConversionSink.close (dart:convert/string_conversion.dart:295:20)
E/flutter (11371): #19 _ConverterStreamEventSink.close (dart:convert/chunked_conversion.dart:78:18)
E/flutter (11371): #20 _SinkTransformerStreamSubscription._handleDone (dart:async/stream_transformers.dart:132:24)
E/flutter (11371): #21 _RootZone.runGuarded (dart:async/zone.dart:1582:10)
E/flutter (11371): #22 _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:392:13)
E/flutter (11371): #23 _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:402:7)
E/flutter (11371): #24 _BufferingStreamSubscription._close (dart:async/stream_impl.dart:291:7)
E/flutter (11371): #25 _ForwardingStream._handleDone (dart:async/stream_pipe.dart:99:10)
E/flutter (11371): #26 _ForwardingStreamSubscription._handleDone (dart:async/stream_pipe.dart:161:13)
E/flutter (11371): #27 _HttpClientResponse.listen. (dart:_http/http_impl.dart:726:15)
E/flutter (11371): #28 _RootZone.runGuarded (dart:async/zone.dart:1582:10)
E/flutter (11371): #29 _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:392:13)
E/flutter (11371): #30 _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:402:7)
E/flutter (11371): #31 _BufferingStreamSubscription._close (dart:async/stream_impl.dart:291:7)
E/flutter (11371): #32 _ForwardingStream._handleDone (dart:async/stream_pipe.dart:99:10)
E/flutter (11371): #33 _ForwardingStreamSubscription._handleDone (dart:async/stream_pipe.dart:161:13)
E/flutter (11371): #34 _RootZone.runGuarded (dart:async/zone.dart:1582:10)
E/flutter (11371): #35 _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:392:13)
E/flutter (11371): #36 _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:402:7)
E/flutter (11371): #37 _BufferingStreamSubscription._close (dart:async/stream_impl.dart:291:7)
E/flutter (11371): #38 _SyncStreamControllerDispatch._sendDone (dart:async/stream_controller.dart:784:19)
E/flutter (11371): #39 _StreamController._closeUnchecked (dart:async/stream_controller.dart:639:7)
E/flutter (11371): #40 _StreamController.close (dart:async/stream_controller.dart:632:5)
E/flutter (11371): #41 _HttpParser._closeIncoming (dart:_http/http_parser.dart:1147:18)
E/flutter (11371): #42 _HttpParser._doParse (dart:_http/http_parser.dart:813:11)
E/flutter (11371): #43 _HttpParser._parse (dart:_http/http_parser.dart:319:7)
E/flutter (11371): #44 _HttpParser._onData (dart:_http/http_parser.dart:873:5)
E/flutter (11371): #45 _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
E/flutter (11371): #46 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
E/flutter (11371): #47 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
E/flutter (11371): #48 _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:776:19)
E/flutter (11371): #49 _StreamController._add (dart:async/stream_controller.dart:650:7)
E/flutter (11371): #50 _StreamController.add (dart:async/stream_controller.dart:598:5)
E/flutter (11371): #51 _Socket._onData (dart:io-patch/socket_patch.dart:2368:41)
E/flutter (11371): #52 _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
E/flutter (11371): #53 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
E/flutter (11371): #54 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
E/flutter (11371): #55 _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:776:19)
E/flutter (11371): #56 _StreamController._add (dart:async/stream_controller.dart:650:7)
E/flutter (11371): #57 _StreamController.add (dart:async/stream_controller.dart:598:5)
E/flutter (11371): #58 _RawSecureSocket._sendReadEvent (dart:io/secure_socket.dart:1114:19)
E/flutter (11371): #59 Timer._createTimer. (dart:async-patch/timer_patch.dart:18:15)
E/flutter (11371): #60 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:398:19)
E/flutter (11371): #61 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:429:5)
E/flutter (11371): #62 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:189:12)
E/flutter (11371):

i guess the app gets stuck in client.dart at the lines 264 and 317, but i don't want to mess up with that file.
what can i do?

thanks

Overall slowness

Hey there!

Just wanted to confirm some expected behavior. Calls are taking about 20 seconds to return a response, is that expected behavior?

If not, any tips on optimizing the prompt for quicker responses?

Can't pass the parameter to OpenAIChatCompletionChoiceMessageModel.

Hi everyone,
I have encountered a problem where I am unable to pass a parameter to the constructor. The suggested solution was to remove the 'const' keyword from the constructor. What modification do I need to make to resolve this issue? Thank you.

  void callOpenAI(String prompt1) async {
    
    Stream<OpenAIStreamChatCompletionModel> chatStream = OpenAI.instance.chat.createStream(
      model: "gpt-3.5-turbo",
      messages: [
        const OpenAIChatCompletionChoiceMessageModel(
          content: prompt1, // 'hello' works.
          role: OpenAIChatMessageRole.user,
        )
      ],
    );
...

image

  /// {@macro openai_chat_completion_choice_message_model}
  const OpenAIChatCompletionChoiceMessageModel({
    required this.role,
    required this.content,
  });

Cannot mock for final class

I'm testing a project using dart_openai. I need to mock all openai requests.
I got error:
The class 'OpenAIImageModel' can't be implemented outside of its library because it's a final class.

The offical issue said they will not support final class mock:
Mocking final classes?

Will you remove the final modifier ? or any workaround?

FormatException

Get an error from sentry, not sure how to produce. Hope could be useful.

FormatException: FormatException: Unexpected character (at character 1)
<html>
^

  File "convert_patch.dart", line 1383, in _ChunkedJsonParser.fail
  File "convert_patch.dart", line 1250, in _ChunkedJsonParser.parseNumber
  File "convert_patch.dart", line 915, in _ChunkedJsonParser.parse
  File "convert_patch.dart", line 35, in _parseJson
  File "json.dart", line 610, in JsonDecoder.convert
  File "json.dart", line 216, in JsonCodec.decode
  File "json.dart", line 155, in jsonDecode
  File "client.dart", line 219, in OpenAINetworkingClient.postStream.<fn>.<fn>
  File "zone.dart", line 1593, in _RootZone.runUnaryGuarded
  File "stream_impl.dart", line 339, in _BufferingStreamSubscription._sendData
  File "stream_impl.dart", line 271, in _BufferingStreamSubscription._add
  File "stream_pipe.dart", line 123, in _ForwardingStreamSubscription._add
  File "stream_pipe.dart", line 253, in _HandleErrorStream._handleData
  File "stream_pipe.dart", line 153, in _ForwardingStreamSubscription._handleData
  File "zone.dart", line 1593, in _RootZone.runUnaryGuarded
  File "stream_impl.dart", line 339, in _BufferingStreamSubscription._sendData
  File "stream_impl.dart", line 271, in _BufferingStreamSubscription._add
  File "stream_pipe.dart", line 123, in _ForwardingStreamSubscription._add
  File "stream_pipe.dart", line 253, in _HandleErrorStream._handleData
  File "stream_pipe.dart", line 153, in _ForwardingStreamSubscription._handleData
  File "zone.dart", line 1593, in _RootZone.runUnaryGuarded
  File "stream_impl.dart", line 339, in _BufferingStreamSubscription._sendData
  File "stream_impl.dart", line 515, in _DelayedData.perform
  File "stream_impl.dart", line 620, in _PendingEvents.handleNext
  File "stream_impl.dart", line 591, in _PendingEvents.schedule.<fn>
  File "schedule_microtask.dart", line 40, in _microtaskLoop
  File "schedule_microtask.dart", line 49, in _startMicrotaskLoop

Connection failed when accessing OpenAI API in Flutter desktop app on macOS

Getting the following error:

[OpenAI] accessing endpoint: /completions
[OpenAI] starting request to https://api.openai.com/v1/completions
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Connection failed
#0      IOClient.send
io_client.dart:88
<asynchronous suspension>
#1      BaseClient._sendUnstreamed
base_client.dart:93
<asynchronous suspension>
#2      _withClient
http.dart:164
<asynchronous suspension>
#3      OpenAINetworkingClient.post
client.dart:105
<asynchronous suspension>
#4      OpenAICompletion.create
completion.dart:118
<asynchronous suspension>

This issue is specific to the Flutter desktop macOS platform. We have tested the same API endpoint on Flutter web and it works without any issues.

Help needed with 'Create a chat completion stream'

Hi! i got some doubts about how to get a stream response from OpenAIStreamChatCompletionModel (building a mobile app).

Looking at the doc it looks like the only thing i need to do to print the chunks of text in response is:

chatStream.listen((chatStreamEvent) {
 print(**chatStreamEvent**); // ...
});

The problem is i'm getting lines like these in response:

OpenAIStreamChatCompletionModel(id: chatcmpl-6y1gmLRNdjiNwX4G2825Df94R2HQU, created: 2023-03-25 17:09:52.000, choices: [OpenAIStreamChatCompletionChoiceModel(index: 0, delta: OpenAIStreamChatCompletionChoiceMessageModel(role: null, content:  in), finishReason: null)])
OpenAIStreamChatCompletionModel(id: chatcmpl-6y1gmLRNdjiNwX4G2825Df94R2HQU, created: 2023-03-25 17:09:52.000, choices: [OpenAIStreamChatCompletionChoiceModel(index: 0, delta: OpenAIStreamChatCompletionChoiceMessageModel(role: null, content:  sol), finishReason: null)])
OpenAIStreamChatCompletionModel(id: chatcmpl-6y1gmLRNdjiNwX4G2825Df94R2HQU, created: 2023-03-25 17:09:52.000, choices: [OpenAIStreamChatCompletionChoiceModel(index: 0, delta: OpenAIStreamChatCompletionChoiceMessageModel(role: null, content: i), finishReason: null)])

I finally got it to work accessing 'chatStreamEvent.choices.first.delta.content' before printing, but i don't know if this is the way it should work or i'm doing something wrong.

Thanks a lot

Context chat conversation Error

How to manage conversation context using you api?
My chatbot does not have possibility for remember chat convesation even if we are on the same page
How can I fix this hug essue

Support for Azure OpenAI

I hope to increase support for Azure OpenAI, just like the openai-python Python library.

#Note: The openai-python library support for Azure OpenAI is in preview.
import os
import openai
openai.api_type = "azure"
openai.api_base = "https://example.openai.azure.com/"
openai.api_version = "2022-12-01"
openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.Completion.create(
  engine="gpt-35-turbo",
  prompt="",
  temperature=1,
  max_tokens=100,
  top_p=0.5,
  frequency_penalty=0,
  presence_penalty=0,
  stop=None)

Response from OpenAIStreamChatCompletionModel missing "usage"

In the response from OpenAI.instance.chat.createStream() I see everything from official API Reference but not "usage".

Official API from OpenAI Reference:

{ "id": "chatcmpl-123", "object": "chat.completion", "created": 1677652288, "choices": [{ "index": 0, "message": { "role": "assistant", "content": "\n\nHello there, how may I assist you today?", }, "finish_reason": "stop" }], "usage": { "prompt_tokens": 9, "completion_tokens": 12, "total_tokens": 21 } }

Thanks a lot for your work!

error use 1.9.0

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
                                                                                                    #0      OpenAINetworkingClient.postStream.<anonymous closure>.<anonymous closure> (package:dart_openai/src/core/networking/client.dart:228:24)
                                                                                                    #1      _RootZone.runUnaryGuarded (dart:async/zone.dart:1593:10)
                                                                                                    #2      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
                                                                                                    #3      _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
                                                                                                    #4      _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:63:11)
                                                                                                    #5      _EventSinkWrapper.add (dart:async/stream_transformers.dart:13:11)
                                                                                                    #6      _StringAdapterSink.add (dart:convert/string_conversion.dart:238:11)
                                                                                                    #7      _LineSplitterSink.close (dart:convert/line_splitter.dart:134:13)
                                                                                                    #8      _SinkTransformerStreamSubscription._handleDone (dart:async/stream_transformers.dart:132:24)
                                                                                                    #9      _RootZone.runGuarded (dart:async/zone.dart:1581:10)
                                                                                                    #10     _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:392:13)
                                                                                                    #11     _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:402:7)
                                                                                                    #12     _BufferingStreamSubscription._close (dart:async/stream_impl.dart:291:7)
                                                                                                    #13     _SinkTransformerStreamSubscription._close (dart:async/stream_transformers.dart:87:11)
                                                                                                    #14     _EventSinkWrapper.close (dart:async/stream_transformers.dart:21:11)
                                                                                                    #15     _StringAdapterSink.close (dart:convert/string_conversion.dart:251:11)
                                                                                                    #16     _Utf8ConversionSink.close (dart:convert/string_conversion.dart:305:20)
                                                                                                    #17     _ConverterStreamEventSink.close (dart:convert/chunked_conversion.dart:81:18)
                                                                                                    #18     _SinkTransformerStreamSubscription._handleDone (dart:async/stream_transformers.dart:132:24)
                                                                                                    #19     _RootZone.runGuarded (dart:async/zone.dart:1581:10)
                                                                                                    #20     _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:392:13)
                                                                                                    #21     _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:402:7)
                                                                                                    #22     _BufferingStreamSubscription._close (dart:async/stream_impl.dart:291:7)
                                                                                                    #23     _ForwardingStream._handleDone (dart:async/stream_pipe.dart:99:10)
                                                                                                    #24     _ForwardingStreamSubscription._handleDone (dart:async/stream_pipe.dart:161:13)
                                                                                                    #25     _HttpClientResponse.listen.<anonymous closure> (dart:_http/http_impl.dart:714:15)
                                                                                                    #26     _RootZone.runGuarded (dart:async/zone.dart:1581:10)
                                                                                                    #27     _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:392:13)
                                                                                                    #28     _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:402:7)
                                                                                                    #29     _BufferingStreamSubscription._close (dart:async/stream_impl.dart:291:7)
                                                                                                    #30     _ForwardingStream._handleDone (dart:async/stream_pipe.dart:99:10)
                                                                                                    #31     _ForwardingStreamSubscription._handleDone (dart:async/stream_pipe.dart:161:13)
                                                                                                    #32     _RootZone.runGuarded (dart:async/zone.dart:1581:10)
                                                                                                    #33     _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:392:13)
                                                                                                    #34     _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:402:7)
                                                                                                    #35     _DelayedDone.perform (dart:async/stream_impl.dart:534:14)
                                                                                                    #36     _PendingEvents.handleNext (dart:async/stream_impl.dart:620:11)
                                                                                                    #37     _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:591:7)
                                                                                                    #38     _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
                                                                                                    #39     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)

Error Whisper formatting to srt, vtt

Hi I'm trying to transcribe a file to srt or vtt.

thats my code:

    var transcription = await OpenAI.instance.audio.createTranscription(
       file: File(file),
       model: "whisper-1",
       language: "en",
       responseFormat: OpenAIAudioResponseFormat.srt
     );
       var transcriptionResult = transcription.text;

But it throws this error:

VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Unexpected character (at line 2, character 1)
00:00:00,000 --> 00:00:06,320
^

#0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
#1 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:913:48)
#2 _parseJson (dart:convert-patch/convert_patch.dart:35:10)
#3 JsonDecoder.convert (dart:convert/json.dart:610:36)
#4 JsonCodec.decode (dart:convert/json.dart:216:41)
#5 jsonDecode (dart:convert/json.dart:155:10)
#6 OpenAINetworkingClient.decodeToMap (package:dart_openai/src/core/networking/client.dart:535:12)
#7 OpenAINetworkingClient.fileUpload (package:dart_openai/src/core/networking/client.dart:475:46)

#8 OpenAIAudio.createTranscription (package:dart_openai/src/instance/audio/audio.dart:56:12)

#9 OpenAiDataSourceImpl.transcribeFile (package:voicetotext/data/network/datasource/impl/openai_datasourc<…>

Not able to calling functions

I'm struggling to send the request using functions, Am I doing something wrong? Maybe this model don't accept the functions?? I'm trying to use the gpt-4 aswell, but without success, keep telling the model doesn't exists.

 final response = await OpenAI.instance.chat.create(
          model: 'gpt-3.5-turbo',
          messages: [
            ..._messages.value?.map((e) => e.toOpenAi()).toList() ?? [],
            userMessage.toOpenAi(),
          ],
          functionCall: FunctionCall.auto,
          functions: [
            OpenAIFunctionModel.withParameters(
              name: 'get_owner_name',
              description: 'Get the name of the owner of the bot',
              parameters: const [],
            ),
          ],
        );
[OpenAI] request to https://api.openai.com/v1/chat/completions finished with status code 404
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: RequestFailedException{message: Unrecognized request argument supplied: functions, statusCode: 404}

When using 'chat_campletion_stream_example.dart,' I always receive "null" at the beginning and end.

When using the code in 'chat_campletion_stream_example.dart,' I always receive "null" at the beginning and end.
The first and the last word are missing.

final chatStreamTest = OpenAI.instance.chat.createStream(
  model: "gpt-3.5-turbo",
  messages: [
    OpenAIChatCompletionChoiceMessageModel(
      content: "hello",
      role: "user",
    )
  ],
);

// Listen to the stream.
chatStreamTest.listen(
      (streamChatCompletion) {
    final content = streamChatCompletion.choices.first.delta.content;
    print(content);
  },
  onError: (error) {
    print(error);
  },
  cancelOnError: false,
  onDone: () {
    print("Done");
  },
);

Does the web support this?

Thank you for creating the Awesome Library.

Does the web support this?
I tried using ChatGPT and it seemed to work fine in Chrome.
If you support it, it looks like you should add web below.

image

Handling Unhandled Exceptions

This error was caused by the openai chat stream api call terminating mid-response. Do you have any recommendations on how to a) Message the user of the OpenAI issue and b) retry the request?

Two scenarios where this happens frequently are 1) when openai apis get busy and they respond with an error to try again later and 2) I exceed the total token limit. In the first case, I want to retry by sending a [Space] to nudge the AI to continue. In the second case I just want to notify the user with a dialog and provide instructions on how to continue.

Thanks again for an excelent package.

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Unexpected end of input (at character 2)
{
^

#0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1383:5)
#1 _ChunkedJsonParser.close (dart:convert-patch/convert_patch.dart:501:7)
#2 _parseJson (dart:convert-patch/convert_patch.dart:36:10)
#3 JsonDecoder.convert (dart:convert/json.dart:610:36)
#4 JsonCodec.decode (dart:convert/json.dart:216:41)
#5 jsonDecode (dart:convert/json.dart:155:10)
#6 OpenAINetworkingClient.decodeToMap (package:dart_openai/src/core/networking/client.dart:518:12)
#7 OpenAINetworkingClient.postStream.. (package:dart_openai/src/core/networking/client.dart:290:37)
#8 _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#9 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#10 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#11 _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:63:11)
#12 _EventSinkWrapper.add (dart:async/stream_transformers.dart:13:11)
#13 _StringAdapterSink.add (dart:convert/string_conversion.dart:228:11)
#14 _LineSplitterSink._addLines (dart:convert/line_splitter.dart:164:13)
#15 _LineSplitterSink.addSlice (dart:convert/line_splitter.dart:131:7)
#16 StringConversionSink.add (dart:convert/string_conversion.dart:39:5)
#17 _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
#18 _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#19 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#20 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#21 _SinkTransformerStreamSubscription._add (dart:async/stream_transformers.dart:63:11)
#22 _EventSinkWrapper.add (dart:async/stream_transformers.dart:13:11)
#23 _StringAdapterSink.add (dart:convert/string_conversion.dart:228:11)
#24 _StringAdapterSink.addSlice (dart:convert/string_conversion.dart:233:7)
#25 _Utf8ConversionSink.addSlice (dart:convert/string_conversion.dart:307:20)
#26 _Utf8ConversionSink.add (dart:convert/string_conversion.dart:300:5)
#27 _ConverterStreamEventSink.add (dart:convert/chunked_conversion.dart:69:18)
#28 _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
#29 _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#30 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#31 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#32 _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#33 _HandleErrorStream._handleData (dart:async/stream_pipe.dart:253:10)
#34 _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#35 _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#36 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#37 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#38 _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)
#39 _HandleErrorStream._handleData (dart:async/stream_pipe.dart:253:10)
#40 _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)
#41 _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#42 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#43 _DelayedData.perform (dart:async/stream_impl.dart:515:14)
#44 _PendingEvents.handleNext (dart:async/stream_impl.dart:620:11)
#45 _PendingEvents.schedule. (dart:async/stream_impl.dart:591:7)
#46 _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#47 _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)

Upgrade http to ^1.0.0

Currently package uses http ^0.13.5 which prevents package usage in projects with http ^1.0.0.

fetch_client has now been updated to use ^1.0.1 so upgrade is possible.

Exception on image.create with b64json responseFormat

You can't actually call the image.create if you choose the b64 response :

final response = await OpenAI.instance.image.create(
    prompt: prompt,
    n: 1,
    size: OpenAIImageSize.size256,
    responseFormat: OpenAIImageResponseFormat.b64Json,
  );

When you do you get this exception

Unhandled exception:
type 'Null' is not a subtype of type 'String'
#0      new OpenAIImageData.fromMap (package:dart_openai/src/core/models/image/image/sub_models/data.dart:18:37)
#1      new OpenAIImageModel.fromMap.<anonymous closure> (package:dart_openai/src/core/models/image/image/image.dart:29:39)
#2      MappedListIterable.elementAt (dart:_internal/iterable.dart:415:31)
#3      ListIterator.moveNext (dart:_internal/iterable.dart:344:26)
#4      new _GrowableList._ofEfficientLengthIterable (dart:core-patch/growable_array.dart:189:27)
#5      new _GrowableList.of (dart:core-patch/growable_array.dart:150:28)
#6      new List.of (dart:core-patch/array_patch.dart:52:28)
#7      ListIterable.toList (dart:_internal/iterable.dart:214:7)
#8      new OpenAIImageModel.fromMap (package:dart_openai/src/core/models/image/image/image.dart:30:12)
#9      OpenAIImages.create.<anonymous closure> (package:dart_openai/src/instance/images/images.dart:72:45)
#10     OpenAINetworkingClient.post (package:dart_openai/src/core/networking/client.dart:130:23)
<asynchronous suspension>
#11     OpenAIImages.create (package:dart_openai/src/instance/images/images.dart:70:12)
<asynchronous suspension>
#12     requestImg (package:gpt_api/gpt_api.dart:45:20)
<asynchronous suspension>
#13     generateFicheImages (file:///Users/rxlabz/projects/lilu/lilu023/protos/gpt_api/bin/gpt_api.dart:108:25)
<asynchronous suspension>
#14     saveLesson (file:///Users/rxlabz/projects/lilu/lilu023/protos/gpt_api/bin/gpt_api.dart:69:5)
<asynchronous suspension>
#15     main (file:///Users/rxlabz/projects/lilu/lilu023/protos/gpt_api/bin/gpt_api.dart:36:3)
<asynchronous suspension>

It's because the OpenAIImageData constructor wait for a non nullable url, and not for the b64json data.

数据流处理bug(数据丢失) / Dataflow processing bug (data loss)

3c8d9ea7c3b74adc1d37cd92a15cb85a
image
image
对比第二张图和第三张图就会发现,即使流数据中内容不为空,也会存在数据部分丢失,我正在尝试修复,我觉得这是一个小bug,但是我的水平还比较低,不一定能成功解决
translated :
Comparing the second picture with the third picture, you will find that even if the content in the stream data is not empty, some data will be lost. I am trying to fix it. I think this is a small bug, but my level is still relatively low , may not be able to successfully resolve.
more details
image
image

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.