Skip to content

Commit 67aef26

Browse files
committed
updates
1 parent a492007 commit 67aef26

11 files changed

Lines changed: 234 additions & 22 deletions

File tree

config/cortex.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,30 @@
193193
],
194194
],
195195

196+
/*
197+
|--------------------------------------------------------------------------
198+
| Prompt Factories
199+
|--------------------------------------------------------------------------
200+
|
201+
| Here you may define the prompt factories.
202+
|
203+
| Supported drivers: "langfuse", "mcp"
204+
|
205+
*/
206+
'prompt_factory' => [
207+
'default' => env('CORTEX_DEFAULT_PROMPT_FACTORY', 'langfuse'),
208+
209+
'langfuse' => [
210+
'username' => env('LANGFUSE_USERNAME', ''),
211+
'password' => env('LANGFUSE_PASSWORD', ''),
212+
'base_uri' => env('LANGFUSE_BASE_URI', 'https://cloud.langfuse.com'),
213+
],
214+
215+
// 'mcp' => [
216+
// 'base_uri' => env('MCP_BASE_URI', 'http://localhost:3000'),
217+
// ],
218+
],
219+
196220
/*
197221
|--------------------------------------------------------------------------
198222
| Embedding Providers

scratchpad.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
declare(strict_types=1);
44

55
use Cortex\Cortex;
6+
use Cortex\Prompts\Prompt;
67
use Cortex\JsonSchema\SchemaFactory;
8+
use Cortex\Prompts\Enums\PromptType;
79
use Cortex\LLM\Data\Messages\UserMessage;
810
use Cortex\LLM\Data\Messages\SystemMessage;
911

@@ -18,11 +20,45 @@
1820

1921
// Or more simply
2022
$result = Cortex::prompt('What is the capital of {country}?')
21-
->llm('anthropic', 'claude-3-5-sonnet-20240620')
23+
->metadata(
24+
provider: 'anthropic',
25+
model: 'claude-3-5-sonnet-20240620',
26+
structuredOutput: SchemaFactory::object()->properties(
27+
SchemaFactory::string('capital'),
28+
),
29+
)
30+
->llm()
2231
->invoke([
2332
'country' => 'France',
2433
]);
2534

35+
// Get a text prompt builder
36+
$prompt = Cortex::prompt('What is the capital of {country}?');
37+
38+
// Get a chat prompt builder
39+
$prompt = Cortex::prompt([
40+
new SystemMessage('You are an expert at geography.'),
41+
new UserMessage('What is the capital of {country}?'),
42+
]);
43+
44+
// Get a prompt from the given factory
45+
$prompt = Cortex::prompt()->factory('langfuse')->make('test-prompt');
46+
47+
// Get a chat prompt builder
48+
$prompt = Cortex::prompt()->builder(PromptType::Chat)->messages([
49+
new SystemMessage('You are an expert at geography.'),
50+
new UserMessage('What is the capital of {country}?'),
51+
]);
52+
53+
// Get a text prompt builder
54+
$prompt = Cortex::prompt()->builder(PromptType::Text);
55+
56+
$prompt = Prompt::factory()->make('test-prompt');
57+
$prompt = Prompt::builder()->messages([
58+
new SystemMessage('You are an expert at geography.'),
59+
new UserMessage('What is the capital of {country}?'),
60+
]);
61+
2662
// Uses default llm driver
2763
$result = Cortex::llm()->invoke([
2864
new SystemMessage('You are a helpful assistant'),

src/Cortex.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,36 @@
55
namespace Cortex;
66

77
use Cortex\Facades\LLM;
8+
use Cortex\Prompts\Prompt;
89
use Cortex\Facades\Embeddings;
910
use Cortex\Tasks\Enums\TaskType;
11+
use Cortex\Prompts\Enums\PromptType;
1012
use Cortex\Tasks\Builders\TextTaskBuilder;
13+
use Cortex\Prompts\Contracts\PromptBuilder;
1114
use Cortex\LLM\Contracts\LLM as LLMContract;
12-
use Cortex\Prompts\Builders\ChatPromptBuilder;
1315
use Cortex\LLM\Data\Messages\MessageCollection;
1416
use Cortex\Tasks\Builders\StructuredTaskBuilder;
1517
use Cortex\Embeddings\Contracts\Embeddings as EmbeddingsContract;
1618

1719
class Cortex
1820
{
1921
/**
20-
* Create a chat prompt builder.
22+
* Create a prompt builder or factory.
2123
*
22-
* @param \Cortex\LLM\Data\Messages\MessageCollection|array<int, \Cortex\LLM\Contracts\Message>|string $messages
24+
* @param \Cortex\LLM\Data\Messages\MessageCollection|array<int, \Cortex\LLM\Contracts\Message>|string|null $messages
25+
*
26+
* @return ($messages is null ? \Cortex\Prompts\Prompt : ($messages is string ? \Cortex\Prompts\Builders\TextPromptBuilder : \Cortex\Prompts\Builders\ChatPromptBuilder))
2327
*/
2428
public static function prompt(
25-
MessageCollection|array|string|null $messages,
26-
): ChatPromptBuilder {
27-
$builder = new ChatPromptBuilder();
28-
29-
if ($messages !== null) {
30-
$builder->messages($messages);
29+
MessageCollection|array|string|null $messages = null,
30+
): Prompt|PromptBuilder {
31+
if (func_num_args() === 0) {
32+
return new Prompt();
3133
}
3234

33-
return $builder;
35+
return is_string($messages)
36+
? Prompt::builder(PromptType::Text)->text($messages)
37+
: Prompt::builder(PromptType::Chat)->messages($messages);
3438
}
3539

3640
/**
@@ -47,9 +51,15 @@ public static function llm(?string $provider = null, ?string $model = null): LLM
4751
return $llm;
4852
}
4953

50-
public static function embeddings(?string $driver = null): EmbeddingsContract
54+
public static function embeddings(?string $driver = null, ?string $model = null): EmbeddingsContract
5155
{
52-
return Embeddings::driver($driver);
56+
$embeddings = Embeddings::driver($driver);
57+
58+
if ($model !== null) {
59+
$embeddings->withModel($model);
60+
}
61+
62+
return $embeddings;
5363
}
5464

5565
/**

src/CortexServiceProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
use Cortex\ModelInfo\ModelInfoFactory;
1010
use Spatie\LaravelPackageTools\Package;
1111
use Cortex\Embeddings\EmbeddingsManager;
12+
use Cortex\Prompts\PromptFactoryManager;
1213
use Cortex\Embeddings\Contracts\Embeddings;
14+
use Cortex\Prompts\Contracts\PromptFactory;
1315
use Illuminate\Contracts\Container\Container;
1416
use Spatie\LaravelPackageTools\PackageServiceProvider;
1517

@@ -30,6 +32,10 @@ public function packageRegistered(): void
3032
$this->app->alias('cortex.embeddings', EmbeddingsManager::class);
3133
$this->app->bind(Embeddings::class, fn(Container $app) => $app->make('cortex.embeddings')->driver());
3234

35+
$this->app->singleton('cortex.prompt_factory', fn(Container $app): PromptFactoryManager => new PromptFactoryManager($app));
36+
$this->app->alias('cortex.prompt_factory', PromptFactoryManager::class);
37+
$this->app->bind(PromptFactory::class, fn(Container $app) => $app->make('cortex.prompt_factory')->driver());
38+
3339
$this->app->singleton('cortex.model_info_factory', function (Container $app): ModelInfoFactory {
3440
$providers = [];
3541
foreach ($app->make('config')->get('cortex.model_info_providers', []) as $provider => $config) {

src/Facades/PromptFactory.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cortex\Facades;
6+
7+
use Illuminate\Support\Facades\Facade;
8+
9+
/**
10+
* @method static \Cortex\Prompts\Contracts\PromptFactory driver(string $driver = null)
11+
* @method static \Cortex\Prompts\Contracts\PromptFactory make(string $name, array<string, mixed> $options = [])
12+
* @method static string getDefaultDriver()
13+
*
14+
* @see \Cortex\Prompts\PromptFactoryManager
15+
*/
16+
class PromptFactory extends Facade
17+
{
18+
protected static function getFacadeAccessor(): string
19+
{
20+
return 'cortex.prompt_factory';
21+
}
22+
}

src/Prompts/Contracts/PromptBuilder.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,35 @@
44

55
namespace Cortex\Prompts\Contracts;
66

7+
use Closure;
8+
use Cortex\Pipeline;
9+
use Cortex\LLM\Contracts\LLM;
10+
use Cortex\JsonSchema\Types\ObjectSchema;
11+
use Cortex\LLM\Data\StructuredOutputConfig;
12+
713
interface PromptBuilder
814
{
915
/**
1016
* Build the prompt template.
1117
*/
1218
public function build(): PromptTemplate;
19+
20+
/**
21+
* Convenience method to build and pipe the prompt template to an LLM.
22+
*/
23+
public function llm(LLM|string|null $provider = null, Closure|string|null $model = null): Pipeline;
24+
25+
/**
26+
* @param array<string, mixed> $parameters
27+
* @param array<int, \Cortex\LLM\Contracts\Tool|\Closure|string> $tools
28+
* @param array<string, mixed> $additional
29+
*/
30+
public function metadata(
31+
?string $provider = null,
32+
?string $model = null,
33+
array $parameters = [],
34+
array $tools = [],
35+
StructuredOutputConfig|ObjectSchema|string|null $structuredOutput = null,
36+
array $additional = [],
37+
): self;
1338
}

src/Prompts/Contracts/PromptFactory.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@
66

77
interface PromptFactory
88
{
9-
public function make(string $name, array $config = []): PromptTemplate;
9+
/**
10+
* Make a prompt template from the given name and options.
11+
*
12+
* @param array<string, mixed> $options
13+
*/
14+
public function make(string $name, array $options = []): PromptTemplate;
1015
}

src/Prompts/Enums/PromptType.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cortex\Prompts\Enums;
6+
7+
use Cortex\Prompts\Builders\ChatPromptBuilder;
8+
use Cortex\Prompts\Builders\TextPromptBuilder;
9+
10+
enum PromptType: string
11+
{
12+
case Chat = 'chat';
13+
case Text = 'text';
14+
15+
public function builder(): ChatPromptBuilder|TextPromptBuilder
16+
{
17+
return match ($this) {
18+
self::Chat => new ChatPromptBuilder(),
19+
self::Text => new TextPromptBuilder(),
20+
};
21+
}
22+
}

src/Prompts/Factories/LangfusePromptFactory.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ public function __construct(
7474
) {}
7575

7676
/**
77-
* @param array{version?: int, label?: string} $config
77+
* @param array{version?: int, label?: string} $options
7878
*/
79-
public function make(string $name, array $config = []): PromptTemplate
79+
public function make(string $name, array $options = []): PromptTemplate
8080
{
81-
$data = $this->getResponseContent($name, $config);
81+
$data = $this->getResponseContent($name, $options);
8282

8383
$builder = match ($data['type']) {
8484
'chat' => $this->buildChatPrompt($data),
@@ -136,18 +136,20 @@ protected function buildTextPrompt(array $data): TextPromptBuilder
136136
}
137137

138138
/**
139+
* @param array{version?: int, label?: string} $options
140+
*
139141
* @return LangfusePromptResponse
140142
*/
141-
protected function getResponseContent(string $name, array $config = []): array
143+
protected function getResponseContent(string $name, array $options = []): array
142144
{
143145
$params = [];
144146

145-
if (isset($config['version'])) {
146-
$params['version'] = $config['version'];
147+
if (isset($options['version'])) {
148+
$params['version'] = $options['version'];
147149
}
148150

149-
if (isset($config['label'])) {
150-
$params['label'] = $config['label'];
151+
if (isset($options['label'])) {
152+
$params['label'] = $options['label'];
151153
}
152154

153155
$uri = sprintf('%s/api/public/v2/prompts/%s', $this->baseUrl, $name);

src/Prompts/Prompt.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cortex\Prompts;
6+
7+
use Cortex\Facades\PromptFactory;
8+
use Cortex\Prompts\Enums\PromptType;
9+
use Cortex\Prompts\Builders\ChatPromptBuilder;
10+
use Cortex\Prompts\Builders\TextPromptBuilder;
11+
use Cortex\Prompts\Contracts\PromptFactory as PromptFactoryContract;
12+
13+
class Prompt
14+
{
15+
/**
16+
* Get the prompt builder for the given type.
17+
*
18+
* @return ($type is \Cortex\Prompts\Enums\PromptType::Chat ? \Cortex\Prompts\Builders\ChatPromptBuilder : \Cortex\Prompts\Builders\TextPromptBuilder)
19+
*/
20+
public static function builder(PromptType $type = PromptType::Chat): ChatPromptBuilder|TextPromptBuilder
21+
{
22+
return $type->builder();
23+
}
24+
25+
/**
26+
* Get the prompt factory for the given driver.
27+
*/
28+
public static function factory(?string $driver = null): PromptFactoryContract
29+
{
30+
return PromptFactory::driver($driver);
31+
}
32+
}

0 commit comments

Comments
 (0)