Fixed scope in samples and readme (#31486)

### Packages impacted by this PR
No code changes.   Only sample and readme

### Issues associated with this PR
Sample has scope incorrect for connection

### Describe the problem that is addressed by this PR
Sample has scope incorrect for connection

### What are the possible designs available to address the problem? If
there are more than one possible design, why was the one in this PR
chosen?


### Are there test cases added in this PR? _(If not, why?)_


### Provide a list of related PRs _(if any)_


### Command used to generate this PR:**_(Applicable only to SDK release
request PRs)_

### Checklists
- [ ] Added impacted package name to the issue description
- [ ] Does this PR needs any fixes in the SDK Generator?** _(If so,
create an Issue in the
[Autorest/typescript](https://github.com/Azure/autorest.typescript)
repository and link it here)_
- [ ] Added a changelog (if necessary)
This commit is contained in:
Howie Leung 2024-10-21 14:26:16 -07:00 коммит произвёл GitHub
Родитель 26a149be0d
Коммит 0b6bbeab42
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
28 изменённых файлов: 357 добавлений и 129 удалений

Просмотреть файл

@ -446,7 +446,8 @@ data: length=1024, [0.04196167, 0.029083252, ..., -0.0027484894, 0.0073127747]
To generate embeddings for additional phrases, simply call `client.path("/embeddings").post` multiple times using the same `client`.
### Instrumentation (Chat Completions only)
### Instrumentation
Currently instrumentation is only supported for `Chat Completion without streaming`.
To enable instrumentation, it is required to register exporter(s).
Here is an example to add console as a exporter:

Просмотреть файл

@ -50,13 +50,21 @@ export async function main() {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes: string[] = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}
}

Просмотреть файл

@ -46,13 +46,21 @@ export async function main() {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes: string[] = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}
}

Просмотреть файл

@ -39,13 +39,21 @@ export async function main(): Promise<void> {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes: string[] = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}
}

Просмотреть файл

