GithubHelp home page GithubHelp logo

chatgpt-php's Introduction

ChatGPT PHP

Packagist Version Packagist PHP Version pre-commit.ci status Packagist License

A convenient PHP wrapper for the OpenAI ChatGPT API.

Installation

You can install ChatGPT PHP via Composer:

composer require sgraaf/chatgpt-php

Usage

Initialize the client

// initialize the client
$client = new ChatGPT\Client('YOUR_OPENAI_API_KEY');

Use a custom system message

// initialize the client
$client = new ChatGPT\Client('YOUR_OPENAI_API_KEY', system_message: 'You are a helpful assistant that translates English to French.');

Start a conversation

// provide an instruction
$message = $client->chat('Who won the world series in 2020?');
var_dump($message);
// string(53) "The Los Angeles Dodgers won the World Series in 2020."

// continue the conversation
$message = $client->chat('Where was it played?');
var_dump($message);
// string(116) "The 2020 World Series was played in Arlington, Texas at the Globe Life Field, the home stadium of the Texas Rangers."

View the conversation history

var_dump($client->messages);
// array(5) {
//   [0]=>
//   array(2) {
//     ["role"]=>
//     string(6) "system"
//     ["content"]=>
//     string(28) "You are a helpful assistant."
//   }
//   [1]=>
//   array(2) {
//     ["role"]=>
//     string(4) "user"
//     ["content"]=>
//     string(33) "Who won the world series in 2020?"
//   }
//   [2]=>
//   array(2) {
//     ["role"]=>
//     string(9) "assistant"
//     ["content"]=>
//     string(53) "The Los Angeles Dodgers won the World Series in 2020."
//   }
//   [3]=>
//   array(2) {
//     ["role"]=>
//     string(4) "user"
//     ["content"]=>
//     string(20) "Where was it played?"
//   }
//   [4]=>
//   array(2) {
//     ["role"]=>
//     string(9) "assistant"
//     ["content"]=>
//     string(116) "The 2020 World Series was played in Arlington, Texas at the Globe Life Field, the home stadium of the Texas Rangers."
//   }
// }

Clear the conversation history

// clear the conversation history
$client->clear();

var_dump($client->messages);
// array(1) {
//   [0]=>
//   array(2) {
//     ["role"]=>
//     string(6) "system"
//     ["content"]=>
//     string(28) "You are a helpful assistant."
//   }
// }

chatgpt-php's People

Contributors

sgraaf avatar

Watchers

 avatar

chatgpt-php's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.