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..5adaa9ec --- /dev/null +++ b/src/ai/.x/templates/includes/openai.js/Main.js @@ -0,0 +1,52 @@ +{{if {_IS_LEARN_DOC_TEMPLATE}}} +const { AzureOpenAI } = require("openai"); +{{else}} +const { OpenAI } = require("openai"); +const { {ClassName} } = require("./OpenAIChatCompletionsClass"); +const { readline } = require("./ReadLineWrapper"); +{{endif}} + +{{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}} + +async function main() { +{{@include openai.js/create.openai.js}} +{{if {_IS_LEARN_DOC_TEMPLATE}}} + for (const choice of result.choices) { + console.log(choice.message); + } +} +{{else}} + // 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(); +} +{{endif}} + +main().catch((err) => { + console.error("The sample encountered an error:", err); + process.exit(1); +}); + +module.exports = { main }; \ 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 935c0f01..243d5e21 100644 --- a/src/ai/.x/templates/includes/openai.js/create.openai.js +++ b/src/ai/.x/templates/includes/openai.js/create.openai.js @@ -14,222 +14,47 @@ {{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}} - // Get the required environment variables +{{@include openai.js/environment.vars.js}} {{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; + {{if {_IS_LEARN_DOC_TEMPLATE}}} + {{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. {{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`; + 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}} - 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('"; + {{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}} + +{{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}} +{{if !{_IS_LEARN_DOC_TEMPLATE}}} +// 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(' { - console.error("The sample encountered an error:", err); - process.exit(1); -}); - -module.exports = { main }; +{{@include openai.js/Main.js}} diff --git a/src/ai/.x/templates/openai-chat-js/_.json b/src/ai/.x/templates/openai-chat-js/_.json index ce31afbb..ae61c609 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", + "_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-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..276d0958 --- /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", + "_TEMPLATE_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" + } +}