anasfik / openai

Dart/Flutter SDK for ChatGPT and all OpenAI APIs (GPT, Dall-e..)
https://pub.dev/packages/dart_openai
MIT License
570 stars 177 forks source link

Unhandled Exception: NoSuchMethodError: Class 'String' has no instance getter 'value'. #135

Closed KyleKun closed 7 months ago

KyleKun commented 10 months ago

I am trying to create a chat completion following exactly the example and get this error:

E/flutter (13810): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: NoSuchMethodError: Class 'String' has no instance getter 'value'.
E/flutter (13810): Receiver: "auto"
E/flutter (13810): Tried calling: value
E/flutter (13810): #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:38:5)
E/flutter (13810): #1      OpenAIChat.create (package:dart_openai/src/instance/chat/chat.dart:94:59)

Any ideas what it could be?

sitrucp commented 9 months ago

Same issue here using the Create chat completion example https://github.com/anasfik/openai#create-chat-completion

After running on Android device get log messages;

Launching lib\main.dart on SM S901W in debug mode... Running Gradle task 'assembleDebug'... 94.2s √ Built build\app\outputs\flutter-apk\app-debug.apk. Installing build\app\outputs\flutter-apk\app-debug.apk... 8.8s I/flutter (29616): Error fetching OpenAI response: NoSuchMethodError: Class 'String' has no instance getter 'value'. I/flutter (29616): Receiver: "auto" I/flutter (29616): Tried calling: value

Edited to add: I commented out the toolChoice parameter and it successfully made the API call. Not sure what that is about but I'll take the win : )

// the actual request. OpenAIChatCompletionModel chatCompletion = await OpenAI.instance.chat.create( model: "gpt-3.5-turbo-1106", responseFormat: {"type": "json_object"}, seed: 6, messages: requestMessages, temperature: 0.2, maxTokens: 500, // toolChoice: "auto", );

umsu2 commented 9 months ago

it's because their docs are out of date. if you look at their source code image

it shows that toolChoice is an object that has an attribute/getter of value which doesn't exist on a String.

fly-qp commented 8 months ago

Temporary workaround if you want to force a function call:

class _ToolChoice {
  final dynamic value;

  _ToolChoice(String functionName)
      : value = {
          "type": "function",
          "function": {"name": "$functionName"}
        };
}
lukaszciastko commented 8 months ago

Temporary workaround if you want to force a function call:

class _ToolChoice {
  final dynamic value;

  _ToolChoice(String functionName)
      : value = {
          "type": "function",
          "function": {"name": "$functionName"}
        };
}

This doesn't help much. Straight away you're getting another error:

Unhandled exception:
RequestFailedException(message: '$.messages[1].content' is invalid. Please check the API reference: https://platform.openai.com/docs/api-reference., statusCode: 400)
#0      OpenAINetworkingClient.post (package:dart_openai/src/core/networking/client.dart:326:7)
<asynchronous suspension>
#1      OpenAIChat.create (package:dart_openai/src/instance/chat/chat.dart:87:12)
<asynchronous suspension>

Has this library been abandoned? It looks like there hasn't been any work on this library for two months, and nothing seems to work.

davidmigloz commented 8 months ago

Has this library been abandoned? It looks like there hasn't been any work on this library for two months, and nothing seems to work.

You can use https://pub.dev/packages/openai_dart instead. It follows the official OpenAI OpenAPI spec, making it much easier to keep it up-to-date and preventing these kinds of issues.

junixapp commented 7 months ago

same issue

anasfik commented 7 months ago

This issue is resolved, let me know if there are any other issues.

Fortunately, this package is not abandoned as I do maintain it.