Skip to content

Commit

Permalink
Claude working with this JSON schema using a tool idea
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Jul 14, 2024
1 parent 41e9ee5 commit a74a216
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
21 changes: 10 additions & 11 deletions Modules/LlmDriver/app/ClaudeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Laravel\Pennant\Feature;
use LlmLaraHub\LlmDriver\Functions\FunctionDto;
use LlmLaraHub\LlmDriver\Requests\MessageInDto;
use LlmLaraHub\LlmDriver\Responses\CompletionResponse;
Expand Down Expand Up @@ -162,13 +163,21 @@ public function modifyPayload(array $payload): array

if (! empty($this->forceTool)) {
$function = [$this->forceTool];

$function = $this->remapFunctions($function);

$payload['tools'] = $function;

$payload['tool_choice'] = [
'type' => 'tool',
'name' => $this->forceTool->name,
];
} else {
if (Feature::active('all_tools')) {
$payload['tools'] = $this->getFunctions();
} else {
$payload['tools'] = [];
}
}

$payload = $this->addJsonFormat($payload);
Expand Down Expand Up @@ -208,12 +217,8 @@ public function completionPool(array $prompts, int $temperature = 0): array
],
];

put_fixture('claude_pre_remap_results_completion_pool.json', $payload);

$payload = $this->modifyPayload($payload);

put_fixture('claude_post_remap_results_completion_pool.json', $payload);

$pool->retry(3, 6000)->withHeaders([
'x-api-key' => $api_token,
'anthropic-beta' => 'tools-2024-04-04',
Expand Down Expand Up @@ -376,10 +381,7 @@ public function remapFunctions(array $functions): array
];
}

$itemsOrProperties = [
'type' => 'object',
'properties' => $properties,
];
$itemsOrProperties = $properties;

if ($type === 'array') {
$itemsOrProperties = [
Expand Down Expand Up @@ -415,7 +417,6 @@ public function remapFunctions(array $functions): array
*/
protected function remapMessages(array $messages, bool $userLast = false): array
{
put_fixture('before_mapping.json', $messages);
$messages = collect($messages)->map(function ($item) {
if ($item->role === 'system') {
$item->role = 'assistant';
Expand Down Expand Up @@ -467,8 +468,6 @@ protected function remapMessages(array $messages, bool $userLast = false): array
}
}

put_fixture('after_mapping.json', $newMessagesArray);

return $newMessagesArray;
}

Expand Down
6 changes: 3 additions & 3 deletions Modules/LlmDriver/tests/Feature/ClaudeClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,15 @@ public function test_get_functions(): void
});
$openaiClient = new \LlmLaraHub\LlmDriver\ClaudeClient();
$response = $openaiClient->getFunctions();

$this->assertNotEmpty($response);
$this->assertIsArray($response);
$first = $response[0];
$this->assertArrayHasKey('name', $first);
$this->assertArrayHasKey('input_schema', $first);

$this->assertNotEmpty(data_get($first, 'input_schema.properties.prompt'));

$this->assertNotEmpty($response);
}

Expand Down Expand Up @@ -244,9 +247,6 @@ public function test_remap_array(): void

$results = (new ClaudeClient)->remapFunctions([$dto]);

//put_fixture('claude_remap_results.json', $results);

//dd($results);

$this->assertEquals(
$shouldBe,
Expand Down

0 comments on commit a74a216

Please sign in to comment.