Delete src/ollama_api.ts
This commit is contained in:
@@ -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<OllamaAPIResponse> {
|
||||
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<string> {
|
||||
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";
|
||||
}
|
||||
Reference in New Issue
Block a user