From 81acdb34be7352000e768ff41e7340285ff8a42c Mon Sep 17 00:00:00 2001 From: fzzinchemical Date: Tue, 25 Mar 2025 20:26:35 +0000 Subject: [PATCH] Delete src/ollama_api.ts --- src/ollama_api.ts | 101 ---------------------------------------------- 1 file changed, 101 deletions(-) delete mode 100755 src/ollama_api.ts diff --git a/src/ollama_api.ts b/src/ollama_api.ts deleted file mode 100755 index 33d2b2b..0000000 --- a/src/ollama_api.ts +++ /dev/null @@ -1,101 +0,0 @@ -import * as dotenv from "jsr:@std/dotenv"; - -const env = dotenv.loadSync(); -const ollamalink: string = env.OLLAMA_API_LINK! + "/api/chat"; -const ollamaMemoryLimit: number = parseInt(env.OLLAMA_MEMORY_LIMIT!); - -// ATTENTION MEMORY LIMIT IS 10! -let memoryActive = false; -const memory: Message[] = []; - -type Message = { - role: string; - content: string; -}; - -type OllamaAPIRequest = { - model: string; - messages: Message[]; - stream: boolean; -}; - -type OllamaAPIResponse = { - model: string; - created_at: string; - message: Message; - done: boolean; - context: number[]; - total_duration: number; - load_duration: number; - prompt_eval_count: number; - prompt_eval_duration: number; - eval_count: number; - eval_duration: number; -}; - -async function makeRequest( - model: string, - prompt: string, -): Promise { - const requestBody: OllamaAPIRequest = { - model: model, - messages: [ - ...memoryActive ? memory : [], - { - role: "user", - content: prompt, - }, - ], - stream: false, - }; - - console.log("Request Body:", JSON.stringify(requestBody)); - - try { - const response = await fetch(ollamalink, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(requestBody), - }); - - if (!response.ok) { - throw new Error(`HTTP error! status: ${response.status}`); - } - - const data: OllamaAPIResponse = await response.json(); - console.log("API Response:", data); - return data; - } catch (error) { - console.error("Error making request:", error); - throw error; - } -} - -export function setMemory(tmp: boolean) { - memoryActive = tmp; -} - -function memoryManager(message: Message) { - if (memory.length >= ollamaMemoryLimit) { - memory.shift(); - } - memory.push(message); -} - -export async function communicate( - model: string, - prompt: string, -): Promise { - const response = await makeRequest(model, prompt); - memoryManager( - { - role: "user", - content: prompt, - }, - ); - memoryManager(response.message); - console.log("Response:", response); - return response.message.content ?? "ERROR: No response"; -}