- 支持 tool.call 和 tool.result 消息类型处理 - 引入 Tool 调度与执行逻辑,支持超时与结果截断 - 增加 ToolRegistry 和 ToolExecutor 管理工具定义与执行 - 更新上下文构建与消息映射逻辑,适配工具闭环处理 - 扩展配置与环境变量,支持 Tool 调用相关选项 - 增强单元测试覆盖工具调用与执行情景 - 更新文档和 OpenAPI,新增工具相关说明与模型定义
200 lines
6.3 KiB
PHP
200 lines
6.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Agent\OpenAi;
|
|
|
|
use App\Models\Message;
|
|
use App\Services\Agent\AgentContext;
|
|
use App\Services\Tool\ToolRegistry;
|
|
|
|
class ChatCompletionsRequestBuilder
|
|
{
|
|
public function __construct(
|
|
private readonly ToolRegistry $toolRegistry,
|
|
private ?string $model = null,
|
|
private ?float $temperature = null,
|
|
private ?float $topP = null,
|
|
private ?bool $includeUsage = null,
|
|
private ?string $toolChoice = null,
|
|
) {
|
|
$this->model = $this->model ?? (string) config('agent.openai.model', 'gpt-4o-mini');
|
|
$this->temperature = $this->temperature ?? (float) config('agent.openai.temperature', 0.7);
|
|
$this->topP = $this->topP ?? (float) config('agent.openai.top_p', 1.0);
|
|
$this->includeUsage = $this->includeUsage ?? (bool) config('agent.openai.include_usage', false);
|
|
$this->toolChoice = $this->toolChoice ?? (string) config('agent.tools.tool_choice', 'auto');
|
|
}
|
|
|
|
/**
|
|
* Builds an OpenAI-compatible Chat Completions payload from AgentContext.
|
|
*
|
|
* @param array<string, mixed> $options
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function build(AgentContext $context, array $options = []): array
|
|
{
|
|
$payload = [
|
|
'model' => (string) ($options['model'] ?? $this->model),
|
|
'messages' => $this->buildMessages($context),
|
|
'stream' => true,
|
|
];
|
|
|
|
if (array_key_exists('temperature', $options)) {
|
|
$payload['temperature'] = (float) $options['temperature'];
|
|
} else {
|
|
$payload['temperature'] = (float) $this->temperature;
|
|
}
|
|
|
|
if (array_key_exists('top_p', $options)) {
|
|
$payload['top_p'] = (float) $options['top_p'];
|
|
} else {
|
|
$payload['top_p'] = (float) $this->topP;
|
|
}
|
|
|
|
if (array_key_exists('max_tokens', $options)) {
|
|
$payload['max_tokens'] = (int) $options['max_tokens'];
|
|
}
|
|
|
|
if (array_key_exists('stop', $options)) {
|
|
$payload['stop'] = $options['stop'];
|
|
}
|
|
|
|
if (array_key_exists('stream_options', $options)) {
|
|
$payload['stream_options'] = $options['stream_options'];
|
|
} elseif ($this->includeUsage) {
|
|
$payload['stream_options'] = ['include_usage' => true];
|
|
}
|
|
|
|
if (array_key_exists('response_format', $options)) {
|
|
$payload['response_format'] = $options['response_format'];
|
|
}
|
|
|
|
$toolsSpec = $this->toolRegistry->openAiToolsSpec();
|
|
|
|
if (! empty($toolsSpec)) {
|
|
$payload['tools'] = $toolsSpec;
|
|
$payload['tool_choice'] = $options['tool_choice'] ?? $this->toolChoice ?? 'auto';
|
|
}
|
|
|
|
return $payload;
|
|
}
|
|
|
|
/**
|
|
* @return array<int, array<string, mixed>>
|
|
*/
|
|
private function buildMessages(AgentContext $context): array
|
|
{
|
|
$messages = [];
|
|
|
|
if ($context->systemPrompt !== '') {
|
|
$messages[] = [
|
|
'role' => 'system',
|
|
'content' => $context->systemPrompt,
|
|
];
|
|
}
|
|
|
|
foreach ($context->messages as $message) {
|
|
$role = $this->mapRole((string) ($message['role'] ?? ''));
|
|
$content = $message['content'] ?? null;
|
|
$type = (string) ($message['type'] ?? '');
|
|
$payload = $message['payload'] ?? null;
|
|
|
|
if (! $role || ! is_string($content) || $content === '') {
|
|
$content = null;
|
|
}
|
|
|
|
if ($type === 'tool.call' && is_array($payload)) {
|
|
$toolCall = $this->normalizeToolCallPayload($payload, $content);
|
|
if ($toolCall) {
|
|
$messages[] = $toolCall;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if ($type === 'tool.result' && is_array($payload)) {
|
|
$toolResult = $this->normalizeToolResultPayload($payload, $content);
|
|
if ($toolResult) {
|
|
$messages[] = $toolResult;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if ($content !== null) {
|
|
$messages[] = [
|
|
'role' => $role,
|
|
'content' => $content,
|
|
];
|
|
}
|
|
}
|
|
|
|
return $messages;
|
|
}
|
|
|
|
private function mapRole(string $role): ?string
|
|
{
|
|
return match ($role) {
|
|
Message::ROLE_USER => 'user',
|
|
Message::ROLE_AGENT => 'assistant',
|
|
Message::ROLE_SYSTEM => 'system',
|
|
default => null,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $payload
|
|
* @return array<string, mixed>|null
|
|
*/
|
|
private function normalizeToolCallPayload(array $payload, ?string $content): ?array
|
|
{
|
|
$toolCallId = $payload['tool_call_id'] ?? null;
|
|
$name = $payload['name'] ?? null;
|
|
$arguments = $payload['arguments'] ?? null;
|
|
|
|
if (! is_string($toolCallId) || ! is_string($name) || $toolCallId === '' || $name === '') {
|
|
return null;
|
|
}
|
|
|
|
$argumentsString = is_string($arguments) ? $arguments : json_encode($arguments, JSON_UNESCAPED_UNICODE);
|
|
|
|
return [
|
|
'role' => 'assistant',
|
|
'content' => $content,
|
|
'tool_calls' => [
|
|
[
|
|
'id' => $toolCallId,
|
|
'type' => 'function',
|
|
'function' => [
|
|
'name' => $name,
|
|
'arguments' => $argumentsString ?? '',
|
|
],
|
|
],
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $payload
|
|
* @return array<string, mixed>|null
|
|
*/
|
|
private function normalizeToolResultPayload(array $payload, ?string $content): ?array
|
|
{
|
|
$toolCallId = $payload['tool_call_id'] ?? null;
|
|
$name = $payload['name'] ?? null;
|
|
|
|
if (! is_string($toolCallId) || $toolCallId === '') {
|
|
return null;
|
|
}
|
|
|
|
$resultContent = $content ?? ($payload['output'] ?? null);
|
|
|
|
if (! is_string($resultContent)) {
|
|
$resultContent = json_encode($payload['output'] ?? $payload, JSON_UNESCAPED_UNICODE);
|
|
}
|
|
|
|
return [
|
|
'role' => 'tool',
|
|
'tool_call_id' => $toolCallId,
|
|
'name' => is_string($name) ? $name : null,
|
|
'content' => $resultContent,
|
|
];
|
|
}
|
|
}
|