Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Functions Calling Example #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ services:
$baseUrl: '%env(WEBHOOK_BASE_URL)%'
$token: '%env(TELEGRAM_TOKEN)%'

App\SymfonyConBot\ToolBox\SerpApi\SerpApiFunction:
$apiKey: '%env(SERP_API_KEY)%'

Probots\Pinecone\Client:
$apiKey: '%env(PINECONE_API_KEY)%'
$environment: '%env(PINECONE_ENVIRONMENT)%'
Expand Down
36 changes: 36 additions & 0 deletions src/Command/DemoChatCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace App\Command;

use App\SymfonyConBot\Message\Message;
use App\SymfonyConBot\Message\MessageBag;
use App\SymfonyConBot\OpenAI\ChatModel;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand('app:demo:chat', description: 'Command for testing chat')]
final class DemoChatCommand extends Command
{
public function __construct(private readonly ChatModel $model)
{
parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$io->title('Demo Chat');

$prompt = $io->ask('What do you want to know?', 'What is the latest Symfony version?');
$response = $this->model->call(new MessageBag(Message::ofUser($prompt)));

$io->block($response['choices'][0]['message']['content']);

return 0;
}
}
36 changes: 36 additions & 0 deletions src/Command/DemoFunctionsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace App\Command;

use App\SymfonyConBot\Message\Message;
use App\SymfonyConBot\Message\MessageBag;
use App\SymfonyConBot\OpenAI\FunctionChain;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand('app:demo:functions', description: 'Command for testing function calls')]
final class DemoFunctionsCommand extends Command
{
public function __construct(private readonly FunctionChain $chain)
{
parent::__construct();
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$io->title('Demo Functions');

$prompt = $io->ask('What do you want to know?', 'What is the latest Symfony version?');
$response = $this->chain->call(new MessageBag(Message::ofUser($prompt)));

$io->writeln($response);

return 0;
}
}
40 changes: 40 additions & 0 deletions src/SymfonyConBot/OpenAI/FunctionChain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace App\SymfonyConBot\OpenAI;

use App\SymfonyConBot\Message\Message;
use App\SymfonyConBot\Message\MessageBag;
use App\SymfonyConBot\ToolBox\FunctionRegistry;

final readonly class FunctionChain
{
public function __construct(
private ChatModel $model,
private FunctionRegistry $functionRegistry,
) {
}

public function call(MessageBag $messages): string
{
$response = $this->model->call($messages, [
'functions' => $this->functionRegistry->getMap(),
]);

while ('function_call' === $response['choices'][0]['finish_reason']) {
['name' => $name, 'arguments' => $arguments] = $response['choices'][0]['message']['function_call'];
$result = $this->functionRegistry->execute($name, $arguments);

$messages[] = Message::ofAssistant(functionCall: [
'name' => $name,
'arguments' => $arguments,
]);
$messages[] = Message::ofFunctionCall($name, $result);

$response = $this->model->call($messages);
}

return $response['choices'][0]['message']['content'];
}
}
32 changes: 32 additions & 0 deletions src/SymfonyConBot/ToolBox/FunctionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\SymfonyConBot\ToolBox;

use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;

/**
* @phpstan-type FunctionParameterDefinition array{
* type: 'object',
* properties: array<string, array{type: string, description: string}>,
* required: list<string>,
* }
*/
#[AutoconfigureTag('llm_chain.function')]
interface FunctionInterface
{
public static function getName(): string;

public static function getDescription(): string;

/**
* @return FunctionParameterDefinition
*/
public static function getParametersDefinition(): array;

/**
* @param array<string, mixed> $arguments
*/
public function execute(array $arguments): string;
}
55 changes: 55 additions & 0 deletions src/SymfonyConBot/ToolBox/FunctionRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace App\SymfonyConBot\ToolBox;

use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;

/**
* @phpstan-import-type FunctionParameterDefinition from FunctionInterface
*/
final readonly class FunctionRegistry
{
/**
* @var array<string, FunctionInterface>
*/
private array $functions;

/**
* @param iterable<FunctionInterface> $functions
*/
public function __construct(
#[TaggedIterator('llm_chain.function', defaultIndexMethod: 'getName')]
iterable $functions,
) {
$this->functions = $functions instanceof \Traversable ? iterator_to_array($functions) : $functions;
}

/**
* @return list<array{
* name: string,
* description: string,
* parameters: FunctionParameterDefinition,
* }>
*/
public function getMap(): array
{
$functionsMap = [];

foreach ($this->functions as $function) {
$functionsMap[] = [
'name' => $function->getName(),
'description' => $function->getDescription(),
'parameters' => $function->getParametersDefinition(),
];
}

return $functionsMap;
}

public function execute(string $name, string $arguments): string
{
return $this->functions[$name]->execute(json_decode($arguments, true));
}
}
70 changes: 70 additions & 0 deletions src/SymfonyConBot/ToolBox/SerpApi/SerpApiFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace App\SymfonyConBot\ToolBox\SerpApi;

use App\SymfonyConBot\ToolBox\FunctionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @phpstan-import-type FunctionParameterDefinition from FunctionInterface
*/
final readonly class SerpApiFunction implements FunctionInterface
{
public function __construct(
private HttpClientInterface $httpClient,
private string $apiKey,
) {
}

public static function getName(): string
{
return 'serpapi';
}

public static function getDescription(): string
{
return 'search for information on the internet';
}

/**
* @return FunctionParameterDefinition
*/
public static function getParametersDefinition(): array
{
return [
'type' => 'object',
'properties' => [
'query' => [
'type' => 'string',
'description' => 'The search query to use',
],
],
'required' => ['query'],
];
}

/**
* @param array{query: string} $arguments
*/
public function execute(array $arguments): string
{
$response = $this->httpClient->request('GET', 'https://serpapi.com/search', [
'query' => [
'q' => $arguments['query'],
'api_key' => $this->apiKey,
],
]);

return sprintf('Results for "%s" are "%s".', $arguments['query'], $this->extractBestResponse($response->toArray()));
}

/**
* @param array<string, mixed> $results
*/
private function extractBestResponse(array $results): string
{
return implode(PHP_EOL, array_map(fn ($story) => sprintf('%s: %s', $story['title'], $story['snippet']), $results['organic_results']));
}
}