0

I have created my custom GPT on https://chat.openai.com/. Now I want to write a flutter App to chat with this custom GPT. I have used chat_gpt_sdk and is able to communicate with Chat GPT. Unfortunately I am not sure how I can communicate with the Custom GPT I have created.

I used dash_chat_2 as the chat interface.

The code used to communicate with the Chat GPT server is as follows:

import 'package:chat_gpt_sdk/chat_gpt_sdk.dart';
import 'package:dash_chat_2/dash_chat_2.dart';
import 'package:flutter/material.dart';

import 'const.dart';

class Home extends StatefulWidget {
  const Home({Key? key}) : super(key: key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  final openAI = OpenAI.instance.build(
    token: openai_apikey,
    baseOption: HttpSetup(// Replace with your GPT API endpoint
        ),
    enableLog: true,
  );
  ChatUser user = ChatUser(
    id: '1',
    firstName: 'Charles',
    lastName: 'Leclerc',
  );

  ChatUser gpt = ChatUser(
    id: '2',
    firstName: 'Chat',
    lastName: 'GPT',
  );

  List<ChatUser> typingUsers = <ChatUser>[];

  List<ChatMessage> messages = <ChatMessage>[];

  @override
  Widget build(BuildContext context) {
    if (messages.isEmpty) {
      messages.add(
        ChatMessage(
          text: 'Hey!',
          user: user,
          createdAt: DateTime.now(),
        ),
      );
    }
    return Scaffold(
      appBar: AppBar(
        title: const Text("Chat GPT"),
      ),
      body: DashChat(
        currentUser: user,
        typingUsers: typingUsers,
        messageOptions: const MessageOptions(
          currentUserContainerColor: Colors.black,
          containerColor: Color.fromRGBO(0, 166, 126, 1),
          textColor: Colors.white,
        ),
        onSend: (ChatMessage m) {
          getChatResponse(m);
        },
        messages: messages,
      ),
    );
  }

  Future<void> getChatResponse(ChatMessage m) async {
    m.text = m.text.trim();
    setState(() {
      messages.insert(0, m);
      typingUsers.add(gpt);
    });

    List<Messages> history = messages.reversed.map((e) {
      if (e.user == user) {
        return Messages(role: Role.user, content: e.text);
      } else {
        return Messages(role: Role.assistant, content: e.text);
      }
    }).toList();
    final request = ChatCompleteText(
      model: Gpt4ChatModel(),
      messages: history,
      maxToken: 200,
    );
    final response = await openAI.onChatCompletion(request: request);
    for (var e in response!.choices) {
      if (e.message != null) {
        setState(() {
          messages.insert(
            0,
            ChatMessage(
              user: gpt,
              createdAt: DateTime.now(),
              text: e.message!.content,
            ),
          );
        });
      }
    }
    typingUsers.remove(gpt);
  }
}

Your help is highly appreciated!

3
  • What do you mean about Custom GPT? Commented Sep 1, 2024 at 3:16
  • 2
    According to this, custom GPTs are only available in ChatGPT. If you want API access, consider using an Assistant. Commented Sep 1, 2024 at 3:20
  • This question is similar to: OpenAI API: Can I access my custom ChatGPT via API?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Sep 2, 2024 at 8:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.