Skip to main content

A tool collection to facilitate the use of ChatGPT API

Project description

chatmanager

chatmanager provides various interfaces to facilitate the use of OpenAI API like ChatGPT. It supports functions including API key management, session managementm, and interaction with ChatGPT with multi-threads, and etc.

Install

Download this repository:

git clone https://github.com/PeiweiHu/chatmanager.git

Build and install locally:

cd chatmanager 
./script/build.sh

Quickstart

Here are two exmples to show how to use chatmanager.

Simple example

from chatmanager import ChatManager, ChatMessage

cm = ChatManager()
cm.add_key('key-name', 'sk-xxxx')
msg = ChatMessage()
msg.push_user("How are you?")
response = cm.send(msg)
print(response.get_msg())

More detailed example

from chatmanager import ChatManager, ChatSetup, ChatMessage, ChatResponse

"""
ChatManager is the main interface that the user uses to send prompts and get the
response. 

ChatSetup stores the configurations.

ChatMessage is the interface to construct the prompts sent to the openai API.

ChatResponse parses and stores the responses from openai API.
"""

ChatSetup.api_base = "https://api.openai.com/v1" # default api_base
cm = ChatManager()

"""
Before communicating with the openai API, set a session. The session is used
to store the <ChatMessage, ChatResponse> pairs during the communication.

The following code set a session named session1.
"""
cm.set_session("session1")

"""
The following code adds new API keys, then sets the strategy for key usage.

Currently there are four strtegies:

    1. default/roll-polling: use in turn
    2. random: choose a random key
    3. least-used: choose the key that has been used the least
    4. rest-time: choose the key that has not been used for the longest time
"""
cm.add_key("key_name1", "sk-xxx")
cm.add_key("key_name2", "sk-yyy")
cm.keys.set_strategy("rest-time")

"""
To construct your prompt sent to ChatGPT (or other models), you can use ChatMessage.
"""
msg = ChatMessage()
msg.push_system("You are an assistant for programming")
msg.push_user("Can you write a loop in C language?")
response = cm.send(msg) # response: Optional[ChatResponse]

if response:
    msg1 = ChatMessage()

    """
    By the following two lines of code, we construct the <msg> and the
    response of <msg> in <msg1>.
    """
    msg1.push_msg(msg.drain())
    msg1.push_assistant(resposne.get_msg())

    new_query = "Is the above loop correct?"
    msg1.push_user(new_query)

    response = cm.send(msg1)

"""
You can also send multiple ChatMessage one time with multi-threads.
"""
msgs = [msg for _ in range(10)]
response_lst = cm.send(msgs, thread_num = 10) # response_lst: List[Optioanl[ChatResponse]]

"""
All the above communication with openai is saved in the session1.
You can export it.

You can also define the export manner. Please check the method export_session of
ChatManager.
"""
exported_json_str = cm.export_session("session1")
with open('log', 'w') as w:
    w.write(exported_json_str)

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

chatmanager-0.0.1.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

chatmanager-0.0.1-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file chatmanager-0.0.1.tar.gz.

File metadata

  • Download URL: chatmanager-0.0.1.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for chatmanager-0.0.1.tar.gz
Algorithm Hash digest
SHA256 180e05c851868cc554c146cea78e71b40d087136015e7f1acda4ad38ac1706ad
MD5 4b4b0bdcc4ac75b80a50514c93c6f4b4
BLAKE2b-256 0cc8f108674ccd7508a9d7d681e2e87a0c0058e733e68bd945988dc1c8317ed6

See more details on using hashes here.

File details

Details for the file chatmanager-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: chatmanager-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for chatmanager-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f6eeac212b78db616fc9dbe8be2b807c9d305a7da80267f4b9bdb00b84d97c5f
MD5 3f172f4e457591ecc2e063c7c1a3a3c7
BLAKE2b-256 cefc89df5ab51e05d6e00e202b2485a946e4092c6dab0cc065a6c3ea4876d73f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page