This Python script integrates with OpenAI's API to create a conversational assistant. It manages messaging threads, sends user inputs, and receives responses from the OpenAI assistant. The script is designed for interactive use and includes functions for pretty-printing responses.
- Python 3.x
openaiPython package
-
Install the OpenAI Python package:
pip install openai
-
Clone or download this script to your local machine.
-
Add the file path to your Python system path:
sys.path.append('C:/Users/YourUsername/PathToScript')
Create a local constants.py file with your documentation it should look something like this:
from openai import OpenAI
assistant_id = 'Inster your assistant's ID here'
api_key = "Instert your API Key here"
client = OpenAI(api_key=api_key)Run the script using Python:
python main.pyYou will be prompted to enter your message. After entering a message, the script will handle the communication with the OpenAI API and display the assistant's response.
submit_message: Submits a message to the OpenAI API.get_response: Retrieves messages from a thread.create_thread_and_run: Creates a new thread and submits the initial user message.pretty_print: Formats and prints messages for readability.wait_on_run: Waits for the OpenAI API to process a submitted message.main: Main function to start the conversational assistant.
MIT
Note: Replace placeholders like YourUsername and PathToScript with your actual username and script path. Also, ensure you have a valid OpenAI API key.