# Simple Wrapper For The ChatGPT API
This module is designed for asynchronous usage and provides a simple interface to the OpenAI ChatGPT API
It also provides a command-line interface for interactive usage.
While it does provide synchronous methods, it is recommended to use the asynchronous methods for better performance.
## Features
- Asynchronous API
- Get/Stream responses
- Save/Load session history
- Change parameters on the fly
- Embed images in messages
- Register functions to `tool_calls` with ease
- Interactive command-line interface
## Installation
```bash
pip install -U ngptbot
```
## Usage
### Command Line
#### Configuration
`gptbot` requires a model and an API key to be set.
You can use config file, command parameters or interactive input to set these values.
By default, `gptbot` will look for files named `config.json` and `session.json` in the user config and cache directory.
```bash
gptbot -c /path/to/config.json # Use a config file
gptbot -m "gpt-3.5-turbo" # Set model, input API key interactively
gptbot -k /path/to/config.json # create config file
```
#### Parameters
- `-c`, `--config`: Path to a JSON file
- `-m`, `--model`: Model name
- `-k`, `--create-config`: Create a config file interactively
- `-s`, `--session`: Session history file
- `-V`, `--version`: Show version
**_Precedence_**: Interactive input > Command parameters > Config file
#### Interactive Mode
This mode mimics a chat interface. You can type your message and get a response.
Commands are started with a `/` and are **case-sensitive**.
- `/exit`: Exit the program
- `/save <path>`: Save the session history to a file
- `/load <path>`: Load a session history from a file
- `/rollback <step>`: Rollback to the previous state
- `/clear`: Clear the session history
- `/role`: Switch role
- `/model`: Switch model
- `/tokens`: Show used tokens
- `/help`: Show help
To embed an image, insert `#image(<url>)` in your message.
Use double `#` to escape embedding.
```Python
await bot.send("""
What are these?
#image(https://example.com/image.png)
#image(file:///path/to/image.png)
#image(base64://<base64-encoded-image>)
""")
```
All URLs will be downloaded and embedded as base64-encoded images.
### API
```python
import asyncio
import gptbot
bot = gptbot.Bot(model="gpt-3.5-turbo", api_key="your-api-key")
async def main():
response = await bot.send("Hello, how are you?")
print(response)
async for r in bot.stream("I'm fine, thank you."):
print(r, end='')
async for res in bot.stream_raw("What is the answer to life, the universe, and everything?"):
"""
stream_raw returns a stream of `FullChunkResponse`.
It contains all raw data from the API.
"""
bot.send_sync("Goodbye!") # Synchronous version of `send`
# Bot class is stateless, to keep track of the conversation, use the `Session` class
session = bot.new_session()
# You can use session just like the bot
async for r in session.stream("Hello!"):
print(r, end='')
print(await session.send("Goodbye!"))
asyncio.run(main())
```
### Registering Functions
Functions are under `Bot.funcs` and can be registered with the `add_func` decorator.
```python
@bot.add_func
def my_func(data: str, default:int = 123): # parameters must be annotated, variadic parameters are not supported
"""Documentation goes here"""
return data # return value must be stringifiable
```
Raw data
{
"_id": null,
"home_page": "https://github.com/VermiIIi0n/GPTBot",
"name": "ngptbot",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.12",
"maintainer_email": null,
"keywords": "utilities",
"author": "VermiIIi0n",
"author_email": "dungeon.behind0t@icloud.com",
"download_url": "https://files.pythonhosted.org/packages/1f/f1/80d6f4059132776474d882e7a50bd66f5747e98b4b0cf577aecbe5aa4a82/ngptbot-0.3.5.tar.gz",
"platform": null,
"description": "# Simple Wrapper For The ChatGPT API\n\nThis module is designed for asynchronous usage and provides a simple interface to the OpenAI ChatGPT API\n\nIt also provides a command-line interface for interactive usage.\n\nWhile it does provide synchronous methods, it is recommended to use the asynchronous methods for better performance.\n\n## Features\n\n- Asynchronous API\n- Get/Stream responses\n- Save/Load session history\n- Change parameters on the fly\n- Embed images in messages\n- Register functions to `tool_calls` with ease\n- Interactive command-line interface\n\n## Installation\n\n```bash\npip install -U ngptbot\n```\n\n## Usage\n\n### Command Line\n\n#### Configuration\n\n`gptbot` requires a model and an API key to be set.\n\nYou can use config file, command parameters or interactive input to set these values.\n\nBy default, `gptbot` will look for files named `config.json` and `session.json` in the user config and cache directory.\n\n```bash\ngptbot -c /path/to/config.json # Use a config file\ngptbot -m \"gpt-3.5-turbo\" # Set model, input API key interactively\ngptbot -k /path/to/config.json # create config file\n```\n\n#### Parameters\n\n- `-c`, `--config`: Path to a JSON file\n- `-m`, `--model`: Model name\n- `-k`, `--create-config`: Create a config file interactively\n- `-s`, `--session`: Session history file\n- `-V`, `--version`: Show version\n\n**_Precedence_**: Interactive input > Command parameters > Config file\n\n#### Interactive Mode\n\nThis mode mimics a chat interface. You can type your message and get a response.\n\nCommands are started with a `/` and are **case-sensitive**.\n\n- `/exit`: Exit the program\n- `/save <path>`: Save the session history to a file\n- `/load <path>`: Load a session history from a file\n- `/rollback <step>`: Rollback to the previous state\n- `/clear`: Clear the session history\n- `/role`: Switch role\n- `/model`: Switch model\n- `/tokens`: Show used tokens\n- `/help`: Show help\n\nTo embed an image, insert `#image(<url>)` in your message.\nUse double `#` to escape embedding.\n\n```Python\nawait bot.send(\"\"\"\nWhat are these?\n#image(https://example.com/image.png)\n#image(file:///path/to/image.png)\n#image(base64://<base64-encoded-image>)\n\"\"\")\n```\n\nAll URLs will be downloaded and embedded as base64-encoded images.\n\n### API\n\n```python\nimport asyncio\nimport gptbot\n\nbot = gptbot.Bot(model=\"gpt-3.5-turbo\", api_key=\"your-api-key\")\n\nasync def main():\n response = await bot.send(\"Hello, how are you?\")\n print(response)\n\n async for r in bot.stream(\"I'm fine, thank you.\"):\n print(r, end='')\n\n async for res in bot.stream_raw(\"What is the answer to life, the universe, and everything?\"):\n \"\"\"\n stream_raw returns a stream of `FullChunkResponse`.\n It contains all raw data from the API.\n \"\"\"\n\n bot.send_sync(\"Goodbye!\") # Synchronous version of `send`\n\n # Bot class is stateless, to keep track of the conversation, use the `Session` class\n session = bot.new_session()\n\n # You can use session just like the bot\n async for r in session.stream(\"Hello!\"):\n print(r, end='')\n\n print(await session.send(\"Goodbye!\"))\n\nasyncio.run(main())\n```\n\n### Registering Functions\n\nFunctions are under `Bot.funcs` and can be registered with the `add_func` decorator.\n\n```python\n@bot.add_func\ndef my_func(data: str, default:int = 123): # parameters must be annotated, variadic parameters are not supported\n \"\"\"Documentation goes here\"\"\"\n return data # return value must be stringifiable\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Wrapper for ChatGPT API",
"version": "0.3.5",
"project_urls": {
"Homepage": "https://github.com/VermiIIi0n/GPTBot",
"Issues": "https://github.com/VermiIIi0n/GPTBot/issues"
},
"split_keywords": [
"utilities"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "e102ad9f0dee973e1685a3e00252acc2759a8c01226ed76f02135a4277a67963",
"md5": "29fb3d46c4b20efbb4598af85eb0b066",
"sha256": "b958fe82657d196c594f8948a25759db299729dedc7a03dd7d02eb84539f3165"
},
"downloads": -1,
"filename": "ngptbot-0.3.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "29fb3d46c4b20efbb4598af85eb0b066",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.12",
"size": 14090,
"upload_time": "2024-06-02T15:46:24",
"upload_time_iso_8601": "2024-06-02T15:46:24.858297Z",
"url": "https://files.pythonhosted.org/packages/e1/02/ad9f0dee973e1685a3e00252acc2759a8c01226ed76f02135a4277a67963/ngptbot-0.3.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "1ff180d6f4059132776474d882e7a50bd66f5747e98b4b0cf577aecbe5aa4a82",
"md5": "12097719313c53de6a7e3643d4bf3898",
"sha256": "95b9221db6b451eacffe378f5761428dbe84d699e20d1db109baacf0621913d1"
},
"downloads": -1,
"filename": "ngptbot-0.3.5.tar.gz",
"has_sig": false,
"md5_digest": "12097719313c53de6a7e3643d4bf3898",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.12",
"size": 15100,
"upload_time": "2024-06-02T15:46:26",
"upload_time_iso_8601": "2024-06-02T15:46:26.872075Z",
"url": "https://files.pythonhosted.org/packages/1f/f1/80d6f4059132776474d882e7a50bd66f5747e98b4b0cf577aecbe5aa4a82/ngptbot-0.3.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-06-02 15:46:26",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "VermiIIi0n",
"github_project": "GPTBot",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "ngptbot"
}