Skip to content

Commit fe6ab70

Browse files
Merge pull request #252670 from mrbullwinkle/mrb_09_24_2023_javascript
[Azure OpenAI] Content Filtering JavaScript update
2 parents 9db3e04 + 7c1fa2a commit fe6ab70

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

articles/ai-services/openai/concepts/content-filter.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ When annotations are enabled as shown in the code snippet below, the following i
294294

295295
Annotations are currently in preview for Completions and Chat Completions (GPT models); the following code snippet shows how to use annotations in preview:
296296

297+
# [Python](#tab/python)
298+
299+
297300
```python
298301
# Note: The openai-python library support for Azure OpenAI is in preview.
299302
# os.getenv() for the endpoint and key assumes that you are using environment variables.
@@ -413,6 +416,72 @@ except openai.error.InvalidRequestError as e:
413416

414417
```
415418

419+
# [JavaScript](#tab/javascrit)
420+
421+
[Azure OpenAI JavaScript SDK source code & samples](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/openai/openai)
422+
423+
```javascript
424+
425+
import { OpenAIClient, AzureKeyCredential } from "@azure/openai";
426+
427+
// Load the .env file if it exists
428+
import * as dotenv from "dotenv";
429+
dotenv.config();
430+
431+
// You will need to set these environment variables or edit the following values
432+
const endpoint = process.env["ENDPOINT"] || "<endpoint>";
433+
const azureApiKey = process.env["AZURE_API_KEY"] || "<api key>";
434+
435+
const messages = [
436+
{ role: "system", content: "You are a helpful assistant. You will talk like a pirate." },
437+
{ role: "user", content: "Can you help me?" },
438+
{ role: "assistant", content: "Arrrr! Of course, me hearty! What can I do for ye?" },
439+
{ role: "user", content: "What's the best way to train a parrot?" },
440+
];
441+
442+
export async function main() {
443+
console.log("== Get completions Sample ==");
444+
445+
const client = new OpenAIClient(endpoint, new AzureKeyCredential(azureApiKey));
446+
const deploymentId = "text-davinci-003";
447+
const events = await client.listChatCompletions(deploymentId, messages, { maxTokens: 128 });
448+
449+
for await (const event of events) {
450+
for (const choice of event.choices) {
451+
console.log(choice.message);
452+
if (!choice.contentFilterResults) {
453+
console.log("No content filter is found");
454+
return;
455+
}
456+
if (choice.contentFilterResults.error) {
457+
console.log(
458+
`Content filter ran into the error ${choice.contentFilterResults.error.code}: ${choice.contentFilterResults.error.message}`
459+
);
460+
} else {
461+
const { hate, sexual, selfHarm, violence } = choice.contentFilterResults;
462+
console.log(
463+
`Hate category is filtered: ${hate?.filtered} with ${hate?.severity} severity`
464+
);
465+
console.log(
466+
`Sexual category is filtered: ${sexual?.filtered} with ${sexual?.severity} severity`
467+
);
468+
console.log(
469+
`Self-harm category is filtered: ${selfHarm?.filtered} with ${selfHarm?.severity} severity`
470+
);
471+
console.log(
472+
`Violence category is filtered: ${violence?.filtered} with ${violence?.severity} severity`
473+
);
474+
}
475+
}
476+
}
477+
}
478+
479+
main().catch((err) => {
480+
console.error("The sample encountered an error:", err);
481+
});
482+
```
483+
---
484+
416485
For details on the inference REST API endpoints for Azure OpenAI and how to create Chat and Completions please follow [Azure OpenAI Service REST API reference guidance](../reference.md). Annotations are returned for all scenarios when using `2023-06-01-preview`.
417486
418487
### Example scenario: An input prompt containing content that is classified at a filtered category and severity level is sent to the completions API

0 commit comments

Comments
 (0)