A simple (for the moment) package that allows you to implement the back-end of a ai chat-bot with openai models in python.
Project description
ChatWeaver
ChatWeaver is a Python library that simplifies the implementation of chatbots powered by OpenAI models. Designed with developers in mind, it provides powerful features and an intuitive interface to enhance the development of conversational AI.
Features
- Chat History Management: Easily track and manage conversation context.
- Message Templates: Automatically remember and include previous messages in prompts.
- File Integration: Add images and PDF files to your prompts seamlessly.
- Custom Model Support: Compatible with various OpenAI models.
- Extensibility: Flexible architecture for scalable chatbot solutions.
Installation
Install ChatWeaver using pip:
pip install chatweaver
Quick Start
Here’s how you can get started with ChatWeaver:
import chatweaver as cw
model = cw.Model(
model="gpt-4o",
api_key="<Your OpenAI API key here>"
)
bot = cw.Bot(
rules=cw.chat_weaver_rules["basic"],
name="AiBot",
cw_model=model
)
chat = cw.Chat(
replies_limit=10,
user="Diego",
cw_bot=bot
)
prompt = "Hi how are you?"
print(chat.get_response(prompt=prompt))
Include images and files
You may also include images and files in the prompt by specifying their file paths.
prompt = "Describe the content of the attached image."
image = "path\\to\\image.png"
print(chat.get_response(prompt=prompt, image_path=image))
Implementation of Model Rules
ChatWeaver allows the customization of chatbot behavior through a set of predefined rules, defined in the variable chat_weaver_rules. These rules determine the role, style, format, and ethical guidelines the bot must follow. Each rule is designed to suit specific scenarios, enhancing interactivity and consistency in the conversation.
Available Rules
- basic: Sets the bot with scientifically accurate and reliable goals. Includes:
- No text formatting.
- Friendly and respectful communication.
- Complete and contextual responses.
- Strict ethical standards.
- default: Optimizes the bot to keep the conversation flowing in a JSON format. Main features:
- Single-line responses using
\n. - JSON structure with keys like
reasoning,reply,result.
- Single-line responses using
- informal_chat: Adjusts the bot for informal conversations. Key features:
- Friendly and conversational language.
- Simple JSON response format.
- formal_chat: Adapts the bot for formal conversations. Includes:
- Polite and respectful communication.
- Formal JSON structure.
- formal_email: Optimizes the bot for formal email exchanges. Features:
- Adherence to email structure with greetings, body, and closing.
- Detailed JSON responses.
Rule Implementation Example
To use a specific rule in your bot:
import chatweaver as cw
model = cw.Model(api_key="<Your OpenAI API key here>")
bot1 = cw.Bot(rules=cw.chat_weaver_rules["basic"], cw_model=model)
bot2 = cw.Bot(rules=cw.chat_weaver_rules["default"], cw_model=model)
prompt = "Hello, how are you?"
response1 = bot1.response(prompt=prompt)["content"]
response2 = bot2.response(prompt=prompt)["content"]
print(f"Response from 'bot1': {response1}\n")
print(f"Response from 'bot2': {response2}")
Saving and retrieving ChatWeaver objects from files
A ChatWeaver object can be saved simply by storing the result of its repr() method. To restore the object, use the load() method.
# Saving the chat
with open("path\\to\\file", "w") as f:
f.write(repr(chat))
# Loading the chat
with open("path\\to\\file", "r") as f:
chat = cw.load(f.read())
Requirements
- Python 3.9 or above.
- OpenAI Python library (openai).
New Updates in the Latest Version of the Python Library (0.2.1)
- Chat messages are now stored as
TextNodeobjects and can be converted back to dictionaries usingdict(<TextNode object>). - Each
TextNodenow includes its own creation date and time. - Each chat session now has its own creation date and time.
- Each chat session now has its own
costin tokens. - Introduced a new
load()function that takes the result ofrepr()from aChatWeaverobject and returns the object itself. This is useful for savingChat/Bot/Modeldata in separate files and restoring them when needed.
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 chatweaver-0.2.1.tar.gz.
File metadata
- Download URL: chatweaver-0.2.1.tar.gz
- Upload date:
- Size: 13.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a8e96b4e052a6625c190b389bc7eb7852c1f2678ddf27f2bc595202964e09f19
|
|
| MD5 |
e1789f337e0d45e9eb8db4f476eabbc0
|
|
| BLAKE2b-256 |
edf537f79a51d36ce70283545fee532e5be16e11eda17abb69468b2dff8a5fcc
|
File details
Details for the file chatweaver-0.2.1-py3-none-any.whl.
File metadata
- Download URL: chatweaver-0.2.1-py3-none-any.whl
- Upload date:
- Size: 11.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd4b13924605161d99728698f003185abdde6cd68bf0dbec4f53ecb82a9e6ee4
|
|
| MD5 |
f87d46aa24a3fa0546ea03d23a2a6812
|
|
| BLAKE2b-256 |
3dc4a912c4e4161142e58adffed7747f704e7e1ae29cadfae88a0ccfdace9fec
|