Skip to content
Closed
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
8 changes: 4 additions & 4 deletions src/Providers/OpenAI/Handlers/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
use Prism\Prism\Exceptions\PrismRateLimitedException;
use Prism\Prism\Providers\OpenAI\Concerns\ProcessesRateLimits;
use Prism\Prism\Providers\OpenAI\Maps\ChatMessageMap;
use Prism\Prism\Providers\OpenAI\Maps\ChatToolChoiceMap;
use Prism\Prism\Providers\OpenAI\Maps\ChatToolMap;
use Prism\Prism\Providers\OpenAI\Maps\FinishReasonMap;
use Prism\Prism\Providers\OpenAI\Maps\ToolChoiceMap;
use Prism\Prism\Providers\OpenAI\Maps\ToolMap;
use Prism\Prism\Text\Chunk;
use Prism\Prism\Text\Request;
use Prism\Prism\ValueObjects\Messages\AssistantMessage;
Expand Down Expand Up @@ -224,8 +224,8 @@ protected function sendRequest(Request $request): Response
'temperature' => $request->temperature(),
'top_p' => $request->topP(),
'metadata' => $request->providerOptions('metadata'),
'tools' => ToolMap::map($request->tools()),
'tool_choice' => ToolChoiceMap::map($request->toolChoice()),
'tools' => ChatToolMap::map($request->tools()),
'tool_choice' => ChatToolChoiceMap::map($request->toolChoice()),
]))
);
} catch (Throwable $e) {
Expand Down
44 changes: 16 additions & 28 deletions src/Providers/OpenAI/Maps/ChatMessageMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ protected function mapToolResultMessage(ToolResultMessage $message): void
{
foreach ($message->toolResults as $toolResult) {
$this->mappedMessages[] = [
'type' => 'function_call_output',
'call_id' => $toolResult->toolCallResultId,
'output' => $toolResult->result,
'role' => 'tool',
'tool_call_id' => $toolResult->toolCallId,
'content' => $toolResult->result,
];
}
}
Expand Down Expand Up @@ -122,31 +122,19 @@ protected static function mapFileParts(array $files): array

protected function mapAssistantMessage(AssistantMessage $message): void
{
if ($message->content !== '' && $message->content !== '0') {
$this->mappedMessages[] = [
'role' => 'assistant',
'content' => $message->content,
];
}
$toolCalls = array_map(fn (ToolCall $toolCall): array => [
'id' => $toolCall->id,
'type' => 'function',
'function' => [
'name' => $toolCall->name,
'arguments' => json_encode($toolCall->arguments()),
],
], $message->toolCalls);

if ($message->toolCalls !== []) {
array_push(
$this->mappedMessages,
...array_filter(
array_map(fn (ToolCall $toolCall): ?array => is_null($toolCall->reasoningId) ? null : [
'type' => 'reasoning',
'id' => $toolCall->reasoningId,
'summary' => $toolCall->reasoningSummary,
], $message->toolCalls)
),
...array_map(fn (ToolCall $toolCall): array => [
'id' => $toolCall->id,
'call_id' => $toolCall->resultId,
'type' => 'function_call',
'name' => $toolCall->name,
'arguments' => json_encode($toolCall->arguments()),
], $message->toolCalls)
);
}
$this->mappedMessages[] = array_filter([
'role' => 'assistant',
'content' => $message->content,
'tool_calls' => $toolCalls,
]);
}
}
32 changes: 32 additions & 0 deletions src/Providers/OpenAI/Maps/ChatToolChoiceMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Prism\Prism\Providers\OpenAI\Maps;

use Prism\Prism\Enums\ToolChoice;

class ChatToolChoiceMap
{
/**
* @return array<string, mixed>|string|null
*/
public static function map(string|ToolChoice|null $toolChoice): string|array|null
{
if (is_string($toolChoice)) {
return [
'type' => 'function',
'function' => [
'name' => $toolChoice,
],
];
}

return match ($toolChoice) {
ToolChoice::Auto => 'auto',
ToolChoice::Any => 'required',
ToolChoice::None => 'none',
null => $toolChoice,
};
}
}
33 changes: 33 additions & 0 deletions src/Providers/OpenAI/Maps/ChatToolMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Prism\Prism\Providers\OpenAI\Maps;

use Prism\Prism\Tool;

