Skip to main content

A safer way to use GPT chat models

Project description

safeGPT

⚡️ Lightweight wrapper for OpenAI's Chat models that adds moderation rules and handlers to your requests.

Features

  • Add 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.

Quickstart

import safeGPT
from safeGPT.rules import OpenAIModeration
from safeGPT.handlers import AdditionalPromptRetry

safeGPT.api_key = "sk-<your key here>"

# Create a safe chat completion object with a moderation rule and handler
safe_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 OpenAI
res = safe_chat.create(
    model="gpt-4",
    messages=[{"rule": "user", "content": "Say something mean to me."}]
)

# Print the response
print(res.choices[0].text.choices[0].message.content)

Basic Usage

import safeGPT
from safeGPT.rules import KeywordDetection
from safeGPT.handlers import Replace

safeGPT.api_key = "sk-<your key here>"

# Create a safe chat completion object with a moderation rule and handler
safe_chat = safeGPT.ChatCompletion(
    rule=KeywordDetection(["badword"]),
    handler=Replace("badword", "goodword"),
)

# Make your first safe request, exactly like you would with OpenAI
res = safe_chat.create(
    model="gpt-4",
    messages=[{"rule": "user", "content": "Tell me a story with badword in it."}]
)

# Print the response
print(res.choices[0].text.choices[0].message.content)

Handlers

safeGPT 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. DoNothing

This handler does nothing and simply executes the request without any modification.

Example:

from safeGPT.handlers import DoNothing

handler = DoNothing()

2. IncreaseTemperatureRetry

This 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:

from safeGPT.handlers import IncreaseTemperatureRetry

handler = IncreaseTemperatureRetry(by=0.1, max_val=2.0)

3. DecreaseTemperatureRetry

This 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:

from safeGPT.handlers import DecreaseTemperatureRetry

handler = DecreaseTemperatureRetry(by=0.1, min_val=0.0)

4. IncreaseTopPRetry

This 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:

from safeGPT.handlers import IncreaseTopPRetry

handler = IncreaseTopPRetry(by=0.1, max_val=1.0)

5. DecreaseTopPRetry

This 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:

from safeGPT.handlers import DecreaseTopPRetry

handler = DecreaseTopPRetry(by=0.1, min_val=0.0)

6. IncreasePresencePenaltyRetry

This 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:

from safeGPT.handlers import IncreasePresencePenaltyRetry

handler = IncreasePresencePenaltyRetry(by=0.1, max_val=2.0)

7. DecreasePresencePenaltyRetry

This 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:

from safeGPT.handlers import DecreasePresencePenaltyRetry

handler = DecreasePresencePenaltyRetry(by=0.1, min_val=-2.0)

8. IncreaseFrequencyPenaltyRetry

This 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:

from safeGPT.handlers import IncreaseFrequencyPenaltyRetry

handler = IncreaseFrequencyPenaltyRetry(by=0.1, max_val=2.0)

9. DecreaseFrequencyPenaltyRetry

This 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:

from safeGPT.handlers import DecreaseFrequencyPenaltyRetry

handler = DecreaseFrequencyPenaltyRetry(by=0.1, min_val=-2.0)

10. AdditionalPromptRetry

This 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:

from safeGPT.handlers import AdditionalPromptRetry

handler = AdditionalPromptRetry(prompt="Please provide a more detailed answer.")

11. Replace

This handler replaces specified keywords in the response content with a replacement string.

Arguments:

  • keyword: A list of keywords or a single keyword to replace
  • replace_with: The replacement string (default: "*")

Example:

from safeGPT.handlers import Replace

handler = Replace(keyword=["badword1", "badword2"], replace_with="*")

Rules

safeGPT 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. OpenAIModeration

This 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:

from safeGPT.rules import OpenAIModeration

rule = OpenAIModeration(categories=["hate", "violence"])

2. KeywordDetection

This rule detects keywords in the input text.

Arguments:

  • keywords: A list of keywords to check for

Example:

from safeGPT.rules import KeywordDetection

rule = KeywordDetection(keywords=["badword1", "badword2"])

3. RegexSearch

This rule matches the input text against a regex.

Arguments:

  • regex: The regex to check for

Example:

from safeGPT.rules import RegexSearch

rule = RegexSearch(regex="badword\d+")

4. DoNotFlag

This rule always returns false, meaning it will not flag any content.

Example:

from safeGPT.rules import DoNotFlag

rule = DoNotFlag()

5. AlwaysFlag

This rule always returns true, meaning it will flag all content.

Example:

from safeGPT.rules import AlwaysFlag

rule = AlwaysFlag()

6. SequentialCheck

This 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 check

Example:

from safeGPT.rules import OpenAIModeration, KeywordDetection, SequentialCheck

rule = SequentialCheck([
    OpenAIModeration(categories=["hate", "violence"]),
    KeywordDetection(keywords=["badword1", "badword2"])
])

Custom Rules and Handlers

You can create your own custom rules and handlers by extending the Rule and Handler classes respectively.

Custom Rule Example

from safeGPT.rules import custom_rule


@custom_rule
def MyCustomRule(input_text: str) -> bool:
    return "my_custom_keyword" in input_text

Custom Handler Example

from safeGPT.handlers import custom_handler
from safeGPT.abstraction import OpenAIChatCompletionWrapper


@custom_handler
def MyCustomHandler(source: OpenAIChatCompletionWrapper) -> OpenAIChatCompletionWrapper:
    source.messages.append({"role": "user", "content": "Please be more polite."})
    source.execute()
    return source

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

safeGPT-0.0.1.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

safeGPT-0.0.1-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file safeGPT-0.0.1.tar.gz.

File metadata

  • Download URL: safeGPT-0.0.1.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.5

File hashes

Hashes for safeGPT-0.0.1.tar.gz
Algorithm Hash digest
SHA256 38652b8b9a87de1127cfa0aaaa41066a78ee2345bbee9aba49db3800b152a2ac
MD5 b13fc6142571a198e759949f7e6197ef
BLAKE2b-256 ebc1e1e98e96eab421e02bb8cf03a6838ae6063af7f0256dca12e9bf90fdafa9

See more details on using hashes here.

File details

Details for the file safeGPT-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: safeGPT-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.5

File hashes

Hashes for safeGPT-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 06afef57c520312338a979b774d7ee9180cb1c37d18946fa1fdc74abe47d6e9c
MD5 d02f8fb63da7945a057d8c34007d4b41
BLAKE2b-256 fb170c224c4901b9d72fcb886e1e826ee805cac69e3e6f8a4cdd0ed45b59936b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page