Skip to content

Commit

Permalink
Moving function check down since I am doing a tool check first that t…
Browse files Browse the repository at this point in the history
…he user selects
  • Loading branch information
alnutile committed Jul 3, 2024
1 parent aadba38 commit 508602b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 77 deletions.
158 changes: 81 additions & 77 deletions Modules/LlmDriver/app/Orchestrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,7 @@ public function handle(
?Filter $filter = null,
string $tool = ''): ?string
{
/**
* We are looking first for functions / agents / tools
*/
Log::info('[LaraChain] Orchestration Function Check');

$functions = LlmDriverFacade::driver($chat->chatable->getDriver())
->functionPromptChat($messagesArray);

Log::info("['LaraChain'] Functions Found?", [
'count' => count($functions),
'functions' => $functions,
]);

if ($tool) {
Log::info('[LaraChain] Orchestration Has Tool', [
Expand All @@ -70,91 +59,106 @@ public function handle(
$this->requiresFollowUp($messagesArray, $chat);
}

} elseif ($this->hasFunctions($functions)) {
Log::info('[LaraChain] Orchestration Has Functions', $functions);
} else {
/**
* We are looking first for functions / agents / tools
*/
Log::info('[LaraChain] Orchestration Function Check');

foreach ($functions as $function) {
$functionName = data_get($function, 'name', null);
$functions = LlmDriverFacade::driver($chat->chatable->getDriver())
->functionPromptChat($messagesArray);

if (is_null($functionName)) {
throw new \Exception('Function name is required');
}
Log::info("['LaraChain'] Functions Found?", [
'count' => count($functions),
'functions' => $functions,
]);

notify_ui($chat, 'We are running the agent back shortly');
if ($this->hasFunctions($functions)) {
Log::info('[LaraChain] Orchestration Has Functions', $functions);

Log::info('[LaraChain] - Running function '.$functionName);
foreach ($functions as $function) {
$functionName = data_get($function, 'name', null);

$functionClass = app()->make($functionName);
if (is_null($functionName)) {
throw new \Exception('Function name is required');
}

$arguments = data_get($function, 'arguments');
notify_ui($chat, 'We are running the agent back shortly');

$arguments = is_array($arguments) ? json_encode($arguments) : '';
Log::info('[LaraChain] - Running function '.$functionName);

$functionDto = FunctionCallDto::from([
'arguments' => $arguments,
'function_name' => $functionName,
'filter' => $filter,
]);
$functionClass = app()->make($functionName);

/** @var FunctionResponse $response */
$response = $functionClass->handle($messagesArray, $chat, $functionDto);
$arguments = data_get($function, 'arguments');

Log::info('[LaraChain] - Function Response', [
'function' => $functionName,
'response' => $response,
]);
$arguments = is_array($arguments) ? json_encode($arguments) : '';

$message = null;
if ($response->save_to_message) {
$functionDto = FunctionCallDto::from([
'arguments' => $arguments,
'function_name' => $functionName,
'filter' => $filter,
]);

$message = $chat->addInput(
message: $response->content,
role: RoleEnum::Assistant,
show_in_thread: true);
}
/** @var FunctionResponse $response */
$response = $functionClass->handle($messagesArray, $chat, $functionDto);

if ($response->prompt) {
PromptHistory::create([
'prompt' => $response->prompt,
'chat_id' => $chat->getChat()->id,
'message_id' => $message?->id,
/** @phpstan-ignore-next-line */
'collection_id' => $chat->getChatable()?->id,
Log::info('[LaraChain] - Function Response', [
'function' => $functionName,
'response' => $response,
]);
}

if (! empty($response->documentChunks)) {
$this->saveDocumentReference(
$message,
$response->documentChunks
);
$message = null;
if ($response->save_to_message) {

$message = $chat->addInput(
message: $response->content,
role: RoleEnum::Assistant,
show_in_thread: true);
}

if ($response->prompt) {
PromptHistory::create([
'prompt' => $response->prompt,
'chat_id' => $chat->getChat()->id,
'message_id' => $message?->id,
/** @phpstan-ignore-next-line */
'collection_id' => $chat->getChatable()?->id,
]);
}

if (! empty($response->documentChunks)) {
$this->saveDocumentReference(
$message,
$response->documentChunks
);
}

$messagesArray = Arr::wrap(MessageInDto::from([
'role' => 'assistant',
'content' => $response->content,
]));

$this->response = $response->content;
$this->requiresFollowup = $response->requires_follow_up_prompt;
}

$messagesArray = Arr::wrap(MessageInDto::from([
'role' => 'assistant',
'content' => $response->content,
]));

$this->response = $response->content;
$this->requiresFollowup = $response->requires_follow_up_prompt;
}

} else {
Log::info('[LaraChain] Orchestration No Functions Default Search And Summarize');
/**
* @NOTE
* this assumes way too much
*/
$message = get_latest_user_content($messagesArray);
} else {
Log::info('[LaraChain] Orchestration No Functions Default Search And Summarize');
/**
* @NOTE
* this assumes way too much
*/
$message = get_latest_user_content($messagesArray);

if (is_null($message)) {
Log::error('[LaraChain] Orchestration No Message Found', [
'messages' => $messagesArray,
]);
throw new \Exception('No message found in incoming messages');
}

if (is_null($message)) {
Log::error('[LaraChain] Orchestration No Message Found', [
'messages' => $messagesArray,
]);
throw new \Exception('No message found in incoming messages');
return SearchAndSummarizeChatRepo::search($chat, $message, $filter);
}

return SearchAndSummarizeChatRepo::search($chat, $message, $filter);
}

$this->requiresFollowUp($messagesArray, $chat);
Expand Down
Binary file modified tests/.DS_Store
Binary file not shown.
Binary file added tests/example-docs/ExampleRFP.pdf
Binary file not shown.

0 comments on commit 508602b

Please sign in to comment.