diff --git a/app/Console/Commands/TestLLM.php b/app/Console/Commands/TestLLM.php index 8fc5ef2..d4278bb 100644 --- a/app/Console/Commands/TestLLM.php +++ b/app/Console/Commands/TestLLM.php @@ -3,9 +3,7 @@ namespace App\Console\Commands; use App\LLM\Qwen; -use App\Models\Assistant; use App\Models\Tool; -use App\Repositories\LLM\AIMessage; use App\Repositories\LLM\ChatEnum; use App\Repositories\LLM\History; use App\Repositories\LLM\HumanMessage; @@ -30,6 +28,7 @@ class TestLLM extends Command /** * Execute the console command. + * * @throws GuzzleException */ public function handle() @@ -45,12 +44,12 @@ public function handle() $llm->setHistory($history); while (true) { -// var_dump($history->getMessages()); + // var_dump($history->getMessages()); $q = $this->ask('请输入问题'); if (empty($q)) { - $q = "北京天气"; + $q = '北京天气'; } $history->addMessage(new HumanMessage($q)); @@ -60,12 +59,12 @@ public function handle() foreach ($s as $item) { if ($item->role == ChatEnum::Tool) { if ($item->processing) { - $this->info("正在执行: " . $item->content); + $this->info('正在执行: '.$item->content); echo "\n"; } else { - $this->info("执行结果: " . $item->content); + $this->info('执行结果: '.$item->content); } - } else if ($item->role == ChatEnum::AssistantChunk) { + } elseif ($item->role == ChatEnum::AssistantChunk) { echo $item->getLastAppend(); } } diff --git a/app/Http/Controllers/Api/ToolController.php b/app/Http/Controllers/Api/ToolController.php index 92bb649..0e13c9f 100644 --- a/app/Http/Controllers/Api/ToolController.php +++ b/app/Http/Controllers/Api/ToolController.php @@ -44,11 +44,10 @@ public function store(Request $request) $json = Http::get($url); - $tool = new Tool(); $tool = $tool->create([ - 'name' => "", - 'description' => "", + 'name' => '', + 'description' => '', 'discovery_url' => $url, 'api_key' => $request->input('api_key'), 'user_id' => $request->user('api')->id, diff --git a/app/LLM/BaseLLM.php b/app/LLM/BaseLLM.php index a4be62f..82feeeb 100644 --- a/app/LLM/BaseLLM.php +++ b/app/LLM/BaseLLM.php @@ -1,14 +1,15 @@ 'application/json', 'Accept' => 'text/event-stream', 'Cache-Control' => 'no-cache', - 'Authorization' => 'Bearer ' . config('services.dashscope.api_key'), + 'Authorization' => 'Bearer '.config('services.dashscope.api_key'), ], ]); - $url = config('services.dashscope.api_base') . '/compatible-mode/v1/chat/completions'; - + $url = config('services.dashscope.api_base').'/compatible-mode/v1/chat/completions'; $this->request_again = true; while ($this->request_again) { - $ai_chunk_message = new AIChunkMessage(""); + $ai_chunk_message = new AIChunkMessage(''); $request_body = [ 'model' => $this->model, @@ -97,7 +97,7 @@ public function streamResponse() $buffer = ''; - while (!$body->eof()) { + while (! $body->eof()) { $data = $body->read(1); $buffer .= $data; @@ -117,7 +117,6 @@ public function streamResponse() break; } - if (empty($d)) { return; } @@ -129,7 +128,7 @@ public function streamResponse() $ai_chunk_message->processing = false; $this->history->addMessage($ai_chunk_message->toAIMessage()); break; -// yield $ai_chunk_message; + // yield $ai_chunk_message; } $delta = $event['choices'][0]['delta']; @@ -147,14 +146,13 @@ public function streamResponse() $r = $this->callTool($this->tool_info['function']['name'], $info); + // $tool_response = [ + // 'name' => $this->tool_info['function']['name'], + // 'role' => 'tool', + // 'content' => $r, + // ]; -// $tool_response = [ -// 'name' => $this->tool_info['function']['name'], -// 'role' => 'tool', -// 'content' => $r, -// ]; - - $tool_call_message = new AIToolCallMessage(content: ""); + $tool_call_message = new AIToolCallMessage(content: ''); $tool_call_message->tool_calls = [ $this->tool_info, ]; @@ -167,15 +165,14 @@ public function streamResponse() yield $tool_call_message; yield $tool_response_message; -// -// -// -// $this->history[] = $tool_response; - -// yield new ToolResponseMessage( -// content: $tool_response_message->content, -// ); + // + // + // + // $this->history[] = $tool_response; + // yield new ToolResponseMessage( + // content: $tool_response_message->content, + // ); // 清空参数,以备二次调用 $this->clearToolInfo(); @@ -202,7 +199,7 @@ public function streamResponse() $this->tool_info['function']['arguments'] .= $call_info['function']['arguments']; } } elseif (empty($delta['choices'][0]['finish_reason'])) { - if (!empty($delta['content'])) { + if (! empty($delta['content'])) { $ai_chunk_message->append($delta['content']); $ai_chunk_message->processing = true; @@ -214,12 +211,10 @@ public function streamResponse() } - $buffer = ''; } } - } } @@ -240,7 +235,6 @@ private function callTool($tool_name, $args): string } - return "[Hint] 没有找到对应的工具 " . $tool_name . ',你确定是这个吗?'; + return '[Hint] 没有找到对应的工具 '.$tool_name.',你确定是这个吗?'; } - } diff --git a/app/Logic/LLMTool.php b/app/Logic/LLMTool.php index 86097fc..70e5da8 100644 --- a/app/Logic/LLMTool.php +++ b/app/Logic/LLMTool.php @@ -36,7 +36,7 @@ public function callTool(string $function_name, $parameters = []): FunctionCall $r->name = $function_name; $r->parameters = $parameters; - if (!$http->ok()) { + if (! $http->ok()) { $r->success = false; $r->result = "[Error] 我们的服务器与工具 $function_name 通讯失败"; } @@ -44,9 +44,10 @@ public function callTool(string $function_name, $parameters = []): FunctionCall $d = $http->json(); // 必须有 success 和 message 两个 - if (!isset($d['success']) || !isset($d['message'])) { + if (! isset($d['success']) || ! isset($d['message'])) { $r->success = false; $r->result = "[Error] 和 工具 $function_name 通讯失败,返回数据格式错误。"; + return $r; } diff --git a/app/Repositories/LLM/AIChunkMessage.php b/app/Repositories/LLM/AIChunkMessage.php index 227a701..f238418 100644 --- a/app/Repositories/LLM/AIChunkMessage.php +++ b/app/Repositories/LLM/AIChunkMessage.php @@ -5,7 +5,8 @@ class AIChunkMessage extends BaseMessage { public ChatEnum $role = ChatEnum::AssistantChunk; - protected string $last_append = ""; + + protected string $last_append = ''; public function toAIMessage(): AIMessage { diff --git a/app/Repositories/LLM/AIToolCallMessage.php b/app/Repositories/LLM/AIToolCallMessage.php index 1ae07b5..e713623 100644 --- a/app/Repositories/LLM/AIToolCallMessage.php +++ b/app/Repositories/LLM/AIToolCallMessage.php @@ -4,9 +4,7 @@ class AIToolCallMessage extends BaseMessage { - public ChatEnum $role = ChatEnum::Assistant; public array $tool_calls; - } diff --git a/app/Repositories/LLM/BaseMessage.php b/app/Repositories/LLM/BaseMessage.php index d8d53bd..e0d87ad 100644 --- a/app/Repositories/LLM/BaseMessage.php +++ b/app/Repositories/LLM/BaseMessage.php @@ -5,6 +5,7 @@ class BaseMessage { public string $content; + public bool $processing = false; public function __construct(string $content) @@ -12,7 +13,6 @@ public function __construct(string $content) $this->content = $content; } - public function append(string $content): void { $this->content .= $content; @@ -27,5 +27,4 @@ public function __toString(): string { return $this->content; } - } diff --git a/app/Repositories/LLM/History.php b/app/Repositories/LLM/History.php index 8be37e9..f6238a8 100644 --- a/app/Repositories/LLM/History.php +++ b/app/Repositories/LLM/History.php @@ -30,7 +30,6 @@ public function getForApi(): array { $history = []; - foreach ($this->history as $h) { $a = [ @@ -47,6 +46,4 @@ public function getForApi(): array return $history; } - - } diff --git a/app/Repositories/LLM/HumanMessage.php b/app/Repositories/LLM/HumanMessage.php index d2dd1c0..b2b8209 100644 --- a/app/Repositories/LLM/HumanMessage.php +++ b/app/Repositories/LLM/HumanMessage.php @@ -5,7 +5,4 @@ class HumanMessage extends BaseMessage { public ChatEnum $role = ChatEnum::Human; - - - } diff --git a/app/Repositories/LLM/ToolMessage.php b/app/Repositories/LLM/ToolMessage.php index 8898fb1..5b74bde 100644 --- a/app/Repositories/LLM/ToolMessage.php +++ b/app/Repositories/LLM/ToolMessage.php @@ -2,8 +2,7 @@ namespace App\Repositories\LLM; -class ToolMessage extends BaseMessage +class ToolMessage extends BaseMessage { public ChatEnum $role = ChatEnum::Tool; - } diff --git a/app/Repositories/LLM/ToolRequestMessage.php b/app/Repositories/LLM/ToolRequestMessage.php index 9b45b0a..9d19493 100644 --- a/app/Repositories/LLM/ToolRequestMessage.php +++ b/app/Repositories/LLM/ToolRequestMessage.php @@ -2,8 +2,9 @@ namespace App\Repositories\LLM; -class ToolRequestMessage extends BaseMessage +class ToolRequestMessage extends BaseMessage { public ChatEnum $role = ChatEnum::Tool; + public bool $processing = true; } diff --git a/app/Repositories/LLM/ToolResponseMessage.php b/app/Repositories/LLM/ToolResponseMessage.php index 4d40287..79be732 100644 --- a/app/Repositories/LLM/ToolResponseMessage.php +++ b/app/Repositories/LLM/ToolResponseMessage.php @@ -7,6 +7,6 @@ class ToolResponseMessage extends BaseMessage public ChatEnum $role = ChatEnum::Tool; public bool $requesting = false; - public string $name; + public string $name; } diff --git a/app/Repositories/Tool/Tool.php b/app/Repositories/Tool/Tool.php index e7d932e..162e72b 100644 --- a/app/Repositories/Tool/Tool.php +++ b/app/Repositories/Tool/Tool.php @@ -13,6 +13,7 @@ class Tool public string $callback_url; public string $description; + public int $tool_id; public array $tool_functions; diff --git a/app/Repositories/Tool/ToolFunction.php b/app/Repositories/Tool/ToolFunction.php index a952ae3..e452ccf 100644 --- a/app/Repositories/Tool/ToolFunction.php +++ b/app/Repositories/Tool/ToolFunction.php @@ -16,6 +16,7 @@ class ToolFunction protected array $data; public array $required; + /** * Create a new class instance. * @@ -33,7 +34,7 @@ public function __construct(array $data) private function parse(): void { $random_str = Str::random(config('settings.function_call.random_prefix_length')); - $this->name = $random_str . '_' . $this->data['name']; + $this->name = $random_str.'_'.$this->data['name']; $this->description = $this->data['description']; // 如果 parameters 不为空,则验证 diff --git a/config/services.php b/config/services.php index 6d397bc..ae09afa 100644 --- a/config/services.php +++ b/config/services.php @@ -40,5 +40,4 @@ 'api_base' => env('DASHSCOPE_API_BASE', 'https://dashscope.aliyuncs.com'), ], - ]; diff --git a/config/settings.php b/config/settings.php index 146bdde..74d4b88 100644 --- a/config/settings.php +++ b/config/settings.php @@ -3,6 +3,6 @@ return [ 'function_call' => [ // 防止命名冲突的随机长度,如果你更改了它,则需要更新全部函数的 name - 'random_prefix_length' => 8 - ] + 'random_prefix_length' => 8, + ], ];