Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion docs/providers/openai.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,17 @@ Prism::text()
->withPrompt('Solve the equation 3x + 10 = 14.')
->withProviderTools([new ProviderTool(type: 'code_interpreter', options: ['container' => ['type' => 'auto']])])
->asText();
```
```

### Additional Message Attributes

Adding optional parameters to a `UserMessage` like the `name` field can be done through the `additionalAttributes` parameter.

```php
Prism::text()
->using('openai', 'gpt-4.1')
->withMessages([
new UserMessage('Who are you?', additionalAttributes: ['name' => 'TJ']),
])
->asText()
```
1 change: 1 addition & 0 deletions src/Providers/OpenAI/Maps/MessageMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ protected function mapUserMessage(UserMessage $message): void
...self::mapDocumentParts($message->documents()),
...self::mapFileParts($message->files()),
],
...$message->additionalAttributes,
];
}

Expand Down
4 changes: 3 additions & 1 deletion src/ValueObjects/Messages/UserMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ class UserMessage implements Message

/**
* @param array<int, Text|Image|Document|OpenAIFile> $additionalContent
* @param array<string, mixed> $additionalAttributes
*/
public function __construct(
public readonly string $content,
public array $additionalContent = []
public array $additionalContent = [],
public readonly array $additionalAttributes = [],
) {
$this->additionalContent[] = new Text($content);
}
Expand Down
17 changes: 17 additions & 0 deletions tests/Providers/OpenAI/MessageMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,23 @@
]]);
});

it('maps user messages with additional attributes', function (): void {
$messageMap = new MessageMap(
messages: [
new UserMessage('Who are you?', additionalAttributes: ['name' => 'TJ']),
],
systemPrompts: []
);

expect($messageMap())->toBe([[
'role' => 'user',
'content' => [
['type' => 'input_text', 'text' => 'Who are you?'],
],
'name' => 'TJ',
]]);
});

it('maps user messages with images from path', function (): void {
$messageMap = new MessageMap(
messages: [
Expand Down