改进 函数存储

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);
$toolRepo = new ToolRepo($json->json());
$tool = new Tool();
$tool->create([
'name' => $toolRepo->name,
'description' => $toolRepo->description,
$tool = $tool->create([
'name' => "",
'description' => "",
'discovery_url' => $url,
'api_key' => $request->input('api_key'),
'data' => $toolRepo,
'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);
}

View File

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

View File

@ -2,18 +2,23 @@
namespace App\Logic;
use App\Models\Tool;
use App\Repositories\LLM\FunctionCall;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Http;
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
{
// 使用 _ 分割
@ -22,7 +27,7 @@ public function callTool(string $function_name, $parameters = []): FunctionCall
$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,
'parameters' => $parameters,
]);

View File

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