-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
107 lines (93 loc) · 3.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
require('dotenv').config()
const Groq = require('groq-sdk')
const TelegramBot = require('node-telegram-bot-api')
const { YoutubeTranscript } = require('youtube-transcript')
const model = process.env.GROQ_MODEL
const groq = new Groq({ apiKey: process.env.GROQ_API_KEY })
const bot = new TelegramBot(process.env.TELEGRAM_TOKEN, { polling: true })
global.getYoutubeTranscript = async ({ youtubeLink }) => {
try {
const transcript = await YoutubeTranscript.fetchTranscript(youtubeLink)
return transcript.map(item => item.text).join(' ').replace(/'/g, '\'')
} catch (err) {
throw new Error(err.message)
}
}
const main = async () => {
try {
console.log('Listening messages')
bot.on('message', async msg => {
const chatId = msg.chat.id
const userMessage = msg?.text?.toString()
console.log(userMessage)
if (userMessage === '/start') {
await bot.sendMessage(chatId, `Hey, ${msg.from.first_name}.`)
await bot.sendMessage(chatId, 'Just send me a YouTube link and I will provide a summary of the video content.')
return
}
const messages = [
{ role: 'system', content: 'You are a bot designed to summarize YouTube videos. When provided with a YouTube link, you will retrieve the transcript using the "getYoutubeTranscript" function and then summarize it. This function should only be run when you receive a YouTube link. If a YouTube link is not provided, kindly remind the user to provide one.' },
{ role: 'user', content: userMessage }
]
const chatCompletion = await groq.chat.completions.create({
model,
messages,
tools: [{
type: 'function',
function: {
name: 'getYoutubeTranscript',
description: 'Retrieves the transcript of any YouTube video using its link',
parameters: {
type: 'object',
required: ['youtubeLink'],
properties: {
youtubeLink: {
type: 'string',
description: 'The link of a YouTube video (e.g. https://www.youtube.com/watch?v=l1U54VZQNA8)'
}
}
}
}
}]
})
let lastMessage = chatCompletion.choices[0]?.message?.content || ''
const toolCalls = chatCompletion.choices[0]?.message.tool_calls
if (toolCalls) {
for (const toolCall of toolCalls) {
await bot.sendMessage(chatId, 'I am now summarizing the video content.')
await bot.sendChatAction(chatId, 'typing')
let output = ''
const functionName = toolCall.function.name
try {
const args = JSON.parse(toolCall.function.arguments)
output = await global[functionName].apply(null, [args])
} catch (err) {
output = err.message || 'Unexpected error'
bot.sendMessage(chatId, err.message)
}
messages.push({
role: 'tool',
content: output,
name: functionName,
tool_call_id: toolCall.id
})
}
const chatCompletion = await groq.chat.completions.create({ model, messages })
lastMessage = chatCompletion.choices[0]?.message?.content || ''
}
bot.sendMessage(chatId, lastMessage)
})
} catch (err) {
console.log(err.message)
}
}
const platform = 'render'
if (platform === 'render') {
const express = require('express')
const app = express()
const port = process.env.PORT || 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => main())
} else {
main()
}