class ChatToolMap
{
/**
* @param Tool[] $tools
* @return array<string, mixed>
*/
public static function Map(array $tools): array
{
return array_map(fn (Tool $tool): array => array_filter([
'type' => 'function',
'function' => [
'name' => $tool->name(),
'description' => $tool->description(),
...count($tool->parameters()) ? [
'parameters' => [
'type' => 'object',
'properties' => $tool->parametersAsArray(),
'required' => $tool->requiredParameters(),
],
] : [],
],
'strict' => (bool) $tool->providerOptions('strict'),
]), $tools);
}
}
44 changes: 19 additions & 25 deletions tests/Fixtures/openai/stream-basic-text-1.json
Original file line number Diff line number Diff line change
@@ -1,46 +1,40 @@
data: {"id":"chatcmpl-Bhk6EgmSSUhFYOW0WQZAmCHBxc2qu","object":"chat.completion.chunk","created":1749765010,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"role":"assistant","content":"","refusal":null},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-Bj8fPblArh9nPJ6mMOZ9Jg6Mf5las","object":"chat.completion.chunk","created":1750097775,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"role":"assistant","content":"","refusal":null},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-Bhk6EgmSSUhFYOW0WQZAmCHBxc2qu","object":"chat.completion.chunk","created":1749765010,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"I"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-Bj8fPblArh9nPJ6mMOZ9Jg6Mf5las","object":"chat.completion.chunk","created":1750097775,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"I"},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-Bhk6EgmSSUhFYOW0WQZAmCHBxc2qu","object":"chat.completion.chunk","created":1749765010,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" am"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-Bj8fPblArh9nPJ6mMOZ9Jg6Mf5las","object":"chat.completion.chunk","created":1750097775,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" am"},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-Bhk6EgmSSUhFYOW0WQZAmCHBxc2qu","object":"chat.completion.chunk","created":1749765010,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" an"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-Bj8fPblArh9nPJ6mMOZ9Jg6Mf5las","object":"chat.completion.chunk","created":1750097775,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" an"},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-Bhk6EgmSSUhFYOW0WQZAmCHBxc2qu","object":"chat.completion.chunk","created":1749765010,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" artificial"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-Bj8fPblArh9nPJ6mMOZ9Jg6Mf5las","object":"chat.completion.chunk","created":1750097775,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" AI"},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-Bhk6EgmSSUhFYOW0WQZAmCHBxc2qu","object":"chat.completion.chunk","created":1749765010,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" intelligence"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-Bj8fPblArh9nPJ6mMOZ9Jg6Mf5las","object":"chat.completion.chunk","created":1750097775,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" developed"},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-Bhk6EgmSSUhFYOW0WQZAmCHBxc2qu","object":"chat.completion.chunk","created":1749765010,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" designed"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-Bj8fPblArh9nPJ6mMOZ9Jg6Mf5las","object":"chat.completion.chunk","created":1750097775,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" by"},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-Bhk6EgmSSUhFYOW0WQZAmCHBxc2qu","object":"chat.completion.chunk","created":1749765010,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" to"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-Bj8fPblArh9nPJ6mMOZ9Jg6Mf5las","object":"chat.completion.chunk","created":1750097775,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" Open"},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-Bhk6EgmSSUhFYOW0WQZAmCHBxc2qu","object":"chat.completion.chunk","created":1749765010,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" assist"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-Bj8fPblArh9nPJ6mMOZ9Jg6Mf5las","object":"chat.completion.chunk","created":1750097775,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"AI"},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-Bhk6EgmSSUhFYOW0WQZAmCHBxc2qu","object":"chat.completion.chunk","created":1749765010,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" with"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-Bj8fPblArh9nPJ6mMOZ9Jg6Mf5las","object":"chat.completion.chunk","created":1750097775,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" to"},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-Bhk6EgmSSUhFYOW0WQZAmCHBxc2qu","object":"chat.completion.chunk","created":1749765010,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" tasks"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-Bj8fPblArh9nPJ6mMOZ9Jg6Mf5las","object":"chat.completion.chunk","created":1750097775,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" assist"},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-Bhk6EgmSSUhFYOW0WQZAmCHBxc2qu","object":"chat.completion.chunk","created":1749765010,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" and"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-Bj8fPblArh9nPJ6mMOZ9Jg6Mf5las","object":"chat.completion.chunk","created":1750097775,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" users"},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-Bhk6EgmSSUhFYOW0WQZAmCHBxc2qu","object":"chat.completion.chunk","created":1749765010,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" answer"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-Bj8fPblArh9nPJ6mMOZ9Jg6Mf5las","object":"chat.completion.chunk","created":1750097775,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" with"},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-Bhk6EgmSSUhFYOW0WQZAmCHBxc2qu","object":"chat.completion.chunk","created":1749765010,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" questions"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-Bj8fPblArh9nPJ6mMOZ9Jg6Mf5las","object":"chat.completion.chunk","created":1750097775,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" various"},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-Bhk6EgmSSUhFYOW0WQZAmCHBxc2qu","object":"chat.completion.chunk","created":1749765010,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" to"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-Bj8fPblArh9nPJ6mMOZ9Jg6Mf5las","object":"chat.completion.chunk","created":1750097775,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" requests"},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-Bhk6EgmSSUhFYOW0WQZAmCHBxc2qu","object":"chat.completion.chunk","created":1749765010,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" the"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-Bj8fPblArh9nPJ6mMOZ9Jg6Mf5las","object":"chat.completion.chunk","created":1750097775,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" and"},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-Bhk6EgmSSUhFYOW0WQZAmCHBxc2qu","object":"chat.completion.chunk","created":1749765010,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" best"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-Bj8fPblArh9nPJ6mMOZ9Jg6Mf5las","object":"chat.completion.chunk","created":1750097775,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" tasks"},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-Bhk6EgmSSUhFYOW0WQZAmCHBxc2qu","object":"chat.completion.chunk","created":1749765010,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" of"},"logprobs":null,"finish_reason":null}]}
data: {"id":"chatcmpl-Bj8fPblArh9nPJ6mMOZ9Jg6Mf5las","object":"chat.completion.chunk","created":1750097775,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"."},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-Bhk6EgmSSUhFYOW0WQZAmCHBxc2qu","object":"chat.completion.chunk","created":1749765010,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" my"},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-Bhk6EgmSSUhFYOW0WQZAmCHBxc2qu","object":"chat.completion.chunk","created":1749765010,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":" ability"},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-Bhk6EgmSSUhFYOW0WQZAmCHBxc2qu","object":"chat.completion.chunk","created":1749765010,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{"content":"."},"logprobs":null,"finish_reason":null}]}

data: {"id":"chatcmpl-Bhk6EgmSSUhFYOW0WQZAmCHBxc2qu","object":"chat.completion.chunk","created":1749765010,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}]}
data: {"id":"chatcmpl-Bj8fPblArh9nPJ6mMOZ9Jg6Mf5las","object":"chat.completion.chunk","created":1750097775,"model":"gpt-4-0613","service_tier":"default","system_fingerprint":null,"choices":[{"index":0,"delta":{},"logprobs":null,"finish_reason":"stop"}]}

data: [DONE]

Loading