-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.js
More file actions
55 lines (48 loc) · 1.58 KB
/
sample.js
File metadata and controls
55 lines (48 loc) · 1.58 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
import ModelClient, { isUnexpected } from "@azure-rest/ai-inference";
import { AzureKeyCredential } from "@azure/core-auth";
import fs from "fs";
import path from "path";
import { fileURLToPath } from 'url';
// Get token from environment
const token = process.env["GITHUB_TOKEN"];
const endpoint = "https://models.github.ai/inference";
const model = "openai/gpt-4o-mini"; // Ensure this is a vision-capable model (multimodal )
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export async function main() {
// Read image file as base64
const imagePath = path.join(__dirname, "contoso_layout_sketch.jpg");
const imageBuffer = fs.readFileSync(imagePath);
const imageBase64 = imageBuffer.toString("base64");
const client = ModelClient(endpoint, new AzureKeyCredential(token));
const response = await client.path("/chat/completions").post({
body: {
messages: [
{
role: "user",
content: [
{
type: "text",
text: "write HTML and CSS code for a webpage based on the following hand-drawn sketch",
},
{
type: "image_url",
image_url: {
url: `data:image/jpeg;base64,${imageBase64}`,
},
},
],
},
],
max_tokens: 1500,
model: model,
},
});
if (isUnexpected(response)) {
throw response.body.error;
}
console.log(response.body.choices[0].message.content);
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});