2024-09-06 15:25:09 -07:00
|
|
|
import { Anthropic } from "@anthropic-ai/sdk";
|
|
|
|
|
2024-05-13 23:00:02 -07:00
|
|
|
export interface Env {
|
2024-09-06 15:25:09 -07:00
|
|
|
ANTHROPIC_API_KEY: string;
|
2024-05-13 23:00:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
2024-09-06 15:25:09 -07:00
|
|
|
async fetch(request: Request, env: Env): Promise<Response> {
|
2024-10-13 22:47:47 -04:00
|
|
|
// Handle CORS preflight requests
|
|
|
|
if (request.method === "OPTIONS") {
|
|
|
|
return new Response(null, {
|
|
|
|
headers: {
|
|
|
|
"Access-Control-Allow-Origin": "*",
|
|
|
|
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
|
|
|
|
"Access-Control-Allow-Headers": "Content-Type",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-09-06 15:25:09 -07:00
|
|
|
if (request.method !== "GET") {
|
|
|
|
return new Response("Method Not Allowed", { status: 405 });
|
|
|
|
}
|
|
|
|
|
|
|
|
const url = new URL(request.url);
|
|
|
|
const instructions = url.searchParams.get("instructions");
|
2024-10-13 22:47:47 -04:00
|
|
|
const context = url.searchParams.get("context");
|
2024-09-06 15:25:09 -07:00
|
|
|
|
2024-10-13 22:47:47 -04:00
|
|
|
if (!instructions) {
|
|
|
|
return new Response("Missing instructions parameter", { status: 400 });
|
|
|
|
}
|
2024-09-06 15:25:09 -07:00
|
|
|
|
2024-10-13 23:04:16 -04:00
|
|
|
const prompt = `You are an intelligent programming assistant. Please respond to the following request concisely:
|
2024-09-06 15:25:09 -07:00
|
|
|
|
2024-10-13 22:47:47 -04:00
|
|
|
${instructions}
|
2024-09-06 15:25:09 -07:00
|
|
|
|
2024-10-13 22:47:47 -04:00
|
|
|
${context ? `Context:\n${context}\n` : ''}
|
2024-09-06 15:25:09 -07:00
|
|
|
|
2024-10-13 22:47:47 -04:00
|
|
|
If your response includes code, please format it using triple backticks (\`\`\`) with the appropriate language identifier. For example:
|
2024-09-06 15:25:09 -07:00
|
|
|
|
2024-10-13 22:47:47 -04:00
|
|
|
\`\`\`python
|
|
|
|
print("Hello, World!")
|
|
|
|
\`\`\`
|
2024-09-06 15:25:09 -07:00
|
|
|
|
2024-10-13 23:04:16 -04:00
|
|
|
Provide a clear and concise explanation along with any code snippets. Keep your response brief and to the point`;
|
2024-09-06 15:25:09 -07:00
|
|
|
|
2024-10-13 22:47:47 -04:00
|
|
|
try {
|
|
|
|
const anthropic = new Anthropic({ apiKey: env.ANTHROPIC_API_KEY });
|
2024-09-06 15:25:09 -07:00
|
|
|
|
2024-10-13 23:04:16 -04:00
|
|
|
const stream = await anthropic.messages.create({
|
2024-10-14 17:11:54 -04:00
|
|
|
model: "claude-3-5-sonnet-20240620",
|
2024-09-06 15:25:09 -07:00
|
|
|
max_tokens: 1024,
|
|
|
|
messages: [{ role: "user", content: prompt }],
|
2024-10-13 23:04:16 -04:00
|
|
|
stream: true,
|
2024-09-06 15:25:09 -07:00
|
|
|
});
|
|
|
|
|
2024-10-13 23:04:16 -04:00
|
|
|
const encoder = new TextEncoder();
|
2024-09-06 15:25:09 -07:00
|
|
|
|
2024-10-13 23:04:16 -04:00
|
|
|
const streamResponse = new ReadableStream({
|
|
|
|
async start(controller) {
|
|
|
|
for await (const chunk of stream) {
|
|
|
|
if (chunk.type === 'content_block_delta' && chunk.delta.type === 'text_delta') {
|
|
|
|
const bytes = encoder.encode(chunk.delta.text);
|
|
|
|
controller.enqueue(bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
controller.close();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return new Response(streamResponse, {
|
2024-10-13 22:47:47 -04:00
|
|
|
headers: {
|
2024-10-13 23:04:16 -04:00
|
|
|
"Content-Type": "text/plain; charset=utf-8",
|
2024-10-13 22:47:47 -04:00
|
|
|
"Access-Control-Allow-Origin": "*",
|
2024-10-13 23:04:16 -04:00
|
|
|
"Cache-Control": "no-cache",
|
|
|
|
"Connection": "keep-alive",
|
2024-10-13 22:47:47 -04:00
|
|
|
},
|
|
|
|
});
|
2024-09-06 15:25:09 -07:00
|
|
|
} catch (error) {
|
|
|
|
console.error("Error:", error);
|
|
|
|
return new Response("Internal Server Error", { status: 500 });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|