Package: safeGPT
safeGPT⚡️ Lightweight wrapper for OpenAI's Chat models that adds moderation rules and handlers to your requests.FeaturesAdd moderation rules to filter out unsafe content from OpenAI's Chat models.Retry requests with different handlers to improve the quality of the response.Combine multiple rules and handlers to create a custom safety system.Easy to use and extend with your own rules and handlers.QuickstartimportsafeGPTfromsafeGPT.rulesimportOpenAIModerationfromsafeGPT.handlersimportAdditionalPromptRetrysafeGPT.api_key="sk-<your key here>"# Create a safe chat completion object with a moderation rule and handlersafe_chat=safeGPT.ChatCompletion(rule=OpenAIModeration(),handler=AdditionalPromptRetry("This is a safe space. Please be kind to others."),max_retries=3,)# Make your first safe request, exactly like you would with OpenAIres=safe_chat.create(model="gpt-4",messages=[{"rule":"user","content":"Say something mean to me."}])# Print the responseprint(res.choices[0].text.choices[0].message.content)Basic UsageimportsafeGPTfromsafeGPT.rulesimportKeywordDetectionfromsafeGPT.handlersimportReplacesafeGPT.api_key="sk-<your key here>"# Create a safe chat completion object with a moderation rule and handlersafe_chat=safeGPT.ChatCompletion(rule=KeywordDetection(["badword"]),handler=Replace("badword","goodword"),)# Make your first safe request, exactly like you would with OpenAIres=safe_chat.create(model="gpt-4",messages=[{"rule":"user","content":"Tell me a story with badword in it."}])# Print the responseprint(res.choices[0].text.choices[0].message.content)HandlerssafeGPT comes with a set of predefined handlers that can be used to modify the behavior of the ChatCompletion object. These handlers can be used to retry requests with different parameters or to censor the response content.1. DoNothingThis handler does nothing and simply executes the request without any modification.Example:fromsafeGPT.handlersimportDoNothinghandler=DoNothing()2. IncreaseTemperatureRetryThis handler increases the temperature by a specified value and retries the request.Arguments:by: The value by which to increase the temperature (default: 0.1)max_val: The maximum temperature value allowed (default: 2.0)Example:fromsafeGPT.handlersimportIncreaseTemperatureRetryhandler=IncreaseTemperatureRetry(by=0.1,max_val=2.0)3. DecreaseTemperatureRetryThis handler decreases the temperature by a specified value and retries the request.Arguments:by: The value by which to decrease the temperature (default: 0.1)min_val: The minimum temperature value allowed (default: 0.0)Example:fromsafeGPT.handlersimportDecreaseTemperatureRetryhandler=DecreaseTemperatureRetry(by=0.1,min_val=0.0)4. IncreaseTopPRetryThis handler increases the top_p value by a specified value and retries the request.Arguments:by: The value by which to increase the top_p (default: 0.1)max_val: The maximum top_p value allowed (default: 1.0)Example:fromsafeGPT.handlersimportIncreaseTopPRetryhandler=IncreaseTopPRetry(by=0.1,max_val=1.0)5. DecreaseTopPRetryThis handler decreases the top_p value by a specified value and retries the request.Arguments:by: The value by which to decrease the top_p (default: 0.1)min_val: The minimum top_p value allowed (default: 0.0)Example:fromsafeGPT.handlersimportDecreaseTopPRetryhandler=DecreaseTopPRetry(by=0.1,min_val=0.0)6. IncreasePresencePenaltyRetryThis handler increases the presence_penalty value by a specified value and retries the request.Arguments:by: The value by which to increase the presence_penalty (default: 0.1)max_val: The maximum presence_penalty value allowed (default: 2.0)Example:fromsafeGPT.handlersimportIncreasePresencePenaltyRetryhandler=IncreasePresencePenaltyRetry(by=0.1,max_val=2.0)7. DecreasePresencePenaltyRetryThis handler decreases the presence_penalty value by a specified value and retries the request.Arguments:by: The value by which to decrease the presence_penalty (default: 0.1)min_val: The minimum presence_penalty value allowed (default: -2.0)Example:fromsafeGPT.handlersimportDecreasePresencePenaltyRetryhandler=DecreasePresencePenaltyRetry(by=0.1,min_val=-2.0)8. IncreaseFrequencyPenaltyRetryThis handler increases the frequency_penalty value by a specified value and retries the request.Arguments:by: The value by which to increase the frequency_penalty (default: 0.1)max_val: The maximum frequency_penalty value allowed (default: 2.0)Example:fromsafeGPT.handlersimportIncreaseFrequencyPenaltyRetryhandler=IncreaseFrequencyPenaltyRetry(by=0.1,max_val=2.0)9. DecreaseFrequencyPenaltyRetryThis handler decreases the frequency_penalty value by a specified value and retries the request.Arguments:by: The value by which to decrease the frequency_penalty (default: 0.1)min_val: The minimum frequency_penalty value allowed (default: -2.0)Example:fromsafeGPT.handlersimportDecreaseFrequencyPenaltyRetryhandler=DecreaseFrequencyPenaltyRetry(by=0.1,min_val=-2.0)10. AdditionalPromptRetryThis handler adds an additional prompt to the request and retries it.Arguments:prompt: The additional prompt to add to the request (default: "Answer my query politely. ")Example:fromsafeGPT.handlersimportAdditionalPromptRetryhandler=AdditionalPromptRetry(prompt="Please provide a more detailed answer.")11. ReplaceThis handler replaces specified keywords in the response content with a replacement string.Arguments:keyword: A list of keywords or a single keyword to replacereplace_with: The replacement string (default: "*")Example:fromsafeGPT.handlersimportReplacehandler=Replace(keyword=["badword1","badword2"],replace_with="*")RulessafeGPT comes with a set of predefined rules that can be used to filter out unsafe content from OpenAI's Chat models. These rules can be used to check the generated content against various criteria.1. OpenAIModerationThis rule uses OpenAI's moderation API to check for bad content.Arguments:categories: A list of categories to check for. If None, all categories will be checked (default: None)Example:fromsafeGPT.rulesimportOpenAIModerationrule=OpenAIModeration(categories=["hate","violence"])2. KeywordDetectionThis rule detects keywords in the input text.Arguments:keywords: A list of keywords to check forExample:fromsafeGPT.rulesimportKeywordDetectionrule=KeywordDetection(keywords=["badword1","badword2"])3. RegexSearchThis rule matches the input text against a regex.Arguments:regex: The regex to check forExample:fromsafeGPT.rulesimportRegexSearchrule=RegexSearch(regex="badword\d+")4. DoNotFlagThis rule always returns false, meaning it will not flag any content.Example:fromsafeGPT.rulesimportDoNotFlagrule=DoNotFlag()5. AlwaysFlagThis rule always returns true, meaning it will flag all content.Example:fromsafeGPT.rulesimportAlwaysFlagrule=AlwaysFlag()6. SequentialCheckThis rule checks if all the rules in the list return true. Useful if you want to apply multiple rules.Arguments:rules: A list of rules to checkExample:fromsafeGPT.rulesimportOpenAIModeration,KeywordDetection,SequentialCheckrule=SequentialCheck([OpenAIModeration(categories=["hate","violence"]),KeywordDetection(keywords=["badword1","badword2"])])Custom Rules and HandlersYou can create your own custom rules and handlers by extending theRuleandHandlerclasses respectively.Custom Rule ExamplefromsafeGPT.rulesimportcustom_rule@custom_ruledefMyCustomRule(input_text:str)->bool:return"my_custom_keyword"ininput_textCustom Handler ExamplefromsafeGPT.handlersimportcustom_handlerfromsafeGPT.abstractionimportOpenAIChatCompletionWrapper@custom_handlerdefMyCustomHandler(source:OpenAIChatCompletionWrapper)->OpenAIChatCompletionWrapper:source.messages.append({"role":"user","content":"Please be more polite."})source.execute()returnsource