-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscratchpad.php
More file actions
142 lines (115 loc) · 3.74 KB
/
scratchpad.php
File metadata and controls
142 lines (115 loc) · 3.74 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
<?php
declare(strict_types=1);
use Cortex\Cortex;
use Cortex\Agents\Agent;
use Cortex\Prompts\Prompt;
use Cortex\JsonSchema\Schema;
use Cortex\Pipeline\RuntimeConfig;
use Cortex\Agents\Prebuilt\WeatherAgent;
use Cortex\LLM\Data\Messages\UserMessage;
use Cortex\LLM\Data\Messages\SystemMessage;
use Cortex\Tools\Prebuilt\GetCurrentWeatherTool;
$prompt = Cortex::prompt([
new SystemMessage('You are an expert at geography.'),
new UserMessage('What is the capital of {country}?'),
]);
$result = $prompt->llm('openai')->invoke([
'country' => 'France',
]);
// Or more simply
$result = Cortex::prompt('What is the capital of {country}?')
->metadata(
provider: 'anthropic',
model: 'claude-3-5-sonnet-20240620',
structuredOutput: Schema::object()->properties(
Schema::string('capital'),
),
)
->llm()
->invoke([
'country' => 'France',
]);
// Get a text prompt builder
$prompt = Cortex::prompt('What is the capital of {country}?');
// Get a chat prompt builder
$prompt = Cortex::prompt([
new SystemMessage('You are an expert at geography.'),
new UserMessage('What is the capital of {country}?'),
]);
// Get a generic agent builder from the prompt
$agentBuilder = $prompt->agent();
$result = $agentBuilder->invoke(input: [
'country' => 'France',
]);
// Get a prompt from the given factory
$prompt = Cortex::prompt()->factory('langfuse')->make('test-prompt');
// Get a chat prompt builder
$prompt = Cortex::prompt()->builder('chat')->messages([
new SystemMessage('You are an expert at geography.'),
new UserMessage('What is the capital of {country}?'),
]);
// Get a text prompt builder
$prompt = Cortex::prompt()->builder('text');
$prompt = Prompt::factory()->make('test-prompt');
$prompt = Prompt::builder()->messages([
new SystemMessage('You are an expert at geography.'),
new UserMessage('What is the capital of {country}?'),
]);
$result = $prompt->agent()->invoke(input: [
'country' => 'France',
]);
// Uses default llm driver
$result = Cortex::llm()->invoke([
new SystemMessage('You are a helpful assistant'),
new UserMessage('What is the capital of France?'),
]);
$schema = Schema::object()->properties(
Schema::string('capital'),
);
$result = Cortex::llm()->withStructuredOutput($schema)->invoke([
new UserMessage('What is the capital of France?'),
]);
// Specifies the driver and other options
$result = Cortex::llm('anthropic', 'claude-3-5-sonnet-20240620')
->withTemperature(0.5)
->withStreaming()
->invoke([
new SystemMessage('You are a helpful assistant'),
new UserMessage('What is the capital of France?'),
]);
$jokeAgent = new Agent(
id: 'joke_generator',
prompt: 'You are a joke generator. You generate jokes about {topic}.',
llm: 'ollama:gpt-oss:20b',
);
$result = $jokeAgent->invoke(input: [
'topic' => 'programming',
]);
$weatherAgent = WeatherAgent::make()->invoke(input: [
'location' => 'Paris',
]);
Cortex::registerAgent('weather_agent', WeatherAgent::class);
Cortex::agent('weather_agent')->invoke(input: [
'location' => 'Paris',
]);
// Cortex::agent('weather_agent')->invoke(config: new RuntimeConfig(input: [
// 'location' => 'Paris',
// ]));
$agent = Cortex::agent()
->withId('weather_agent')
->withPrompt('You are a weather agent. You tell the weather in {location}.')
->withLLM('lmstudio/openai/gpt-oss-20b')
->withTools([
GetCurrentWeatherTool::class,
])
->withOutput([
Schema::string('location')->required(),
Schema::string('summary')->required(),
])
->withMaxSteps(3)
->withStrict(true);
$result = $agent->invoke(input: [
'location' => 'London',
]);
$result = Cortex::embeddings('openai')
->invoke('Lorem ipsum dolor sit amet');