Skip to content

Instantly share code, notes, and snippets.

@CleverProgrammer
Created May 16, 2024 08:44
Show Gist options
  • Save CleverProgrammer/5db8719b6a27ddbb9df59751bd1f9bd8 to your computer and use it in GitHub Desktop.
Save CleverProgrammer/5db8719b6a27ddbb9df59751bd1f9bd8 to your computer and use it in GitHub Desktop.
'use server';
import { streamObject } from 'ai';
import { openai } from '@ai-sdk/openai';
import { createStreamableValue } from 'ai/rsc';
import { z } from 'zod';
import { createAnthropic } from '@ai-sdk/anthropic';
const anthropicAI = createAnthropic({
// custom settings
apiKey: process.env.ANTHROPIC_API_KEY || '',
});
const haiku = 'claude-3-haiku-20240307';
const opus = 'claude-3-opus-20240229';
export async function generate(input: string) {
'use server';
const stream = createStreamableValue();
(async () => {
const { partialObjectStream } = await streamObject({
model: anthropicAI(haiku),
system: 'You generate three notifications for a messages app.',
prompt: input,
schema: z.object({
notifications: z.array(
z.object({
name: z.string().describe('Name of a fictional person.'),
message: z.string().describe('Do not use emojis or links.'),
minutesAgo: z.number(),
})
),
}),
});
for await (const partialObject of partialObjectStream) {
stream.update(partialObject);
}
stream.done();
})();
return { object: stream.value };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment