Skip to content

Commit 27d1d4d

Browse files
committed
feat: update generation code samples
1 parent e67fd76 commit 27d1d4d

3 files changed

Lines changed: 264 additions & 100 deletions

File tree

packages/generators/src/root.js

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@ const AliasPageGenerator = require('./generators/alias');
33
const HandlebarsPageGenerator = require('./generators/common/handlebars');
44
const PathPlugin = require('./generators/plugins/path');
55
const StorePlugin = require('./generators/plugins/store');
6-
const { TEMPLATE, readTemplate, replaceTemplate, SECTION, CATEGORY, ALIAS_MAP, MODELS_TO_ALIAS_MAP } = require('./templates');
6+
const {
7+
TEMPLATE,
8+
readTemplate,
9+
replaceTemplate,
10+
SECTION,
11+
CATEGORY,
12+
ALIAS_MAP,
13+
MODELS_TO_ALIAS_MAP,
14+
} = require('./templates');
715
const { MODELS_URL, OPENAPI_URL } = require('./config');
816
const CustomGenerator = require('./generators/custom');
917
const VenderPageGenerator = require('./generators/vendor');
@@ -18,24 +26,26 @@ const DOCS_PATH = '/api-references';
1826

1927
const root = {
2028
next: ModelsGenerator.build({
21-
url: path.join(__dirname, "json_for-docs_generation.json"), //MODELS_URL,
29+
url: path.join(__dirname, 'json_for-docs_generation.json'), //MODELS_URL,
2230
openapi: {
2331
url: OPENAPI_URL,
2432
},
2533
effects: [PathPlugin.traverse(DOCS_PATH)],
26-
next:[
34+
next: [
2735
CategoryPageGenerator.build({
2836
next: [
2937
VenderPageGenerator.build({
3038
next: [
3139
AliasPageGenerator.build({
3240
effects: [
3341
StorePlugin.store((...args) => ({
34-
content: (_, c) => JSON.stringify(jsonModify(c.openapi, ...args)),
42+
content: (_, c) =>
43+
JSON.stringify(jsonModify(c.openapi, ...args)),
3544
path: `${PathPlugin.path(...args)}.json`,
3645
})),
3746
StorePlugin.store((...args) => ({
38-
content: (_, c) => JSON.stringify(jsonModifyForPair(c.openapi, ...args)),
47+
content: (_, c) =>
48+
JSON.stringify(jsonModifyForPair(c.openapi, ...args)),
3949
path: `${PathPlugin.path(getArgs(...args))}.json`,
4050
})),
4151
],
@@ -56,50 +66,49 @@ const root = {
5666
// ],
5767
// }),
5868
// }),
59-
]
69+
],
6070
}),
6171

6272
plugins: [new PathPlugin(), new StorePlugin()],
6373
};
6474

65-
const pairMap = {}
75+
const pairMap = {};
6676
const getArgs = (args) => {
67-
const alias = args[args.plugins[0].id].tags.at(-1)
68-
if(pairMap[alias]) {
69-
return pairMap[alias]
77+
const alias = args[args.plugins[0].id].tags.at(-1);
78+
if (pairMap[alias]) {
79+
return pairMap[alias];
7080
}
71-
return args
72-
}
81+
return args;
82+
};
7383
const jsonModify = (data, args) => {
74-
const key = Object.keys(data.schema.paths)[0]
75-
const alias = args[args.plugins[0].id].tags.at(-1)
84+
const key = Object.keys(data.schema.paths)[0];
85+
const alias = args[args.plugins[0].id].tags.at(-1);
7686
const root = PathPlugin.root(args);
77-
const file = path.relative(root, PathPlugin.path(args))
78-
MODELS_TO_ALIAS_MAP[data.model].path = file
87+
const file = path.relative(root, PathPlugin.path(args));
88+
MODELS_TO_ALIAS_MAP[data.model].path = file;
7989
// save for pair fetch in alias
8090
if (data.pair.has) {
81-
8291
const cloned = _.cloneDeep(args);
83-
cloned[cloned.plugins[0].id].path = cloned[cloned.plugins[0].id].path + '-pair'
84-
savePair(alias, cloned)
92+
cloned[cloned.plugins[0].id].path =
93+
cloned[cloned.plugins[0].id].path + '-pair';
94+
savePair(alias, cloned);
8595
}
86-
87-
return data.schema
88-
}
96+
97+
return data.schema;
98+
};
8999

90100
const savePair = (alias, cloned) => {
91-
if(!pairMap[alias])
92-
pairMap[alias] = cloned
93-
}
101+
if (!pairMap[alias]) pairMap[alias] = cloned;
102+
};
94103

95104
// if there is a second endpoint for this alias, a json with the '-pair' appended is created
96105
const jsonModifyForPair = (data, args) => {
97-
if (data.pair.has) {
98-
return data.pair.schema
106+
if (data.pair.has) {
107+
return data.pair.schema;
99108
}
100109

101-
return data.schema
102-
}
110+
return data.schema;
111+
};
103112

104113
module.exports = {
105114
root,
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
function generateCodeSample(fetchedModels, modelName) {
2+
const model = fetchedModels.find((m) => m.name === modelName);
3+
if (!model) {
4+
return;
5+
}
6+
const additionalParamsMap = {
7+
chatAudio: {
8+
python: `,
9+
"modalities": ["text", "audio"],
10+
"audio": {"voice": "alloy", "format": "pcm16"}`,
11+
js: `,
12+
modalities: ['text', 'audio'],
13+
audio: { voice: 'alloy', format: 'pcm16' },`,
14+
},
15+
textToImage: {
16+
python: `,
17+
"prompt": "A T-Rex relaxing on a beach, lying on a sun lounger and wearing sunglasses."`,
18+
js: `,
19+
prompt: 'A T-Rex relaxing on a beach, lying on a sun lounger and wearing sunglasses.',`,
20+
},
21+
imageToImage: {
22+
python: `,
23+
"prompt": "Add a crown to the T-rex's head.",
24+
"${
25+
modelName.includes('image-to-image') || modelName === 'triposr'
26+
? 'image_url'
27+
: 'image'
28+
}": "https://raw.githubusercontent.com/aimlapi/api-docs/main/reference-files/t-rex.png"`,
29+
js: `,
30+
prompt: "Add a crown to the T-rex's head.",
31+
${
32+
modelName.includes('image-to-image') || modelName === 'triposr'
33+
? 'image_url'
34+
: 'image'
35+
}: 'https://raw.githubusercontent.com/aimlapi/api-docs/main/reference-files/t-rex.png',`,
36+
},
37+
};
38+
let additionalParamKey;
39+
if (modelName.includes('-audio-')) {
40+
additionalParamKey = 'chatAudio';
41+
} else if (model.category === 'image-models') {
42+
additionalParamKey = model.features?.includes('image-to-image')
43+
? 'imageToImage'
44+
: 'textToImage';
45+
}
46+
if (model.category === 'text-models-llm') {
47+
return [
48+
{
49+
lang: 'JavaScript',
50+
source: `async function main() {
51+
try {
52+
const response = await fetch('https://api.aimlapi.com/v1/chat/completions', {
53+
method: 'POST',
54+
headers: {
55+
'Authorization': 'Bearer <YOUR_AIMLAPI_KEY>',
56+
'Content-Type': 'application/json',
57+
},
58+
body: JSON.stringify({
59+
model: '${modelName}',
60+
messages:[
61+
{
62+
role:'user',
63+
content: 'Hello'
64+
}
65+
]${additionalParamKey ? additionalParamsMap[additionalParamKey].js : ''}
66+
}),
67+
});
68+
69+
if (!response.ok) {
70+
throw new Error('HTTP error!');
71+
}
72+
73+
const data = await response.json();
74+
console.log(JSON.stringify(data, null, 2));
75+
76+
} catch (error) {
77+
console.error('Error', error);
78+
}
79+
}
80+
81+
main();`,
82+
},
83+
{
84+
lang: 'Python',
85+
source: `import requests
86+
87+
response = requests.post(
88+
"https://api.aimlapi.com/v1/chat/completions",
89+
headers={
90+
"Content-Type":"application/json",
91+
"Authorization":"Bearer <YOUR_AIMLAPI_KEY>",
92+
},
93+
json={
94+
"model":"${modelName}",
95+
"messages":[
96+
{
97+
"role":"user",
98+
"content":"Hello"
99+
}
100+
]${
101+
additionalParamKey
102+
? additionalParamsMap[additionalParamKey].python
103+
: ''
104+
}
105+
}
106+
)
107+
108+
data = response.json()
109+
print(data)`,
110+
},
111+
];
112+
} else if (modelName === 'openai/gpt-image-1') {
113+
return [
114+
{
115+
lang: 'JavaScript',
116+
source: `import fs from 'fs';
117+
import fetch from 'node-fetch';
118+
import FormData from 'form-data';
119+
120+
async function main() {
121+
const formData = new FormData();
122+
formData.append('model', 'openai/gpt-image-1');
123+
formData.append('prompt', "Add a crown to the T-rex's head.");
124+
formData.append('image', fs.createReadStream('<YOUR_IMAGE_PATH.png>'), {
125+
filename: '<IMAGE_NAME>',
126+
contentType: 'image/png',
127+
});
128+
129+
const response = await fetch('https://api.aimlapi.com/v1/images/edits', {
130+
method: 'POST',
131+
headers: {
132+
Authorization: 'Bearer <YOUR_AIMLAPI_KEY>',
133+
...formData.getHeaders(),
134+
},
135+
body: formData,
136+
});
137+
138+
const data = await response.json();
139+
console.log(data);
140+
}
141+
142+
main();`,
143+
},
144+
{
145+
lang: 'Python',
146+
source: `import requests
147+
with open("<YOUR_IMAGE_PATH.png>", "rb") as file:
148+
files = {"image": ("<IMAGE_NAME>", file, "image/png")}
149+
response = requests.post(
150+
"https://api.aimlapi.com/v1/images/edits",
151+
headers={
152+
"Authorization":"Bearer <YOUR_AIMLAPI_KEY>",
153+
},
154+
data={
155+
"model":"${modelName}",
156+
"prompt": "Add a crown to the T-rex's head."
157+
},
158+
files=files
159+
)
160+
161+
data = response.json()
162+
print(data)`,
163+
},
164+
];
165+
} else if (model.category === 'image-models') {
166+
return [
167+
{
168+
lang: 'JavaScript',
169+
source: `async function main() {
170+
try {
171+
const response = await fetch('https://api.aimlapi.com/v1/images/generations', {
172+
method: 'POST',
173+
headers: {
174+
'Authorization': 'Bearer <YOUR_AIMLAPI_KEY>',
175+
'Content-Type': 'application/json',
176+
},
177+
body: JSON.stringify({
178+
model: '${modelName}'${
179+
additionalParamKey ? additionalParamsMap[additionalParamKey].js : ''
180+
}
181+
}),
182+
});
183+
184+
if (!response.ok) {
185+
throw new Error('HTTP error!');
186+
}
187+
188+
const data = await response.json();
189+
console.log(JSON.stringify(data, null, 2));
190+
191+
} catch (error) {
192+
console.error('Error', error);
193+
}
194+
}
195+
196+
main();`,
197+
},
198+
{
199+
lang: 'Python',
200+
source: `import requests
201+
202+
response = requests.post(
203+
"https://api.aimlapi.com/v1/images/generations",
204+
headers={
205+
"Content-Type":"application/json",
206+
"Authorization":"Bearer <YOUR_AIMLAPI_KEY>",
207+
},
208+
json={
209+
"model":"${modelName}"${
210+
additionalParamKey
211+
? additionalParamsMap[additionalParamKey].python
212+
: ''
213+
}
214+
}
215+
)
216+
217+
data = response.json()
218+
print(data)`,
219+
},
220+
];
221+
}
222+
}
223+
224+
module.exports = generateCodeSample;

0 commit comments

Comments
 (0)