Last active
July 3, 2023 23:04
-
-
Save dabit3/d975c087d470d84c7198ee4390fa8434 to your computer and use it in GitHub Desktop.
Base example of GPT functions in TypeScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NextRequest, NextResponse } from 'next/server' | |
import Replicate from 'replicate' | |
const replicate = new Replicate({ | |
auth: process.env.REPLICATE_TOKEN || '' | |
}) | |
const KEY = process.env.OPENAI_API_KEY || '' | |
const base_uri = 'https://api.openai.com/v1/chat/completions' | |
const headers = { | |
'Content-Type': 'application/json', | |
'Authorization': `Bearer ${KEY}` | |
} | |
const data = { | |
'model': 'gpt-4' | |
} | |
export async function POST(req: NextRequest, res: NextResponse) { | |
try { | |
const { query } = await req.json() | |
const requestData = { | |
...data, | |
'messages': [ | |
{'role': 'user', 'content': query } | |
], | |
functions: [ | |
{ | |
name: 'createMusic', | |
description: 'call this function if the request asks to generate music', | |
parameters: { | |
type: 'object', | |
properties: { | |
prompt: { | |
type: 'string', | |
description: 'the exact prompt passed in' | |
} | |
} | |
} | |
}, | |
{ | |
name: 'createImage', | |
description: 'call this function if the request asks to generate an image', | |
parameters: { | |
type: 'object', | |
properties: { | |
prompt: { | |
type: 'string', | |
description: 'the exact prompt passed in' | |
} | |
} | |
} | |
} | |
], | |
function_call: 'auto' | |
} | |
const response = await fetch(base_uri, { | |
method: 'POST', | |
headers, | |
body: JSON.stringify(requestData) | |
}) | |
const json = await response.json() | |
let choice = json.choices[0] | |
const { function_call } = choice.message | |
console.log('function_call: ', function_call) | |
if (function_call) { | |
const args = JSON.parse(function_call.arguments) | |
if (function_call.name === 'createMusic') { | |
const output = await replicate.run( | |
'joehoover/musicgen:7a76a8258b23fae65c5a22debb8841d1d7e816b75c2f24218cd2bd8573787906', | |
{ | |
input: { | |
model_version: 'melody', | |
...args | |
} | |
} | |
) | |
return NextResponse.json({ | |
data: output, | |
type: 'audio' | |
}); | |
} | |
if (function_call.name === 'createImage') { | |
const output = await replicate.run( | |
'ai-forever/kandinsky-2:601eea49d49003e6ea75a11527209c4f510a93e2112c969d548fbb45b9c4f19f', | |
{ | |
input: { | |
...args | |
} | |
} | |
) | |
return NextResponse.json({ | |
data: output, | |
type: 'image' | |
}); | |
} | |
} | |
else { | |
return NextResponse.json({ | |
data: choice.message.content, | |
type: 'text', | |
}); | |
} | |
} catch (err) { | |
console.log('error: ', err) | |
return NextResponse.json({ error: err }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment