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

Fix environment variable loading, add OpenAI Organization support #23

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions app/lib/getHtmlFromOpenAI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import {
export async function getHtmlFromOpenAI({
image,
apiKey,
openAIOrganizationId,
text,
grid,
theme = 'light',
previousPreviews = [],
}: {
image: string
apiKey: string
openAIOrganizationId: string
text: string
theme?: string
grid?: {
Expand Down Expand Up @@ -102,13 +104,19 @@ export async function getHtmlFromOpenAI({

let json = null

const headers: Record<string, string> = {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
};

if (openAIOrganizationId) {
headers['OpenAI-Organization'] = openAIOrganizationId;
}

try {
const resp = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
headers,
body: JSON.stringify(body),
})
json = await resp.json()
Expand Down
2 changes: 2 additions & 0 deletions app/lib/makeReal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export async function makeReal(editor: Editor, apiKey: string) {
// Get the selected shapes (we need at least one)
const selectedShapes = editor.getSelectedShapes()

const openAIOrganizationId = process.env.OPENAI_ORGANIZATION_ID ?? ''
if (selectedShapes.length === 0) throw Error('First select something to make real.')

// Create the preview shape
Expand Down Expand Up @@ -58,6 +59,7 @@ export async function makeReal(editor: Editor, apiKey: string) {
const json = await getHtmlFromOpenAI({
image: dataUrl,
apiKey,
openAIOrganizationId,
text: getSelectionAsText(editor),
previousPreviews,
grid,
Expand Down
7 changes: 6 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
/** @type {import('next').NextConfig} */
const nextConfig = {}
const nextConfig = {
env: {
OPENAI_API_KEY: process.env.OPENAI_API_KEY,
OPENAI_ORGANIZATION_ID: process.env.OPENAI_ORGANIZATION_ID,
},
}

module.exports = nextConfig