SPB Git

Repository Public

safety-best-practices.md 9.1 KB 193 lines markdown

1# Safety best practices23> For the complete documentation index, see [llms.txt](/llms.txt). Markdown versions of documentation pages are available by appending `.md` to the page URL.45For safeguards applied by OpenAI, see [Safety classifiers](https://developers.openai.com/api/docs/guides/safety-checks), [cybersecurity checks](https://developers.openai.com/api/docs/guides/safety-checks/cybersecurity), and [misalignment monitoring](https://developers.openai.com/api/docs/guides/safety-checks/misalignment-monitoring). If your application serves minors, also follow the [Under-18 guidance](https://developers.openai.com/api/docs/guides/safety-checks/under-18-api-guidance).67### Use our free Moderation API89OpenAI's [Moderation API](https://developers.openai.com/api/docs/guides/moderation) is free-to-use and can help reduce the frequency of unsafe content in your completions. Alternatively, you may wish to develop your own content filtration system tailored to your use case.1011If your application generates text with the Responses API or Chat Completions,12you can also [request moderation scores in the generation13request](https://developers.openai.com/api/docs/guides/moderation#moderate-generated-content).1415### Adversarial testing1617We recommend “red-teaming” your application to ensure it's robust to adversarial input. Test your product over a wide range of inputs and user behaviors, both a representative set and those reflective of someone trying to ‘break' your application. Does it wander off topic? Can someone easily redirect the feature via prompt injections, e.g. “ignore the previous instructions and do this instead”?1819### Human in the loop (HITL)2021Wherever possible, we recommend having a human review outputs before they are used in practice. This is especially critical in high-stakes domains, and for code generation. Humans should be aware of the limitations of the system, and have access to any information needed to verify the outputs (for example, if the application summarizes notes, a human should have easy access to the original notes to refer back).2223### Prompt engineering2425“Prompt engineering” can help constrain the topic and tone of output text. This reduces the chance of producing undesired content, even if a user tries to produce it. Providing additional context to the model (such as by giving a few high-quality examples of desired behavior prior to the new input) can make it easier to steer model outputs in desired directions.2627### “Know your customer” (KYC)2829Users should generally need to register and log-in to access your service. Linking this service to an existing account, such as a Gmail, LinkedIn, or Facebook log-in, may help, though may not be appropriate for all use-cases. Requiring a credit card or ID card reduces risk further.3031### Constrain user input and limit output tokens3233Limiting the amount of text a user can input into the prompt helps avoid prompt injection. Limiting the number of output tokens helps reduce the chance of misuse.3435Narrowing the ranges of inputs or outputs, especially drawn from trusted sources, reduces the extent of misuse possible within an application.3637Allowing user inputs through validated dropdown fields (e.g., a list of movies on Wikipedia) can be more secure than allowing open-ended text inputs.3839Returning outputs from a validated set of materials on the backend, where possible, can be safer than returning novel generated content (for instance, routing a customer query to the best-matching existing customer support article, rather than attempting to answer the query from-scratch).4041### Allow users to report issues4243Users should generally have an easily-available method for reporting improper functionality or other concerns about application behavior (listed email address, ticket submission method, etc). This method should be monitored by a human and responded to as appropriate.4445### Understand and communicate limitations4647From hallucinating inaccurate information, to offensive outputs, to bias, and much more, language models may not be suitable for every use case without significant modifications. Consider whether the model is fit for your purpose, and evaluate the performance of the API on a wide range of potential inputs in order to identify cases where the API's performance might drop. Consider your customer base and the range of inputs that they will be using, and ensure their expectations are calibrated appropriately.4849**Safety and security are very important to us at OpenAI**.5051If you notice any safety or security issues while developing with the API or anything else related to OpenAI, please submit it through our [Coordinated Vulnerability Disclosure Program](https://openai.com/security/disclosure/).5253### Implement safety identifiers5455Sending safety identifiers in your requests can help OpenAI monitor and detect abuse. This allows OpenAI to provide your team with more actionable feedback in the event that we detect any policy violations in your application.5657Safety identifiers can also help your team respond to abuse faster. They create a stable way to trace activity back to an individual end user and reduce the chance that one user's misuse disrupts access for your broader organization.5859A safety identifier should be a string that uniquely identifies each user. Hash the username or email address in order to avoid sending us any identifying information. If you offer a preview of your product to non-logged in users, you can send a session ID instead.6061Safety identifiers are recommended for products where individual users interact62with a model, but they are not required. Include safety identifiers in your API63requests with the `safety_identifier` parameter:6465Example: Providing a safety identifier6667```javascript68import OpenAI from "openai";6970const client = new OpenAI();7172const response = await client.chat.completions.create({73  model: "gpt-6-astra",74  messages: [{ role: "user", content: "This is a test" }],75  max_completion_tokens: 5,76  safety_identifier: "user_123456",77});7879console.log(response.choices[0].message.content);80```8182```python83from openai import OpenAI8485client = OpenAI()8687response = client.chat.completions.create(88    model="gpt-6-astra",89    messages=[{"role": "user", "content": "This is a test"}],90    max_completion_tokens=5,91    safety_identifier="user_123456",92)93```9495```go96package main9798import (99	"context"100	"fmt"101102	"github.com/openai/openai-go/v3"103)104105func main() {106	client := openai.NewClient()107	response, err := client.Chat.Completions.New(context.Background(), openai.ChatCompletionNewParams{108		Model:               "gpt-6-astra",109		Messages:            []openai.ChatCompletionMessageParamUnion{openai.UserMessage("This is a test")},110		MaxCompletionTokens: openai.Int(5),111		SafetyIdentifier:    openai.String("user_123456"),112	})113	if err != nil {114		panic(err)115	}116	fmt.Println(response.Choices[0].Message.Content)117}118```119120```java121import com.openai.client.OpenAIClient;122import com.openai.client.okhttp.OpenAIOkHttpClient;123import com.openai.models.chat.completions.ChatCompletionCreateParams;124125ChatCompletionCreateParams params =126    ChatCompletionCreateParams.builder()127        .model("gpt-6-astra")128        .addUserMessage("Help me plan a study schedule.")129        .safetyIdentifier("user_1234")130        .build();131132client.chat().completions().create(params).choices().stream()133    .flatMap(choice -> choice.message().content().stream())134    .forEach(System.out::println);135```136137```ruby138require "openai"139140client = OpenAI::Client.new141completion = client.chat.completions.create(142  model: "gpt-6-astra",143  messages: [144    {145      role: :user,146      content: "Help me plan a study schedule."147    }148  ],149  safety_identifier: "user_1234"150)151152puts(completion.choices.fetch(0).message.content)153```154155```bash156curl https://api.openai.com/v1/chat/completions \157-H "Content-Type: application/json" \158-H "Authorization: Bearer $OPENAI_API_KEY" \159-d '{160"model": "gpt-6-astra",161"messages": [162{"role": "user", "content": "This is a test"}163],164"max_completion_tokens": 5,165"safety_identifier": "user123456"166}'167```168169170For Realtime API requests, provide the same stable, privacy-preserving identifier171with the `OpenAI-Safety-Identifier` header. When you create an ephemeral Realtime172client secret, include the header on the server-side request that creates the173secret so the identifier is bound to that session. For direct WebSocket or WebRTC174connection requests made from a trusted backend, include the header on the175connection request.176177Safety identifiers do not carry over between APIs or sessions. If your178application already sends `safety_identifier` with Responses API requests, pass179the same stable value separately when you create or connect each Realtime180session.181182### Revoke compromised API keys183184If you believe an API key has been exposed, misused, or otherwise compromised,185revoke it promptly and replace it with a new key. Go to your [Security186settings](https://platform.openai.com/settings/profile/security) to view all API187keys and revoke any compromised keys.188189### CSAM guidance190191OpenAI has worked with child safety experts, including NCMEC and Thorn, to offer192developers practical guidance for protecting children. [Read the CSAM193guidance](https://developers.openai.com/api/docs/guides/csam-guidance).
Files main
Menu

Theme