-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathMessagesTest.php
More file actions
172 lines (158 loc) · 6.1 KB
/
MessagesTest.php
File metadata and controls
172 lines (158 loc) · 6.1 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
namespace Tests\Services;
use Anthropic\Client;
use Anthropic\Core\Util;
use Anthropic\Messages\Message;
use Anthropic\Messages\MessageTokensCount;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
/**
* @internal
*/
#[CoversNothing]
final class MessagesTest extends TestCase
{
protected Client $client;
protected function setUp(): void
{
parent::setUp();
$testUrl = Util::getenv('TEST_API_BASE_URL') ?: 'http://127.0.0.1:4010';
$client = new Client(apiKey: 'my-anthropic-api-key', baseUrl: $testUrl);
$this->client = $client;
}
#[Test]
public function testCreate(): void
{
$result = $this->client->messages->create(
maxTokens: 1024,
messages: [['content' => 'Hello, world', 'role' => 'user']],
model: 'claude-opus-4-6',
);
// @phpstan-ignore-next-line method.alreadyNarrowedType
$this->assertInstanceOf(Message::class, $result);
}
#[Test]
public function testCreateWithOptionalParams(): void
{
$result = $this->client->messages->create(
maxTokens: 1024,
messages: [['content' => 'Hello, world', 'role' => 'user']],
model: 'claude-opus-4-6',
cacheControl: ['type' => 'ephemeral', 'ttl' => '5m'],
container: 'container',
inferenceGeo: 'inference_geo',
metadata: ['userID' => '13803d75-b4b5-4c3e-b2a2-6f21399b021b'],
outputConfig: [
'effort' => 'low',
'format' => ['schema' => ['foo' => 'bar'], 'type' => 'json_schema'],
],
serviceTier: 'auto',
stopSequences: ['string'],
system: [
[
'text' => 'Today\'s date is 2024-06-01.',
'type' => 'text',
'cacheControl' => ['type' => 'ephemeral', 'ttl' => '5m'],
'citations' => [
[
'citedText' => 'cited_text',
'documentIndex' => 0,
'documentTitle' => 'x',
'endCharIndex' => 0,
'startCharIndex' => 0,
'type' => 'char_location',
],
],
],
],
temperature: 1,
thinking: ['type' => 'adaptive', 'display' => 'summarized'],
toolChoice: ['type' => 'auto', 'disableParallelToolUse' => true],
tools: [
[
'inputSchema' => [
'type' => 'object',
'properties' => ['location' => 'bar', 'unit' => 'bar'],
'required' => ['location'],
],
'name' => 'name',
'allowedCallers' => ['direct'],
'cacheControl' => ['type' => 'ephemeral', 'ttl' => '5m'],
'deferLoading' => true,
'description' => 'Get the current weather in a given location',
'eagerInputStreaming' => true,
'inputExamples' => [['foo' => 'bar']],
'strict' => true,
'type' => 'custom',
],
],
topK: 5,
topP: 0.7,
);
// @phpstan-ignore-next-line method.alreadyNarrowedType
$this->assertInstanceOf(Message::class, $result);
}
#[Test]
public function testCountTokens(): void
{
$result = $this->client->messages->countTokens(
messages: [['content' => 'string', 'role' => 'user']],
model: 'claude-mythos-preview',
);
// @phpstan-ignore-next-line method.alreadyNarrowedType
$this->assertInstanceOf(MessageTokensCount::class, $result);
}
#[Test]
public function testCountTokensWithOptionalParams(): void
{
$result = $this->client->messages->countTokens(
messages: [['content' => 'string', 'role' => 'user']],
model: 'claude-mythos-preview',
cacheControl: ['type' => 'ephemeral', 'ttl' => '5m'],
outputConfig: [
'effort' => 'low',
'format' => ['schema' => ['foo' => 'bar'], 'type' => 'json_schema'],
],
system: [
[
'text' => 'Today\'s date is 2024-06-01.',
'type' => 'text',
'cacheControl' => ['type' => 'ephemeral', 'ttl' => '5m'],
'citations' => [
[
'citedText' => 'cited_text',
'documentIndex' => 0,
'documentTitle' => 'x',
'endCharIndex' => 0,
'startCharIndex' => 0,
'type' => 'char_location',
],
],
],
],
thinking: ['type' => 'adaptive', 'display' => 'summarized'],
toolChoice: ['type' => 'auto', 'disableParallelToolUse' => true],
tools: [
[
'inputSchema' => [
'type' => 'object',
'properties' => ['location' => 'bar', 'unit' => 'bar'],
'required' => ['location'],
],
'name' => 'name',
'allowedCallers' => ['direct'],
'cacheControl' => ['type' => 'ephemeral', 'ttl' => '5m'],
'deferLoading' => true,
'description' => 'Get the current weather in a given location',
'eagerInputStreaming' => true,
'inputExamples' => [['foo' => 'bar']],
'strict' => true,
'type' => 'custom',
],
],
);
// @phpstan-ignore-next-line method.alreadyNarrowedType
$this->assertInstanceOf(MessageTokensCount::class, $result);
}
}