From ec9e0dadea01bb59493d19c068a37d67d7467d80 Mon Sep 17 00:00:00 2001 From: Michael Poluektov Date: Wed, 14 Aug 2024 14:33:20 +0100 Subject: [PATCH] remove frontend functions migrated to backend --- src/lib/apis/ollama/index.ts | 50 -------------- src/lib/apis/openai/index.ts | 125 ----------------------------------- 2 files changed, 175 deletions(-) diff --git a/src/lib/apis/ollama/index.ts b/src/lib/apis/ollama/index.ts index 084d2d5f1..c4c449156 100644 --- a/src/lib/apis/ollama/index.ts +++ b/src/lib/apis/ollama/index.ts @@ -1,5 +1,4 @@ import { OLLAMA_API_BASE_URL } from '$lib/constants'; -import { titleGenerationTemplate } from '$lib/utils'; export const getOllamaConfig = async (token: string = '') => { let error = null; @@ -203,55 +202,6 @@ export const getOllamaModels = async (token: string = '') => { }); }; -// TODO: migrate to backend -export const generateTitle = async ( - token: string = '', - template: string, - model: string, - prompt: string -) => { - let error = null; - - template = titleGenerationTemplate(template, prompt); - - console.log(template); - - const res = await fetch(`${OLLAMA_API_BASE_URL}/api/generate`, { - method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - Authorization: `Bearer ${token}` - }, - body: JSON.stringify({ - model: model, - prompt: template, - stream: false, - options: { - // Restrict the number of tokens generated to 50 - num_predict: 50 - } - }) - }) - .then(async (res) => { - if (!res.ok) throw await res.json(); - return res.json(); - }) - .catch((err) => { - console.log(err); - if ('detail' in err) { - error = err.detail; - } - return null; - }); - - if (error) { - throw error; - } - - return res?.response.replace(/["']/g, '') ?? 'New Chat'; -}; - export const generatePrompt = async (token: string = '', model: string, conversation: string) => { let error = null; diff --git a/src/lib/apis/openai/index.ts b/src/lib/apis/openai/index.ts index b075d634b..2bb11d12a 100644 --- a/src/lib/apis/openai/index.ts +++ b/src/lib/apis/openai/index.ts @@ -1,6 +1,4 @@ import { OPENAI_API_BASE_URL } from '$lib/constants'; -import { titleGenerationTemplate } from '$lib/utils'; -import { type Model, models, settings } from '$lib/stores'; export const getOpenAIConfig = async (token: string = '') => { let error = null; @@ -330,126 +328,3 @@ export const synthesizeOpenAISpeech = async ( return res; }; - -export const generateTitle = async ( - token: string = '', - template: string, - model: string, - prompt: string, - chat_id?: string, - url: string = OPENAI_API_BASE_URL -) => { - let error = null; - - template = titleGenerationTemplate(template, prompt); - - console.log(template); - - const res = await fetch(`${url}/chat/completions`, { - method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - Authorization: `Bearer ${token}` - }, - body: JSON.stringify({ - model: model, - messages: [ - { - role: 'user', - content: template - } - ], - stream: false, - // Restricting the max tokens to 50 to avoid long titles - max_tokens: 50, - ...(chat_id && { chat_id: chat_id }), - title: true - }) - }) - .then(async (res) => { - if (!res.ok) throw await res.json(); - return res.json(); - }) - .catch((err) => { - console.log(err); - if ('detail' in err) { - error = err.detail; - } - return null; - }); - - if (error) { - throw error; - } - - return res?.choices[0]?.message?.content.replace(/["']/g, '') ?? 'New Chat'; -}; - -export const generateSearchQuery = async ( - token: string = '', - model: string, - previousMessages: string[], - prompt: string, - url: string = OPENAI_API_BASE_URL -): Promise => { - let error = null; - - // TODO: Allow users to specify the prompt - // Get the current date in the format "January 20, 2024" - const currentDate = new Intl.DateTimeFormat('en-US', { - year: 'numeric', - month: 'long', - day: '2-digit' - }).format(new Date()); - - const res = await fetch(`${url}/chat/completions`, { - method: 'POST', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - Authorization: `Bearer ${token}` - }, - body: JSON.stringify({ - model: model, - // Few shot prompting - messages: [ - { - role: 'assistant', - content: `You are tasked with generating web search queries. Give me an appropriate query to answer my question for google search. Answer with only the query. Today is ${currentDate}.` - }, - { - role: 'user', - content: prompt - } - // { - // role: 'user', - // content: - // (previousMessages.length > 0 - // ? `Previous Questions:\n${previousMessages.join('\n')}\n\n` - // : '') + `Current Question: ${prompt}` - // } - ], - stream: false, - // Restricting the max tokens to 30 to avoid long search queries - max_tokens: 30 - }) - }) - .then(async (res) => { - if (!res.ok) throw await res.json(); - return res.json(); - }) - .catch((err) => { - console.log(err); - if ('detail' in err) { - error = err.detail; - } - return undefined; - }); - - if (error) { - throw error; - } - - return res?.choices[0]?.message?.content.replace(/["']/g, '') ?? undefined; -};