-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
120 lines (106 loc) · 4.37 KB
/
example.php
File metadata and controls
120 lines (106 loc) · 4.37 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
// NovaPAI PHP SDK Example
// Install: composer require openai-php/client
// Docs: https://novapai.ai
require_once 'vendor/autoload.php';
use OpenAI;
$client = OpenAI::factory()
->withApiKey('your-api-key')
->withBaseUri('https://api.novapai.ai/router/v1')
->make();
// ── Basic Chat ──────────────────────────────────────────────
function basicChat($client) {
$response = $client->chat()->create([
'model' => 'deepseek-v4-pro',
'messages' => [
['role' => 'system', 'content' => 'You are a helpful assistant.'],
['role' => 'user', 'content' => 'Hello!'],
],
]);
echo $response->choices[0]->message->content . PHP_EOL;
}
// ── Streaming ───────────────────────────────────────────────
function streamChat($client) {
$stream = $client->chat()->createStreamed([
'model' => 'deepseek-v4-pro',
'messages' => [
['role' => 'user', 'content' => 'Tell me a joke'],
],
]);
foreach ($stream as $response) {
echo $response->choices[0]->delta->content ?? '';
flush();
}
echo PHP_EOL;
}
// ── Multi-turn Conversation ─────────────────────────────────
function multiTurnChat($client) {
$messages = [['role' => 'system', 'content' => 'You are a helpful assistant.']];
$chat = function (string $userInput) use ($client, &$messages): string {
$messages[] = ['role' => 'user', 'content' => $userInput];
$response = $client->chat()->create([
'model' => 'deepseek-v4-pro',
'messages' => $messages,
]);
$reply = $response->choices[0]->message->content;
$messages[] = ['role' => 'assistant', 'content' => $reply];
return $reply;
};
echo $chat('What is 1+1?') . PHP_EOL;
echo $chat('Multiply that by 10') . PHP_EOL;
}
// ── Function Calling ────────────────────────────────────────
function functionCalling($client) {
$response = $client->chat()->create([
'model' => 'deepseek-v4-pro',
'messages' => [
['role' => 'user', 'content' => "What's the weather in Tokyo?"],
],
'tools' => [[
'type' => 'function',
'function' => [
'name' => 'get_weather',
'description' => 'Get current weather for a city',
'parameters' => [
'type' => 'object',
'properties' => [
'city' => ['type' => 'string', 'description' => 'City name']
],
'required' => ['city']
]
]
]]
]);
$toolCall = $response->choices[0]->message->toolCalls[0];
echo "Function: {$toolCall->function->name}" . PHP_EOL;
echo "Args: {$toolCall->function->arguments}" . PHP_EOL;
// Continue with tool result
$result = json_encode(['city' => 'Tokyo', 'temperature' => 22, 'condition' => 'sunny']);
$final = $client->chat()->create([
'model' => 'deepseek-v4-pro',
'messages' => [
['role' => 'user', 'content' => "What's the weather in Tokyo?"],
['role' => 'assistant', 'toolCalls' => [$toolCall->toArray()]],
['role' => 'tool', 'tool_call_id' => $toolCall->id, 'content' => $result],
]
]);
echo $final->choices[0]->message->content . PHP_EOL;
}
// ── JSON Mode (Structured Output) ───────────────────────────
function jsonMode($client) {
$response = $client->chat()->create([
'model' => 'deepseek-v4-pro',
'messages' => [
['role' => 'system', 'content' => 'Extract company info as JSON.'],
['role' => 'user', 'content' => 'Apple Inc. is based in Cupertino, founded in 1976.'],
],
'response_format' => ['type' => 'json_object']
]);
$data = json_decode($response->choices[0]->message->content, true);
echo json_encode($data, JSON_PRETTY_PRINT) . PHP_EOL;
}
basicChat($client);
streamChat($client);
multiTurnChat($client);
functionCalling($client);
jsonMode($client);