Skip to content

Commit

Permalink
feat(llama): allow chat-style input
Browse files Browse the repository at this point in the history
  • Loading branch information
sdip15fa committed Feb 14, 2024
1 parent 3fb8c4a commit c02a9c7
Showing 1 changed file with 51 additions and 3 deletions.
54 changes: 51 additions & 3 deletions commands/llama.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const execute = async (client: Client, msg: Message, args: string[]) => {
if (!config.cf_worker.url) {
return client.sendMessage(
chatId,
"Sorry, cf worker url not specified in the environment variable."
"Sorry, cf worker url not specified in the environment variable.",
);
}

Expand All @@ -21,19 +21,67 @@ const execute = async (client: Client, msg: Message, args: string[]) => {
}

const text = args.join(" ") || quotedMsg.body;
const messages: { role: "system" | "user"; content: string }[] = [];

if (
args.length &&
msg.hasQuotedMsg &&
quotedMsg.fromMe &&
quotedMsg.body.startsWith("Llama:")
) {
let currMsg: Message | null = msg;
while (currMsg?.body) {
messages.unshift({
role:
currMsg.body.startsWith("Llama:") && currMsg.fromMe
? "system"
: "user",
content: currMsg.body,
});
if (currMsg.hasQuotedMsg) {
const nextQuotedMsg = await currMsg.getQuotedMessage();
if (!nextQuotedMsg?.body) {
break;
}

if (
currMsg.body.startsWith("Llama:") &&
currMsg.fromMe &&
nextQuotedMsg.fromMe &&
nextQuotedMsg.body.startsWith("Llama:")
) {
break;
}

if (
!(currMsg.body.startsWith("Llama:") && currMsg.fromMe) &&
!(nextQuotedMsg.fromMe && nextQuotedMsg.body.startsWith("Llama:"))
) {
break;
}

currMsg = nextQuotedMsg;
} else {
break;
}
}
}

const username = config.cf_worker.username;
const password = config.cf_worker.password;

const encodedCredentials = Buffer.from(`${username}:${password}`).toString(
"base64"
"base64",
);
const authHeader = `Basic ${encodedCredentials}`;

// Call Llama model with the obtained text
const response = await axios.get<{ response: string }>(config.cf_worker.url, {
params: {
prompt: text,
...(!messages?.length && { prompt: text }),
...(messages?.length && {
messages: encodeURIComponent(JSON.stringify(messages)),
}),
},
headers: {
Authorization: authHeader,
Expand Down

0 comments on commit c02a9c7

Please sign in to comment.