改进 函数存储

This commit is contained in:
Twilight 2024-07-25 01:26:29 +08:00
parent ab03c27304
commit af6171f5fc
4 changed files with 26 additions and 14 deletions

View File

@ -44,18 +44,23 @@ public function store(Request $request)
$json = Http::get($url); $json = Http::get($url);
$toolRepo = new ToolRepo($json->json());
$tool = new Tool(); $tool = new Tool();
$tool->create([ $tool = $tool->create([
'name' => $toolRepo->name, 'name' => "",
'description' => $toolRepo->description, 'description' => "",
'discovery_url' => $url, 'discovery_url' => $url,
'api_key' => $request->input('api_key'), 'api_key' => $request->input('api_key'),
'data' => $toolRepo,
'user_id' => $request->user('api')->id, 'user_id' => $request->user('api')->id,
]); ]);
$toolRepo = new ToolRepo($tool->id, $json->json());
$tool->update([
'name' => $toolRepo->name,
'description' => $toolRepo->description,
'data' => $toolRepo,
]);
return $this->created($tool); return $this->created($tool);
} }

View File

@ -231,7 +231,7 @@ private function callTool($tool_name, $args): string
foreach ($tool->data['tool_functions'] as $f) { foreach ($tool->data['tool_functions'] as $f) {
if ($f['function']['name'] == $tool_name) { if ($f['function']['name'] == $tool_name) {
$c = new LLMTool(); $c = new LLMTool();
$c->setCallbackUrl($tool->data['callback_url']); $c->setTool($tool->data['tool_id']);
$r = $c->callTool($tool_name, $args); $r = $c->callTool($tool_name, $args);
return $r->result; return $r->result;

View File

@ -2,18 +2,23 @@
namespace App\Logic; namespace App\Logic;
use App\Models\Tool;
use App\Repositories\LLM\FunctionCall; use App\Repositories\LLM\FunctionCall;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
class LLMTool class LLMTool
{ {
protected string $callback_url; protected Tool $tool;
public function setCallbackUrl(string $callback_url): void public function setTool(int $tool_id): void
{ {
$this->callback_url = $callback_url; $this->tool = Tool::findOrFail($tool_id);
} }
/**
* @throws ConnectionException
*/
public function callTool(string $function_name, $parameters = []): FunctionCall public function callTool(string $function_name, $parameters = []): FunctionCall
{ {
// 使用 _ 分割 // 使用 _ 分割
@ -22,7 +27,7 @@ public function callTool(string $function_name, $parameters = []): FunctionCall
$function_name = substr($function_name, $prefix_length); $function_name = substr($function_name, $prefix_length);
$http = Http::post($this->callback_url, [ $http = Http::withToken($this->tool->api_key)->post($this->tool->data['callback_url'], [
'function_name' => $function_name, 'function_name' => $function_name,
'parameters' => $parameters, 'parameters' => $parameters,
]); ]);
@ -31,7 +36,7 @@ public function callTool(string $function_name, $parameters = []): FunctionCall
$r->name = $function_name; $r->name = $function_name;
$r->parameters = $parameters; $r->parameters = $parameters;
if (! $http->ok()) { if (!$http->ok()) {
$r->success = false; $r->success = false;
$r->result = "[Error] 我们的服务器与工具 $function_name 通讯失败"; $r->result = "[Error] 我们的服务器与工具 $function_name 通讯失败";
} }
@ -39,13 +44,13 @@ public function callTool(string $function_name, $parameters = []): FunctionCall
$d = $http->json(); $d = $http->json();
// 必须有 success 和 message 两个 // 必须有 success 和 message 两个
if (! isset($d['success']) || ! isset($d['message'])) { if (!isset($d['success']) || !isset($d['message'])) {
$r->success = false; $r->success = false;
$r->result = "[Error] 和 工具 $function_name 通讯失败,返回数据格式错误。"; $r->result = "[Error] 和 工具 $function_name 通讯失败,返回数据格式错误。";
return $r; return $r;
} }
$r->success= $d['success']; $r->success = $d['success'];
$r->result = $d['message']; $r->result = $d['message'];
return $r; return $r;

View File

@ -13,6 +13,7 @@ class Tool
public string $callback_url; public string $callback_url;
public string $description; public string $description;
public int $tool_id;
public array $tool_functions; public array $tool_functions;
@ -21,9 +22,10 @@ class Tool
/** /**
* @throws Exception * @throws Exception
*/ */
public function __construct(array $data) public function __construct(int $tool_id, array $data)
{ {
$this->data = $data; $this->data = $data;
$this->tool_id = $tool_id;
$this->parse(); $this->parse();
} }