This Python script enables interaction with the OpenAI GPT-3 assistant via the OpenAI API to retrieve information from uploaded documents using a vector store.
Before running the script, ensure you have the following installed:
- Python 3.x
- Required Python packages (
openai
,python-dotenv
): Install usingpip install openai python-dotenv
-
Obtain OpenAI API Key:
- Sign up for OpenAI and obtain your API key.
- Set your OpenAI API key in the environment variable
OPENAI_API_TOKEN
.
-
Assistant and Vector Store IDs:
- Replace
assistant_id
andvs_ID
in the script with your specific OpenAI assistant ID and vector store ID. - Setup Vector Store:
- Create or use an existing vector store on OpenAI.
- Upload documents containing information you want the assistant to retrieve into the vector store.
- Replace
-
Run the Script:
- Execute the script by running
python main.py
in your terminal or IDE.
- Execute the script by running
-
Start Conversation:
- Upon running, the script will prompt you to enter a message.
- Type you and press
Enter
to send it to the OpenAI assistant.
-
Exit Conversation:
- To exit the conversation, type
exit
and pressEnter
.
- To exit the conversation, type
load_openai_assistant(assistant_id, vs_ID)
: Initializes the OpenAI assistant and creates a new thread for interaction.wait_on_run(run, thread)
: Waits for the assistant run to complete or progress to the next stage.get_assistant_response(thread, assistant_id, user_input)
: Sends user input to the assistant and retrieves the response.
- The script handles API-related errors such as
APIError
,APIConnectionError
, andRateLimitError
gracefully.
For more details about the OpenAI API and assistants, refer to the OpenAI API Documentation.
$ python your_script_name.py
Welcome! Let's start a conversation with the OpenAI assistant.
You: Hello
Assistant: Hi there! How can I assist you today?
You: Retrieve information from document
Assistant: Please provide specific details or keywords from the document you need information about.
You: exit
Exiting...