Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add templates for JS Chat quickstart and viewcode #313

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/ai/.x/templates/openai-chat-js/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function main() {
{{else}}
const chat = new {ClassName}(OPENAI_MODEL_NAME, AZURE_OPENAI_SYSTEM_PROMPT, openai);
{{endif}}

// Loop until the user types 'exit'
while (true) {

Expand All @@ -37,4 +37,4 @@ main().catch((err) => {
process.exit(1);
});

module.exports = { main };
module.exports = { main };
36 changes: 36 additions & 0 deletions src/ai/.x/templates/openai-chat-quickstart-js/Main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { AzureOpenAI } = require("openai");

// Load the .env file if it exists
const dotenv = require("dotenv");
dotenv.config();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line what puts the env vars into the process.env object? Where did the env vars get defined?


// You will need to set these environment variables or edit the following values
const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>";
const apiVersion = "2024-05-01-preview";
const deployment = process.env["AZURE_OPENAI_DEPLOYMENT"] || "<deployment name>"; //This must match your deployment name.
require("dotenv/config");

async function main() {

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: "",
});

for (const choice of result.choices) {
console.log(choice.message);
}
}

main().catch((err) => {
console.error("The sample encountered an error:", err);
});

module.exports = { main };
13 changes: 13 additions & 0 deletions src/ai/.x/templates/openai-chat-quickstart-js/_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"_LongName": "OpenAI Chat Completions quickstart",
"_ShortName": "openai-chat-quickstart",
"_Language": "JavaScript",

"ClassName": "OpenAIChatCompletionsClass",
"AZURE_OPENAI_AUTH_METHOD": "KEY",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the values in this _.json file used anywhere? I'm not seeing them elsewhere.

"OPENAI_CLOUD": "Azure",

"_IS_BROWSER_TEMPLATE": "false",
"_IMPORT_EXPORT_USING_ES6": "false",
"__process_env_or_import_meta_env": "process.env"
}
17 changes: 17 additions & 0 deletions src/ai/.x/templates/openai-chat-quickstart-js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "openai-chat-docs",
"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",
"dotenv": "^16.0.0"
}
}
25 changes: 25 additions & 0 deletions src/ai/.x/templates/openai-chat-studioviewcode-js/Main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { AzureOpenAI } from "openai";
import { DefaultAzureCredential, getBearerTokenProvider } from "@azure/identity";

export async function main() {
const scope = "https://cognitiveservices.azure.com/.default";
const azureADTokenProvider = getBearerTokenProvider(new DefaultAzureCredential(), scope);
const deployment = "gpt-35-turbo";
const apiVersion = "2024-04-01-preview";
const client = new AzureOpenAI({ azureADTokenProvider, deployment, apiVersion });
const result = await client.chat.completions.create({
messages: [
{ role: "system", content: "You are a helpful assistant. You will talk like a pirate." },
{ role: "user", content: "Can you help me?" },
],
model: '',
});

for (const choice of result.choices) {
console.log(choice.message);
}
}

main().catch((err) => {
console.error("The sample encountered an error:", err);
});
14 changes: 14 additions & 0 deletions src/ai/.x/templates/openai-chat-studioviewcode-js/_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"_LongName": "OpenAI Chat Completions playground viewcode",
"_ShortName": "openai-chat-studioviewcode",
"_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"
}
17 changes: 17 additions & 0 deletions src/ai/.x/templates/openai-chat-studioviewcode-js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "openai-chat-viewcode",
"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"
}
}

15 changes: 15 additions & 0 deletions tests/ai-chat-quickstart-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
- area: ai dev new openai-chat-quickstart-js (key)
tests:

- class: dev new openai-chat-quickstart (javascript)
steps:
- name: generate template
command: ai dev new openai-chat-quickstart --javascript
- name: install dependencies
bash: |
cd openai-chat-quickstart-js
npm install
- name: run template
command: ai dev shell --bash "cd openai-chat-quickstart-js;node Main.js"
expect: |
The output should have positive confirmation that other Azure AI services support managed keys.