Skip to content

Commit

Permalink
I am slowly going insane, inv where is the singularity bomb I cant ta…
Browse files Browse the repository at this point in the history
…ke this anymore
  • Loading branch information
Erisfiregamer1 committed Mar 4, 2024
1 parent f62c099 commit 2b20412
Show file tree
Hide file tree
Showing 10 changed files with 401 additions and 88 deletions.
23 changes: 9 additions & 14 deletions bots/chatgpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ if (!Deno.env.get("OPENAI_API_KEY")) {
console.warn("No OpenAI API key provided! ChatGPT will be unavailable.");
isEnabled = false;
}

type response = {
oaires: types.Response;
messages: types.Message[];
};

// const db = await Deno.openKv("./db.sqlite")

const tools: types.Tool[] = [{
Expand Down Expand Up @@ -101,15 +95,15 @@ async function doTools(

const newres = await send(messages, null, "tool_res", callback);

return newres;
return newres.resp;
}

export async function send(
messages: types.Message[],
prompt: string | null,
userid: string,
callback: (type: string, data: types.Response) => void,
): Promise<types.Response> {
): Promise<types.llmFileResponseGPT> {
// here we go

if (!isEnabled) {
Expand Down Expand Up @@ -145,24 +139,25 @@ export async function send(
}),
});

const resp: types.Response | types.Error = await res.json();
let resp: types.Response | types.Error = await res.json();

if (types.isError(resp)) {
// Fuck.
throw resp.error.message; // well at least they know why the fuck it crashed??
}

let finalresp = resp;

messages.push(resp.choices[0].message);

if (resp.choices[0].finish_reason === "tool_calls") {
callback("function", resp);

finalresp = await doTools(resp, messages, callback);
resp = await doTools(resp, messages, callback);
} else {
callback("complete", finalresp);
callback("complete", resp);
}

return finalresp;
return {
resp,
messages,
};
}
108 changes: 108 additions & 0 deletions bots/claude_3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import * as types from "./types.ts";

export const requested_values = {
env: ["ANTHROPIC_API_KEY"],
images: true,
};

// const db = await Deno.openKv("./db.sqlite")

function arrayBufferToBase64(buffer: ArrayBuffer) {
let binary = '';
const bytes = new Uint8Array(buffer);
const len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary); // Base64 encode the binary string
}

export async function send(
messages: types.Message[],
prompt: string | null,
callback: (type: string, data: types.llmFileResponseClaude) => void,
values: types.Values,
): Promise<types.llmFileResponseClaude> {
// here we go

const sysprompt =
"You are Claude 3 Opus, an LLM by Anthropic. You are running through a Discord bot named LLM Bot, by Eris.";

const contentarr: types.AnthropicContentPart[] = [];

if (values.images) {
// Use map to create an array of promises
const imagePromises = values.images.map(async (image_url) => {
const img = await fetch(image_url);

const imageType = img.headers.get("Content-Type");

if (!imageType || !imageType.startsWith("image/")) {
throw new Error("Whoever's managing the images for this did an absolutely terrible job.");
} else {
const imageArrayBuffer = await img.arrayBuffer();
console.log(imageArrayBuffer);
const imageData = arrayBufferToBase64(imageArrayBuffer);
contentarr.push({
type: "image",
source: {
type: "base64",
media_type: imageType,
data: imageData,
},
});
console.log(imageType);
}
});

// Wait for all the image processing to complete
await Promise.all(imagePromises);
}

// After all images are processed, push the text content
contentarr.push({
type: "text",
text: prompt!
});


let msg = {
role: "user",
content: contentarr,
};

messages.push(msg);

const res = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": `${values.env.ANTHROPIC_API_KEY}`,
"anthropic-version": "2023-06-01",
},
body: JSON.stringify({
model: "claude-3-opus-20240229",
max_tokens: 4096,
messages: messages,
system: sysprompt,
}),
});

const resp: types.claudeResponse | types.Error = await res.json();

if (types.isError(resp)) {
// Fuck.
throw resp.error.message; // well at least they know why the fuck it crashed??
}

messages.push({ role: "assistant", content: resp.content[0].text });

const fresp = {
resp,
messages,
};

callback("complete", fresp);

return fresp;
}
15 changes: 3 additions & 12 deletions bots/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@ export let isEnabled = true;

import * as types from "./types.ts";

import * as vdb from "../vdb.ts";

if (!Deno.env.get("OPENAI_API_KEY")) {
console.warn("No OpenAI API key provided! ChatGPT will be unavailable.");
isEnabled = false;
}

type response = {
res: types.geminiResponse;
messages: types.Message[];
};

