forked from laravel/ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
140 lines (112 loc) · 4.02 KB
/
Copy pathfunctions.php
File metadata and controls
140 lines (112 loc) · 4.02 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
<?php
namespace Laravel\Ai;
use Closure;
use Illuminate\Container\Container;
use Illuminate\JsonSchema\Types\ArrayType;
use Illuminate\JsonSchema\Types\BooleanType;
use Illuminate\JsonSchema\Types\IntegerType;
use Illuminate\JsonSchema\Types\NumberType;
use Illuminate\JsonSchema\Types\ObjectType;
use Illuminate\JsonSchema\Types\StringType;
use Illuminate\JsonSchema\Types\Type;
use Illuminate\Pipeline\Pipeline;
use Illuminate\Support\Str;
use Laravel\Ai\Contracts\Agent;
/**
* Get an ad-hoc agent instance.
*/
function agent(
string $instructions = '',
iterable $messages = [],
iterable $tools = [],
?Closure $schema = null,
): Agent {
return $schema
? new StructuredAnonymousAgent($instructions, $messages, $tools, $schema)
: new AnonymousAgent($instructions, $messages, $tools);
}
/**
* Get a new pipeline instance.
*/
function pipeline(): Pipeline
{
return new Pipeline(Container::getInstance());
}
/**
* Generate a new ULID.
*/
function ulid(): string
{
return strtolower((string) Str::ulid());
}
/**
* Generate fake data from a JSON schema type.
*/
function generate_fake_data_for_json_schema_type(Type $type): mixed
{
$attributes = (fn () => get_object_vars($type))->call($type);
if (isset($attributes['enum']) && is_array($attributes['enum']) && count($attributes['enum']) > 0) {
$enumValue = $attributes['enum'][array_rand($attributes['enum'])];
return $type::class === ArrayType::class
? [$enumValue]
: $enumValue;
}
if (isset($attributes['default'])) {
return $attributes['default'];
}
return match ($type::class) {
ObjectType::class => (function () use ($attributes) {
$result = [];
foreach ($attributes['properties'] ?? [] as $key => $property) {
$result[$key] = generate_fake_data_for_json_schema_type($property);
}
return $result;
})(),
ArrayType::class => (function () use ($attributes) {
$min = $attributes['minItems'] ?? 1;
$max = $attributes['maxItems'] ?? max($min, 3);
$count = random_int($min, $max);
if (! isset($attributes['items'])) {
return [];
}
$result = [];
for ($i = 0; $i < $count; $i++) {
$result[] = generate_fake_data_for_json_schema_type(
$attributes['items']
);
}
return $result;
})(),
'Illuminate\\JsonSchema\\Types\\AnyOfType' => (function () use ($attributes) {
return generate_fake_data_for_json_schema_type($attributes['schemas'][0] ?? throw new \RuntimeException('AnyOf schema must contain at least one branch.'));
})(),
StringType::class => (function () use ($attributes) {
if (isset($attributes['format'])) {
return match ($attributes['format']) {
'date' => date('Y-m-d'),
'date-time' => date('c'),
'email' => 'user@example.com',
'time' => date('H:i:s'),
'uri', 'url' => 'https://example.com',
'uuid' => (string) Str::uuid(),
default => 'string',
};
}
$min = $attributes['minLength'] ?? 1;
$max = $attributes['maxLength'] ?? max($min, 10);
return Str::random(random_int($min, $max));
})(),
IntegerType::class => (function () use ($attributes) {
$min = $attributes['minimum'] ?? 0;
$max = $attributes['maximum'] ?? max($min, 100);
return random_int($min, $max);
})(),
NumberType::class => (function () use ($attributes) {
$min = $attributes['minimum'] ?? 0.0;
$max = $attributes['maximum'] ?? max($min, 100.0);
return $min + mt_rand() / mt_getrandmax() * ($max - $min);
})(),
BooleanType::class => random_int(0, 1) === 0,
default => null,
};
}