Skip to content

Commit

Permalink
add function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Apr 30, 2024
1 parent 88fc503 commit 67940bd
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 18 deletions.
6 changes: 2 additions & 4 deletions Modules/LlmDriver/app/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ function ($item) {
}
)->implode('\n');


$systemPrompt = <<<EOD
You are a helpful assistant in a RAG system with tools and functions to help perform tasks.
When you find the right function make sure to return just the JSON that represents the requirements of that function.
Expand All @@ -200,14 +199,13 @@ functions to choose from will start with ### START FUNCTION and end with ### END
{$functionsEncoded}
EOD;


$messages = $this->messagesToArray($messages);

if(!collect($messages)->first(
if (! collect($messages)->first(
function ($message) {
return $message['role'] === 'system';
}
)){
)) {
$messages = Arr::prepend($messages, [
'content' => $systemPrompt,
'role' => 'system',
Expand Down
14 changes: 6 additions & 8 deletions Modules/LlmDriver/app/GroqClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function chat(array $messages): CompletionResponse
]);

if (! $results->ok()) {
$error = $this->getError($results);;
$error = $this->getError($results);
Log::error('Groq API Error '.$error);
throw new \Exception('Groq API Error '.$error);
}
Expand Down Expand Up @@ -124,9 +124,9 @@ public function functionPromptChat(array $messages, array $only = []): array
$maxTokens = $this->getConfig('groq')['max_tokens'];

$messages = $this->insertFunctionsIntoMessageArray($messages);

//put_fixture("groq_functions_prompt.json",$messages);

$results = $this->getClient()->post('/chat/completions', [
'model' => $model,
'max_tokens' => $maxTokens,
Expand All @@ -143,13 +143,13 @@ public function functionPromptChat(array $messages, array $only = []): array

//put_fixture("groq_functions_response.json", $results->json());

foreach($results->json()['choices'] as $content) {
foreach ($results->json()['choices'] as $content) {
$functionArray = data_get($content, 'message.content', []);
$functionArray = json_decode($functionArray, true);
foreach($functionArray as $possibleFunction) {
foreach ($functionArray as $possibleFunction) {
$functions[] = $possibleFunction;
}

}

/**
Expand Down Expand Up @@ -200,6 +200,4 @@ public function getFunctions(): array
];
})->toArray();
}


}
6 changes: 0 additions & 6 deletions Modules/LlmDriver/tests/Feature/GroqClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@

namespace Tests\Feature;

use Feature;
use Illuminate\Support\Facades\Http;
use LlmLaraHub\LlmDriver\ClaudeClient;
use LlmLaraHub\LlmDriver\GroqClient;
use LlmLaraHub\LlmDriver\Requests\MessageInDto;
use LlmLaraHub\LlmDriver\Responses\CompletionResponse;
use LlmLaraHub\LlmDriver\Responses\EmbeddingsResponseDto;
use Tests\TestCase;

class GroqClientTest extends TestCase
{


public function test_completion(): void
{
$client = new GroqClient();
Expand All @@ -31,7 +26,6 @@ public function test_completion(): void

}


public function test_chat(): void
{
$client = new GroqClient();
Expand Down

0 comments on commit 67940bd

Please sign in to comment.