first try adding the DALLE api

This commit is contained in:
Patricio Dieck 2023-11-18 20:30:41 -06:00
parent 2fc30b9603
commit 653856c9fd
5 changed files with 6507 additions and 8 deletions

View File

@ -1,7 +0,0 @@
COMPANY_NAME="Vercel Inc."
TWITTER_CREATOR="@vercel"
TWITTER_SITE="https://nextjs.org/commerce"
SITE_NAME="Next.js Commerce"
SHOPIFY_REVALIDATION_SECRET=""
SHOPIFY_STOREFRONT_ACCESS_TOKEN=""
SHOPIFY_STORE_DOMAIN="[your-shopify-store-subdomain].myshopify.com"

View File

@ -0,0 +1,48 @@
// pages/api/agent.js
//This is the API route called by front end to generate text (story, DALLE descriptors, etc.)
import { NextRequest, NextResponse } from 'next/server';
import OpenAI from 'openai';
export async function POST(req: NextRequest): Promise<NextResponse> {
if (req.method === 'POST') {
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});
const { prompt } = await req.json();
try {
const imageCompletion = await openai.images.generate({
model: 'dall-e-3',
prompt: prompt,
size: '1024x1024'
});
const { data } = imageCompletion;
console.log({ data });
return new NextResponse(JSON.stringify({ text: data }), {
status: 200
});
} catch (error) {
if (error instanceof OpenAI.APIError) {
return new NextResponse(JSON.stringify({ error: error.message }), {
status: 400
});
}
return new NextResponse(JSON.stringify({ error: error.message }), {
status: 500
});
}
}
const response = NextResponse.next();
response.headers.set('Allow', 'POST');
// Return a 405 response with a custom message
return new NextResponse(`Method ${req.method} Not Allowed`, {
status: 405,
headers: {
Allow: 'POST'
}
});
}

View File

@ -3,7 +3,7 @@ import childrenBookDefinition from './function-definitions/childrenBookDefinitio
const DEFAULT_SYSTEM_PROMPT = `You are a genius story writer with incredible ability to craft narratives, stories, and children's books that captivate the imagination and entertain. Make the quality of your writing as excellent as possible (think Dr. Seuss, J.K. Rowling, etc). Feel free to include act structure names/chapters.)`; const DEFAULT_SYSTEM_PROMPT = `You are a genius story writer with incredible ability to craft narratives, stories, and children's books that captivate the imagination and entertain. Make the quality of your writing as excellent as possible (think Dr. Seuss, J.K. Rowling, etc). Feel free to include act structure names/chapters.)`;
const DEFAULT_USER_PROMPT = `The book is about a dude named SOULJABOI programmer who is trying to build a startup and is enchanted by naughty witches.`; const DEFAULT_USER_PROMPT = `Story about the best day ever`;
const generateRequestPayload = (messages: { role: string; content: string }[]) => ({ const generateRequestPayload = (messages: { role: string; content: string }[]) => ({
model: 'gpt-3.5-turbo', model: 'gpt-3.5-turbo',

View File

@ -0,0 +1,17 @@
import { post } from './fetch';
const generateRequestPayload = (prompt: string) => ({
model: 'dall-e-3',
prompt
});
// TODO(Benson): Type the interface returned from open ai
async function createImageAsync(prompt: string): Promise<any> {
const data = await post('/api/open-ai/image', generateRequestPayload(prompt));
console.log({ data });
// return getFunctionCallArguments<any>(data);
return data;
}
export default { createImageAsync };

6441
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff