-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding youtube transcription tool
- Loading branch information
1 parent
ea023f8
commit 66f90e9
Showing
5 changed files
with
313 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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); | ||
} | ||
} |