Skip to content

Latest commit

 

History

History
177 lines (147 loc) · 4.9 KB

google.md

File metadata and controls

177 lines (147 loc) · 4.9 KB

Using Google DeepMind models

This document contains details on how to use Google DeepMind models. It's important to understand that every AI model has its own capabilities and limitations.


Chat completitions and vision

With the chat completitions you can communicate with the model using natural language. In addition you can set behaviour parameters, use tools and send files.

API Endpoint
https://generativelanguage.googleapis.com/{apiVersion}/models/{modelName}:generateContent
Default api version is v1beta. You can change it to v1 by passing apiVersion option to the thread.

Supported message roles
system, user, assistant

Supported message types
text, file (images, audio, video and documents)

Supported tools
function calling

Supported models:

  • google-gemini-1.5-flash
  • google-gemini-1.5-flash-8b
  • google-gemini-1.5-pro

Simple chat

$thread = new AIpi\Thread('google-gemini-1.5-flash', $my_google_key);
$thread->AddMessage(new AIpi\Message('Hi, who are you?'), ['temperature' => 0.5]);

$message = $thread->Run();
if ($message) 
{
    echo $message->content."\r\n"; // I am a large language model, trained by Google.
    print_r($thread->GetUsage());
    echo "\r\n\r\n";
}
else echo $thread->GetLastError();

Role play

$thread = new AIpi\Thread('google-gemini-1.5-flash', $my_google_key);
$thread->AddMessage(new AIpi\Message('You are helpfull assitant of ecommerce shop?', MessageRole::SYSTEM));
$thread->AddMessage(new AIpi\Message('Hi, I have problem with my order.', MessageRole::USER));

$message = $thread->Run();
if ($message) 
{
    echo $message->content."\r\n\r\n";
    print_r($thread->GetUsage());
    print_r($thread->messages);
    echo "\r\n\r\n";
}
else echo $thread->GetLastError();

Vision

$thread = new AIpi\Thread('google-gemini-1.5-flash', $my_google_key);
$thread->AddMessage(new Message('What\'s on the photo?'));

// Gemini work with file uploads
$src = file_get_contents('https://onlinejpgtools.com/images/examples-onlinejpgtools/orange-tabby-cat.jpg');
$thread->AddMessage(new Message($src, ['type' => MessageType::FILE]));

$message = $thread->Run();
if ($message) 
{
    echo $message->content."\r\n"; // That's a fluffy ginger cat grooming itself...
    print_r($thread->GetUsage());
    echo "\r\n\r\n";
}
else echo $thread->GetLastError();

Tools (function calling)

/** ************************** */
/** Define simple weather tool */
/** ************************** */
$weatherInfo = new FunctionCall(
    // Name
    'get_weather_info',
    // Description
    'Get weather info by city name and country code.',
    // Accepted properties
    [   
        'city' => 'string',
        'countryCode' => 'string',
    ],
    // Property attributes
    [
        'required' => ['city', 'countryCode'],
        'descriptions' => [
            'city' => 'The city name.',
            'countryCode' => 'The country code.',
        ]
    ],
    // Callback function
    function($args) {
        return ['weather' => 'sunny'];
    }
);

/** ********************************** */
/** Create a new thread with the tool */
/** ********************************** */
$thread = new AIpi\Thread('google-gemini-1.5-flash', $my_google_key);

// Load OpenMeto tool from the toolbox
$thread->AddTool($weatherInfo);

// Or you can get one from the toolbox
//$thread->AddTool(new AIpi\Toolbox\OpenMeteo());

$thread->AddMessage(new Message('You are a helpful assistant that can get weather info.', MessageRole::SYSTEM));
$thread->AddMessage(new Message('What is the weather right now in LA?', MessageRole::USER));
$message = $thread->Run();
if ($message)
{
    echo 'ASSISTANT: '.$message->content."\r\n";
    print_r($thread->GetUsage());
}
else 
{
    echo $thread->GetLastError();
}



Embeddings

Generate text embeddings for building RAG applications.

API Endpoint
https://generativelanguage.googleapis.com/{apiVersion}/models/{modelName}:embedContent
Default api version is v1beta. You can change it to v1 by passing apiVersion option to the thread.

Supported message roles
system and user (both will be merged into one single input)

Supported message types
text

Supported tools
N/A

Supported models

  • google-text-embedding-004

Example

$thread = new Thread('google-text-embedding-004', $my_google_key);
$thread->AddMessage(new Message('The red fox jumps over the lazy dog.'));

$message = $thread->Run();
if ($message) 
{
    echo $message->content."\r\n"; // Embeddings
}
else echo $thread->GetLastError();



Further reading