Skip to content

armgit / my-yt

Public Imported from GitHub
0

A clean and minimal youtube frontend, without all the ads and whistles

JavaScript

extract llm function

Christian Fei995096c0f41f
diff --git a/lib/llm.js b/lib/llm.js
new file mode 100644
index 0000000..82a4e72
--- /dev/null
+++ b/lib/llm.js
@@ -0,0 +1,34 @@
+// export async function fetchAIResponseLMSTUDIO (system, prompt, model = "meta-llama-3-8b-instruct") {
+export async function fetchAIResponseLMSTUDIO (system, prompt, model = "llama-3.2-3b-instruct") {
+ if (prompt.length > 10000) prompt = prompt.substring(0, 10000)
+ try {
+ const response = await fetch('http://0.0.0.0:1234/v1/chat/completions', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json'
+ },
+ body: JSON.stringify({
+ "model": model,
+ "messages": [
+ {"role": "system", "content": system},
+ {"role": "user", "content": prompt}
+ ],
+ "temperature": 0.9,
+ "max_tokens": -1,
+ "stream": false,
+ // "stop": "\n"
+ })
+ })
+ if (!response.ok) {
+ throw new Error(`HTTP error! Status: ${response.status}`)
+ }
+ const data = await response.json()
+ // console.log(JSON.stringify(data, null, 2))
+ let message = data.choices[data.choices.length - 1]?.message?.content || ''
+ message = message.replace('<|eot_id|>', '')
+ return message
+ } catch (error) {
+ console.error('Error summarizing text:', error)
+ return ''
+ }
+}
\ No newline at end of file
diff --git a/lib/subtitles-summary.js b/lib/subtitles-summary.js
index d7e40b2..c2a43d2 100644
--- a/lib/subtitles-summary.js
+++ b/lib/subtitles-summary.js
@@ -1,5 +1,6 @@
import fs from "fs"
import { getVideoSubtitles } from "./youtube.js"
+import { fetchAIResponseLMSTUDIO } from "./llm.js"
export async function summarizeVideo(id, repo, callback = () => {}) {
try {
@@ -11,12 +12,13 @@ export async function summarizeVideo(id, repo, callback = () => {}) {
// if (process.env.KEEP_SUBTITLES === 'true')
fs.writeFileSync(`./data/videos/${id}-sub.en.txt`, cleanedTranscript)
- const prompt = `
+ const userPrompt = `
Extract the key points from the following video transcript:
${cleanedTranscript.replace('\n', ' ')}`
- // console.log('prompt', prompt)
- const summary = await fetchAIResponseLMSTUDIO(prompt)
+ // console.log('userPrompt', userPrompt)
+ const systemPrompt = "You're a helpful assistant that sumarizes a video transcript in max 10 sentences, extracting the key points"
+ const summary = await fetchAIResponseLMSTUDIO(systemPrompt, userPrompt)
// console.log('summary', summary)
repo.setVideoSummary(id, summary)
@@ -36,34 +38,3 @@ export function cleanTranscript (transcript) {
}
-async function fetchAIResponseLMSTUDIO (prompt) {
- if (prompt.length > 10000) prompt = prompt.substring(0, 10000)
- try {
- const response = await fetch('http://0.0.0.0:1234/v1/chat/completions', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify({
- "model": "meta-llama-3-8b-instruct",
- "messages": [
- {"role": "system", "content": "You're a helpful assistant that sumarizes a video transcript in max 10 sentences, extracting the key points"},
- {"role": "user", "content": prompt}
- ],
- "temperature": 0.9,
- // "max_tokens": 2000,
- "stream": false,
- // "stop": "\n"
- })
- })
- if (!response.ok) {
- throw new Error(`HTTP error! Status: ${response.status}`)
- }
- const data = await response.json()
- // console.log(JSON.stringify(data, null, 2))
- return data.choices[data.choices.length - 1]?.message?.content || ''
- } catch (error) {
- console.error('Error summarizing text:', error)
- return ''
- }
-}
\ No newline at end of file