From d16035c934a6cd168ce2aaf96f3f67e87329d7d5 Mon Sep 17 00:00:00 2001 From: brandom Date: Tue, 11 Jun 2024 11:30:55 -0700 Subject: [PATCH 1/7] create environmentVars template --- .../includes/openai.js/create.openai.js | 196 +---------------- .../includes/openai.js/environment.vars.js | 206 ++++++++++++++++++ 2 files changed, 207 insertions(+), 195 deletions(-) create mode 100644 src/ai/.x/templates/includes/openai.js/environment.vars.js diff --git a/src/ai/.x/templates/includes/openai.js/create.openai.js b/src/ai/.x/templates/includes/openai.js/create.openai.js index 935c0f01..efb8677c 100644 --- a/src/ai/.x/templates/includes/openai.js/create.openai.js +++ b/src/ai/.x/templates/includes/openai.js/create.openai.js @@ -19,202 +19,8 @@ // SEE: https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety {{endif}} - // Get the required environment variables -{{if {_USE_OPENAI_CLOUD_AZURE}}} - {{if {_USE_AZURE_OPENAI_WITH_KEY}}} - const AZURE_OPENAI_API_KEY = {__process_env_or_import_meta_env}.AZURE_OPENAI_API_KEY ?? ""; - {{else if {_IS_BROWSER_TEMPLATE}}} - const AZURE_CLIENT_ID = {__process_env_or_import_meta_env}.AZURE_CLIENT_ID ?? null; - const AZURE_TENANT_ID = {__process_env_or_import_meta_env}.AZURE_TENANT_ID ?? null; - {{endif}} - const AZURE_OPENAI_API_VERSION = {__process_env_or_import_meta_env}.AZURE_OPENAI_API_VERSION ?? ""; - {{if !{_IS_OPENAI_ASST_TEMPLATE}}} - const AZURE_OPENAI_CHAT_DEPLOYMENT = {__process_env_or_import_meta_env}.AZURE_OPENAI_CHAT_DEPLOYMENT ?? ""; - {{endif}} - const AZURE_OPENAI_ENDPOINT = {__process_env_or_import_meta_env}.AZURE_OPENAI_ENDPOINT ?? ""; - {{if {_IS_OPENAI_ASST_TEMPLATE}}} - const AZURE_OPENAI_BASE_URL = `${AZURE_OPENAI_ENDPOINT.replace(/\/+$/, '')}/openai`; - {{else}} - const AZURE_OPENAI_BASE_URL = `${AZURE_OPENAI_ENDPOINT.replace(/\/+$/, '')}/openai/deployments/${AZURE_OPENAI_CHAT_DEPLOYMENT}`; - {{endif}} - {{if {_USE_OPENAI_CLOUD_OPENAI}}} - - {{endif}} -{{endif}} -{{if {_USE_OPENAI_CLOUD_OPENAI}}} - const OPENAI_API_KEY = {__process_env_or_import_meta_env}.OPENAI_API_KEY ?? ""; - {{if !{_IS_OPENAI_ASST_TEMPLATE}}} - const OPENAI_MODEL_NAME = {__process_env_or_import_meta_env}.OPENAI_MODEL_NAME ?? ""; - {{endif}} - const OPENAI_ORG_ID = {__process_env_or_import_meta_env}.OPENAI_ORG_ID ?? null; -{{endif}} - - // Check if the required environment variables are set -{{if {_USE_OPENAI_CLOUD_AZURE}}} - const azureOk = - {{if {_USE_AZURE_OPENAI_WITH_KEY}}} - AZURE_OPENAI_API_KEY != null && !AZURE_OPENAI_API_KEY.startsWith('"; + const apiKey = process.env["AZURE_OPENAI_API_KEY"] || ""; + const apiVersion = "2024-05-01-preview"; + const deployment = "gpt-4o"; //This must match your deployment name. + require("dotenv/config"); + {{else}} + const AZURE_OPENAI_API_KEY = {__process_env_or_import_meta_env}.AZURE_OPENAI_API_KEY ?? ""; + {{else if {_IS_BROWSER_TEMPLATE}}} + const AZURE_CLIENT_ID = {__process_env_or_import_meta_env}.AZURE_CLIENT_ID ?? null; + const AZURE_TENANT_ID = {__process_env_or_import_meta_env}.AZURE_TENANT_ID ?? null; + {{endif}} + const AZURE_OPENAI_API_VERSION = {__process_env_or_import_meta_env}.AZURE_OPENAI_API_VERSION ?? ""; + {{if !{_IS_OPENAI_ASST_TEMPLATE}}} + const AZURE_OPENAI_CHAT_DEPLOYMENT = {__process_env_or_import_meta_env}.AZURE_OPENAI_CHAT_DEPLOYMENT ?? ""; + {{endif}} + const AZURE_OPENAI_ENDPOINT = {__process_env_or_import_meta_env}.AZURE_OPENAI_ENDPOINT ?? ""; + {{if {_IS_OPENAI_ASST_TEMPLATE}}} + const AZURE_OPENAI_BASE_URL = `${AZURE_OPENAI_ENDPOINT.replace(/\/+$/, '')}/openai`; + {{else}} + const AZURE_OPENAI_BASE_URL = `${AZURE_OPENAI_ENDPOINT.replace(/\/+$/, '')}/openai/deployments/${AZURE_OPENAI_CHAT_DEPLOYMENT}`; + {{endif}} + {{endif}} +{{endif}} +{{if {_USE_OPENAI_CLOUD_OPENAI}}} + const OPENAI_API_KEY = {__process_env_or_import_meta_env}.OPENAI_API_KEY ?? ""; + {{if !{_IS_OPENAI_ASST_TEMPLATE}}} + const OPENAI_MODEL_NAME = {__process_env_or_import_meta_env}.OPENAI_MODEL_NAME ?? ""; + {{endif}} + const OPENAI_ORG_ID = {__process_env_or_import_meta_env}.OPENAI_ORG_ID ?? null; +{{endif}} + +// Check if the required environment variables are set +{{if {_USE_OPENAI_CLOUD_AZURE}}} + const azureOk = + {{if {_USE_AZURE_OPENAI_WITH_KEY}}} + AZURE_OPENAI_API_KEY != null && !AZURE_OPENAI_API_KEY.startsWith(' Date: Tue, 11 Jun 2024 11:31:13 -0700 Subject: [PATCH 2/7] move Main.js to common template --- .../.x/templates/includes/openai.js/Main.js | 40 ++++++++++++++++++ src/ai/.x/templates/openai-chat-js/Main.js | 41 +------------------ 2 files changed, 41 insertions(+), 40 deletions(-) create mode 100644 src/ai/.x/templates/includes/openai.js/Main.js diff --git a/src/ai/.x/templates/includes/openai.js/Main.js b/src/ai/.x/templates/includes/openai.js/Main.js new file mode 100644 index 00000000..065a227a --- /dev/null +++ b/src/ai/.x/templates/includes/openai.js/Main.js @@ -0,0 +1,40 @@ +const { OpenAI } = require('openai'); +const { {ClassName} } = require("./OpenAIChatCompletionsClass"); +const { readline } = require("./ReadLineWrapper"); + +async function main() { + + // What's the system prompt? + const AZURE_OPENAI_SYSTEM_PROMPT = process.env.AZURE_OPENAI_SYSTEM_PROMPT ?? "You are a helpful AI assistant."; + + {{@include openai.js/create.openai.js}} + + // Create the streaming chat completions helper + {{if contains(toupper("{OPENAI_CLOUD}"), "AZURE")}} + const chat = new {ClassName}(AZURE_OPENAI_CHAT_DEPLOYMENT, AZURE_OPENAI_SYSTEM_PROMPT, openai); + {{else}} + const chat = new {ClassName}(OPENAI_MODEL_NAME, AZURE_OPENAI_SYSTEM_PROMPT, openai); + {{endif}} + + // Loop until the user types 'exit' + while (true) { + + // Get user input + const input = await readline.question('User: '); + if (input === 'exit' || input === '') break; + + // Get the response + const response = await chat.getResponse(input); + process.stdout.write(`\nAssistant: ${response}\n\n`); + } + + console.log('Bye!'); + process.exit(); +} + +main().catch((err) => { + console.error("The sample encountered an error:", err); + process.exit(1); +}); + +module.exports = { main }; diff --git a/src/ai/.x/templates/openai-chat-js/Main.js b/src/ai/.x/templates/openai-chat-js/Main.js index c68b9d35..07ce0d64 100644 --- a/src/ai/.x/templates/openai-chat-js/Main.js +++ b/src/ai/.x/templates/openai-chat-js/Main.js @@ -1,40 +1 @@ -const { OpenAI } = require('openai'); -const { {ClassName} } = require("./OpenAIChatCompletionsClass"); -const { readline } = require("./ReadLineWrapper"); - -async function main() { - - // What's the system prompt? - const AZURE_OPENAI_SYSTEM_PROMPT = process.env.AZURE_OPENAI_SYSTEM_PROMPT ?? "You are a helpful AI assistant."; - - {{@include openai.js/create.openai.js}} - - // Create the streaming chat completions helper - {{if contains(toupper("{OPENAI_CLOUD}"), "AZURE")}} - const chat = new {ClassName}(AZURE_OPENAI_CHAT_DEPLOYMENT, AZURE_OPENAI_SYSTEM_PROMPT, openai); - {{else}} - const chat = new {ClassName}(OPENAI_MODEL_NAME, AZURE_OPENAI_SYSTEM_PROMPT, openai); - {{endif}} - - // Loop until the user types 'exit' - while (true) { - - // Get user input - const input = await readline.question('User: '); - if (input === 'exit' || input === '') break; - - // Get the response - const response = await chat.getResponse(input); - process.stdout.write(`\nAssistant: ${response}\n\n`); - } - - console.log('Bye!'); - process.exit(); -} - -main().catch((err) => { - console.error("The sample encountered an error:", err); - process.exit(1); -}); - -module.exports = { main }; +{{@include openai.js/Main.js}} From 881e50c1a85e39e7c8a48882ef65aadb340a394f Mon Sep 17 00:00:00 2001 From: brandom Date: Tue, 11 Jun 2024 13:55:51 -0700 Subject: [PATCH 3/7] interlace learn docs content --- .../.x/templates/includes/openai.js/Main.js | 27 ++++----- .../includes/openai.js/create.openai.js | 56 +++++++++++++++---- .../includes/openai.js/environment.vars.js | 52 +++++++++-------- src/ai/.x/templates/openai-chat-js/_.json | 5 +- 4 files changed, 93 insertions(+), 47 deletions(-) diff --git a/src/ai/.x/templates/includes/openai.js/Main.js b/src/ai/.x/templates/includes/openai.js/Main.js index 065a227a..8b9ef0ba 100644 --- a/src/ai/.x/templates/includes/openai.js/Main.js +++ b/src/ai/.x/templates/includes/openai.js/Main.js @@ -1,21 +1,21 @@ -const { OpenAI } = require('openai'); -const { {ClassName} } = require("./OpenAIChatCompletionsClass"); -const { readline } = require("./ReadLineWrapper"); +{{if {_IS_LEARN_DOC_TEMPLATE}}} + const { AzureOpenAI } = require("openai"); +{{else}} + const { OpenAI } = require("openai"); + const { {ClassName} } = require("./OpenAIChatCompletionsClass"); + const { readline } = require("./ReadLineWrapper"); +{{endif}} async function main() { - // What's the system prompt? - const AZURE_OPENAI_SYSTEM_PROMPT = process.env.AZURE_OPENAI_SYSTEM_PROMPT ?? "You are a helpful AI assistant."; - {{@include openai.js/create.openai.js}} - // Create the streaming chat completions helper - {{if contains(toupper("{OPENAI_CLOUD}"), "AZURE")}} - const chat = new {ClassName}(AZURE_OPENAI_CHAT_DEPLOYMENT, AZURE_OPENAI_SYSTEM_PROMPT, openai); - {{else}} - const chat = new {ClassName}(OPENAI_MODEL_NAME, AZURE_OPENAI_SYSTEM_PROMPT, openai); - {{endif}} - +if{{ {_IS_LEARN_DOC_TEMPLATE} || {_IS_STUDIO_VIEW_CODE_TEMPLATE}}} + for (const choice of result.choices) { + console.log(choice.message); + } +} +{{else}} // Loop until the user types 'exit' while (true) { @@ -38,3 +38,4 @@ main().catch((err) => { }); module.exports = { main }; +{{endif}} \ No newline at end of file diff --git a/src/ai/.x/templates/includes/openai.js/create.openai.js b/src/ai/.x/templates/includes/openai.js/create.openai.js index efb8677c..cc4ac196 100644 --- a/src/ai/.x/templates/includes/openai.js/create.openai.js +++ b/src/ai/.x/templates/includes/openai.js/create.openai.js @@ -14,28 +14,50 @@ {{if {_USE_OPENAI_CLOUD_AZURE} && {_USE_OPENAI_CLOUD_OPENAI}}} {{set _USE_OPENAI_CLOUD_EITHER=true}} {{endif}} -{{if {_USE_OPENAI_CLOUD_OPENAI} || {_USE_AZURE_OPENAI_WITH_KEY}}} +{{if !{_IS_LEARN_DOC_TEMPLATE}}} + // What's the system prompt? + const AZURE_OPENAI_SYSTEM_PROMPT = process.env.AZURE_OPENAI_SYSTEM_PROMPT ?? "You are a helpful AI assistant."; + + {{if {_USE_OPENAI_CLOUD_OPENAI} || {_USE_AZURE_OPENAI_WITH_KEY}}} // NOTE: Never deploy your API Key in client-side environments like browsers or mobile apps // SEE: https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety - + {{endif}} {{endif}} {{@include openai.js/environment.vars.js}} +{{if {_IS_AUTHOR_COMMENT} }} +// For some Learn docs and snippets, error handling or additional helpers are excluded to keep +// the code scoped and focused on the main concept being showcased. +{{endif}} +{{if !{_IS_LEARN_DOC_TEMPLATE}}} + {{if {_USE_OPENAI_CLOUD_AZURE}}} {{if {_USE_OPENAI_CLOUD_OPENAI}}} - // Create the OpenAI client + {{if {_IS_LEARN_DOC_TEMPLATE}}} + const client = new AzureOpenAI({ endpoint, apiKey, apiVersion, deployment }); + const result = await client.chat.completions.create({ + messages: [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: "Does Azure OpenAI support customer managed keys?" }, + { role: "assistant", content: "Yes, customer managed keys are supported by Azure OpenAI?" }, + { role: "user", content: "Do other Azure AI services support this too?" }, + ], + model: "", + }); + {{else}} + // Create the AzureOpenAI client console.log(azureOk ? 'Using Azure OpenAI (w/ API Key)...' : 'Using OpenAI...'); - const openai = !azureOk - ? new OpenAI({ + const client = !azureOk + ? new AzureOpenAI({ apiKey: OPENAI_API_KEY, {{if {_IS_BROWSER_TEMPLATE}}} dangerouslyAllowBrowser: true {{endif}} }) - : new OpenAI({ + : new AzureOpenAI({ apiKey: AZURE_OPENAI_API_KEY, baseURL: AZURE_OPENAI_BASE_URL, defaultQuery: { 'api-version': AZURE_OPENAI_API_VERSION }, @@ -44,10 +66,11 @@ dangerouslyAllowBrowser: true {{endif}} }); + {{endif}} {{else if {_USE_AZURE_OPENAI_WITH_KEY}}} - // Create the OpenAI client + // Create the AzureOpenAI client console.log('Using Azure OpenAI (w/ API Key)...'); - const openai = new OpenAI({ + const client = new AzureOpenAI({ apiKey: AZURE_OPENAI_API_KEY, baseURL: AZURE_OPENAI_BASE_URL, defaultQuery: { 'api-version': AZURE_OPENAI_API_VERSION }, @@ -77,9 +100,9 @@ throw error; } - // Create the OpenAI client + // Create the AzureOpenAI client console.log('Using Azure OpenAI (w/ AAD)...'); - const openai = new OpenAI({ + const client = new AzureOpenAI({ apiKey: '', baseURL: AZURE_OPENAI_BASE_URL, defaultQuery: { 'api-version': AZURE_OPENAI_API_VERSION }, @@ -99,4 +122,17 @@ dangerouslyAllowBrowser: true {{endif}} }); +{{endif}} + +{{if {_IS_AUTHOR_COMMENT} }} +// For some Learn docs and snippets, error handling or additional helpers are excluded to keep +// the code scoped and focused on the main concept being showcased. +{{endif}} +{{if !{_IS_LEARN_DOC_TEMPLATE}}} + // Create the streaming chat completions helper + {{if contains(toupper("{OPENAI_CLOUD}"), "AZURE")}} + const chat = new {ClassName}(AZURE_OPENAI_CHAT_DEPLOYMENT, AZURE_OPENAI_SYSTEM_PROMPT, client); + {{else}} + const chat = new {ClassName}(OPENAI_MODEL_NAME, AZURE_OPENAI_SYSTEM_PROMPT, openai); + {{endif}} {{endif}} \ No newline at end of file diff --git a/src/ai/.x/templates/includes/openai.js/environment.vars.js b/src/ai/.x/templates/includes/openai.js/environment.vars.js index e1137177..65c79bb1 100644 --- a/src/ai/.x/templates/includes/openai.js/environment.vars.js +++ b/src/ai/.x/templates/includes/openai.js/environment.vars.js @@ -2,31 +2,31 @@ {{if {_USE_OPENAI_CLOUD_AZURE}}} {{if {_USE_AZURE_OPENAI_WITH_KEY}}} {{if {_IS_LEARN_DOC_TEMPLATE}}} - // Load the .env file if it exists - const dotenv = require("dotenv"); - dotenv.config(); + // Load the .env file if it exists + const dotenv = require("dotenv"); + dotenv.config(); - // You will need to set these environment variables or edit the following values - const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || ""; - const apiKey = process.env["AZURE_OPENAI_API_KEY"] || ""; - const apiVersion = "2024-05-01-preview"; - const deployment = "gpt-4o"; //This must match your deployment name. - require("dotenv/config"); + // You will need to set these environment variables or edit the following values + const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || ""; + const apiKey = process.env["AZURE_OPENAI_API_KEY"] || ""; + const apiVersion = "2024-05-01-preview"; + const deployment = "gpt-4o"; //This must match your deployment name. + require("dotenv/config"); {{else}} - const AZURE_OPENAI_API_KEY = {__process_env_or_import_meta_env}.AZURE_OPENAI_API_KEY ?? ""; - {{else if {_IS_BROWSER_TEMPLATE}}} - const AZURE_CLIENT_ID = {__process_env_or_import_meta_env}.AZURE_CLIENT_ID ?? null; - const AZURE_TENANT_ID = {__process_env_or_import_meta_env}.AZURE_TENANT_ID ?? null; - {{endif}} - const AZURE_OPENAI_API_VERSION = {__process_env_or_import_meta_env}.AZURE_OPENAI_API_VERSION ?? ""; - {{if !{_IS_OPENAI_ASST_TEMPLATE}}} - const AZURE_OPENAI_CHAT_DEPLOYMENT = {__process_env_or_import_meta_env}.AZURE_OPENAI_CHAT_DEPLOYMENT ?? ""; - {{endif}} - const AZURE_OPENAI_ENDPOINT = {__process_env_or_import_meta_env}.AZURE_OPENAI_ENDPOINT ?? ""; - {{if {_IS_OPENAI_ASST_TEMPLATE}}} - const AZURE_OPENAI_BASE_URL = `${AZURE_OPENAI_ENDPOINT.replace(/\/+$/, '')}/openai`; - {{else}} - const AZURE_OPENAI_BASE_URL = `${AZURE_OPENAI_ENDPOINT.replace(/\/+$/, '')}/openai/deployments/${AZURE_OPENAI_CHAT_DEPLOYMENT}`; + const AZURE_OPENAI_API_KEY = {__process_env_or_import_meta_env}.AZURE_OPENAI_API_KEY ?? ""; + {{else if {_IS_BROWSER_TEMPLATE}}} + const AZURE_CLIENT_ID = {__process_env_or_import_meta_env}.AZURE_CLIENT_ID ?? null; + const AZURE_TENANT_ID = {__process_env_or_import_meta_env}.AZURE_TENANT_ID ?? null; + {{endif}} + const AZURE_OPENAI_API_VERSION = {__process_env_or_import_meta_env}.AZURE_OPENAI_API_VERSION ?? ""; + {{if !{_IS_OPENAI_ASST_TEMPLATE}}} + const AZURE_OPENAI_CHAT_DEPLOYMENT = {__process_env_or_import_meta_env}.AZURE_OPENAI_CHAT_DEPLOYMENT ?? ""; + {{endif}} + const AZURE_OPENAI_ENDPOINT = {__process_env_or_import_meta_env}.AZURE_OPENAI_ENDPOINT ?? ""; + {{if {_IS_OPENAI_ASST_TEMPLATE}}} + const AZURE_OPENAI_BASE_URL = `${AZURE_OPENAI_ENDPOINT.replace(/\/+$/, '')}/openai`; + {{else}} + const AZURE_OPENAI_BASE_URL = `${AZURE_OPENAI_ENDPOINT.replace(/\/+$/, '')}/openai/deployments/${AZURE_OPENAI_CHAT_DEPLOYMENT}`; {{endif}} {{endif}} {{endif}} @@ -38,6 +38,11 @@ const OPENAI_ORG_ID = {__process_env_or_import_meta_env}.OPENAI_ORG_ID ?? null; {{endif}} +{{if {_IS_AUTHOR_COMMENT} }} +// For some Learn docs and snippets, error handling or additional checks are excluded to keep +// the code scoped and focused on the main concept being showcased. +{{endif}} +{{if !{_IS_LEARN_DOC_TEMPLATE}}} // Check if the required environment variables are set {{if {_USE_OPENAI_CLOUD_AZURE}}} const azureOk = @@ -204,3 +209,4 @@ process.exit(1); {{endif}} } +{{endif}} \ No newline at end of file diff --git a/src/ai/.x/templates/openai-chat-js/_.json b/src/ai/.x/templates/openai-chat-js/_.json index ce31afbb..56912442 100644 --- a/src/ai/.x/templates/openai-chat-js/_.json +++ b/src/ai/.x/templates/openai-chat-js/_.json @@ -10,5 +10,8 @@ "_IS_BROWSER_TEMPLATE": "false", "_IMPORT_EXPORT_USING_ES6": "false", "__process_env_or_import_meta_env": "process.env", - "_IS_OPENAI_ASST_TEMPLATE": "false" + "_IS_OPENAI_ASST_TEMPLATE": "false", + "_IS_LEARN_DOC_TEMPLATE": "false", + "_IS_AUTHOR_COMMENT": "false", + "_IS_STUDIO_VIEW_CODE_TEMPLATE": "false" } \ No newline at end of file From 85eb780286cc3330abf88be12e26b6a728b0a20f Mon Sep 17 00:00:00 2001 From: brandom Date: Tue, 11 Jun 2024 14:39:11 -0700 Subject: [PATCH 4/7] tidy whitespace and if/endif matches --- .../.x/templates/includes/openai.js/Main.js | 27 ++++++++---- .../includes/openai.js/create.openai.js | 16 ++----- .../includes/openai.js/environment.vars.js | 43 +++++++------------ .../.x/templates/openai-chat-js/package.json | 3 +- .../openai-chat-learndocs-js/Main.js | 1 + .../templates/openai-chat-learndocs-js/_.json | 17 ++++++++ .../openai-chat-learndocs-js/package.json | 16 +++++++ 7 files changed, 74 insertions(+), 49 deletions(-) create mode 100644 src/ai/.x/templates/openai-chat-learndocs-js/Main.js create mode 100644 src/ai/.x/templates/openai-chat-learndocs-js/_.json create mode 100644 src/ai/.x/templates/openai-chat-learndocs-js/package.json diff --git a/src/ai/.x/templates/includes/openai.js/Main.js b/src/ai/.x/templates/includes/openai.js/Main.js index 8b9ef0ba..d2899ef0 100644 --- a/src/ai/.x/templates/includes/openai.js/Main.js +++ b/src/ai/.x/templates/includes/openai.js/Main.js @@ -1,16 +1,27 @@ {{if {_IS_LEARN_DOC_TEMPLATE}}} - const { AzureOpenAI } = require("openai"); +const { AzureOpenAI } = require("openai"); {{else}} - const { OpenAI } = require("openai"); - const { {ClassName} } = require("./OpenAIChatCompletionsClass"); - const { readline } = require("./ReadLineWrapper"); +const { OpenAI } = require("openai"); +const { {ClassName} } = require("./OpenAIChatCompletionsClass"); +const { readline } = require("./ReadLineWrapper"); {{endif}} -async function main() { - - {{@include openai.js/create.openai.js}} +{{if {_IS_LEARN_DOC_TEMPLATE}}} +// Load the .env file if it exists +const dotenv = require("dotenv"); +dotenv.config(); + +// You will need to set these environment variables or edit the following values +const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || ""; +const apiKey = process.env["AZURE_OPENAI_API_KEY"] || ""; +const apiVersion = "2024-05-01-preview"; +const deployment = "gpt-4o"; //This must match your deployment name. +require("dotenv/config"); +{{endif}} -if{{ {_IS_LEARN_DOC_TEMPLATE} || {_IS_STUDIO_VIEW_CODE_TEMPLATE}}} +async function main() { +{{@include openai.js/create.openai.js}} +{{if {_IS_LEARN_DOC_TEMPLATE}}} for (const choice of result.choices) { console.log(choice.message); } diff --git a/src/ai/.x/templates/includes/openai.js/create.openai.js b/src/ai/.x/templates/includes/openai.js/create.openai.js index cc4ac196..d39c7442 100644 --- a/src/ai/.x/templates/includes/openai.js/create.openai.js +++ b/src/ai/.x/templates/includes/openai.js/create.openai.js @@ -23,18 +23,9 @@ // SEE: https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety {{endif}} {{endif}} - {{@include openai.js/environment.vars.js}} - -{{if {_IS_AUTHOR_COMMENT} }} -// For some Learn docs and snippets, error handling or additional helpers are excluded to keep -// the code scoped and focused on the main concept being showcased. -{{endif}} -{{if !{_IS_LEARN_DOC_TEMPLATE}}} - {{if {_USE_OPENAI_CLOUD_AZURE}}} - {{if {_USE_OPENAI_CLOUD_OPENAI}}} - {{if {_IS_LEARN_DOC_TEMPLATE}}} + {{if {_IS_LEARN_DOC_TEMPLATE}}} const client = new AzureOpenAI({ endpoint, apiKey, apiVersion, deployment }); const result = await client.chat.completions.create({ messages: [ @@ -45,7 +36,8 @@ ], model: "", }); - {{else}} + {{else}} + {{if {_USE_OPENAI_CLOUD_OPENAI}}} // Create the AzureOpenAI client console.log(azureOk ? 'Using Azure OpenAI (w/ API Key)...' @@ -66,7 +58,6 @@ dangerouslyAllowBrowser: true {{endif}} }); - {{endif}} {{else if {_USE_AZURE_OPENAI_WITH_KEY}}} // Create the AzureOpenAI client console.log('Using Azure OpenAI (w/ API Key)...'); @@ -112,6 +103,7 @@ {{endif}} }); {{endif}} + {{endif}} {{else}} // Create the OpenAI client console.log('Using OpenAI...'); diff --git a/src/ai/.x/templates/includes/openai.js/environment.vars.js b/src/ai/.x/templates/includes/openai.js/environment.vars.js index 65c79bb1..2dcbe32f 100644 --- a/src/ai/.x/templates/includes/openai.js/environment.vars.js +++ b/src/ai/.x/templates/includes/openai.js/environment.vars.js @@ -1,34 +1,23 @@ +{{if !{_IS_LEARN_DOC_TEMPLATE}}} // Get the required environment variables {{if {_USE_OPENAI_CLOUD_AZURE}}} {{if {_USE_AZURE_OPENAI_WITH_KEY}}} - {{if {_IS_LEARN_DOC_TEMPLATE}}} - // Load the .env file if it exists - const dotenv = require("dotenv"); - dotenv.config(); - - // You will need to set these environment variables or edit the following values - const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || ""; - const apiKey = process.env["AZURE_OPENAI_API_KEY"] || ""; - const apiVersion = "2024-05-01-preview"; - const deployment = "gpt-4o"; //This must match your deployment name. - require("dotenv/config"); - {{else}} - const AZURE_OPENAI_API_KEY = {__process_env_or_import_meta_env}.AZURE_OPENAI_API_KEY ?? ""; - {{else if {_IS_BROWSER_TEMPLATE}}} - const AZURE_CLIENT_ID = {__process_env_or_import_meta_env}.AZURE_CLIENT_ID ?? null; - const AZURE_TENANT_ID = {__process_env_or_import_meta_env}.AZURE_TENANT_ID ?? null; - {{endif}} - const AZURE_OPENAI_API_VERSION = {__process_env_or_import_meta_env}.AZURE_OPENAI_API_VERSION ?? ""; - {{if !{_IS_OPENAI_ASST_TEMPLATE}}} - const AZURE_OPENAI_CHAT_DEPLOYMENT = {__process_env_or_import_meta_env}.AZURE_OPENAI_CHAT_DEPLOYMENT ?? ""; - {{endif}} - const AZURE_OPENAI_ENDPOINT = {__process_env_or_import_meta_env}.AZURE_OPENAI_ENDPOINT ?? ""; - {{if {_IS_OPENAI_ASST_TEMPLATE}}} - const AZURE_OPENAI_BASE_URL = `${AZURE_OPENAI_ENDPOINT.replace(/\/+$/, '')}/openai`; - {{else}} - const AZURE_OPENAI_BASE_URL = `${AZURE_OPENAI_ENDPOINT.replace(/\/+$/, '')}/openai/deployments/${AZURE_OPENAI_CHAT_DEPLOYMENT}`; - {{endif}} + const AZURE_OPENAI_API_KEY = {__process_env_or_import_meta_env}.AZURE_OPENAI_API_KEY ?? ""; + {{else if {_IS_BROWSER_TEMPLATE}}} + const AZURE_CLIENT_ID = {__process_env_or_import_meta_env}.AZURE_CLIENT_ID ?? null; + const AZURE_TENANT_ID = {__process_env_or_import_meta_env}.AZURE_TENANT_ID ?? null; {{endif}} + const AZURE_OPENAI_API_VERSION = {__process_env_or_import_meta_env}.AZURE_OPENAI_API_VERSION ?? ""; + {{if !{_IS_OPENAI_ASST_TEMPLATE}}} + const AZURE_OPENAI_CHAT_DEPLOYMENT = {__process_env_or_import_meta_env}.AZURE_OPENAI_CHAT_DEPLOYMENT ?? ""; + {{endif}} + const AZURE_OPENAI_ENDPOINT = {__process_env_or_import_meta_env}.AZURE_OPENAI_ENDPOINT ?? ""; + {{if {_IS_OPENAI_ASST_TEMPLATE}}} + const AZURE_OPENAI_BASE_URL = `${AZURE_OPENAI_ENDPOINT.replace(/\/+$/, '')}/openai`; + {{else}} + const AZURE_OPENAI_BASE_URL = `${AZURE_OPENAI_ENDPOINT.replace(/\/+$/, '')}/openai/deployments/${AZURE_OPENAI_CHAT_DEPLOYMENT}`; + {{endif}} +{{endif}} {{endif}} {{if {_USE_OPENAI_CLOUD_OPENAI}}} const OPENAI_API_KEY = {__process_env_or_import_meta_env}.OPENAI_API_KEY ?? ""; diff --git a/src/ai/.x/templates/openai-chat-js/package.json b/src/ai/.x/templates/openai-chat-js/package.json index ff130119..a43dd84e 100644 --- a/src/ai/.x/templates/openai-chat-js/package.json +++ b/src/ai/.x/templates/openai-chat-js/package.json @@ -13,5 +13,4 @@ "@azure/identity": "4.1.0", "openai": "^4.31.0" } - } - \ No newline at end of file +} diff --git a/src/ai/.x/templates/openai-chat-learndocs-js/Main.js b/src/ai/.x/templates/openai-chat-learndocs-js/Main.js new file mode 100644 index 00000000..07ce0d64 --- /dev/null +++ b/src/ai/.x/templates/openai-chat-learndocs-js/Main.js @@ -0,0 +1 @@ +{{@include openai.js/Main.js}} diff --git a/src/ai/.x/templates/openai-chat-learndocs-js/_.json b/src/ai/.x/templates/openai-chat-learndocs-js/_.json new file mode 100644 index 00000000..2881c2c2 --- /dev/null +++ b/src/ai/.x/templates/openai-chat-learndocs-js/_.json @@ -0,0 +1,17 @@ +{ + "_LongName": "OpenAI Chat Completions quickstart docs", + "_ShortName": "openai-chat-learndocs", + "_Language": "JavaScript", + + "ClassName": "OpenAIChatCompletionsClass", + "AZURE_OPENAI_AUTH_METHOD": "KEY", + "OPENAI_CLOUD": "Azure", + + "_IS_BROWSER_TEMPLATE": "false", + "_IMPORT_EXPORT_USING_ES6": "false", + "__process_env_or_import_meta_env": "process.env", + "_IS_OPENAI_ASST_TEMPLATE": "false", + "_IS_STUDIO_VIEW_CODE_TEMPLATE": "false", + "_IS_LEARN_DOC_TEMPLATE": "true", + "_IS_AUTHOR_COMMENT": "false" +} \ No newline at end of file diff --git a/src/ai/.x/templates/openai-chat-learndocs-js/package.json b/src/ai/.x/templates/openai-chat-learndocs-js/package.json new file mode 100644 index 00000000..1d7cf5e1 --- /dev/null +++ b/src/ai/.x/templates/openai-chat-learndocs-js/package.json @@ -0,0 +1,16 @@ +{ + "name": "openai-chat-learndocs", + "version": "1.0.0", + "description": "", + "main": "Main.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "webpack": "webpack" + }, + "author": "", + "license": "MIT", + "dependencies": { + "@azure/identity": "4.1.0", + "openai": "^4.31.0" + } +} From 025f108b48f4fb930000bcf452fae45900d3c6ac Mon Sep 17 00:00:00 2001 From: brandom Date: Tue, 11 Jun 2024 14:50:45 -0700 Subject: [PATCH 5/7] add reader's note --- src/ai/.x/templates/includes/openai.js/create.openai.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ai/.x/templates/includes/openai.js/create.openai.js b/src/ai/.x/templates/includes/openai.js/create.openai.js index d39c7442..7a519fdf 100644 --- a/src/ai/.x/templates/includes/openai.js/create.openai.js +++ b/src/ai/.x/templates/includes/openai.js/create.openai.js @@ -26,6 +26,11 @@ {{@include openai.js/environment.vars.js}} {{if {_USE_OPENAI_CLOUD_AZURE}}} {{if {_IS_LEARN_DOC_TEMPLATE}}} + {{if {_IS_AUTHOR_COMMENT} }} + // There's likely opportunity to further reduce here and force more overlap across + // all of our samples. For the time being, keeping this unique/separate for the initial + // pitch to showcase the integration with Learn docs. + {{endif}} const client = new AzureOpenAI({ endpoint, apiKey, apiVersion, deployment }); const result = await client.chat.completions.create({ messages: [ From 5b2758c5f3eff9b5e7bed221fab0288587fbcb34 Mon Sep 17 00:00:00 2001 From: brandom Date: Tue, 11 Jun 2024 14:53:33 -0700 Subject: [PATCH 6/7] rename template comment macro --- src/ai/.x/templates/includes/openai.js/create.openai.js | 4 ++-- src/ai/.x/templates/includes/openai.js/environment.vars.js | 2 +- src/ai/.x/templates/openai-chat-js/_.json | 2 +- src/ai/.x/templates/openai-chat-learndocs-js/_.json | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ai/.x/templates/includes/openai.js/create.openai.js b/src/ai/.x/templates/includes/openai.js/create.openai.js index 7a519fdf..243d5e21 100644 --- a/src/ai/.x/templates/includes/openai.js/create.openai.js +++ b/src/ai/.x/templates/includes/openai.js/create.openai.js @@ -26,7 +26,7 @@ {{@include openai.js/environment.vars.js}} {{if {_USE_OPENAI_CLOUD_AZURE}}} {{if {_IS_LEARN_DOC_TEMPLATE}}} - {{if {_IS_AUTHOR_COMMENT} }} + {{if {_TEMPLATE_AUTHOR_COMMENT} }} // There's likely opportunity to further reduce here and force more overlap across // all of our samples. For the time being, keeping this unique/separate for the initial // pitch to showcase the integration with Learn docs. @@ -121,7 +121,7 @@ }); {{endif}} -{{if {_IS_AUTHOR_COMMENT} }} +{{if {_TEMPLATE_AUTHOR_COMMENT} }} // For some Learn docs and snippets, error handling or additional helpers are excluded to keep // the code scoped and focused on the main concept being showcased. {{endif}} diff --git a/src/ai/.x/templates/includes/openai.js/environment.vars.js b/src/ai/.x/templates/includes/openai.js/environment.vars.js index 2dcbe32f..0422ce49 100644 --- a/src/ai/.x/templates/includes/openai.js/environment.vars.js +++ b/src/ai/.x/templates/includes/openai.js/environment.vars.js @@ -27,7 +27,7 @@ const OPENAI_ORG_ID = {__process_env_or_import_meta_env}.OPENAI_ORG_ID ?? null; {{endif}} -{{if {_IS_AUTHOR_COMMENT} }} +{{if {_TEMPLATE_AUTHOR_COMMENT} }} // For some Learn docs and snippets, error handling or additional checks are excluded to keep // the code scoped and focused on the main concept being showcased. {{endif}} diff --git a/src/ai/.x/templates/openai-chat-js/_.json b/src/ai/.x/templates/openai-chat-js/_.json index 56912442..ae61c609 100644 --- a/src/ai/.x/templates/openai-chat-js/_.json +++ b/src/ai/.x/templates/openai-chat-js/_.json @@ -12,6 +12,6 @@ "__process_env_or_import_meta_env": "process.env", "_IS_OPENAI_ASST_TEMPLATE": "false", "_IS_LEARN_DOC_TEMPLATE": "false", - "_IS_AUTHOR_COMMENT": "false", + "_TEMPLATE_AUTHOR_COMMENT": "false", "_IS_STUDIO_VIEW_CODE_TEMPLATE": "false" } \ No newline at end of file diff --git a/src/ai/.x/templates/openai-chat-learndocs-js/_.json b/src/ai/.x/templates/openai-chat-learndocs-js/_.json index 2881c2c2..276d0958 100644 --- a/src/ai/.x/templates/openai-chat-learndocs-js/_.json +++ b/src/ai/.x/templates/openai-chat-learndocs-js/_.json @@ -13,5 +13,5 @@ "_IS_OPENAI_ASST_TEMPLATE": "false", "_IS_STUDIO_VIEW_CODE_TEMPLATE": "false", "_IS_LEARN_DOC_TEMPLATE": "true", - "_IS_AUTHOR_COMMENT": "false" + "_TEMPLATE_AUTHOR_COMMENT": "false" } \ No newline at end of file From dbac46f22980b803d96db9208d87aaec6ec7552c Mon Sep 17 00:00:00 2001 From: brandom Date: Tue, 11 Jun 2024 15:29:19 -0700 Subject: [PATCH 7/7] minor update --- src/ai/.x/templates/includes/openai.js/Main.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ai/.x/templates/includes/openai.js/Main.js b/src/ai/.x/templates/includes/openai.js/Main.js index d2899ef0..5adaa9ec 100644 --- a/src/ai/.x/templates/includes/openai.js/Main.js +++ b/src/ai/.x/templates/includes/openai.js/Main.js @@ -42,11 +42,11 @@ async function main() { console.log('Bye!'); process.exit(); } +{{endif}} main().catch((err) => { console.error("The sample encountered an error:", err); process.exit(1); }); -module.exports = { main }; -{{endif}} \ No newline at end of file +module.exports = { main }; \ No newline at end of file