Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: fix nuxt instructions to use properly cased environment variable #3971

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

JessicaSachs
Copy link

@JessicaSachs JessicaSachs commented Dec 4, 2024

Instructions are currently broken. Nuxt environment variables configured in runtimeConfig within nuxt.config.ts will retain case. We configured the process.env var to be called OPENAI_API_KEY, so the eventHandler needs to call it the same thing.

To reproduce, follow the docs:

// server/event.ts - some file with an event handler, from this tutorial
import { streamText } from 'ai';
import { createOpenAI } from '@ai-sdk/openai';

export default defineLazyEventHandler(async () => {
  debugger; // Inspect `useRuntimeConfig()`
  const apiKey = useRuntimeConfig().openaiApiKey;
  if (!apiKey) throw new Error('Missing OpenAI API key');
  const openai = createOpenAI({
    apiKey: apiKey,
  });

  return defineEventHandler(async (event: any) => {
    const { messages } = await readBody(event);

    const result = streamText({
      model: openai('gpt-4o'),
      messages,
    });

    return result.toDataStreamResponse();
  });
});

Instructions currently broken. Nuxt environment variables retain case. Previous instructions asked to use OPENAI_API_KEY, so the eventHandler needs the same case.
@lgrammel
Copy link
Collaborator

lgrammel commented Dec 4, 2024

The docs are based on our (working) Nuxt example, see e.g. here: https://github.com/vercel/ai/blob/main/examples/nuxt-openai/server/api/chat.ts

I suspect that the difference stems from how you set up your nuxt.config.js - here is what we currently do: https://github.com/vercel/ai/blob/main/examples/nuxt-openai/nuxt.config.ts

That being said, I like having OPENAI_API_KEY better - but I want to keep all docs consistent, and also follow Nuxt best practices. What are the Nuxt best practices for naming such API keys in env variables?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants