-
Notifications
You must be signed in to change notification settings - Fork 24
feat: adding youtube transcription tool #8
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,12 +29,16 @@ | |
"probots-io/pinecone-php": "^1.0", | ||
"symfony/clock": "^6.4 || ^7.1", | ||
"symfony/console": "^6.4 || ^7.1", | ||
"symfony/css-selector": "^6.4 || ^7.1", | ||
"symfony/dom-crawler": "^6.4 || ^7.1", | ||
"symfony/var-dumper": "^6.4 || ^7.1" | ||
}, | ||
"suggest": { | ||
"codewithkyrian/chromadb-php": "For using the ChromaDB as retrieval vector store.", | ||
"probots-io/pinecone-php": "For using the Pinecone as retrieval vector store.", | ||
"symfony/clock": "For using the clock tool." | ||
"symfony/clock": "For using the clock tool.", | ||
"symfony/css-selector": "For using the YouTube transcription tool.", | ||
"symfony/dom-crawler": "For using the YouTube transcription tool." | ||
Comment on lines
37
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Symfony we avoid/banned using suggestions, instead we decide on runtime, if we need something and I like it more |
||
}, | ||
"config": { | ||
"sort-packages": true | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
use PhpLlm\LlmChain\Chain; | ||
use PhpLlm\LlmChain\Message\Message; | ||
use PhpLlm\LlmChain\Message\MessageBag; | ||
use PhpLlm\LlmChain\OpenAI\Model\Gpt; | ||
use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version; | ||
use PhpLlm\LlmChain\OpenAI\Runtime\OpenAI; | ||
use PhpLlm\LlmChain\ToolBox\ParameterAnalyzer; | ||
use PhpLlm\LlmChain\ToolBox\Registry; | ||
use PhpLlm\LlmChain\ToolBox\Tool\YouTubeTranscriber; | ||
use PhpLlm\LlmChain\ToolBox\ToolAnalyzer; | ||
use Symfony\Component\HttpClient\HttpClient; | ||
|
||
require_once dirname(__DIR__).'/vendor/autoload.php'; | ||
|
||
$httpClient = HttpClient::create(); | ||
$runtime = new OpenAI($httpClient, getenv('OPENAI_API_KEY')); | ||
$llm = new Gpt($runtime, Version::GPT_4o_MINI); | ||
|
||
$transcriber = new YouTubeTranscriber($httpClient); | ||
$registry = new Registry(new ToolAnalyzer(new ParameterAnalyzer()), [$transcriber]); | ||
$chain = new Chain($llm, $registry); | ||
|
||
$messages = new MessageBag(Message::ofUser('Please summarize this video for me: https://www.youtube.com/watch?v=6uXW-ulpj0s')); | ||
$response = $chain->call($messages); | ||
|
||
echo $response.PHP_EOL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\ToolBox\Tool; | ||
|
||
use PhpLlm\LlmChain\ToolBox\AsTool; | ||
use Symfony\Component\CssSelector\CssSelectorConverter; | ||
use Symfony\Component\DomCrawler\Crawler; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
#[AsTool('youtube_transcript', 'Fetches the transcript of a YouTube video')] | ||
final readonly class YouTubeTranscriber | ||
{ | ||
public function __construct( | ||
private HttpClientInterface $client, | ||
) { | ||
if (!class_exists(Crawler::class)) { | ||
throw new \LogicException('The Symfony DomCrawler component is required to use this tool.'); | ||
} | ||
if (!class_exists(CssSelectorConverter::class)) { | ||
throw new \LogicException('The Symfony CSS Selector component is required to use this tool.'); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $videoId The ID of the YouTube video | ||
*/ | ||
public function __invoke(string $videoId): string | ||
{ | ||
// Fetch the HTML content of the YouTube video page | ||
$htmlResponse = $this->client->request('GET', 'https://youtube.com/watch?v='.$videoId); | ||
$html = $htmlResponse->getContent(); | ||
|
||
// Use DomCrawler to parse the HTML | ||
$crawler = new Crawler($html); | ||
|
||
// Extract the script containing the ytInitialPlayerResponse | ||
$scriptContent = $crawler->filter('script')->reduce(function (Crawler $node) { | ||
return str_contains($node->text(), 'var ytInitialPlayerResponse = {'); | ||
})->text(); | ||
|
||
// Extract and parse the JSON data from the script | ||
$start = strpos($scriptContent, 'var ytInitialPlayerResponse = ') + strlen('var ytInitialPlayerResponse = '); | ||
$dataString = substr($scriptContent, $start); | ||
$dataString = substr($dataString, 0, strrpos($dataString, ';') ?: null); | ||
$data = json_decode(trim($dataString), true); | ||
|
||
// Extract the URL for the captions | ||
if (!isset($data['captions']['playerCaptionsTracklistRenderer']['captionTracks'][0]['baseUrl'])) { | ||
throw new \Exception('Captions are not available for this video.'); | ||
} | ||
$captionsUrl = $data['captions']['playerCaptionsTracklistRenderer']['captionTracks'][0]['baseUrl']; | ||
|
||
// Fetch and parse the captions XML | ||
$xmlResponse = $this->client->request('GET', $captionsUrl); | ||
$xmlContent = $xmlResponse->getContent(); | ||
$xmlCrawler = new Crawler($xmlContent); | ||
|
||
// Collect all text elements from the captions | ||
$transcript = $xmlCrawler->filter('text')->each(function (Crawler $node) { | ||
return $node->text().' '; | ||
}); | ||
|
||
return implode(PHP_EOL, $transcript); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we talk consistently about
toolbox
instead oftoolchain
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, good point, already legacy after that universal chain refactoring