PHP code example of yandricr / gpti-php

1. Go to this page and download the library: Download yandricr/gpti-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yandricr / gpti-php example snippets




use gpti\...; // select model

$res = new ...();
$res->setAPI(user:"user-xxxxxxxx", secret:"nx-xxxxxxx-xxxxx-xxxxx");



use gpti\gpt;

$res = new gpt(messages:array(
    [
        "role" => "assistant",
        "content" => "Hello! How are you today?"
    ],
    [
        "role" => "user",
        "content" => "Hello, my name is Yandri."
    ],
    [
        "role" => "assistant",
        "content" => "Hello, Yandri! How are you today?"
    ]
), prompt:"Can you repeat my name?", model:"GPT-4", markdown:false);
$res->execute();

if($res->error() != null){
    print(json_encode($res->error()));
} else {
    print(json_encode($res->result()));
}



use gpti\gptturbo;

$res = new gptturbo(messages:array(
    [
        "role" => "assistant",
        "content" => "Hello! How are you today?"
    ],
    [
        "role" => "user",
        "content" => "Hello, my name is Yandri."
    ],
    [
        "role" => "assistant",
        "content" => "Hello, Yandri! How are you today?"
    ],
    [
        "role" => "user",
        "content" => "Can you repeat my name?"
    ]
), markdown:false, stream:false);
$res->execute();

if($res->error() != null){
    print(json_encode($res->error()));
} else {
    print(json_encode($res->result()));
}



use gpti\gptturbo;

$res = new gptturbo(messages:array(
    [
        "role" => "assistant",
        "content" => "Hello! How are you today?"
    ],
    [
        "role" => "user",
        "content" => "Hello, my name is Yandri."
    ],
    [
        "role" => "assistant",
        "content" => "Hello, Yandri! How are you today?"
    ],
    [
        "role" => "user",
        "content" => "Can you repeat my name?"
    ]
), markdown:false, stream:true);
$res->execute();

if($res->error() != null){
    print(json_encode($res->error()));
} else {
    foreach($res->stream() as $data){
        print(json_encode($data));
    }
}



use gpti\gptweb;

$res = new gptweb(prompt:"Are you familiar with the movie Wonka released in 2023?", markdown:false);
$res->execute();

if($res->error() != null){
    print(json_encode($res->error()));
} else {
    print(json_encode($res->result()));
}



use gpti\gpt4o;

$res = new gpt4o(messages:array(
    [
        "role" => "assistant",
        "content" => "Hello! How are you today?"
    ],
    [
        "role" => "user",
        "content" => "Hello, my name is Yandri."
    ],
    [
        "role" => "assistant",
        "content" => "Hello, Yandri! How are you today?"
    ],
    [
        "role" => "user",
        "content" => "Can you repeat my name?"
    ]
), markdown:false, stream:false);
$res->execute();

if($res->error() != null){
    print(json_encode($res->error()));
} else {
    print(json_encode($res->result()));
}



use gpti\gpt4o;

$res = new gpt4o(messages:array(
    [
        "role" => "assistant",
        "content" => "Hello! How are you today?"
    ],
    [
        "role" => "user",
        "content" => "Hello, my name is Yandri."
    ],
    [
        "role" => "assistant",
        "content" => "Hello, Yandri! How are you today?"
    ],
    [
        "role" => "user",
        "content" => "Can you repeat my name?"
    ]
), markdown:false, stream:true);
$res->execute();

if($res->error() != null){
    print(json_encode($res->error()));
} else {
    foreach($res->stream() as $data){
        print(json_encode($data));
    }
}



use gpti\bing;

$res = new bing(messages:array(
    [
        "role" => "assistant",
        "content" => "Hello! How can I help you today? 😊"
    ],
    [
        "role" => "user",
        "content" => "Hi, tell me the names of the movies released in 2023."
    ]
), conversation_style:"Balanced", markdown:false, stream:false);
$res->execute();

if($res->error() != null){
    print(json_encode($res->error()));
} else {
    print(json_encode($res->result()));
}



use gpti\bing;

$res = new bing(messages:array(
    [
        "role" => "assistant",
        "content" => "Hello! How can I help you today? 😊"
    ],
    [
        "role" => "user",
        "content" => "Hi, tell me the names of the movies released in 2023."
    ]
), markdown:false, stream:true);
$res->execute();

if($res->error() != null){
    print(json_encode($res->error()));
} else {
    foreach($res->stream() as $data){
        print(json_encode($data));
    }
}



use gpti\llama;

$res = new llama(messages:array(
    [
        "role" => "user",
        "content" => "Hello, my name is Yandri."
    ]
), conversation_style:"Balanced", markdown:false, stream:false);
$res->execute();

if($res->error() != null){
    print(json_encode($res->error()));
} else {
    print(json_encode($res->result()));
}



use gpti\llama;

$res = new llama(messages:array(
    [
        "role" => "user",
        "content" => "Hello, my name is Yandri."
    ]
), markdown:false, stream:true);
$res->execute();

if($res->error() != null){
    print(json_encode($res->error()));
} else {
    foreach($res->stream() as $data){
        print(json_encode($data));
    }
}



use gpti\blackbox;

$res = new blackbox(messages:array(
    [
        "role" => "user",
        "content" => "Hello, my name is Yandri."
    ]
), conversation_style:"Balanced", markdown:false, stream:false);
$res->execute();

if($res->error() != null){
    print(json_encode($res->error()));
} else {
    print(json_encode($res->result()));
}



use gpti\blackbox;

$res = new blackbox(messages:array(
    [
        "role" => "user",
        "content" => "Hello, my name is Yandri."
    ]
), markdown:false, stream:true);
$res->execute();

if($res->error() != null){
    print(json_encode($res->error()));
} else {
    foreach($res->stream() as $data){
        print(json_encode($data));
    }
}



use gpti\imageai;

$res = new imageai(prompt:"cat color red", model:"dalle", response:"url", data:[]);
$res->execute();

if($res->error() != null){
    print(json_encode($res->error()));
} else {
    print(json_encode($res->result()));
}
bash
  composer