-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathexample.php
More file actions
76 lines (62 loc) · 2.67 KB
/
example.php
File metadata and controls
76 lines (62 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
require __DIR__ . '/vendor/autoload.php';
use RPurinton\GeminiPHP\{GeminiClient, GeminiPrompt};
// See:
// https://github.com/rpurinton/gemini-php
// https://cloud.google.com/vertex-ai/docs/generative-ai/model-reference/overview
try {
// Create a GeminiClient object
$example_client = file_get_contents(__DIR__ . '/example-client.json') or throw new \Exception('Unable to read example-client.json');
$example_client = json_decode($example_client, true) or throw new \Exception('Unable to decode example-client.json');
$client = new GeminiClient($example_client) or throw new \Exception('Unable to create GeminiClient object');
// Create a GeminiPrompt object
$example_prompt = file_get_contents(__DIR__ . '/example-prompt.json') or throw new \Exception('Unable to read example-prompt.json');
$example_prompt = json_decode($example_prompt, true) or throw new \Exception('Unable to decode example-prompt.json');
$prompt = new GeminiPrompt($example_prompt) or throw new \Exception('Unable to create GeminiPrompt object');
} catch (\Exception $e) {
echo ('fatal> ' . $e->getMessage() . PHP_EOL);
exit(1);
}
// Create a function to handle commands
$commands = function ($user_input) use ($prompt): bool {
$command = strtolower($user_input);
switch ($command) {
case '':
return true;
case 'exit':
case 'quit':
exit(0);
case 'clear':
$prompt->resetContent();
echo ('Prompt cleared.' . PHP_EOL);
return true;
case 'help':
echo ('Commands: exit, quit, clear, help' . PHP_EOL);
return true;
default:
return false;
}
};
echo ('Press CTRL+C to exit...' . PHP_EOL);
while (true) {
try {
// Get user input
$user_input = readline('user> ');
// Check for commands
if ($commands($user_input)) continue;
// Add the user input to the prompt
$prompt->push(['role' => 'user', 'parts' => ['text' => $user_input]]);
// Send the prompt to the Gemini API and get the response
$response = $client->getResponse($prompt->toJson()); // Returns a GeminiResponse Object
// Get the usage metadata if you need it
$usageMetadata = $response->getUsageMetadata();
// Get the response text
$assistant_output = $response->getText();
// Display the response text
echo ('assistant> ' . $assistant_output . PHP_EOL);
// Add the response to the prompt
$prompt->push(['role' => 'assistant', 'parts' => ['text' => $assistant_output]]);
} catch (\Exception $e) {
echo ('error> ' . $e->getMessage() . PHP_EOL);
}
}