Skip to main content

mychatgpt is a small and useful Python module that provides functions for interacting with OpenAI's GPT models to create conversational agents. This module allows users to have interactive conversations with the GPT models and keeps track of the conversation history in your Python Projects and Jupyter Notebooks.

Project description

PyChatGPT 2.0: Python Module

Open In Colab

pychatgpt is a small and useful Python module that provides functions for interacting with OpenAI's GPT models to create conversational agents. This module allows users to have interactive conversations with the GPT models and keeps track of the conversation history in your Python Projects and Jupyter Notebooks.

Now implemented with vision, hearing and drawing functions.

Installation

Install package with pip:

pip install git+https://github.com/johndef64/pychatgpt.git

To use this module, you need an OpenAI API key. You have to provide your API key when requested once and it will be stored as openai_api_key.txt in your working directory.

Usage

The module provides the following main functions:

  1. op.ask_gpt(prompt, *parameters*):
    This basic function takes a prompt as input and generates a single response from the GPT chosen model. It returns the generated response and update chat_log.json. You can simply use op.ask_gpt(prompt) and keep the default parameters.

  2. op.send_message(message,*parameters*):
    This main function allows for a more interactive conversation with the GPT chosen model. It takes a message as input, generates a response from the model, and updates the conversation history. It also logs the conversation in the chat_log.txt file.
    This function is implemented with GPT vision, Text2Speech and Dall-E functionalities. Use op.send_message(message) keeping the default parameters or change them as function operators:

from pychatgpt import GPT
op = GPT()

op.send_message('Your message goes here',
                model='gpt-3.5-turbo', # choose openai model 
                system='',          # add 'system' instruction
                img = '',           # insert an image path to activate gpt vision
                maxtoken=800,       # max tokens in reply
                temperature=1,      # output randomness [0-2]
                lag=0.00,           # word streaming lag

                create=False,       # image prompt
                dalle="dall-e-2",   # choose dall-e model
                size='512x512',

                play= False,        # play audio response
                voice='nova',       # choose voice (op.voices)
                tts="tts-1",        # choose tts model
				
                save_chat=True,     # update chat_log.txt
                to_clip=False,      # send reply to clipboard
                reinforcement=False,
                
                print_reply=True, 
                print_user=False,
                print_token=True,
                )
op.add_persona('Elon Musk')
op.send_message("""What do you think about OpenAI?""", 'gpt-4o')
op.chat('Your message goes here',
        max=1000,
        img='',
        paste=False,
        clip=True,
        token=False,
        translate = False,
        create=False)
elon = GPT('Elon Musk')
elon.chat("""What do you think about OpenAI?""")
vincent = GPT('Vincent Van Gogh')
vincent.chat("""Tell me what you see. Can you paint it?""", img=vincent.dummy_img)
  1. op.send_image(url,*parameters*) insert in your chat context gpt-vision, activate a multimodal chat
op.send_image(image="https://repo.com/image.jpg",
              message="What’s in this image?",
              system = '',     # add 'system' instruction
              model= "gpt-4o", #"gpt-4-turbo", "gpt-4-vision-preview"
              maxtoken=1000, 
              lag=0.00, printreply=True, to_clip=True)
  1. op.create_image(prompt,*parameters*)
op.create_image(prompt= "a cute kitten",
                model="dall-e-2",
                size='512x512',
                response_format='b64_json',
                quality="standard",
                time_flag=True, show_image=True)
  1. Whisper and Text-to-Speech
op.whisper(filepath, # audio.mp3, audio.wav
           translate = False,
           response_format = "text",
           print_transcriprion = True)

op.text2speech(text,
               voice="alloy",
               filename="speech.mp3",
               model="tts-1",
               speed=1,
               play=False)

voices = ['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer']
response_formats = ["mp3", "flac", "aac", "opus"]
  1. Speak With...
op.speak(who='',  
         message='', 
         system='',  
         voice='nova', 
         language='eng', 
         gpt='gpt-4-turbo', 
         tts= 'tts-1', 
         max=1000, 
         printall=False)

# Use an in-build assistant or any character of your choice, example:
socrates = GPT('Socrates')
socrates.speak('Tell me about the Truth.', 'onyx')

# Endless chat, keyboard controlled
socrates.speak_loop(who='', system='', voice='nova',  gpt='gpt-4o', tts= 'tts-1', max=1000, language='eng', printall=False, exit_chat='stop')
  1. Talk With...
dua = GPT('Dua Lipa')
dua.talk(voice='nova', language='eng', gpt='gpt-4-turbo', tts= 'tts-1', max=1000, printall=False)

# Endless talk, keyboard controlled
dua.talk_loop(who, voice='nova', language='eng', gpt='gpt-4-turbo', tts= 'tts-1', max=1000, printall=False, chat='alt' , exit='shift')
nietzsche = GPT('Friedrich Nietzsche')
nietzsche.talk_loop('onyx')

The module also provides additional utility functions for managing the conversation, such as clearing the chat history, setting a persona, and setting system instructions, save/load chats.

  1. choose_model()
  2. clear_chat()
  3. expand_chat()
  4. save_chat()
  5. load_chat()
  6. load_file()

To set-up multiple conversations or change the API-key, follow the example proposed in pychatgpt_trial.ipynb

In-Build Assistants

op.display_assistants()
from pychatgpt import delamain
# Call an assistant simply by name
delamain.chat('your message',
              gpt='gpt-4o', 
              max = 1000, 
              clip=True)  

#n.b. assistants sends reply to clipboard by default

An extract of the Assistants provided:

Role Assistant Name Reply type
Copilots delamain python (main)
oracle python
roger R
Formatters schematizer bulletpoint
prompt_maker promt
Scientific Assistants galileo markdown
newton python
leonardo text
turing python
penrose text
Characters bestie text
julia text
Translators english text
italian text
portuguese text
japanese text

Notes

The code in this module assumes that the conversation history is stored in a global variable named chat_thread. Use print(op.chat_thread) to show conversation history and op.chat_thread.pop() to remove last interacition. op.send_message('clearchat') to start a new conversation.

Using op.send_message(), the code checks if the total number of tokens exceeds the model's maximum context length (gpt 3.5 turbo-16k: 16,384 tokens). If it does, a warning message indicates that the token limit is being reached and then then the first part of the conversation will automatically be deleted to make room for the next interaction.

Openai-based applications

Some other python applications executable in Terminal that take advantage of openai modulo features:

  • auto-gpt
  • gpt-cli
  • rec-whisper-paste

Author

Written by: JohnDef64

Acknowledgment

This module only facilitates interaction with the OpenAI API and keeps track of it. OpenAI holds all rights to ChatGPT.

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

mychatgpt-0.1.tar.gz (64.3 kB view details)

Uploaded Source

File details

Details for the file mychatgpt-0.1.tar.gz.

File metadata

  • Download URL: mychatgpt-0.1.tar.gz
  • Upload date:
  • Size: 64.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.0

File hashes

Hashes for mychatgpt-0.1.tar.gz
Algorithm Hash digest
SHA256 0ea9168cb81452e50df1478b867808cbb7d57e1d0b3e6a5bd39301d70e63c7a5
MD5 5a9600e6250cdb60f7f325107f6ce3c5
BLAKE2b-256 445ca09ca7b51e3c6b7df11822eb2651d565123a6e9f717334d56c9d9052a210

See more details on using hashes here.

Supported by

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