增加 用户基础信息传递
This commit is contained in:
parent
01ca320e06
commit
cd3a01bfc1
@ -8,6 +8,7 @@
|
|||||||
use App\Repositories\LLM\ChatEnum;
|
use App\Repositories\LLM\ChatEnum;
|
||||||
use App\Repositories\LLM\History;
|
use App\Repositories\LLM\History;
|
||||||
use App\Repositories\LLM\HumanMessage;
|
use App\Repositories\LLM\HumanMessage;
|
||||||
|
use GuzzleHttp\Exception\GuzzleException;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
@ -103,14 +104,18 @@ public function stream(string $stream_id)
|
|||||||
|
|
||||||
$tools = $chat->assistant->tools()->get();
|
$tools = $chat->assistant->tools()->get();
|
||||||
$llm->setTools($tools);
|
$llm->setTools($tools);
|
||||||
|
|
||||||
$llm->setHistory($history);
|
$llm->setHistory($history);
|
||||||
|
$llm->setUser($chat->user);
|
||||||
|
|
||||||
$last_human_message = $histories[$histories->count() - 1];
|
$last_human_message = $histories[$histories->count() - 1];
|
||||||
|
|
||||||
$history->addMessage(new HumanMessage($last_human_message));
|
$history->addMessage(new HumanMessage($last_human_message));
|
||||||
|
|
||||||
|
try {
|
||||||
$stream = $llm->streamResponse();
|
$stream = $llm->streamResponse();
|
||||||
|
} catch (GuzzleException) {
|
||||||
|
return $this->badRequest('请求失败,请重试。');
|
||||||
|
}
|
||||||
|
|
||||||
$response = response()->stream(function () use (&$stream, &$chat) {
|
$response = response()->stream(function () use (&$stream, &$chat) {
|
||||||
// 循环输出
|
// 循环输出
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\LLM;
|
namespace App\LLM;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
use App\Repositories\LLM\History;
|
use App\Repositories\LLM\History;
|
||||||
use Illuminate\Database\Eloquent\Collection;
|
use Illuminate\Database\Eloquent\Collection;
|
||||||
|
|
||||||
@ -10,6 +11,7 @@ interface BaseLLM
|
|||||||
public function setHistory(History $history);
|
public function setHistory(History $history);
|
||||||
|
|
||||||
public function setTools(Collection $tools);
|
public function setTools(Collection $tools);
|
||||||
|
public function setUser(User $user);
|
||||||
|
|
||||||
public function streamResponse();
|
public function streamResponse();
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace App\LLM;
|
namespace App\LLM;
|
||||||
|
|
||||||
use App\Logic\LLMTool;
|
use App\Logic\LLMTool;
|
||||||
|
use App\Models\User;
|
||||||
use App\Repositories\LLM\AIChunkMessage;
|
use App\Repositories\LLM\AIChunkMessage;
|
||||||
use App\Repositories\LLM\AIToolCallMessage;
|
use App\Repositories\LLM\AIToolCallMessage;
|
||||||
use App\Repositories\LLM\History;
|
use App\Repositories\LLM\History;
|
||||||
@ -12,6 +13,7 @@
|
|||||||
use GuzzleHttp\Exception\ClientException;
|
use GuzzleHttp\Exception\ClientException;
|
||||||
use GuzzleHttp\Exception\GuzzleException;
|
use GuzzleHttp\Exception\GuzzleException;
|
||||||
use Illuminate\Database\Eloquent\Collection;
|
use Illuminate\Database\Eloquent\Collection;
|
||||||
|
use Illuminate\Http\Client\ConnectionException;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
class Qwen implements BaseLLM
|
class Qwen implements BaseLLM
|
||||||
@ -30,7 +32,7 @@ class Qwen implements BaseLLM
|
|||||||
|
|
||||||
private bool $retry = false;
|
private bool $retry = false;
|
||||||
|
|
||||||
private int $retires = 0;
|
private User $user;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
@ -55,6 +57,11 @@ public function setHistory(History $history): void
|
|||||||
$this->history = $history;
|
$this->history = $history;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setUser(User $user): void
|
||||||
|
{
|
||||||
|
$this->user = $user;
|
||||||
|
}
|
||||||
|
|
||||||
public function setTools(Collection $tools): void
|
public function setTools(Collection $tools): void
|
||||||
{
|
{
|
||||||
$this->tools = $tools;
|
$this->tools = $tools;
|
||||||
@ -137,7 +144,7 @@ public function streamResponse()
|
|||||||
// log
|
// log
|
||||||
$event = json_decode($d, true);
|
$event = json_decode($d, true);
|
||||||
|
|
||||||
Log::debug('event', $event);
|
// Log::debug('event', $event);
|
||||||
|
|
||||||
if (isset($event['choices'][0])) {
|
if (isset($event['choices'][0])) {
|
||||||
$finish_reason = $event['choices'][0]['finish_reason'];
|
$finish_reason = $event['choices'][0]['finish_reason'];
|
||||||
@ -148,12 +155,12 @@ public function streamResponse()
|
|||||||
$this->history->addMessage($ai_message);
|
$this->history->addMessage($ai_message);
|
||||||
yield $ai_message;
|
yield $ai_message;
|
||||||
|
|
||||||
Log::debug('stop!');
|
// Log::debug('stop!');
|
||||||
|
|
||||||
break;
|
break;
|
||||||
} elseif ($finish_reason == 'tool_calls') {
|
} elseif ($finish_reason == 'tool_calls') {
|
||||||
$this->retry = true;
|
$this->retry = true;
|
||||||
Log::debug('finished_reason is tool call, set retry');
|
// Log::debug('finished_reason is tool call, set retry');
|
||||||
}
|
}
|
||||||
|
|
||||||
$delta = $event['choices'][0]['delta'];
|
$delta = $event['choices'][0]['delta'];
|
||||||
@ -172,7 +179,7 @@ public function streamResponse()
|
|||||||
|
|
||||||
yield $tool_req;
|
yield $tool_req;
|
||||||
|
|
||||||
Log::debug('tool req', [$tool_req]);
|
// Log::debug('tool req', [$tool_req]);
|
||||||
|
|
||||||
$tool_call_message = new AIToolCallMessage(content: '');
|
$tool_call_message = new AIToolCallMessage(content: '');
|
||||||
$tool_call_message->tool_calls = [
|
$tool_call_message->tool_calls = [
|
||||||
@ -188,8 +195,8 @@ public function streamResponse()
|
|||||||
$this->history->addMessage($tool_call_message);
|
$this->history->addMessage($tool_call_message);
|
||||||
$this->history->addMessage($tool_response_message);
|
$this->history->addMessage($tool_response_message);
|
||||||
|
|
||||||
Log::debug('tool call message', [$tool_call_message]);
|
// Log::debug('tool call message', [$tool_call_message]);
|
||||||
Log::debug('tool response', [$tool_response_message]);
|
// Log::debug('tool response', [$tool_response_message]);
|
||||||
|
|
||||||
yield $tool_call_message;
|
yield $tool_call_message;
|
||||||
yield $tool_response_message;
|
yield $tool_response_message;
|
||||||
@ -222,7 +229,7 @@ public function streamResponse()
|
|||||||
|
|
||||||
yield $ai_chunk_message;
|
yield $ai_chunk_message;
|
||||||
|
|
||||||
Log::debug('chunk', [$ai_chunk_message]);
|
// Log::debug('chunk', [$ai_chunk_message]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->retry = false;
|
$this->retry = false;
|
||||||
@ -240,6 +247,9 @@ public function streamResponse()
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ConnectionException
|
||||||
|
*/
|
||||||
private function callTool($tool_name, $args): string
|
private function callTool($tool_name, $args): string
|
||||||
{
|
{
|
||||||
// 遍历 tools, 找到
|
// 遍历 tools, 找到
|
||||||
@ -248,6 +258,11 @@ private function callTool($tool_name, $args): string
|
|||||||
if ($f['function']['name'] == $tool_name) {
|
if ($f['function']['name'] == $tool_name) {
|
||||||
$c = new LLMTool();
|
$c = new LLMTool();
|
||||||
$c->setTool($tool->data['tool_id']);
|
$c->setTool($tool->data['tool_id']);
|
||||||
|
|
||||||
|
if (isset($this->user)) {
|
||||||
|
$c->setUser($this->user);
|
||||||
|
}
|
||||||
|
|
||||||
$r = $c->callTool($tool_name, $args);
|
$r = $c->callTool($tool_name, $args);
|
||||||
|
|
||||||
return $r->result;
|
return $r->result;
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace App\Logic;
|
namespace App\Logic;
|
||||||
|
|
||||||
use App\Models\Tool;
|
use App\Models\Tool;
|
||||||
|
use App\Models\User;
|
||||||
use App\Repositories\LLM\FunctionCall;
|
use App\Repositories\LLM\FunctionCall;
|
||||||
use Illuminate\Http\Client\ConnectionException;
|
use Illuminate\Http\Client\ConnectionException;
|
||||||
use Illuminate\Support\Facades\Http;
|
use Illuminate\Support\Facades\Http;
|
||||||
@ -10,12 +11,18 @@
|
|||||||
class LLMTool
|
class LLMTool
|
||||||
{
|
{
|
||||||
protected Tool $tool;
|
protected Tool $tool;
|
||||||
|
protected User $user;
|
||||||
|
|
||||||
public function setTool(int $tool_id): void
|
public function setTool(int $tool_id): void
|
||||||
{
|
{
|
||||||
$this->tool = Tool::findOrFail($tool_id);
|
$this->tool = Tool::findOrFail($tool_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setUser(User $user):void
|
||||||
|
{
|
||||||
|
$this->user = $user;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws ConnectionException
|
* @throws ConnectionException
|
||||||
*/
|
*/
|
||||||
@ -27,15 +34,32 @@ public function callTool(string $function_name, $parameters = []): FunctionCall
|
|||||||
|
|
||||||
$function_name = substr($function_name, $prefix_length);
|
$function_name = substr($function_name, $prefix_length);
|
||||||
|
|
||||||
$http = Http::withToken($this->tool->api_key)->post($this->tool->data['callback_url'], [
|
$data = [
|
||||||
'function_name' => $function_name,
|
'function_name' => $function_name,
|
||||||
'parameters' => $parameters,
|
'parameters' => $parameters,
|
||||||
]);
|
'user' => [],
|
||||||
|
];
|
||||||
|
|
||||||
$r = new FunctionCall();
|
$r = new FunctionCall();
|
||||||
$r->name = $function_name;
|
$r->name = $function_name;
|
||||||
$r->parameters = $parameters;
|
$r->parameters = $parameters;
|
||||||
|
|
||||||
|
if (empty($this->tool->api_key)) {
|
||||||
|
$r->success = false;
|
||||||
|
$r->result = "[Error] 没有找到 API KEY,请先在工具设置中设置 API KEY。";
|
||||||
|
|
||||||
|
return $r;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($this->user)) {
|
||||||
|
$data['user'] = [
|
||||||
|
'id' => $this->user->external_id,
|
||||||
|
'name' => $this->user->name
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$http = Http::withToken($this->tool->api_key)->post($this->tool->data['callback_url'], $data);
|
||||||
|
|
||||||
if (! $http->ok()) {
|
if (! $http->ok()) {
|
||||||
$r->success = false;
|
$r->success = false;
|
||||||
$r->result = "[Error] 我们的服务器与工具 $function_name 通讯失败";
|
$r->result = "[Error] 我们的服务器与工具 $function_name 通讯失败";
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace App\Repositories\LLM;
|
namespace App\Repositories\LLM;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
|
||||||
class FunctionCall
|
class FunctionCall
|
||||||
{
|
{
|
||||||
public string $name;
|
public string $name;
|
||||||
@ -11,4 +13,6 @@ class FunctionCall
|
|||||||
public bool $success;
|
public bool $success;
|
||||||
|
|
||||||
public string $result;
|
public string $result;
|
||||||
|
|
||||||
|
public User $user;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user