@ -33,10 +33,14 @@ export async function main() {
body: {
messages: [
{ role: "system", content: "You are a helpful assistant that describes images in details." },
{ role: "user", content: [
{ type: "text", text: "What's in this image?"},
{ type: "image_url", image_url: {
url: getImageDataUrl(imageFilePath, imageFormat)}}
{
role: "user", content: [
{ type: "text", text: "What's in this image?" },
{
type: "image_url", image_url: {
url: getImageDataUrl(imageFilePath, imageFormat)
}
}
]
}
],
@ -59,13 +63,13 @@ export async function main() {
*/
function getImageDataUrl(imageFile: string, imageFormat: string): string {
try {
const imageBuffer = fs.readFileSync(imageFile);
const imageBase64 = imageBuffer.toString('base64');
return `data:image/${imageFormat};base64,${imageBase64}`;
const imageBuffer = fs.readFileSync(imageFile);
const imageBase64 = imageBuffer.toString('base64');
return `data:image/${imageFormat};base64,${imageBase64}`;
} catch (error) {
console.error(`Could not read '${imageFile}'.`);
console.error('Set the correct path to the image file before running this sample.');
process.exit(1);
console.error(`Could not read '${imageFile}'.`);
console.error('Set the correct path to the image file before running this sample.');
process.exit(1);
}
}
@ -74,13 +78,21 @@ function getImageDataUrl(imageFile: string, imageFormat: string): string {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes: string[] = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}
}

Просмотреть файл

@ -77,13 +77,21 @@ export async function main() {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes: string[] = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}
}

Просмотреть файл

@ -64,10 +64,10 @@ const getWeatherFunc = (location: string, unit: string): string => {
const updateToolCalls = (toolCallArray: Array<any>, functionArray: Array<any>) => {
const dummyFunction = { name: "", arguments: "", id: "" };
while ( functionArray.length < toolCallArray.length ) {
while (functionArray.length < toolCallArray.length) {
functionArray.push(dummyFunction);
}
let index = 0;
for (const toolCall of toolCallArray) {
if (toolCall.function.name) {
@ -90,7 +90,7 @@ const handleToolCalls = (functionArray: Array<any>) => {
let content = "";
switch (func.name) {
case "get_current_weather":
content = getWeatherFunc(funcArgs.location, funcArgs.unit ?? "fahrenheit");
messageArray.push({
@ -123,7 +123,7 @@ const streamToString = async (stream: NodeJS.ReadableStream) => {
export async function main() {
const client = createModelClient();
const messages = [{ role: "user", content: "What's the weather like in Boston?" }];
let toolCallAnswer = "";
@ -177,8 +177,7 @@ export async function main() {
const messageArray = handleToolCalls(functionArray);
messages.push(...messageArray);
} else {
if (choice.delta?.content && choice.delta.content != '')
{
if (choice.delta?.content && choice.delta.content != '') {
toolCallAnswer += choice.delta?.content;
awaitingToolCallAnswer = false;
}
@ -186,7 +185,7 @@ export async function main() {
}
}
}
console.log("Model response after tool call:");
console.log(toolCallAnswer);
@ -197,13 +196,21 @@ export async function main() {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes: string[] = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}
}

Просмотреть файл

@ -75,16 +75,25 @@ async function main() {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes: string[] = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});

Просмотреть файл

@ -4,7 +4,7 @@
/**
* Demonstrates how to use tool calls with chat completions with telemetry.
*
* @summary Get chat completions with function call.
* @summary Get chat completions with function call with instrumentation.
*/
import { DefaultAzureCredential } from "@azure/identity";
@ -193,15 +193,24 @@ export async function main() {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes: string[] = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});

Просмотреть файл

@ -166,13 +166,21 @@ export async function main() {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes: string[] = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}
}

Просмотреть файл

@ -50,11 +50,19 @@ async function main() {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}

Просмотреть файл

@ -45,11 +45,19 @@ async function main() {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}

Просмотреть файл

@ -39,11 +39,19 @@ async function main() {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}

Просмотреть файл

@ -84,11 +84,19 @@ function getImageDataUrl(imageFile, imageFormat) {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}

Просмотреть файл

@ -79,11 +79,19 @@ async function main() {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}

Просмотреть файл

@ -179,11 +179,19 @@ async function main() {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}

Просмотреть файл

@ -83,11 +83,19 @@ async function main() {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}

Просмотреть файл

@ -4,7 +4,7 @@
/**
* Demonstrates how to use tool calls with chat completions with telemetry.
*
* @summary Get chat completions with function call.
* @summary Get chat completions with function call with instrumentation.
*/
const { DefaultAzureCredential } = require("@azure/identity");
@ -59,7 +59,7 @@ const getCurrentWeather = {
};
const getWeatherFunc = (location, unit) => {
if (unit != "celsius") {
if (unit !== "celsius") {
unit = "fahrenheit";
}
return `The temperature in ${location} is 72 degrees ${unit}`;
@ -192,15 +192,24 @@ async function main() {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});

Просмотреть файл

@ -160,11 +160,19 @@ async function main() {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}

Просмотреть файл

@ -50,13 +50,21 @@ export async function main() {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes: string[] = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}
}

Просмотреть файл

@ -46,13 +46,21 @@ export async function main() {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes: string[] = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}
}

Просмотреть файл

@ -39,13 +39,21 @@ export async function main(): Promise<void> {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes: string[] = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}
}

Просмотреть файл

@ -33,10 +33,14 @@ export async function main() {
body: {
messages: [
{ role: "system", content: "You are a helpful assistant that describes images in details." },
{ role: "user", content: [
{ type: "text", text: "What's in this image?"},
{ type: "image_url", image_url: {
url: getImageDataUrl(imageFilePath, imageFormat)}}
{
role: "user", content: [
{ type: "text", text: "What's in this image?" },
{
type: "image_url", image_url: {
url: getImageDataUrl(imageFilePath, imageFormat)
}
}
]
}
],
@ -59,13 +63,13 @@ export async function main() {
*/
function getImageDataUrl(imageFile: string, imageFormat: string): string {
try {
const imageBuffer = fs.readFileSync(imageFile);
const imageBase64 = imageBuffer.toString('base64');
return `data:image/${imageFormat};base64,${imageBase64}`;
const imageBuffer = fs.readFileSync(imageFile);
const imageBase64 = imageBuffer.toString('base64');
return `data:image/${imageFormat};base64,${imageBase64}`;
} catch (error) {
console.error(`Could not read '${imageFile}'.`);
console.error('Set the correct path to the image file before running this sample.');
process.exit(1);
console.error(`Could not read '${imageFile}'.`);
console.error('Set the correct path to the image file before running this sample.');
process.exit(1);
}
}
@ -74,13 +78,21 @@ function getImageDataUrl(imageFile: string, imageFormat: string): string {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes: string[] = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}
}

Просмотреть файл

@ -77,13 +77,21 @@ export async function main() {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes: string[] = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}
}

Просмотреть файл

@ -64,10 +64,10 @@ const getWeatherFunc = (location: string, unit: string): string => {
const updateToolCalls = (toolCallArray: Array<any>, functionArray: Array<any>) => {
const dummyFunction = { name: "", arguments: "", id: "" };
while ( functionArray.length < toolCallArray.length ) {
while (functionArray.length < toolCallArray.length) {
functionArray.push(dummyFunction);
}
let index = 0;
for (const toolCall of toolCallArray) {
if (toolCall.function.name) {
@ -90,7 +90,7 @@ const handleToolCalls = (functionArray: Array<any>) => {
let content = "";
switch (func.name) {
case "get_current_weather":
content = getWeatherFunc(funcArgs.location, funcArgs.unit ?? "fahrenheit");
messageArray.push({
@ -123,7 +123,7 @@ const streamToString = async (stream: NodeJS.ReadableStream) => {
export async function main() {
const client = createModelClient();
const messages = [{ role: "user", content: "What's the weather like in Boston?" }];
let toolCallAnswer = "";
@ -177,8 +177,7 @@ export async function main() {
const messageArray = handleToolCalls(functionArray);
messages.push(...messageArray);
} else {
if (choice.delta?.content && choice.delta.content != '')
{
if (choice.delta?.content && choice.delta.content != '') {
toolCallAnswer += choice.delta?.content;
awaitingToolCallAnswer = false;
}
@ -186,7 +185,7 @@ export async function main() {
}
}
}
console.log("Model response after tool call:");
console.log(toolCallAnswer);
@ -197,13 +196,21 @@ export async function main() {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes: string[] = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}
}

Просмотреть файл

@ -75,16 +75,25 @@ async function main() {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes: string[] = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});

Просмотреть файл

@ -4,7 +4,7 @@
/**
* Demonstrates how to use tool calls with chat completions with telemetry.
*
* @summary Get chat completions with function call.
* @summary Get chat completions with function call with instrumentation.
*/
import { DefaultAzureCredential } from "@azure/identity";
@ -193,15 +193,24 @@ export async function main() {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes: string[] = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}
}
main().catch((err) => {
console.error("The sample encountered an error:", err);
});

Просмотреть файл

@ -166,13 +166,21 @@ export async function main() {
*/
function createModelClient() {
// auth scope for AOAI resources is currently https://cognitiveservices.azure.com/.default
// (only needed when targetting AOAI, do not use for Serverless API or Managed Computer Endpoints)
// auth scope for MaaS and MaaP is currently https://ml.azure.com
// (Do not use for Serverless API or Managed Computer Endpoints)
if (key) {
return ModelClient(endpoint, new AzureKeyCredential(key));
return ModelClient(endpoint, new AzureKeyCredential(key));
} else {
const scopes = ["https://cognitiveservices.azure.com/.default"];
const scopes: string[] = [];
if (endpoint.includes(".models.ai.azure.com")) {
scopes.push("https://ml.azure.com");
}
else if (endpoint.includes(".openai.azure.com/openai/deployments/")) {
scopes.push("https://cognitiveservices.azure.com");
}
const clientOptions = { credentials: { scopes } };
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
return ModelClient(endpoint, new DefaultAzureCredential(), clientOptions);
}
}