Skip to content

armgit / my-yt

Public Imported from GitHub
0

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

JavaScript

feat: support env vars for llm settings

Christian Fei49d3fea21b83
diff --git a/README.md b/README.md
index 0f13758..a7de8ee 100644
--- a/README.md
+++ b/README.md
@@ -51,6 +51,18 @@ docker run -p 3000:3000 -v /path/to/your/data/folder/for/persistence:/app/data c
docker run -d -p 3000:3000 -v $HOME/my-yt-data:/app/data christianfei/my-yt:latest
```
+## Environment variables for LLM integration
+
+The default values are
+```
+AI_MODEL meta-llama-3.1-8b-instruct
+AI_HOST http://127.0.0.1:1234
+AI_ENDPOINT /v1/chat/completions
+AI_APIKEY
+```
+
+Simply set the env variables to your needs, by following the format above (e.g. url starting with "http", no ending slash, AI_ENDPOINT with leading slash and path)
+
## Why?
diff --git a/lib/llm.js b/lib/llm.js
index 853d91d..d056948 100644
--- a/lib/llm.js
+++ b/lib/llm.js
@@ -1,16 +1,25 @@
export async function fetchCompletionLMSTUDIO (
system,
- prompt,
- model = 'meta-llama-3.1-8b-instruct',
- endpoint = 'http://127.0.0.1:1234/v1/chat/completions'
+ prompt,
+ {
+ model,
+ host,
+ endpoint,
+ apiKey
+ } = {
+ model: 'meta-llama-3.1-8b-instruct',
+ host: 'http://127.0.0.1:1234',
+ endpoint: '/v1/chat/completions',
+ apiKey: ''
+ }
) {
if (prompt.length > 10000) prompt = prompt.substring(0, 10000)
try {
- const response = await fetch(endpoint, {
+ const response = await fetch(host + endpoint, {
method: 'POST',
- headers: {
+ headers: Object.assign({
'Content-Type': 'application/json'
- },
+ }, apiKey ? {'Authorization': `Bearer ${apiKey}`} : {}),
body: JSON.stringify({
model,
messages: [
diff --git a/lib/server.js b/lib/server.js
index 0b5ec2d..280ce9b 100644
--- a/lib/server.js
+++ b/lib/server.js
@@ -21,6 +21,13 @@ export function createServer ({repo, connections = []}) {
})
}
+ const llmSettings = {
+ model: process.env.AI_MODEL || 'meta-llama-3.1-8b-instruct',
+ host: process.env.AI_HOST ||'http://127.0.0.1:1234',
+ endpoint: process.env.AI_ENDPOINT ||'/v1/chat/completions',
+ apiKey: process.env.AI_APIKEY || ''
+ }
+
return http.createServer(async (req, res) => {
const url = new URL(req.url, `http://${req.headers.host}`)
@@ -33,7 +40,7 @@ export function createServer ({repo, connections = []}) {
if (url.pathname === '/download-video' && req.method === 'POST')
return downloadVideoHandler(req, res, repo, connections)
if (url.pathname === '/summarize-video' && req.method === 'POST')
- return summarizeVideoHandler(req, res, repo, connections)
+ return summarizeVideoHandler(req, res, repo, connections, llmSettings)
if (url.pathname === '/ignore-video' && req.method === 'POST')
return ignoreVideoHandler(req, res, repo, connections)
if (url.pathname === '/videos' && req.method === 'GET')
@@ -42,7 +49,7 @@ export function createServer ({repo, connections = []}) {
return watchVideoHandler(req, res)
if (url.pathname.match('\/captions\/.*') && req.method === 'GET')
return captionsHandler(req, res)
-
+
/* client */
if (url.pathname === '/main.css')
return fileHandler('client/main.css', 'text/css')(req, res)
@@ -121,11 +128,11 @@ export function createServer ({repo, connections = []}) {
res.end('Download started')
}
- async function summarizeVideoHandler(req, res, repo, connections = []) {
+ async function summarizeVideoHandler(req, res, repo, connections = [], llmSettings = {}) {
const body = await getBody(req)
const { id } = JSON.parse(body)
- summarizeVideo(id, repo, (line) =>
+ summarizeVideo(id, repo, llmSettings, (line) =>
broadcastSSE(JSON.stringify({type: 'download-log-line', line}), connections))
.then(({summary, transcript}) =>
broadcastSSE(JSON.stringify({type: 'summary', summary, transcript, videoId: id}), connections))
diff --git a/lib/subtitles-summary.js b/lib/subtitles-summary.js
index b0f9c9e..f94c195 100644
--- a/lib/subtitles-summary.js
+++ b/lib/subtitles-summary.js
@@ -2,7 +2,7 @@ import fs from "fs"
import { getVideoSubtitles } from "./youtube.js"
import { fetchCompletionLMSTUDIO } from "./llm.js"
-export async function summarizeVideo(id, repo, callback = () => {}) {
+export async function summarizeVideo(id, repo, llmSettings = {}, callback = () => {}) {
return new Promise(async (resolve, reject) => {
try {
const transcript = await getVideoSubtitles(id, callback)
@@ -13,11 +13,12 @@ export async function summarizeVideo(id, repo, callback = () => {}) {
console.log('cleanedTranscript.length', cleanedTranscript.length)
const userPrompt = `
- Extract the key points from the following video transcript:
-
- ${cleanedTranscript}`
+Extract the key points from the following video transcript:
+
+${cleanedTranscript}`.trim()
+
const systemPrompt = "You're a helpful assistant that sumarizes a video transcript in max 10 sentences, extracting the key points"
- const summary = await fetchCompletionLMSTUDIO(systemPrompt, userPrompt)
+ const summary = await fetchCompletionLMSTUDIO(systemPrompt, userPrompt, llmSettings)
repo.setVideoSummary(id, summary)
console.log('Summary completed')