<?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: Anuj Pathak</title>
    <description>The latest articles on DEV Community by Anuj Pathak (@ctrlaltdelete).</description>
    <link>https://dev.to/ctrlaltdelete</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%2F2495320%2F0677000f-cbaa-49ce-be80-67e7d559b92c.jpg</url>
      <title>DEV Community: Anuj Pathak</title>
      <link>https://dev.to/ctrlaltdelete</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ctrlaltdelete"/>
    <language>en</language>
    <item>
      <title>How to Build a Simple AI Chatbot with GPT APIs</title>
      <dc:creator>Anuj Pathak</dc:creator>
      <pubDate>Sun, 01 Dec 2024 01:23:15 +0000</pubDate>
      <link>https://dev.to/ctrlaltdelete/how-to-build-a-simple-ai-chatbot-with-gpt-apis-34fm</link>
      <guid>https://dev.to/ctrlaltdelete/how-to-build-a-simple-ai-chatbot-with-gpt-apis-34fm</guid>
      <description>&lt;p&gt;Building an AI chatbot has never been easier, thanks to OpenAI’s powerful GPT APIs. In this guide, we’ll walk through creating a basic chatbot using the OpenAI GPT API. By the end, you’ll have a functional chatbot capable of answering questions or holding conversations in a natural language.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What You'll Need&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;API Access:&lt;/strong&gt;&lt;br&gt;
Sign up for OpenAI and get your API key from the &lt;a href="https://platform.openai.com/docs/overview" rel="noopener noreferrer"&gt;OpenAI Developer Platform&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Programming Language:&lt;/strong&gt;&lt;br&gt;
We’ll use Python for this tutorial.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Environment Setup:&lt;/strong&gt;&lt;br&gt;
Install Python on your system, and ensure you have the following libraries:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;openai (to interact with GPT APIs)&lt;/li&gt;
&lt;li&gt;flask (optional, for building a web interface)&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Step 1: Setting Up Your Python Environment&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Install Required Libraries&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Run the following commands to install the necessary Python libraries:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pip install openai flask&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Step 2: Writing the Chatbot Code&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Here’s the code for a simple GPT-powered chatbot:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Example&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 openai

# Set up your API key
openai.api_key = "your_openai_api_key"

def chatbot(prompt):
    try:
        # Call the GPT API
        response = openai.Completion.create(
            engine="text-davinci-003",  # You can also use 'gpt-3.5-turbo' or other models
            prompt=prompt,
            max_tokens=150,
            temperature=0.7,
        )
        # Extract and return the text
        return response.choices[0].text.strip()
    except Exception as e:
        return f"Error: {str(e)}"

if __name__ == "__main__":
    print("AI Chatbot: Type 'exit' to quit.")
    while True:
        user_input = input("You: ")
        if user_input.lower() == "exit":
            print("Goodbye!")
            break
        reply = chatbot(user_input)
        print(f"Bot: {reply}")

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;strong&gt;Step 3: Running the Chatbot&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Save the code in a file, e.g., chatbot.py.&lt;/li&gt;
&lt;li&gt;Open your terminal or command prompt.&lt;/li&gt;
&lt;li&gt;Run the chatbot:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;python chatbot.py&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Create a Web Interface(Optional)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Using Flask, you can make your chatbot accessible through a web page. Here’s an example:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flask Integration Code&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;from flask import Flask, request, jsonify
import openai

app = Flask(__name__)
openai.api_key = "your_openai_api_key"

@app.route("/chat", methods=["POST"])
def chat():
    data = request.json
    user_message = data.get("message", "")
    response = openai.Completion.create(
        engine="text-davinci-003",
        prompt=user_message,
        max_tokens=150,
        temperature=0.7,
    )
    return jsonify({"reply": response.choices[0].text.strip()})

if __name__ == "__main__":
    app.run(debug=True)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How to Use the Web Interface:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Save the Flask code in app.py.&lt;/li&gt;
&lt;li&gt;Run it&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;python app.py&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;3.Send a POST request with a JSON payload to *&lt;em&gt;&lt;a href="http://127.0.0.1:5000/chat" rel="noopener noreferrer"&gt;http://127.0.0.1:5000/chat&lt;/a&gt; *&lt;/em&gt; using tools like Postman or a frontend app:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{&lt;br&gt;
    "message": "Hello, how are you?"&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You’ll get a response like:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{&lt;br&gt;
    "reply": "I'm just a bot, but I'm doing well! How can I assist you?"&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Sample Output&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Terminal Interaction:&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;AI Chatbot: Type 'exit' to quit.
You: What is the capital of France?
Bot: The capital of France is Paris.
You: Who discovered gravity?
Bot: Sir Isaac Newton is credited with the discovery of gravity.
You: exit
Goodbye!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all folks! Happy coding.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
