Public
Back
Version 8
4/17/2024
import { Hono } from 'https://deno.land/x/hono/mod.ts';
import process from "node:process";
import { OpenAI } from "https://esm.sh/langchain/llms/openai";
const app = new Hono();
app.get('/', async (c) => {
const prompt = c.req.query('p') || "Tell me a joke";
const headers = new Headers({
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
});
const body = new ReadableStream({
async start(controller) {
const chat = new OpenAI({
maxTokens: 25,
streaming: true,
openAIApiKey: process.env.OPENAI_API_KEY,
});
try {
await chat.call(prompt, undefined, [{
handleLLMNewToken: (token: string) => {
controller.enqueue(`data: ${JSON.stringify({ token })}\n\n`);
},
}]);
} catch (error) {
controller.enqueue(`data: ${JSON.stringify({ error: error.message })}\n\n`);
}
controller.close();
}
});
yawnxyz-openaihonochatstreamexample.web.val.run
Updated: June 6, 2024