baseUrl() !== '' && $this->apiKey() !== ''; } /** * 简单的聊天补全。 * * @param array $messages */ public function chat(array $messages, array $options = []): string { if (! $this->isConfigured()) { throw new \RuntimeException('LLM 未配置:请设置 LLM_BASE_URL 与 LLM_API_KEY'); } $response = Http::timeout(60) ->withToken($this->apiKey()) ->post($this->baseUrl().'/chat/completions', [ 'model' => $options['model'] ?? $this->model(), 'messages' => $messages, 'temperature' => $options['temperature'] ?? 0.3, 'max_tokens' => $options['max_tokens'] ?? 1024, ]); if ($response->failed()) { Log::error('LLM 请求失败', ['status' => $response->status(), 'body' => $response->body()]); throw new \RuntimeException('LLM 请求失败:HTTP '.$response->status()); } $content = $response->json('choices.0.message.content', ''); return is_array($content) ? json_encode($content, JSON_UNESCAPED_UNICODE) : (string) $content; } }