<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Riyas Pullur</title>
    <description>The latest articles on DEV Community by Riyas Pullur (@riyaspullurofficial).</description>
    <link>https://dev.to/riyaspullurofficial</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F794613%2Fc6f53c29-46fd-4cfa-b540-c3ede76972db.jpeg</url>
      <title>DEV Community: Riyas Pullur</title>
      <link>https://dev.to/riyaspullurofficial</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/riyaspullurofficial"/>
    <language>en</language>
    <item>
      <title>Chat GPT OpenAI using Flutter</title>
      <dc:creator>Riyas Pullur</dc:creator>
      <pubDate>Sat, 28 Jan 2023 08:52:28 +0000</pubDate>
      <link>https://dev.to/riyaspullurofficial/chat-gpt-openai-using-flutter-48bh</link>
      <guid>https://dev.to/riyaspullurofficial/chat-gpt-openai-using-flutter-48bh</guid>
      <description>&lt;p&gt;&lt;strong&gt;How integrate chatGPT openAI in flutter&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is chatGPT?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;ChatGPT is a chatbot launched by OpenAI in November 2022. It is built on top of OpenAI’s GPT-3 family of large language models, and is fine-tuned with both supervised and reinforcement learning techniques.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;in pubspec.yaml&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;chat_gpt_sdk: ^1.0.2+1
velocity_x: ^3.6.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;main.dart&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Chat GPT',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
         useMaterial3: true,
        primarySwatch: Colors.blue,
      ),
      home: ChatScreen(),
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;chatscreen.dart&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'dart:async';

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

import 'chat_message.dart';

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

  @override
  State&amp;lt;ChatScreen&amp;gt; createState() =&amp;gt; _ChatScreenState();
}

class _ChatScreenState extends State&amp;lt;ChatScreen&amp;gt; {
  final TextEditingController _textEditingController = TextEditingController();
  final List&amp;lt;ChatMessage&amp;gt; _messages = [];
  ChatGPT? chatGPT;
  StreamSubscription? _subscription;

  @override
  void initState() {
    chatGPT = ChatGPT.instance;
    super.initState();
  }

  @override
  void dispose() {
    _subscription?.cancel();
    super.dispose();
  }

  void sendMessage() {
    /*   final String text = _textEditingController.text;
    if (text.isNotEmpty) {
      _textEditingController.clear();
      final ChatMessage message = ChatMessage(text: text, sender: "Me");
    }*/

    ChatMessage _message =
        ChatMessage(text: _textEditingController.text, sender: "User");
    setState(() {
      _messages.insert(0, _message);
    });

    _textEditingController.clear();

    final request = CompleteReq(
        prompt: _message.text, model: kTranslateModelV3, max_tokens: 200);

    _subscription = chatGPT!
        .builder("Your-api-Key")
        .onCompleteStream(request: request)
        .listen((response) {
      Vx.log(response!.choices[0].text);
      ChatMessage botMessage =
          ChatMessage(text: response.choices[0].text, sender: "bot");

      setState(() {
        _messages.insert(0, botMessage);
      });
    });
  }

  Widget _buildTextComposer() {
    return Row(
      children: [
        Expanded(
            child: TextField(
          onSubmitted: (value) =&amp;gt; sendMessage(),
          controller: _textEditingController,
          decoration:
              const InputDecoration.collapsed(hintText: "Send a message"),
        )),
        IconButton(onPressed: () =&amp;gt; sendMessage(), icon: const Icon(Icons.send))
      ],
    ).px12();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      appBar: AppBar(
        backgroundColor: Colors.black,
        title: Text(
          "Chat GPT OpenAI",
          style: TextStyle(color: Colors.white),
        ),
      ),
      body: SafeArea(
        child: Container(
          padding: EdgeInsets.only(left: 10, right: 10, bottom: 5),
          child: Column(
            children: [
              Flexible(
                  child: /*Container(
                height: context.screenHeight,
              )*/

                      ListView.builder(
                          reverse: true,
                          padding: Vx.m8,
                          itemCount: _messages.length,
                          itemBuilder: (cnx, index) {
                            return /*Container(
                              height: 100,
                              color: Colors.red,
                            ).p(5)*/
                                _messages[index];
                          })),
              Container(
                decoration: BoxDecoration(color: context.cardColor),
                child: _buildTextComposer(),
              )
            ],
          ),
        ),
      ),
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;API generation below link&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;a href="https://beta.openai.com/account/api-keys" rel="noopener noreferrer"&gt;https://beta.openai.com/account/api-keys&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;chat_message.dart&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'package:flutter/material.dart';
import 'package:velocity_x/velocity_x.dart';

class ChatMessage extends StatelessWidget {
  ChatMessage({Key? key, required this.text, required this.sender})
      : super(key: key);

  final String text;
  final String sender;

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Container(
          margin: EdgeInsets.only(right: 16),
          child: CircleAvatar(
            backgroundColor: Colors.white,
            child: Text(
              sender[0],
              style: TextStyle(color: Colors.black),
            ),
          ),
        ),
        Expanded(
            child: Column(
          children: [
            Text(
              sender,
              /* style: Theme.of(context).textTheme.subtitle1,*/
              style: TextStyle(color: Colors.white),
            )
                .text
                .subtitle1(context)
                .make()
                .box
                .alignCenter
                .white
                .alignCenterLeft
                .p3
                .makeCentered(),
            Container(
              margin: EdgeInsets.only(top: 5.0),
              child: Text(
                text.trim(),
                style: TextStyle(color: Colors.white),
              ),
            )
          ],
        ))
      ],
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Github Repository link&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://github.com/riyaspullurofficial/chatGPTapp-flutter" rel="noopener noreferrer"&gt;https://github.com/riyaspullurofficial/chatGPTapp-flutter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Request any answer by any question clear and exact matched answer provide openAI.&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
  </channel>
</rss>
