-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.js
More file actions
58 lines (50 loc) · 1.81 KB
/
Copy pathsample.js
File metadata and controls
58 lines (50 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import ModelClient, { isUnexpected } from "@azure-rest/ai-inference";
import { AzureKeyCredential } from "@azure/core-auth";
import { readFileSync } from "node:fs";
const token = process.env["GITHUB_TOKEN"];
const endpoint = "https://models.github.ai/inference";
const modelName = "meta/Llama-3.2-11B-Vision-Instruct";
export async function main() {
const client = ModelClient(
endpoint,
new AzureKeyCredential(token),
);
const response = await client.path("/chat/completions").post({
body: {
messages: [
{ role: "system", content: "You are a front end developer." },
{ role: "user", content: [
{ type: "text", text: "Develop front end of the web page as shown in the sketch. Provide the code in HTML and CSS"},
{ type: "image_url", image_url: {
url: getImageDataUrl("contoso_layout_sketch.jpg", "jpg"), details: "low"}}
]
}
],
model: modelName
}
});
if (isUnexpected(response)) {
throw response.body.error;
}
console.log(response.body.choices[0].message.content);
}
/**
* Get the data URL of an image file.
* @param {string} imageFile - The path to the image file.
* @param {string} imageFormat - The format of the image file. For example: "jpeg", "png".
* @returns {string} The data URL of the image.
*/
function getImageDataUrl(imageFile, imageFormat) {
try {
const imageBuffer = readFileSync(imageFile);
const imageBase64 = imageBuffer.toString('base64');
return `data:image/${imageFormat};base64,${imageBase64}`;
} catch (error) {
console.error(`Could not read '${imageFile}'.`);
console.error('Set the correct path to the image file before running this sample.');
process.exit(1);
}
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});