Skip to content

Commit cf656ca

Browse files
committed
add new rate-limited getChatCompletion for compat with AI SDK
1 parent 8561361 commit cf656ca

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
const extrovert = require('extrovert');
4+
5+
module.exports = extrovert.toNetlifyFunction(require('../../src/actions/getChatCompletion'));

src/actions/getChatCompletion.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'use strict';
2+
3+
const Archetype = require('archetype');
4+
const connect = require('../../src/db');
5+
6+
const CreateChatMessageParams = new Archetype({
7+
messages: [{
8+
role: {
9+
$type: 'string'
10+
},
11+
content: [{
12+
type: {
13+
$type: 'string',
14+
$required: true
15+
},
16+
text: {
17+
$type: 'string'
18+
}
19+
}]
20+
}],
21+
model: {
22+
$type: 'string',
23+
$enum: ['gpt-4o', 'gpt-4.1-nano']
24+
}
25+
}).compile('CreateChatMessageParams');
26+
27+
module.exports = async function createChatMessage(params) {
28+
const { messages, model } = new CreateChatMessageParams(params);
29+
30+
if (!messages) {
31+
throw new Error('Missing messages');
32+
}
33+
34+
const db = await connect();
35+
const { RateLimit } = db.models;
36+
37+
await RateLimit.checkRateLimit('openai', 1000);
38+
39+
const res = await getChatCompletion(messages, { model });
40+
41+
return {
42+
response: res.choices?.[0]?.message?.content
43+
};
44+
};
45+
46+
async function getChatCompletion(messages, options = {}) {
47+
const response = await fetch('https://api.openai.com/v1/chat/completions', {
48+
method: 'POST',
49+
headers: {
50+
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
51+
'Content-Type': 'application/json'
52+
},
53+
body: JSON.stringify({
54+
max_tokens: 2500,
55+
...options,
56+
model: options?.model || 'gpt-4o',
57+
messages
58+
})
59+
});
60+
61+
if (!response.ok) {
62+
throw new Error(`OpenAI API request failed with status ${response.status}: ${await response.text()}`);
63+
}
64+
65+
return await response.json();
66+
}

0 commit comments

Comments
 (0)