Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ protected function prepareToolsParam(array $functionDeclarations): array
{
$tools = [];
foreach ($functionDeclarations as $functionDeclaration) {
$functionDeclaration = $this->prepareFunctionDeclarationForRequest($functionDeclaration);
$tools[] = [
'type' => 'function',
'function' => $functionDeclaration->toArray(),
Expand All @@ -501,6 +502,23 @@ protected function prepareToolsParam(array $functionDeclarations): array
return $tools;
}

/**
* Prepares a function declaration for the provider request.
*
* Providers can override this method to adapt a function declaration's
* input schema to the compatibility requirements of their API.
*
* @since n.e.x.t
*
* @param FunctionDeclaration $functionDeclaration The function declaration.
* @return FunctionDeclaration The prepared function declaration.
*/
protected function prepareFunctionDeclarationForRequest(
FunctionDeclaration $functionDeclaration
): FunctionDeclaration {
return $functionDeclaration;
}

/**
* Prepares the response format parameter for the API request.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,44 @@ public function testPrepareToolsParam(): void
$this->assertEquals($functionDeclaration2->toArray(), $prepared[1]['function']);
}

/**
* Tests that providers can customize function declarations before serialization.
*
* @return void
*/
public function testPrepareToolsParamAllowsProviderCustomization(): void
{
$functionDeclaration = new FunctionDeclaration(
'original_name',
'Description',
['type' => 'object']
);
$model = new class (
$this->modelMetadata,
$this->providerMetadata,
$this->mockHttpTransporter,
$this->mockRequestAuthentication
) extends MockOpenAiCompatibleTextGenerationModel {
/**
* {@inheritDoc}
*/
protected function prepareFunctionDeclarationForRequest(
FunctionDeclaration $functionDeclaration
): FunctionDeclaration {
return new FunctionDeclaration(
$functionDeclaration->getName() . '_prepared',
$functionDeclaration->getDescription(),
$functionDeclaration->getParameters()
);
}
};

$prepared = $model->exposePrepareToolsParam([$functionDeclaration]);

$this->assertSame('original_name_prepared', $prepared[0]['function']['name']);
$this->assertSame(['type' => 'object'], $prepared[0]['function']['parameters']);
}

/**
* Tests prepareResponseFormatParam() with null schema.
*
Expand Down
Loading