Git Product home page Git Product logo

openai-client's Introduction

openai-client

A very simple client for the OpenAI API.

Installation

composer require mathsgod/openai-client

Usage

Create a client instance with your API key.

use OpenAI\Client;
$client=new Client("OPEN_API_KEY");

Chat completion

$data = $client->chatCompletions()->create([
    "model" => "gpt-4o-mini",
    "messages" => [
        ["role" => "user", "content" => "Hi"]
    ]
]);

print_r($data);
Array
(
    [id] => chatcmpl-1234
    [object] => chat.completion
    [created] => 1722324090
    [model] => gpt-4o-mini-2024-07-18
    [choices] => Array
        (
            [0] => Array
                (
                    [index] => 0
                    [message] => Array
                        (
                            [role] => assistant
                            [content] => Hello! How can I assist you today?
                        )

                    [logprobs] =>
                    [finish_reason] => stop
                )

        )

    [usage] => Array
        (
            [prompt_tokens] => 8
            [completion_tokens] => 9
            [total_tokens] => 17
        )

    [system_fingerprint] => fp_1234
)

Function call

$data=$client->chatCompletions()->create([
   "model" => "gpt-4o-mini",
    "messages" => [
      ["role" => "user", "content" => "What is the price of iphone14?"]
    ],
    "functions" =>[
        [
            "name" => "get_iphone_price",
            "description" => "Get the price of iphone",
            "parameters" => [
                "type" => "object",
                "properties" => [
                    "model" => [
                        "type" => "string",
                        "description" => "The model of the iphone"
                    ]
                ],
                "required" => ["model"]
            ],
        ]
    ]
]);

print_r($data);
Array
(
    [id] => chatcmpl-1234
    [object] => chat.completion
    [created] => 1722324296
    [model] => gpt-4o-mini-2024-07-18
    [choices] => Array
        (
            [0] => Array
                (
                    [index] => 0
                    [message] => Array
                        (
                            [role] => assistant
                            [content] =>
                            [function_call] => Array
                                (
                                    [name] => get_iphone_price
                                    [arguments] => {"model":"iPhone 14"}
                                )

                        )

                    [logprobs] =>
                    [finish_reason] => function_call
                )

        )

    [usage] => Array
        (
            [prompt_tokens] => 60
            [completion_tokens] => 19
            [total_tokens] => 79
        )

    [system_fingerprint] => fp_1234
)

Images

Create image

print_r($client->images()->generations([
    "model" => "dall-e-3",
    "prompt" => "a white siamese cat",
    "n" => 1,
    "size" => "1024x1024"
]));

Embeddings

print_r($client->embeddings()->create([
    "model" => "text-embedding-3-small",
    "input"=>"I feel great",
]));

Audio

Speech

print_r($client->audio()->speech([
    "model"=>"tts-1",
    "input"=>"Hello, how are you?",
    "voice"=>"alloy"
]));

Transcriptions

print_r($client->audio()->transcriptions([
    "model"=>"whisper-1",
    "file"=>fopen('/path/to/audio.mp3', 'r')
]));

Translation

print_r($client->audio()->translation([
    "model"=>"whisper-1",
    "file"=>fopen('/path/to/audio.mp3', 'r')
]));

Assistants

Create

$client->assistants()->create([
    "model" => "gpt-4o-mini",
]);

List

$client->assistants()->list();

Retrieve

$client->assistants()->retrieve("asst_1234");

Delete

$client->assistant("asst_1234")->delete();

Threads

Create

$client->threads()->create(); //return Thread object

Messages

Create

$client->thread("thread_1234")->messages()->create([
    "role" => "user",
    "content" => "Hello"
]);

Create with stream

$stream = $thread->runs()->createStream([
    "assistant_id" => "asst_1234",
]);

$stream->on("thread.message.delta", function ($data) {
    echo $data;
    echo "\n";
});

$stream->on("thread.message.completed", function ($data) {
    echo $data;
    echo "\n";
});

$stream->on("done", function () use (&$thread) {
    echo "End\n";
});

Example

$thread = $client->threads()->create([
    "messages" => [
        [
            "role" => "user",
            "content" => "Hi"
        ]
    ]
]);

$data=$thread->runs()->create([
    "assistant_id" => "asst_1234", //assistant_id
]);

print_r($data); 

openai-client'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.