Back

Step by Step Guide to Building a ChatGPT API Integration in PHP

Aug 1, 20245 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of AI-powered conversations? You're in the right place. We're about to embark on a journey to integrate ChatGPT into your PHP project using the nifty orhanerday/open-ai package. Buckle up, because this is going to be a fun ride!

Prerequisites

Before we jump in, make sure you've got these basics covered:

  • A PHP environment (you've got this, right?)
  • Composer installed (because who doesn't love dependency management?)
  • An OpenAI API key (if you don't have one, grab it here)

Installation

Let's kick things off by installing the orhanerday/open-ai package. Fire up your terminal and run:

composer require orhanerday/open-ai

Easy peasy, right?

Setting up the OpenAI Client

Now, let's get that OpenAI client up and running:

<?php require_once 'vendor/autoload.php'; use Orhanerday\OpenAi\OpenAi; $open_ai = new OpenAi('your-api-key-here');

Replace 'your-api-key-here' with your actual API key. Keep it secret, keep it safe!

Making API Requests

Time to make our first request to ChatGPT. Here's a simple example:

$chat = $open_ai->chat([ 'model' => 'gpt-3.5-turbo', 'messages' => [ [ "role" => "user", "content" => "Hello, ChatGPT!" ] ], 'temperature' => 1.0, 'max_tokens' => 100, 'frequency_penalty' => 0, 'presence_penalty' => 0, ]); $response = json_decode($chat); echo $response->choices[0]->message->content;

Boom! You've just had your first conversation with ChatGPT.

Implementing Chat Functionality

Let's create a simple chat interface. Here's a basic HTML form and PHP script:

<form method="POST"> <input type="text" name="message" placeholder="Type your message..."> <button type="submit">Send</button> </form>
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { $user_message = $_POST['message']; $chat = $open_ai->chat([ 'model' => 'gpt-3.5-turbo', 'messages' => [ [ "role" => "user", "content" => $user_message ] ], ]); $response = json_decode($chat); echo $response->choices[0]->message->content; }

Advanced Features

Want to spice things up? Let's adjust some parameters:

$chat = $open_ai->chat([ 'model' => 'gpt-3.5-turbo', 'messages' => [ [ "role" => "system", "content" => "You are a helpful assistant." ], [ "role" => "user", "content" => $user_message ] ], 'temperature' => 0.7, 'max_tokens' => 150, 'frequency_penalty' => 0.5, 'presence_penalty' => 0.5, ]);

Play around with these values to see how they affect the responses!

Error Handling and Best Practices

Always wrap your API calls in try-catch blocks:

try { $chat = $open_ai->chat([/* ... */]); } catch (Exception $e) { echo "Oops! Something went wrong: " . $e->getMessage(); }

And don't forget about rate limits. Be a good API citizen!

Conclusion

And there you have it! You've successfully integrated ChatGPT into your PHP project. Pretty cool, huh? Remember, this is just the beginning. There's a whole world of possibilities out there. So go forth and create something awesome!

Resources

Happy coding, and may your conversations with ChatGPT be ever insightful!