格式化

This commit is contained in:
Twilight 2024-07-25 01:26:40 +08:00
parent af6171f5fc
commit e879784209
17 changed files with 46 additions and 59 deletions

View File

@ -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()
@ -50,7 +49,7 @@ public function handle()
$q = $this->ask('请输入问题');
if (empty($q)) {
$q = "北京天气";
$q = '北京天气';
}
$history->addMessage(new HumanMessage($q));
@ -60,10 +59,10 @@ 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);
}
} elseif ($item->role == ChatEnum::AssistantChunk) {
echo $item->getLastAppend();

View File

@ -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,

View File

@ -1,14 +1,15 @@
<?php
namespace App\LLM;
use App\Repositories\LLM\History;
use Illuminate\Database\Eloquent\Collection;
interface BaseLLM
{
public function setHistory(History $history);
public function setTools(Collection $tools);
public function streamResponse();
}

View File

@ -21,6 +21,7 @@ class Qwen implements BaseLLM
private array $tool_info = [];
private Collection $tools;
private array $tool_functions = [];
const END_OF_MESSAGE = "/\r\n\r\n|\n\n|\r\r/";
@ -75,11 +76,10 @@ public function streamResponse()
$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,
@ -117,7 +117,6 @@ public function streamResponse()
break;
}
if (empty($d)) {
return;
}
@ -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_call_message = new AIToolCallMessage(content: "");
$tool_call_message = new AIToolCallMessage(content: '');
$tool_call_message->tool_calls = [
$this->tool_info,
];
@ -176,7 +174,6 @@ public function streamResponse()
// content: $tool_response_message->content,
// );
// 清空参数,以备二次调用
$this->clearToolInfo();
@ -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.',你确定是这个吗?';
}
}

View File

@ -47,6 +47,7 @@ public function callTool(string $function_name, $parameters = []): FunctionCall
if (! isset($d['success']) || ! isset($d['message'])) {
$r->success = false;
$r->result = "[Error] 和 工具 $function_name 通讯失败,返回数据格式错误。";
return $r;
}

View File

@ -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
{

View File

@ -4,9 +4,7 @@
class AIToolCallMessage extends BaseMessage
{
public ChatEnum $role = ChatEnum::Assistant;
public array $tool_calls;
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -5,7 +5,4 @@
class HumanMessage extends BaseMessage
{
public ChatEnum $role = ChatEnum::Human;
}

View File

@ -5,5 +5,4 @@
class ToolMessage extends BaseMessage
{
public ChatEnum $role = ChatEnum::Tool;
}

View File

@ -5,5 +5,6 @@
class ToolRequestMessage extends BaseMessage
{
public ChatEnum $role = ChatEnum::Tool;
public bool $processing = true;
}

View File

@ -7,6 +7,6 @@ class ToolResponseMessage extends BaseMessage
public ChatEnum $role = ChatEnum::Tool;
public bool $requesting = false;
public string $name;
public string $name;
}

View File

@ -13,6 +13,7 @@ class Tool
public string $callback_url;
public string $description;
public int $tool_id;
public array $tool_functions;

View File

@ -16,6 +16,7 @@ class ToolFunction
protected array $data;
public array $required;
/**
* Create a new class instance.
*

View File

@ -40,5 +40,4 @@
'api_base' => env('DASHSCOPE_API_BASE', 'https://dashscope.aliyuncs.com'),
],
];

View File

@ -3,6 +3,6 @@
return [
'function_call' => [
// 防止命名冲突的随机长度,如果你更改了它,则需要更新全部函数的 name
'random_prefix_length' => 8
]
'random_prefix_length' => 8,
],
];