litellm/llms/openai/completion/guardrail_translation/README.md
Handler for processing OpenAI's text completion endpoint (/v1/completions) with guardrails.
This handler processes text completion requests by:
Single Prompt:
{
"model": "gpt-3.5-turbo-instruct",
"prompt": "Say this is a test",
"max_tokens": 7,
"temperature": 0
}
Multiple Prompts (Batch):
{
"model": "gpt-3.5-turbo-instruct",
"prompt": [
"Tell me a joke",
"Write a poem"
],
"max_tokens": 50
}
{
"id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7",
"object": "text_completion",
"created": 1589478378,
"model": "gpt-3.5-turbo-instruct",
"choices": [
{
"text": "\n\nThis is indeed a test",
"index": 0,
"logprobs": null,
"finish_reason": "length"
}
],
"usage": {
"prompt_tokens": 5,
"completion_tokens": 7,
"total_tokens": 12
}
}
The handler is automatically discovered and applied when guardrails are used with the text completion endpoint.
curl -X POST 'http://localhost:4000/v1/completions' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer your-api-key' \
-d '{
"model": "gpt-3.5-turbo-instruct",
"prompt": "Say this is a test",
"guardrails": ["content_moderation"],
"max_tokens": 7
}'
The guardrail will be applied to both:
curl -X POST 'http://localhost:4000/v1/completions' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer your-api-key' \
-d '{
"model": "gpt-3.5-turbo-instruct",
"prompt": "My name is John Doe and my email is [email protected]",
"guardrails": ["mask_pii"],
"metadata": {
"guardrails": ["mask_pii"]
}
}'
curl -X POST 'http://localhost:4000/v1/completions' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer your-api-key' \
-d '{
"model": "gpt-3.5-turbo-instruct",
"prompt": [
"Tell me about AI",
"What is machine learning?"
],
"guardrails": ["content_filter"],
"max_tokens": 100
}'
prompt (string or list of strings)choices[*].text (string)Override these methods to customize behavior:
process_input_messages(): Customize how prompts are processedprocess_output_response(): Customize how completion texts are processedCallTypes.text_completion - Synchronous text completionCallTypes.atext_completion - Asynchronous text completion