Skip to content

Commit

Permalink
Merge pull request #596 from koreyspace/main
Browse files Browse the repository at this point in the history
Added Github Models JS Lessons
  • Loading branch information
koreyspace authored Sep 17, 2024
2 parents 8139eff + 3d3daf2 commit fe0fdb9
Show file tree
Hide file tree
Showing 17 changed files with 2,448 additions and 58 deletions.
80 changes: 80 additions & 0 deletions 06-text-generation-apps/js-githubmodels/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import ModelClient from "@azure-rest/ai-inference";
import { AzureKeyCredential } from "@azure/core-auth";

const token = process.env["GITHUB_TOKEN"];
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);
});
285 changes: 285 additions & 0 deletions 06-text-generation-apps/js-githubmodels/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions 06-text-generation-apps/js-githubmodels/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"type": "module",
"dependencies": {
"@azure-rest/ai-inference": "latest",
"@azure/core-auth": "latest",
"@azure/core-sse": "latest"
}
}
7 changes: 6 additions & 1 deletion 06-text-generation-apps/typescript/recipe-app/.env-sample
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Used to authenticate with the Azure OpenAI. Retrieve these

# values from an Azure OpenAI instance in the Azure Portal.

ENDPOINT="https://<resource name>.openai.azure.com"
AZURE_API_KEY="<azure api key>"
OPENAI_API_KEY="<openai api key>"
OPENAI_API_KEY="<openai api key>"

# get your pat token from: https://github.com/settings/tokens?type=beta

GITHUB_TOKEN="github_pat*****"
Loading

0 comments on commit fe0fdb9

Please sign in to comment.