A clean and minimal youtube frontend, without all the ads and whistles
JavaScript
refactor: anthropic llm provider extends generic
Christian Fei2237c065f56f
diff --git a/lib/llm/providers/anthropic.js b/lib/llm/providers/anthropic.jsindex b1b4bf3..5740da8 100644--- a/lib/llm/providers/anthropic.js+++ b/lib/llm/providers/anthropic.js@@ -1,8 +1,10 @@+import { Generic } from "./generic.js"+const ANTHROPIC_VERSION = '2023-06-01'const MAX_PROMPT_LEN = 10000const MAX_TOKENS = 1024-export class Anthropic {+export class Anthropic extends Generic {constructor(system, {model,host,@@ -10,58 +12,40 @@ export class Anthropic {apiKey,temperature,max_tokens = MAX_TOKENS,- }) {+ } = args) {+ super(system, args)this.system = system- this.headers = {+ this.model = model+ this.host = host+ this.endpoint = endpoint+ this.apiKey = apiKey+ this.temperature = temperature+ this.max_tokens = max_tokens+ }++ getHeaders () {+ return {'Content-Type': 'application/json','anthropic-version': ANTHROPIC_VERSION,- 'x-api-key': apiKey- }-- this.settings = {- model,- host,- endpoint,- apiKey,- temperature,- max_tokens+ 'x-api-key': this.apiKey}}- async summarize(prompt) {- if (prompt.length > MAX_PROMPT_LEN) prompt = prompt.substring(0, MAX_PROMPT_LEN)-- const body = JSON.stringify({- model: this.settings.model,- temperature: Number(this.settings.temperature),+ getBody (prompt) {+ return JSON.stringify({+ model: this.model,+ temperature: Number(this.temperature),system: this.system,- max_tokens: this.settings.max_tokens,+ max_tokens: this.max_tokens,messages: [{ role: "user", content: prompt }]})+ }- try {- const response = await fetch(this.settings.host + this.settings.endpoint, {- method: 'POST',- headers: this.headers,- body- })-- const data = await response.json()- if (!response.ok) {- console.error(data)- throw new Error(`HTTP error! Status: ${response.status}`)- }-- const messages = data?.content || []- let message = messages[messages.length - 1].text || ''-- message = message.replace('<|eot_id|>', '')- return message- } catch (error) {- console.error('Error summarizing text:', error)- throw error- }+ getMessage (data) {+ const messages = data?.content || []+ const message = messages[messages.length - 1].text || ''+ return message.replace('<|eot_id|>', '')}}diff --git a/lib/llm/providers/generic.js b/lib/llm/providers/generic.jsindex bf25c88..0a7eb2f 100644--- a/lib/llm/providers/generic.js+++ b/lib/llm/providers/generic.js@@ -19,7 +19,6 @@ export class Generic {}getHeaders () {- console.log('this headers called - generic')return Object.assign({'Content-Type': 'application/json',}, this.apiKey ? { 'Authorization': `Bearer ${this.apiKey}` } : {})@@ -36,6 +35,12 @@ export class Generic {})}+ getMessage (data) {+ const messages = data?.choices || []+ const message = messages[messages.length - 1]?.message?.content || ''+ return message.replace('<|eot_id|>', '')+ }+async summarize(prompt) {if (prompt.length > MAX_PROMPT_LEN) prompt = prompt.substring(0, MAX_PROMPT_LEN)@@ -52,9 +57,7 @@ export class Generic {throw new Error(`HTTP error! Status: ${response.status}`)}- let message = data.choices[data.choices.length - 1]?.message?.content || ''- message = message.replace('<|eot_id|>', '')- return message+ return this.getMessage(data)} catch (error) {console.error('Error summarizing text:', error)throw error