Skip to content

Commit

Permalink
Refactored the getHtmlFromOpenAI function to `getHtmlFromOpenRouter…
Browse files Browse the repository at this point in the history
…` and updated the API endpoint and headers to use the OpenRouter service.
  • Loading branch information
RobinVivant committed Jul 17, 2024
1 parent e46ef22 commit a2dd232
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
14 changes: 8 additions & 6 deletions app/lib/getHtmlFromOpenAI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
OPEN_AI_SYSTEM_PROMPT,
} from '../prompt'

export async function getHtmlFromOpenAI({
export async function getHtmlFromOpenRouter({
image,
apiKey,
text,
Expand All @@ -24,7 +24,7 @@ export async function getHtmlFromOpenAI({
}
previousPreviews?: PreviewShape[]
}) {
if (!apiKey) throw Error('You need to provide an API key (sorry)')
if (!apiKey) throw Error('You need to provide an OpenRouter API key (sorry)')

const messages: GPT4VCompletionRequest['messages'] = [
{
Expand Down Expand Up @@ -92,7 +92,7 @@ export async function getHtmlFromOpenAI({
})

const body: GPT4VCompletionRequest = {
model: 'gpt-4-vision-preview',
model: 'openai/gpt-4-vision-preview',
max_tokens: 4096,
temperature: 0,
messages,
Expand All @@ -103,17 +103,19 @@ export async function getHtmlFromOpenAI({
let json = null

try {
const resp = await fetch('https://api.openai.com/v1/chat/completions', {
const resp = await fetch('https://openrouter.ai/api/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
'Authorization': `Bearer ${apiKey}`,
'HTTP-Referer': 'https://your-site-url.com', // Replace with your actual site URL
'X-Title': 'Make Real Starter', // Replace with your app's name
},
body: JSON.stringify(body),
})
json = await resp.json()
} catch (e: any) {
throw Error(`Could not contact OpenAI: ${e.message}`)
throw Error(`Could not contact OpenRouter: ${e.message}`)
}

return json
Expand Down
6 changes: 3 additions & 3 deletions app/makeReal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Editor, createShapeId, getSvgAsImage, track } from '@tldraw/tldraw'
import { getSelectionAsText } from './lib/getSelectionAsText'
import { getHtmlFromOpenAI } from './lib/getHtmlFromOpenAI'
import { getHtmlFromOpenRouter } from './lib/getHtmlFromOpenAI'
import { blobToBase64 } from './lib/blobToBase64'
import { addGridToSvg } from './lib/addGridToSvg'
import { PreviewShape } from './PreviewShape/PreviewShape'
Expand Down Expand Up @@ -55,7 +55,7 @@ export async function makeReal(editor: Editor, apiKey: string) {

// Send everything to OpenAI and get some HTML back
try {
const json = await getHtmlFromOpenAI({
const json = await getHtmlFromOpenRouter({
image: dataUrl,
apiKey,
text: getSelectionAsText(editor),
Expand All @@ -65,7 +65,7 @@ export async function makeReal(editor: Editor, apiKey: string) {
})

if (!json) {
throw Error('Could not contact OpenAI.')
throw Error('Could not contact OpenRouter.')
}

if (json?.error) {
Expand Down

0 comments on commit a2dd232

Please sign in to comment.