Skip to content

Commit

Permalink
- refactor: transcript youtube to use config in place of parameters (#35
Browse files Browse the repository at this point in the history
)

* - refactor: transcript youtube to use config vars

* - fix: typo in test

* - cicd: bump version
  • Loading branch information
agallardol authored Sep 5, 2024
1 parent d32ce05 commit 27ec609
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 30 deletions.
10 changes: 4 additions & 6 deletions apps/shinkai-tool-youtube-transcript/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ test('exists definition', async () => {
test('transcript video', async () => {
const tool = new Tool({});
const result = await tool.run({
// Video about Shinkai Sheets
url: 'https://youtu.be/RxxuM4wbVQc',
model: 'llama3.1:8b-instruct-q4_1'
});
expect(result.data.transcript).toBeInstanceOf(Array);
expect(result.data.transcript.length).toBeGreaterThan(0);
Expand All @@ -21,14 +19,14 @@ test('transcript video', async () => {


// test('transcript video using openai', async () => {
// const tool = new Tool({});
// const result = await tool.run({
// // Video about Shinkai Sheets
// url: 'https://youtu.be/RxxuM4wbVQc',
// const tool = new Tool({
// apiUrl: 'https://api.openai.com/v1',
// apiKey: '',
// model: 'gpt-4o-mini'
// });
// const result = await tool.run({
// url: 'https://youtu.be/RxxuM4wbVQc',
// });
// expect(result.data.transcript).toBeInstanceOf(Array);
// expect(result.data.transcript.length).toBeGreaterThan(0);
// expect(result.data.message.length).toBeGreaterThan(0);
Expand Down
40 changes: 21 additions & 19 deletions apps/shinkai-tool-youtube-transcript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { ToolDefinition } from 'libs/shinkai-tools-builder/src/tool-definition';
import { TranscriptResponse, YoutubeTranscript } from 'youtube-transcript';
import OpenAI from 'openai';

type Config = {};
type Params = {
url: string;
type Config = {
apiUrl?: string;
apiKey?: string;
model: string;
model?: string;
};
type Params = {
url: string;
};
type Result = { transcript: TranscriptResponse[]; message: string };

Expand All @@ -20,17 +21,8 @@ export class Tool extends BaseTool<Config, Params, Result> {
author: 'Shinkai',
keywords: ['youtube', 'transcript', 'video', 'captions', 'subtitles'],
configurations: {
type: 'object',
properties: {},
required: [],
},
parameters: {
type: 'object',
properties: {
url: {
type: 'string',
description: 'The URL of the YouTube video to transcribe',
},
apiUrl: {
type: 'string',
description: 'The OpenAI api compatible URL',
Expand All @@ -44,6 +36,17 @@ export class Tool extends BaseTool<Config, Params, Result> {
model: {
type: 'string',
description: 'The model to use for generating the summary',
nullable: true,
},
},
required: [],
},
parameters: {
type: 'object',
properties: {
url: {
type: 'string',
description: 'The URL of the YouTube video to transcribe',
},
},
required: ['url'],
Expand Down Expand Up @@ -80,10 +83,10 @@ export class Tool extends BaseTool<Config, Params, Result> {
const message: OpenAI.ChatCompletionUserMessageParam = {
role: 'user',
content: `
According to this transcription of a youtube video (which is in csv separated by ';'):
According to this transcription of a youtube video (which is in csv separated by ':::'):
offset;text
${transcript.map((v) => `${Math.floor(v.offset)};${v.text}`).join('\n')}
${transcript.map((v) => `${Math.floor(v.offset)}:::${v.text}`).join('\n')}
---------------
The video URL is ${params.url}
Expand All @@ -96,16 +99,15 @@ export class Tool extends BaseTool<Config, Params, Result> {
`,
};

let url = params.apiUrl || 'http://127.0.0.1:11435';
let url = this.config?.apiUrl || 'http://127.0.0.1:11435';
url = url?.endsWith('/v1') ? url : `${url}/v1`;
console.log('url', url);
const client = new OpenAI({
baseURL: url,
apiKey: params.apiKey || '',
apiKey: this.config?.apiKey || '',
});
try {
const response = await client.chat.completions.create({
model: params.model,
model: this.config?.model || 'llama3.1:8b-instruct-q4_1',
messages: [message],
stream: false,
});
Expand Down
7 changes: 5 additions & 2 deletions libs/shinkai-tools-runner/src/lib.test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ async fn shinkai_tool_aave_loan_requester() {

#[tokio::test]
async fn shinkai_tool_youtube_transcript() {
let tool_definition = get_tool("shinkai-youtube-transcript").unwrap();
let tool_definition = get_tool("shinkai-tool-youtube-transcript").unwrap();
let tool = Tool::new(
tool_definition.code.clone().unwrap(),
serde_json::Value::Null,
Expand All @@ -401,5 +401,8 @@ async fn shinkai_tool_youtube_transcript() {
)
.await;
assert!(run_result.is_ok());
assert!(!run_result.unwrap().data["transcript"].as_array().unwrap().is_empty());
assert!(!run_result.unwrap().data["transcript"]
.as_array()
.unwrap()
.is_empty());
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shinkai_protocol/source",
"version": "0.7.6",
"version": "0.7.7",
"description": "This repository serves as the ecosystem to execute Shinkai tools, provided by the Shinkai team or third-party developers, in a secure environment. It provides a sandboxed space for executing these tools, ensuring that they run safely and efficiently, while also allowing for seamless integration with Rust code.",
"main": "index.js",
"author": "",
Expand Down

0 comments on commit 27ec609

Please sign in to comment.