// const db = await Deno.openKv("./db.sqlite")

async function processGeminiMessages(
Expand Down Expand Up @@ -79,7 +72,7 @@ export async function send(
messages: types.Message[],
prompt: string | null,
images: string[],
): Promise<response> {
): Promise<types.llmFileResponseGemini> {
// here we go

if (!isEnabled) {
Expand Down Expand Up @@ -187,10 +180,8 @@ export async function send(
content: resp.candidates[0].content.parts[0].text,
});

let finalResp = {
res: resp,
return {
resp,
messages,
};

return finalResp;
}
35 changes: 14 additions & 21 deletions bots/gpt_4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ if (!Deno.env.get("OPENAI_API_KEY")) {
isEnabled = false;
}

type response = {
oaires: types.Response;
messages: types.Message[];
};

// const db = await Deno.openKv("./db.sqlite")

const tools: types.Tool[] = [{
Expand All @@ -36,14 +31,14 @@ const tools: types.Tool[] = [{
}];

async function doTools(
oaires: types.Response,
res: types.Response,
messages: types.Message[],
): Promise<response> {
if (oaires.choices[0].finish_reason !== "tool_calls") {
): Promise<types.Response> {
if (res.choices[0].finish_reason !== "tool_calls") {
throw "What The Shit?";
}

const toolCalls = oaires.choices[0].message.tool_calls!;
const toolCalls = res.choices[0].message.tool_calls!;

const promises = toolCalls.map(async (tool) => {
if (tool.function.name === "use-database") {
Expand Down Expand Up @@ -74,16 +69,16 @@ async function doTools(

const newres = await send(messages, null, "tool_res");

console.log(newres);
console.log(newres.resp);

return newres;
return newres.resp;
}

export async function send(
messages: types.Message[],
prompt: string | null,
userid: string,
): Promise<response> {
): Promise<types.llmFileResponseGPT> {
// here we go

if (!isEnabled) {
Expand Down Expand Up @@ -113,30 +108,28 @@ export async function send(
Authorization: `Bearer ${Deno.env.get("OPENAI_API_KEY")}`,
},
body: JSON.stringify({
model: "gpt-4-1106-preview",
model: "gpt-4-turbo-preview",
messages: messages,
user: userid,
tools,
}),
});

const resp: types.Response | types.Error = await res.json();
let resp: types.Response | types.Error = await res.json();

if (types.isError(resp)) {
// Fuck.
throw resp.error.message; // well at least they know why the fuck it crashed??
}

let finalresp: response = {
oaires: resp,
messages,
};

messages.push(resp.choices[0].message);

if (resp.choices[0].finish_reason === "tool_calls") {
finalresp = await doTools(resp, messages);
resp = await doTools(resp, messages);
}

return finalresp;
return {
resp,
messages,
};
}
11 changes: 3 additions & 8 deletions bots/gpt_4_vision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,14 @@ if (!Deno.env.get("OPENAI_API_KEY")) {
isEnabled = false;
}

type response = {
oaires: types.Response;
messages: types.Message[];
};

// const db = await Deno.openKv("./db.sqlite")

export async function send(
messages: types.Message[],
prompt: string | null,
userid: string,
images: string[],
): Promise<response> {
): Promise<types.llmFileResponse> {
// here we go

if (!isEnabled) {
Expand Down Expand Up @@ -82,8 +77,8 @@ export async function send(
throw resp.error.message; // well at least they know why the fuck it crashed??
}

let finalresp: response = {
oaires: resp,
const finalresp = {
resp,
messages,
};

Expand Down
19 changes: 6 additions & 13 deletions bots/mixtral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ export const requested_values = {
env: ["GROQ_API_KEY"],
};

type response = {
oaires: types.Response;
messages: types.Message[];
};

// const db = await Deno.openKv("./db.sqlite")

export async function send(
messages: types.Message[],
prompt: string | null,
prompt: string,
callback: (type: string, data: types.Response) => void,
values: types.Values,
): Promise<types.Response> {
Expand All @@ -23,16 +18,14 @@ export async function send(
messages.push({
role: "system",
content:
"You are ChatGPT, an LLM by OpenAI. You are running through a Discord bot named LLM Bot, by Eris.",
"You are Mixtral, an LLM by Mistral AI. You are running through a Discord bot named LLM Bot, by Eris.",
});
}

if (prompt !== null) {
messages.push({
role: "user",
content: prompt,
});
}
messages.push({
role: "user",
content: prompt,
});

const res = await fetch("https://api.groq.com/openai/v1/chat/completions", {
method: "POST",
Expand Down
Loading

0 comments on commit 2b20412

Please sign in to comment.