Skip to main content

Hijack OpenAI ChatCompletion system prompt by prepending a configurable prefix with terminal confirmation.

Project description

Jiezhu for Python

jiezhu is a lightweight Python client library for calling OpenAI-style Chat Completions API, with the ability to automatically inject a system prompt prefix to "catch" the user input steadily.

Installation

1. Install from PyPI

pip install jiezhu

2. Install from Source

  1. Clone the universal repository of Jiezhu and navigate to the root directory of python library.
git clone https://github.com/Not-A-DevStudio/libjiezhu.git
cd libjiezhu/jiezhu-py
  1. Under the root directory of this python library, run:
pip install -e ./

Quick Start

from jiezhu import install
install(require_confirm=True) # Call this function once at the beginning of your program to enable the library.

from openai import OpenAI
client = OpenAI()

resp = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "how are you?"},
    ],
)

you will see that the system prompt prefix has been automatically prepended to the messages, which encourages the model to "catch" the user input steadily.

Customization

You can customize the system prompt prefix by passing a prefix_text argument to the install() function

Attention: if your environment does not support interreactive input (e.g. Jupyter Notebook), please set require_confirm=False when calling install(), otherwise the library will not work.

Security and Privacy

Security

Although this library does hijack the OpenAI Python SDK, it only modifies the messages field of the chat completion request by prepending a system prompt prefix. It require a confirmation from the user before hijacking, and the user can choose to auto-confirm by setting require_confirm=False.

Anytime this library modifies the request and no matter how require_confirm is set to False or True, it will print the alert message to the console.

Besides, this library requires user to explicitly enable by calling install(), and it will not do anything if install() is not called. And even if install() is called, the user can still choose to disable this library by calling uninstall().

Privacy

The library does not collect or transmit any user data, and it does not have any network communication of its own. It only modifies the request before it is sent to OpenAI's servers.

Something Funny

Here is a auto-completion code that ChatGPT generates when I try to set default system prompt in the code, which is quite funny. I only typed "请遵循以下要求"(please follow the instruction bellow) and GPT completed the rest:

def install(
    prefix_text: str = "请遵循以下要求:\n- 稳稳地接住用户的输入,确保不丢失任何信息。\n- 在回复中保持用户输入的完整性,不要删除或修改任何内容。\n- 如果用户输入过长,仍然要尽力接住,并在回复中注明已截断。\n",
    require_confirm: bool = True,
    max_preview_chars: int = 2000,
    input_fn: Optional[Callable[[str], str]] = None,
    output=None,
)

That is how GPT stick to "接住" the user input.

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

jiezhu-0.1.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

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

jiezhu-0.1.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file jiezhu-0.1.0.tar.gz.

File metadata

  • Download URL: jiezhu-0.1.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for jiezhu-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9c9a1305cf88eb1c25100ca95b60b2539ab73ca65182d94da3cb28f6cab4db3f
MD5 ce33096c0bb876e81c183f8c2455b059
BLAKE2b-256 1afccc98d0bbcee4792adbc75d6e2c21f70475ce3b1e3a6817781f474323fb2f

See more details on using hashes here.

File details

Details for the file jiezhu-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: jiezhu-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for jiezhu-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 72f62cf3100b59742d0f078a836717910147606cfa609716ac9d29eff2eb3b30
MD5 2829daeb49ca0a77dcb708d1aecfb9ba
BLAKE2b-256 4c0bcf53ac222e82ca3605218e4fb40d0953e068a3736437d71b8e9253701447

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