OpenAI GPT integration project using Python.
This project provide the below features:
OpenAI Chat- "Given a list of messages comprising a conversation, the model will return a response."
OpenAI Exception Handler- Consider specific exception scenarios to handle it.
Token Counter- Counts how many tokens exist in the input, estimate the output tokens, and then select a proper model to use it.
Chatbot UI- Frontend application in HTML, CSS and Javascript to interaction with the solution.
OpenAI Assistants- "The Assistants API allows you to build AI assistants within your own applications. An Assistant has instructions and can leverage models, tools, and knowledge to respond to user queries."
- Reference: https://platform.openai.com/docs/assistants/overview
Thread History- Assistant will consider or not the previous conversation in the future responses.
Knowledge Retrieval- "Retrieval augments the Assistant with knowledge from outside its model, such as proprietary product information or documents provided by your users. Once a file is uploaded and passed to the Assistant, OpenAI will automatically chunk your documents, index and store the embeddings, and implement vector search to retrieve relevant content to answer user queries."
- Reference: https://platform.openai.com/docs/assistants/tools/knowledge-retrieval
Function Calling- "Similar to the Chat Completions API, the Assistants API supports function calling. Function calling allows you to describe functions to the Assistants and have it intelligently return the functions that need to be called along with their arguments. The Assistants API will pause execution during a Run when it invokes functions, and you can supply the results of the function call back to continue the Run execution."
- Reference: https://platform.openai.com/docs/assistants/tools/function-calling
Download the project using the below GIT command:
$ git clone https://github.com/diogoaltoe/openai-python.git
Using the Terminal, execute the command below.
On Windows:
.venv\Scripts\activate
On Unix or MacOS:
source .venv/bin/activate
Use pip to install the project dependencies listed in the requirements.txt file:
pip install -r requirements.txt
To publish the static assets, execute the command below:
python manage.py collectstatic
Create inside root folder a .env file with the below variables:
SECRET_KEY=<your_django_key>
OPENAI_API_KEY=<your_openai_api_key>
OPENAI_API_ASSISTANT_ID=<your_openai_api_assistant_id>
To use OpenAI API, you need to:
- Go to OpenAI Platform Playground (https://platform.openai.com);
- In the left menu, click on
API keysoption; - Click on
+ Create new secret keybutton; - Fill the
Namefield and click onCreate secret keybutton; - Copy the
keyand set theOPENAI_API_KEYenvironment variable inside the created.envfile (seeEnvironment Variables).
To use Assistants feature, you need to:
- Go to OpenAI Platform Playground (https://platform.openai.com/playground);
- Select
Assistantsoption; - Create a new assistant;
- In
Namefield, give a proper name for it; - In
Instructionsfield, add these instructions:
You are an e-commerce customer service chatbot.
You should not answer questions that are not related to e-commerce!
- Copy the
Assistant IDthat is below to theNamefield and set theOPENAI_API_ASSISTANT_IDenvironment variable inside the created.envfile (seeEnvironment Variables).
To work with Retrieval feature, you need to:
- Go to OpenAI Platform Playground (https://platform.openai.com/playground);
- Select
Assistantsoption; - In
TOOLSoption:- Enable
Retrieval; - Upload the 2 files existing inside
chatbot/datafolder:- info.md
- policy.md
- Enable
To work with Functions feature, you need to:
- Go to OpenAI Platform Playground (https://platform.openai.com/playground);
- Select
Assistantsoption; - In
TOOLSoption:- In
Functions, click in+Functionbutton; - Open the
validate_promotional_code.jsonfile insidechatbot/data, copy the content and paste it in the function field; - Then, click on
Savebutton; - It should save the new function and show below
Functionsthevalidate_promotional_codefunction name.
- In
Run the project using the command below:
python manage.py runserver
Then access the application, clicking in the link: http://127.0.0.1:8000/
In the top bar of EcoMart Chat, in the center, you have the option of using the bot in Chat or Assistant mode.
Still at the top right, you have the option to clear the conversation history (Trash icon). If you are using the bot in Assistant mode, it will lose the past reference of the conversation.
In the bottom bar, you can enter your message and click the Send button to send the message.
For more all the libraries, check requirements.txt file.
This project is licensed under the MIT License.



