Assistant API calling in Flutter [SOLVED]

Hello, I’m trying to use Assistant API in Flutter.
Below me is the code for the Chatcompletion I want. I first had a message interruption because of the time was the short. That’s why I tried to change connectionTimeOut to a minute. But I still get an error that it wont work. Visible under the code.

import 'package:chat_gpt_sdk/chat_gpt_sdk.dart';

Future<void> getChatCompletion() async {
    const String token = 'API-KEY';
    final openAI = OpenAI.instance.build(
        token: token,
        baseOption: HttpSetup(
          receiveTimeout: const Duration(seconds: 5),
          connectTimeout: const Duration(seconds: 60)),
        enableLog: true);
    const String assistantId = "ASSISTANT-ID";

    final request = CreateThreadAndRun(assistantId: assistantId, thread: {
      "messages": [
        {"role": "user", "content": "$_linesText"}
      ],
    });

    // Create the thread and run
    final response =
        await openAI.threads.runs.createThreadAndRun(request: request);

    // Extract threadId from the response
    final threadId = response.threadId;

    // Retrieve messages
    final mMessage = await openAI.threads.messages.retrieveMessage(
      threadId: threadId,
      messageId: '',
    );

    print("Response: ${mMessage.content[0].text?.value}");
  }
[OpenAI] starting request https://api.openai.com/v1/threads/runs
[OpenAI] request body :{assistant_id: asst_k66DjzXXXXXXXmqJTbe3, thread: {messages: [{role: user, content: [VUILZAK GROEN 50L, 20 MARLBORO RED, 20 MARLBORO RED, B.MASSARD BRUT, SUPER PAPRIKA LAYS, 4X20CLFEVERTREEMED, 10M DDL BAKPAPIER, 10C MANO PUISS BIO, 180G TONY CHOCOLAD, 6X1.5L COKE ZER0]}]}}
E/flutter (22020): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: status code :500  message :{message: null, code: null, type: null}
E/flutter (22020): 
E/flutter (22020): #0      OpenAIClient.post (package:chat_gpt_sdk/src/client/openai_client.dart:229:7)
E/flutter (22020): <asynchronous suspension>
E/flutter (22020): #1      _ScannerState.getChatCompletion (package:smartberging/pages/Scanner.dart:334:9)
E/flutter (22020): <asynchronous suspension>
E/flutter (22020): 
[OpenAI] error code: null, message :The connection errored: Failed host lookup: 'api.openai.com' This indicates an error which most likely cannot be solved by the library. data:null

Hey TmDee, did you resolve the issue?
I am trying to use chat_gpt_sdk in flutter with a custom assistant but can’t make it work either

Yes I did!
This is how I did it:

Future<List<String>> getChatCompletion() async {
    String token = dotenv.get('OPENAI_KEY');
    String orgId = dotenv.get('OPENAI_ORG');
    final openAI = OpenAI.instance.build(
        token: token,
        orgId: orgId,
        baseOption: HttpSetup(receiveTimeout: const Duration(seconds: 5)),
        enableLog: true);

    String assistantId = dotenv.get('OPENAI_ASSISTID');

    final thread = await openAI.threads.createThread(request: ThreadRequest());
    final requestMessage = CreateMessage(
      role: 'user',
      content:
          'I want you to link these products: $_linesText to ingredient groups of the content of your csv file. The second list with previous matched ingredients: $previousMatchedIngredients .',
    );
    await openAI.threads.messages.createMessage(
      threadId: thread.id,
      request: requestMessage,
    );

    final request = CreateRun(assistantId: assistantId);

    final CreateRunResponse responseRun = await openAI.threads.runs
        .createRun(threadId: thread.id, request: request);

    String status = '';

    while (status != 'completed') {
      await Future.delayed(const Duration(seconds: 5));
      final mRunSteps = await openAI.threads.runs
          .retrieveRun(threadId: thread.id, runId: responseRun.id);
      status = mRunSteps.status;
    }
    final mmRunSteps =
        await openAI.threads.messages.listMessage(threadId: thread.id);
    final responseData = mmRunSteps.data[0].content[0].text?.value;

    final List<String>? data = responseData?.split('\n');
    print(data);
2 Likes

Hi TmDee. Can I ask what OpenAI model are you using right now? Because I am trying to use chat_gpt_sdk but facing the error “The requested model ‘gpt-4o’ cannot be used with the Assistants API in v1”?