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

feat: add tool call event #187

Merged
merged 1 commit into from
Jan 5, 2025
Merged

feat: add tool call event #187

merged 1 commit into from
Jan 5, 2025

Conversation

chr-hertel
Copy link
Member

Closes #168

@chr-hertel
Copy link
Member Author

chr-hertel commented Jan 4, 2025

Tests currently failing since i moved tool result conversion into the chain processor, but would that be a way to go for you @OskarStark ?

final class SpecificToolCallSubscriber implement EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            ToolCallExecuted::class => 'handleToolCall',
        ];
    }

    public function handleToolCall(ToolCallExecuted $event): void
    {
        foreach ($event->toolCallResults as $toolCallResult) {
            if ('xyz' === $toolCallResult->toolCall->name) {
                $event->response = new StructuredResponse($toolCallResult->result);
            }
        }
    }
}

See example:

$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']);
$llm = new GPT(GPT::GPT_4O_MINI);
$openMeteo = new OpenMeteo(HttpClient::create());
$toolBox = new ToolBox(new ToolAnalyzer(), [$openMeteo]);
$eventDispatcher = new EventDispatcher();
$processor = new ChainProcessor($toolBox, $eventDispatcher);
$chain = new Chain($platform, $llm, [$processor], [$processor]);
// Add tool call result listener to enforce chain exits direct with structured response for weather tools
$eventDispatcher->addListener(ToolCallExecuted::class, function (ToolCallExecuted $event): void {
foreach ($event->toolCallResults as $toolCallResult) {
if (str_starts_with($toolCallResult->toolCall->name, 'weather_')) {
$event->response = new StructuredResponse($toolCallResult->result);
}
}
});
$messages = new MessageBag(Message::ofUser('How is the weather currently in Berlin?'));
$response = $chain->call($messages);
dump($response->getContent());

@OskarStark
Copy link
Contributor

Uhhhhhhh that looks nice 😍 will have a detailed look when on my Mac 💻👍🏻

Thanks for taking time

@chr-hertel chr-hertel force-pushed the feat-toolcall-event branch from f096dba to 9a5330e Compare January 4, 2025 17:13
Comment on lines 36 to 37
$eventDispatcher->addListener(ToolCallExecuted::class, function (ToolCallExecuted $event): void {
foreach ($event->toolCallResults as $toolCallResult) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The event name is singular and then you iterate over all toolCallResults (plural), that feels weird. Shouldn't we throw an event for every ToolCall which was executed?

Otherwise rename it to ToolCallsExecuted?

Or am I missing sth.?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right. should be plural

@OskarStark
Copy link
Contributor

I like it 👍 Looks like I can achieve my goals with it 🕺

@OskarStark OskarStark added the enhancement New feature or request label Jan 4, 2025
@chr-hertel chr-hertel force-pushed the feat-toolcall-event branch from 9a5330e to 4413820 Compare January 5, 2025 13:37
@chr-hertel chr-hertel force-pushed the feat-toolcall-event branch from 4413820 to 30e1ff7 Compare January 5, 2025 13:40
@chr-hertel chr-hertel merged commit fc8db39 into main Jan 5, 2025
7 checks passed
@chr-hertel chr-hertel deleted the feat-toolcall-event branch January 5, 2025 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Let the tool decide if the chain should be called again with the return value
2 participants