Skip to content

Commit 45bf959

Browse files
committed
feat: add snippets for music, stt, tts
1 parent d9fb84f commit 45bf959

3 files changed

Lines changed: 120 additions & 58 deletions

File tree

packages/generators/src/utils/generate-code-sample.js

Lines changed: 117 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,65 @@ async function fetchModelsCop(modelName) {
66
);
77
}
88

9-
async function generateCodeSample(fetchedModels, modelName) {
9+
function buildCodeSamplesByBody(body) {
10+
return [
11+
{
12+
lang: 'JavaScript',
13+
source: `async function main() {
14+
const response = await fetch('https://api.aimlapi.com/v2/video/generations', {
15+
method: 'POST',
16+
headers: {
17+
'Authorization': 'Bearer <YOUR_API_KEY>',
18+
'Content-Type': 'application/json',
19+
},
20+
body: JSON.stringify(${body.replace(/\n/g, '\n ')}),
21+
});
22+
23+
const data = await response.json();
24+
console.log(JSON.stringify(data, null, 2));
25+
}
26+
27+
main();`,
28+
},
29+
{
30+
lang: 'Python',
31+
source: `import requests
32+
33+
response = requests.post(
34+
"https://api.aimlapi.com/v2/video/generations",
35+
headers={
36+
"Content-Type": "application/json",
37+
"Authorization": "Bearer <YOUR_API_KEY>",
38+
},
39+
json=${body.replace(/\n/g, '\n ')}
40+
)
41+
42+
data = response.json()
43+
print(data)`,
44+
},
45+
{
46+
lang: 'cURL',
47+
source: `curl -L \\
48+
--request POST \\
49+
--url 'https://api.aimlapi.com/v2/video/generations' \\
50+
--header 'Authorization: Bearer <YOUR_API_KEY>' \\
51+
--header 'Content-Type: application/json' \\
52+
--data '${body.replace(/'/g, "\\'").replace(/\n/g, '\n ')}'`,
53+
},
54+
{
55+
lang: 'HTTP',
56+
source: `POST / HTTP/1.1
57+
Host: test.com
58+
Authorization: Bearer <YOUR_API_KEY>
59+
Content-Type: application/json
60+
Accept: */*
61+
62+
${body}`,
63+
},
64+
];
65+
}
66+
67+
async function generateCodeSample(fetchedModels, modelName, schema, path) {
1068
const model = fetchedModels.find((m) => m.name === modelName);
1169
if (!model) {
1270
return;
@@ -486,61 +544,66 @@ Accept: */*
486544

487545
const jsonBody = JSON.stringify(customParams, null, 2);
488546

489-
return [
490-
{
491-
lang: 'JavaScript',
492-
source: `async function main() {
493-
const response = await fetch('https://api.aimlapi.com/v2/video/generations', {
494-
method: 'POST',
495-
headers: {
496-
'Authorization': 'Bearer <YOUR_API_KEY>',
497-
'Content-Type': 'application/json',
498-
},
499-
body: JSON.stringify(${jsonBody.replace(/\n/g, '\n ')}),
500-
});
501-
502-
const data = await response.json();
503-
console.log(JSON.stringify(data, null, 2));
504-
}
505-
506-
main();`,
507-
},
508-
{
509-
lang: 'Python',
510-
source: `import requests
511-
512-
response = requests.post(
513-
"https://api.aimlapi.com/v2/video/generations",
514-
headers={
515-
"Content-Type": "application/json",
516-
"Authorization": "Bearer <YOUR_API_KEY>",
517-
},
518-
json=${jsonBody.replace(/\n/g, '\n ')}
519-
)
547+
return buildCodeSamplesByBody(jsonBody);
548+
} else if (model.category === 'music-models') {
549+
const requiredModelParams = schema?.required || [];
550+
const customParams = {
551+
model: modelName,
552+
};
553+
if (
554+
requiredModelParams.includes('prompt') &&
555+
requiredModelParams.includes('lyrics')
556+
) {
557+
Object.assign(customParams, {
558+
prompt:
559+
'A calm and soothing instrumental music with gentle piano and soft strings.',
560+
lyrics:
561+
'[Verse]\nStreetlights flicker, the night breeze sighs\nShadows stretch as I walk alone\nAn old coat wraps my silent sorrow\nWandering, longing, where should I go\n[Chorus]\nPushing the wooden door, the aroma spreads\nIn a familiar corner, a stranger gazes back\nWarm lights flicker, memories awaken\nIn this small cafe, I find my way\n[Verse]\nRaindrops tap on the windowpane\nA melody plays, soft and low\nThe clink of cups, the murmur of dreams\nIn this haven, I find my home\n[Chorus]\nPushing the wooden door, the aroma spreads\nIn a familiar corner, a stranger gazes back\nWarm lights flicker, memories awaken\nIn this small cafe, I find my way',
562+
});
563+
} else if (requiredModelParams.includes('prompt')) {
564+
Object.assign(customParams, {
565+
prompt: 'lo-fi pop hip-hop ambient music',
566+
});
567+
} else {
568+
console.error(`Unable to generate code sample for model ${modelName}`);
569+
return;
570+
}
571+
const jsonBody = JSON.stringify(customParams, null, 2);
520572

521-
data = response.json()
522-
print(data)`,
523-
},
524-
{
525-
lang: 'cURL',
526-
source: `curl -L \\
527-
--request POST \\
528-
--url 'https://api.aimlapi.com/v2/video/generations' \\
529-
--header 'Authorization: Bearer <YOUR_API_KEY>' \\
530-
--header 'Content-Type: application/json' \\
531-
--data '${jsonBody.replace(/'/g, "\\'").replace(/\n/g, '\n ')}'`,
532-
},
533-
{
534-
lang: 'HTTP',
535-
source: `POST / HTTP/1.1
536-
Host: test.com
537-
Authorization: Bearer <YOUR_API_KEY>
538-
Content-Type: application/json
539-
Accept: */*
573+
return buildCodeSamplesByBody(jsonBody);
574+
} else if (model.category === 'speech-models') {
575+
const requiredModelParams = schema?.required || [];
576+
const customParams = {
577+
model: modelName,
578+
};
579+
if (path.includes('stt')) {
580+
Object.assign(customParams, {
581+
url: 'https://audio-samples.github.io/samples/mp3/blizzard_primed/sample-0.mp3',
582+
});
583+
} else if (requiredModelParams.includes('text')) {
584+
Object.assign(customParams, {
585+
text: 'Cities of the future promise to radically transform how people live, work, and move. Instead of sprawling layouts, we’ll see vertical structures that integrate residential, work, and public spaces into single, self-sustaining ecosystems. Architecture will adapt to climate conditions, and buildings will be energy-efficient—generating power through solar panels, wind turbines, and even foot traffic.',
586+
});
587+
if (
588+
requiredModelParams.includes('voice') &&
589+
schema.properties?.voice?.enum?.[0]
590+
) {
591+
Object.assign(customParams, {
592+
voice: schema.properties?.voice?.enum?.[0],
593+
});
594+
}
595+
} else if (requiredModelParams.includes('script')) {
596+
Object.assign(customParams, {
597+
script:
598+
'Speaker 1: Wow, whats happening, Alice? \nSpeaker 2: Oh, just the usual… a full-blown AI revolution. Nothing to worry about',
599+
});
600+
} else {
601+
console.error(`Unable to generate code sample for model ${modelName}`);
602+
return;
603+
}
604+
const jsonBody = JSON.stringify(customParams, null, 2);
540605

541-
${jsonBody}`,
542-
},
543-
];
606+
return buildCodeSamplesByBody(jsonBody);
544607
}
545608
}
546609

packages/generators/src/utils/parse-openapi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ const parseOpenapi = async (openapi, fetchedModels) => {
212212

213213
const { paths, ...rest } = openapi;
214214

215-
const xCodeSamples = await generateCodeSample(fetchedModels, model);
215+
const xCodeSamples = await generateCodeSample(fetchedModels, model, union, path);
216216

217217
const transformed = {
218218
paths: {

packages/openapi/descriptions/response-api.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -875,9 +875,8 @@ We generally recommend altering this or temperature but not both.`,
875875
},
876876
truncation: {
877877
desc: `The truncation strategy to use for the model response.
878-
879-
- auto: If the context of this response and previous ones exceeds the model's context window size, the model will truncate the response to fit the context window by dropping input items in the middle of the conversation.
880-
- disabled (default): If a model response will exceed the context window size for a model, the request will fail with a 400 error.
878+
- auto: If the context of this response and previous ones exceeds the model's context window size, the model will truncate the response to fit the context window by dropping input items in the middle of the conversation.
879+
- disabled (default): If a model response will exceed the context window size for a model, the request will fail with a 400 error.
881880
`,
882881
},
883882
};

0 commit comments

Comments
 (0)