hono openai示例


import 'dotenv/config';
import { Hono } from 'hono';
import { streamText } from 'hono/streaming';
import OpenAI from 'openai';

const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
  baseURL: process.env.OPENAI_API_BASE_URL,
});

const app = new Hono()

app.post('/ai', async (c) => {
  const body = await c.req.json<{ prompt: string }>();
  const aiStream = await openai.chat.completions.create({
    model: 'gpt-4o-mini',
    messages: [{ role: 'user', content: body.prompt }],
    stream: true,
  });
  return streamText(c, async (stream) => {
    for await (const chunk of aiStream) {
      stream.write(chunk.choices[0]?.delta?.content ?? '');
    }
  });
})

export default app
posted @ 2024-12-04 19:07  卓能文  阅读(36)  评论(0)    收藏  举报