Skip to content

Commit 7c1fa2a

Browse files
committed
update
1 parent 03f0bae commit 7c1fa2a

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
@@ -295,6 +295,9 @@ When annotations are enabled as shown in the code snippet below, the following i
295295

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

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

415418
```
416419

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