-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli-chat.php
More file actions
104 lines (80 loc) · 2.86 KB
/
cli-chat.php
File metadata and controls
104 lines (80 loc) · 2.86 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env php
<?php
/**
* CLI Chat — Simple conversational agent using php-agents.
*
* Usage:
* php examples/cli-chat.php
* php examples/cli-chat.php --model qwen3
*
* Requires Ollama running locally with a model pulled:
* ollama pull llama3.2
*
* To use a different provider, swap OllamaProvider for:
* - OpenAICompatibleProvider (OpenAI, OpenRouter, etc.)
* - AnthropicProvider (Claude)
*
* @see QUICKSTART.md for provider examples.
*/
declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';
use CarmeloSantana\PHPAgents\Message\AssistantMessage;
use CarmeloSantana\PHPAgents\Message\Conversation;
use CarmeloSantana\PHPAgents\Message\SystemMessage;
use CarmeloSantana\PHPAgents\Message\UserMessage;
use CarmeloSantana\PHPAgents\Provider\OllamaProvider;
// --- Configuration -----------------------------------------------------------
$model = 'llama3.2';
// Parse --model flag from CLI arguments
foreach ($argv as $i => $arg) {
if ($arg === '--model' && isset($argv[$i + 1])) {
$model = $argv[$i + 1];
}
}
$systemPrompt = <<<'PROMPT'
You are a helpful assistant. Answer questions clearly and concisely.
Keep responses focused and well-structured. Use markdown formatting when it improves readability.
PROMPT;
// --- Setup -------------------------------------------------------------------
$provider = new OllamaProvider(model: $model);
// Verify Ollama is reachable
if (!$provider->isAvailable()) {
fwrite(STDERR, "Error: Cannot connect to Ollama at http://localhost:11434\n");
fwrite(STDERR, "Make sure Ollama is running: ollama serve\n");
exit(1);
}
$conversation = new Conversation();
$conversation->add(new SystemMessage($systemPrompt));
echo "php-agents CLI Chat (model: {$model})\n";
echo "Type your message and press Enter. Type 'exit' or 'quit' to stop.\n";
echo str_repeat('─', 60) . "\n\n";
// --- REPL Loop ---------------------------------------------------------------
while (true) {
echo "\033[1;32mYou:\033[0m ";
$input = fgets(STDIN);
if ($input === false) {
// EOF (e.g. piped input ended)
break;
}
$input = trim($input);
if ($input === '' ) {
continue;
}
if (in_array(strtolower($input), ['exit', 'quit', 'q'], true)) {
echo "\nGoodbye!\n";
break;
}
$conversation->add(new UserMessage($input));
try {
$response = $provider->chat($conversation->messages(), []);
} catch (\Throwable $e) {
echo "\033[1;31mError:\033[0m {$e->getMessage()}\n\n";
continue;
}
$conversation->add(new AssistantMessage($response->content));
echo "\033[1;34mAssistant:\033[0m {$response->content}\n\n";
// Show token usage when available
if ($response->usage !== null && $response->usage->totalTokens > 0) {
echo "\033[2m[tokens: {$response->usage->totalTokens}]\033[0m\n\n";
}
}