Skip to content

Commit

Permalink
feat(cf-worker): allow chat-style input for llama
Browse files Browse the repository at this point in the history
  • Loading branch information
sdip15fa committed Feb 14, 2024
1 parent 223a08e commit 3fb8c4a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
37 changes: 21 additions & 16 deletions cf-worker/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Ai } from '@cloudflare/ai/dist/ai.js';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { Ai } from '@cloudflare/ai';
import { Env } from './types.js';
import { BadRequestException } from './exceptions.js';
import { basicAuthentication, verifyCredentials } from './auth.js';
Expand Down Expand Up @@ -47,23 +49,26 @@ export default {
const params = url.searchParams;

const prompt = params.get('prompt');
let messages: { role: 'user' | 'system'; content: string }[] = [];
try {
messages = JSON.parse(decodeURIComponent(params.get('messages')) || '[]');
} catch {
return new BadRequestException('Messages must be a valid JSON array.');
}

let response: string;
// prompt - simple completion style input
const simple = {
prompt: `Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n Instruction: ${prompt}\n\n Response: \n\n`,
};
const response = await ai.run('@hf/thebloke/zephyr-7b-beta-awq', simple);
// tasks.push({ inputs: simple, response });

/*// messages - chat style input
let chat = {
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'Who won the world series in 2020?' }
]
};
response = await ai.run('@cf/meta/llama-2-7b-chat-int8', chat);
tasks.push({ inputs: chat, response });*/
if (!messages?.length) {
const simple = {
prompt: `Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n Instruction: ${prompt}\n\n Response: \n\n`,
};
response = await ai.run('@hf/thebloke/zephyr-7b-beta-awq', simple);
} else {
const chat = {
messages,
};
response = await ai.run('@cf/meta/llama-2-7b-chat-int8', chat);
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand Down
1 change: 0 additions & 1 deletion cf-worker/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ export interface Env {
AUTH_USER: string;
AUTH_PASSWORD: string;
AI: string;
MONGO_APP_ID: string;
}

0 comments on commit 3fb8c4a

Please sign in to comment.