Skip to content

Commit

Permalink
Removed obligatory setting up of openrouter or bing (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luisotee authored Dec 20, 2023
1 parent bf52157 commit cf54e34
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 16 deletions.
24 changes: 16 additions & 8 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
# Obligatory Environment Variables
# ==============================

# ------------------------------
# Obligatory if you're using Bing:
# ------------------------------

# See README.md to learn how to get these
# You need to set it up to use bing or sydney
BING_COOKIES=""
# Leave this empty if you're not going to use bing or sydney
BING_COOKIES="" # MUID=3F

# This must be set if you are going to use any other model than Bing/Sydney.
OPENROUTER_API_KEY="sk..."
# ------------------------------
# Obligatory if you're using OpenRouter models:
# ------------------------------

# This is the API that OpenRouter will use to search for information.
# You can get one at https://www.searchapi.io/
# It's obligatory to set this up, you can get a free API key with 100 requests per month.
SEARCH_API=""
# You can get this at https://openrouter.ai/
# Leave this empty if you're not going to use any other model than Bing/Sydney
OPENROUTER_API_KEY="" # sk-90...

# ==============================
# Features Environment Variables
Expand Down Expand Up @@ -48,6 +52,10 @@ TRANSCRIPTION_LANGUAGE="auto" # Example: "pt" (portuguese), "en" (english), "es
# OpenRouter Features:
# ------------------------------

# This is the API that OpenRouter will use to search for information.
# You can get one at https://www.searchapi.io/
SEARCH_API="Your API key here" # Do not leave this empty

# This is the memory that OpenRouter will use, options are "buffer" or "summary"
# Buffer saves OPENROUTER_MSG_MEMORY_LIMIT ammount of messages in memory to use for context, anything past that is ignored
# Summary makes a summary of the conversation and uses that as context.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "whatsapp-ai-assistant",
"version": "2.2.0",
"version": "2.2.1",
"description": "WhatsApp chatbot",
"module": "src/index.ts",
"type": "module",
Expand Down
3 changes: 2 additions & 1 deletion src/clients/bing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// @ts-ignore
import { BingAIClient } from "@waylaidwanderer/chatgpt-api";
import KeyvSqlite from "@keyv/sqlite";
import { BING_COOKIES } from "../constants";

function convertFileToSQLite(string: string) {
// Check if the inputString starts with "file:./"
Expand All @@ -20,6 +21,6 @@ const store = new KeyvSqlite({
});

export const bing = new BingAIClient({
cookies: process.env.BING_COOKIES,
cookies: BING_COOKIES,
cache: { store },
});
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ export const SUMMARY_LLM_MODEL = process.env.SUMMARY_LLM_MODEL as string;
export const DEBUG_SUMMARY = process.env.DEBUG_SUMMARY as string;
export const LOG_MESSAGES = process.env.LOG_MESSAGES as string;
export const SEARCH_API = process.env.SEARCH_API as string;
export const BING_COOKIES = process.env.BING_COOKIES as string;
14 changes: 10 additions & 4 deletions src/handlers/message/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import {
} from "../llm-models/completion-bing.ts";
import { log } from "../../helpers/utils";
import {
BING_COOKIES,
BOT_PREFIX,
ENABLE_REMINDERS,
ENABLE_SOURCES,
ENABLE_SUGGESTIONS,
OPENROUTER_API_KEY,
} from "../../constants";
import { handleReminderFor } from "../reminder/reminder.ts";
import { getLLMModel, updateWaMessageId } from "../../crud/conversation";
Expand All @@ -27,8 +29,9 @@ export async function handleMessage(message: Message) {

try {
const context = await createContextFromMessage(message);
let response: string | null = null;

if (llmModel === "bing") {
if (llmModel === "bing" && BING_COOKIES !== "") {
const completion = await getCompletionWithBing(
message,
context,
Expand All @@ -45,15 +48,18 @@ export async function handleMessage(message: Message) {
response = response + "\n\n" + getSuggestions(completion);
if (ENABLE_SOURCES === "true")
response = response + "\n\n" + getSources(completion);
} else {
} else if (llmModel !== "bing" && OPENROUTER_API_KEY !== "") {
console.log("Using Open Router");
response = await getCompletionWithOpenRouter(
message,
context,
streamingReply
);
// if (ENABLE_REMINDERS === "true")
// response = await handleReminderFor(message, response);
}
else {
throw new Error("No LLM model specified or no API key provided for the selected model");
}

if (!response) throw new Error("No response from LLM");

// @ts-ignore
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import dayjs from "dayjs";
export function checkEnv() {
if (!process.env.OPENROUTER_API_KEY) {
console.warn(
"OPENROUTER_API_KEY not provided. You must set OPENROUTER_API_KEY. Please check your .env file."
"OPENROUTER_API_KEY not provided. You will not be able to use any other model than Bing."
);
} else {
if (!process.env.SEARCH_API) {
Expand All @@ -18,7 +18,7 @@ export function checkEnv() {

if (!process.env.BING_COOKIES) {
console.warn(
"BING_COOKIES not provided. The bot will work, but you may soon need to solve captchas."
"BING_COOKIES not provided. You will not be able to use the Bing model."
);
}

Expand Down

0 comments on commit cf54e34

Please sign in to comment.