Git Product home page Git Product logo

openai-chat's Introduction

openai-chat

Installation

composer require mathsgod/openai-chat

Usage

To use the OpenAI chat, you need to create a new instance of the System class and pass the OpenAI API key as the first argument.

use OpenAI\Chat\System;

$system = new System($_ENV['OPENAI_API_KEY']);

echo $system->ask("Hello");

Add a tool

use OpenAI\Chat\Attributes\Tool;
use OpenAI\Chat\Attributes\Parameter;

#[Tool(description: 'Get the release date of iphone')]
function getIPhoneReleaseDate(#[Parameter("model of the phone")] string $model)
{
    return ["date" => "2022-09-14", "model" => $model];
}

$system->addTool(Closure::fromCallable("getIPhoneReleaseDate"));

echo $system->ask("When will iPhone 14 be released?");

Add a tool from a class method

class Controller
{
    public $price = "$799";

    #[Tool(description: 'Get the price of iphone')]
    public function getIPhonePrice(#[Parameter("model of the phone")] string $model)
    {
        return ["price" => $this->price, "model" => $model];
    }
}

$system->addTool(Closure::fromCallable([new Controller(), "getIPhonePrice"]));

echo $system->ask("What is the price and release date of iphone14?");

Get usage records

After run the code above, you can get the usage records

print_r($system->getUsages());

Streaming

$stream = $system->askAsStream("What is the price and release date of iphone14?");

$stream->on('data', function ($data) {
    echo $data;
});

openai-chat's People

Contributors

mathsgod avatar

Watchers

 avatar  avatar

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.