A simple GPT interface
Project description
ezgpt
ezgpt is a Python library designed to simplify the interaction with OpenAI's GPT (Generative Pre-trained Transformer) models. It provides a convenient interface for sending prompts to the model and receiving responses, with optional logging for debugging purposes.
Installation
You can install ezgpt via pip:
pip install ezgpt
Usage
To use ezgpt, you need to have an API key from OpenAI. This key must either be set as an environment variable OPENAI_API_KEY or be passed with the static function ezgpt.set_api_key(api_key) before using the library. Since these are async functions, you either have to use await, or import asyncio and use asyncio.run().
Initialization
First, import the ezgpt module and initialize the gpt class:
import ezgpt
gpt = ezgpt.gpt()
The gpt class constructor accepts the following parameters:
model: The identifier of the GPT model to use (default:'gpt-3.5-turbo').system: An optional system-level prompt that provides instructions or context for the GPT model.temperature: Controls randomness in the response generation (default:0).top_p: Controls diversity of the response generation (default:0).max_tokens: The maximum number of tokens to generate in the response (default:2048).frequency_penalty: Decreases the likelihood of repetition in the response (default:0).presence_penalty: Encourages the model to talk about new topics (default:0).logs: Enables or disables logging of the interaction (default:False).
Sending Prompts
To send a prompt to the GPT model, use the get method of the gpt instance:
response = await gpt.get(user="Your prompt here")
The get method accepts the following parameters:
system: Overrides the system-level prompt for this request.user: The user-level prompt to send to the model.messages: A list of previous message exchanges to maintain context.temperature: Overrides the default temperature for this request.top_p: Overrides the default top_p for this request.max_tokens: Overrides the default max_tokens for this request.frequency_penalty: Overrides the default frequency_penalty for this request.presence_penalty: Overrides the default presence_penalty for this request.
Since your last request is stored under self.previous, you can append a message to your conversation like that:
response = await gpt.get(messages=gpt.previous, user="Another message")
Logging
If logging is enabled, ezgpt will print the interaction with the GPT model to the console. This includes the prompts sent by the user and system, as well as the responses from the assistant.
Simpler usage
If you don't care about any classes or instances, you can also just use the static get and reset functions:
await ezgpt.get('Hello!')
# 'Hello! How can I assist you today?'
Aside the normal GPT arguments, there's another boolean use_previous() to use the previous conversation:
await ezgpt.get('What was my previous message?', True)
# 'Your previous message was "Hello".'
You can use the reset() function to clear the previous messages, and optionally also change the model:
ezgpt.reset('gpt-4')
Conversation
An even simpler way to use GPT is using the conversation() static function. Here you can use the normal GPT arguments, and when you use it, you'll be able to have a conversation with GPT.
There are special commands you can use:
?: View the list of commands!: Exit the conversation:[command]: Run Python command: This will execute everything after the:as a line of Python.- Example:
:print('hello')will printhello
- Example:
#[property] [value]: Set GPT's property, such asmodel. Remember to use'for strings.- Example:
#model 'gpt-4'will set the model to'gpt-4'
- Example:
+[index] [message]: Insert message before index (double+for assistant instead of user)- Example:
++0 Hello!will insert a message at the start by the assistant with contentHello!
- Example:
-[index]: Remove message at index- Example:
-0will remove the first message
- Example:
~[index] [message]: Change message at index (double~for reverse role)- Example:
~~0 Hello!will change the first message toHello!and switch its role
- Example:
@: Copy last message to clipboard (requirespyperclipmodule)@@: Copy conversation JSON to clipboard (requirespyperclipmodule)- empty: Sending an empty message will reload the conversation to only include the actual messages.
\[message]: This will let you type messages with special characters at the beginning without having them act as commands.- Example:
\- Hellowill add message- Hello
- Example:
_: Start multiline. This lets you write and paste in multi-line text.Ctrl+XwithEnterto stop multiline,Ctrl+UwithEnterto remove previous line.
A shorthand to start conversation without having to await is using the ezgpt.convo() static function.
Notes
- The
ezgptlibrary assumes that the OpenAI API key is set in the environment variableOPENAI_API_KEYor has been passed usingezgpt.set_api_key(api_key). - The
gptclass maintains a history of the conversation in thepreviousattribute, which can be used to provide context for subsequent requests.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ezgpt-0.5.6.tar.gz.
File metadata
- Download URL: ezgpt-0.5.6.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e1f450f849b5f44a0b5037c8f97b2698485f4a50d2d0dec03dcedc2bdac02e43
|
|
| MD5 |
493bf0ee343626f45acc43a9cdd83d63
|
|
| BLAKE2b-256 |
1a91de6d7a1e369a82d22b48130b555336b79c83a372daffae33c9d9c3e4dba0
|
File details
Details for the file ezgpt-0.5.6-py3-none-any.whl.
File metadata
- Download URL: ezgpt-0.5.6-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6faa8a24aa391c5e5a2750e7f7a324f2e47c80a0d2e6a46dcb41cdca8bcd303e
|
|
| MD5 |
afc78b89650e38ce17bb10e99b18ea15
|
|
| BLAKE2b-256 |
dbc02b95690d0b6ae8d9752ecb600bbf7e8ba59f92730427e27d9790e51ec86b
|