forked from microsoft/generative-ai-for-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
85 lines (62 loc) · 2.37 KB
/
app.js
File metadata and controls
85 lines (62 loc) · 2.37 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import ModelClient from "@azure-rest/ai-inference";
import { AzureKeyCredential } from "@azure/core-auth";
// SECURITY: Validate required environment variable
const token = process.env["GITHUB_TOKEN"];
if (!token) {
throw new Error("GITHUB_TOKEN environment variable is required. Please set it before running this application.");
}
const endpoint = "https://models.inference.ai.azure.com";
const modelName = "gpt-4o";
export async function main() {
console.log("== Recipe Recommendation App ==");
console.log("Number of recipes: (for example: 5): ");
const numRecipes = "3";
console.log("List of ingredients: (for example: chicken, potatoes, and carrots): ");
const ingredients = "chocolate";
console.log("Filter (for example: vegetarian, vegan, or gluten-free): ");
const filter = "peanuts";
const promptText = `Show me ${numRecipes} recipes for a dish with the following ingredients: ${ingredients}. Per recipe, list all the ingredients used, no ${filter}: `;
const client = new ModelClient(endpoint, new AzureKeyCredential(token));
const response = await client.path("/chat/completions").post({
body: {
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: promptText }
],
model: modelName,
temperature: 1.0,
max_tokens: 1000,
top_p: 1.0
}
});
try {
if (response.status !== "200") {
throw response.body.error;
}
console.log(response.body.choices[0].message.content);
const oldPromptResult = response.body.choices[0].message.content;
const promptShoppingList = 'Produce a shopping list, and please do not include the following ingredients that I already have at home: ';
const newPrompt = `Given ingredients at home: ${ingredients} and these generated recipes: ${oldPromptResult}, ${promptShoppingList}`;
const shoppingListMessage =
await client.path("/chat/completions").post({
body: {
messages: [
{
role: 'system',
content: 'Here is your shopping list:'
},
{
role: 'user',
content: newPrompt
},
],
model: modelName,
}
})
} catch (error) {
console.log('The sample encountered an error: ', error);
}
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});