A Python library for interacting with OpenAI's ChatGPT API.
Project description
ChatAssist
An advanced Python library for handling ChatGPT interactions.
Features
- Save and Resume Conversations: Save conversation history to a file and reload it later.
- Proxy Support: Send API requests through a user-defined proxy.
- Configurable Parameters: Customize settings like
temperature,top_p, andmax_tokens. - Stream Output: Display responses incrementally as they are generated.
- Command-Line Interface: Quickly interact with ChatGPT from the terminal.
- Graphical User Interface: User-friendly GUI for non-technical users.
- Markdown Rendering in GUI: Display formatted text and code blocks in responses.
- Built-in Logging: Log all interactions for debugging or auditing purposes.
- Dynamic Model Selection: Switch between models like GPT-3.5-Turbo or GPT-4.
Installation
From Source
Install the library from the source code:
pip install .
For development mode:
pip install -e .
Verify Installation
Run this command to verify:
python -c "import chatassist; print(chatassist.__version__)"
You should see the version number printed.
Usage
Python API
The ChatAssist library provides a simple API for interacting with ChatGPT.
from chatassist.api import ChatGPTAPI
from chatassist.utils import load_api_key
# Load the API key
api_key = load_api_key("api_key.txt")
# Initialize the ChatGPT API client
api = ChatGPTAPI(api_key=api_key)
# Send a message
response = api.send_message("Hello, ChatGPT!")
print(response)
Saving and Resuming Conversations
Use the Conversation class to manage chat histories:
from chatassist.conversation import Conversation
conversation = Conversation()
conversation.add_message("user", "Hello!")
conversation.add_message("assistant", "Hi there! How can I assist you?")
# Save to a file
conversation.save_to_file("conversation.json")
# Reload the conversation
loaded_conversation = Conversation()
loaded_conversation.load_from_file("conversation.json")
print(loaded_conversation.get_history())
Using Proxies
Pass a proxy configuration to route API requests through a proxy server:
api = ChatGPTAPI(api_key=api_key, proxies={"http": "http://proxy.example.com:8080"})
response = api.send_message("What is Python?")
print(response)
Command-Line Interface
The CLI allows you to interact with ChatGPT directly from the terminal.
Basic Usage
chatassist-cli "What is the capital of France?" --api-key-path api_key.txt
Export Conversation
Save the conversation to a file:
chatassist-cli "Tell me about Python." --export conversation.json
Graphical User Interface
The GUI provides an intuitive way to interact with ChatGPT.
Launching the GUI
Run the following command:
python -m chatassist.gui
Features
-
Type and Send Messages:
- Enter your message in the input field and press "Send".
- Responses are displayed in the chat window.
-
Save and Load Conversations:
- Export the conversation to a file for later use.
- Reload previous conversations using the "Load Conversation" option.
Configuration Parameters
The ChatAssist API allows you to fine-tune interactions with the following parameters:
temperature: Controls the randomness of responses.top_p: Limits responses to the top tokens in cumulative probability.max_tokens: Restricts the number of tokens in the response.model: Allows you to switch between models, e.g.,gpt-3.5-turboorgpt-4.
response = api.send_message(
"Tell me about AI.",
temperature=0.5,
top_p=0.8,
max_tokens=500
)
print(response)
Practical Examples
1. Simple FAQ Bot
questions = ["What is Python?", "What is AI?", "Who created ChatGPT?"]
for question in questions:
response = api.send_message(question)
print(f"Q: {question}
A: {response}
")
2. Programming Assistant
prompt = "Explain the concept of recursion with a Python example."
response = api.send_message(prompt)
print(response)
3. Travel Guide
prompt = "What are the best tourist attractions in Paris?"
response = api.send_message(prompt)
print(response)
4. Math Solver
problem = "What is the result of 123 * 456?"
response = api.send_message(problem)
print(response)
5. Language Translator
prompt = "Translate 'Hello, how are you?' into Spanish."
response = api.send_message(prompt)
print(response)
Testing
Run All Tests
Navigate to the tests/ directory and run:
python -m unittest discover tests
Verify Individual Tests
To run a specific test file:
python tests/test_api.py
Steps to Run Examples
- Clone the repository:
git clone https://github.com/tirotir-ir/chatassist.git
cd chatassist
- Create and fill the api_key.txt file with your OpenAI API key:
echo "your-openai-api-key" > api_key.txt
Replace your-openai-api-key with your actual API key. This file is required for running the examples and communicating with the OpenAI API.
- Navigate to the examples/ directory:
cd examples
4. Run an example script:
python 14_advanced_chatbot_gui.py
Replace 14_advanced_chatbot_gui.py with the desired example script from the examples/ directory.
Troubleshooting
Common Issues
- API Key Not Found:
- Ensure the
api_key.txtfile exists and contains a valid API key.
- Ensure the
- Module Not Found:
- Verify the library is installed using
pip show chatassist.
- Verify the library is installed using
Debugging
Enable detailed logging by adding:
import logging
logging.basicConfig(level=logging.DEBUG)
License
This project is licensed under the MIT License.
Contact and Support
For questions or support, please raise an issue on the project repository or contact the maintainer.
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 chatassist-0.1.0.tar.gz.
File metadata
- Download URL: chatassist-0.1.0.tar.gz
- Upload date:
- Size: 12.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11e4b17c18884c8631f6688e5d13aa7b05acb9de28dcd56209e8e96398c1f744
|
|
| MD5 |
0d14396c84a32d514c6265224e6ce7b0
|
|
| BLAKE2b-256 |
77c916815c2419e2bca099a0c624f3a02055087b1f3d6e490edcb29a5df24ac5
|
File details
Details for the file chatassist-0.1.0-py3-none-any.whl.
File metadata
- Download URL: chatassist-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c58646542ca04a2616767ae09ffa5d23786b375d84af1e540cd0c5cf4e72c1d2
|
|
| MD5 |
e7208dad062e10d54a1507a59c0ae49d
|
|
| BLAKE2b-256 |
65fee9a9699e7916d0c504270191a228163fb1c3e011acbd4011f311a915b99f
|