├── requirements.txt
├── Sample.png
├── .gitignore
├── LEARN.md
├── Code
├── ChatBot.py
├── GuiChatBot.py
├── BotDefinition.py
└── templates
│ └── index.html
├── CONTRIBUTING.md
├── LICENSE
└── README.md
/requirements.txt:
--------------------------------------------------------------------------------
1 | openai
2 | flask
--------------------------------------------------------------------------------
/Sample.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/iSiddharth20/Chatbot-Using-OpenAI-API/HEAD/Sample.png
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_store
2 | venv/
3 | __pycache__/
4 | .vscode/
5 | .ipynb_checkpoints
6 | Icon?
7 | Venv
--------------------------------------------------------------------------------
/LEARN.md:
--------------------------------------------------------------------------------
1 | All details provided in README.md on the Homepage and Comments are Available in the Codebase.
2 |
--------------------------------------------------------------------------------
/Code/ChatBot.py:
--------------------------------------------------------------------------------
1 | # Importing Bot Defination
2 | from BotDefinition import OpenAIBot
3 |
4 | '''Choose whichever model you want to use'''
5 | # engine = "gpt-3.5-turbo"
6 | # engine = "gpt-3.5-turbo-16k"
7 | engine = "gpt-4"
8 |
9 | '''
10 | Creating the ChatBot
11 | Each Object of "OpenAIBot(engine)" will retain the conversation history and context unless the session is terminated
12 | '''
13 | chatbot = OpenAIBot(engine)
14 |
15 | while True:
16 | # Get Prompt from User
17 | prompt = input()
18 |
19 | # User can stop the chat by sending 'End Chat' as a Prompt
20 | if prompt.upper() == 'END CHAT':
21 | break
22 |
23 | # Generate and Print the Response from ChatBot
24 | response = chatbot.generate_response(prompt)
25 | print(response)
--------------------------------------------------------------------------------
/Code/GuiChatBot.py:
--------------------------------------------------------------------------------
1 | # This is a web version of ChatBot.py, which uses Flask to create a web app.
2 |
3 | # Importing Libraries
4 | from flask import Flask, render_template, request
5 | from BotDefinition import OpenAIBot
6 |
7 | # Creating the Flask App
8 | app = Flask(__name__)
9 |
10 | # Importing Bot Defination
11 | chatbot = OpenAIBot("gpt-4")
12 |
13 | @app.route('/')
14 | def index():
15 | return render_template('index.html')
16 |
17 | @app.route('/chat', methods=['POST'])
18 | def chat():
19 | # Get Prompt from User
20 | prompt = request.form['prompt']
21 |
22 | # User can stop the chat by sending 'End Chat' as a Prompt
23 | if prompt.upper() == 'END CHAT':
24 | return 'END CHAT'
25 |
26 | # Generate and Print the Response from ChatBot
27 | response = chatbot.generate_response(prompt)
28 | return response
29 |
30 | if __name__ == '__main__':
31 | app.run(debug=True)
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contributing to [Chatbot-Using-OpenAI-API](https://github.com/iSiddharth20/Chatbot-Using-OpenAI-API)
2 |
3 | Thank you for considering contributing to [Chatbot-Using-OpenAI-API](https://github.com/iSiddharth20/Chatbot-Using-OpenAI-API)! Your Effort and Contributions are crucial for the success of this project and greatly appreciated. Below you'll find the process and guidelines for contributing.
4 |
5 | ### Pull Requests
6 | - Fork the repository.
7 | - Include descriptive comments in code.
8 | - Include descriptive commit messages.
9 | - Use present tense ("Add feature" not "Added feature")
10 |
11 | ### Code Styleguide
12 | - Follow the coding style present in the existing codebase (indentation, comments style, etc.).
13 | - Include comments explaining why certain pieces of code were implemented.
14 | - Write tests for the new code you're submitting.
15 |
16 | ## Acknowledgments
17 | Thanks to all the contributors who have helped this project grow!
18 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2023 Siddharth Kekre
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ChatBot using OpenAI (ChatGPT) API (with GUI)
2 | Ready to Use Code to have your own ChatBot using OpenAI API.
3 |
Also added WebApp Template for Reference! Thanks to [Lyubomir](https://github.com/LyubomirT)
4 |
5 | ### Steps to Use the ChatBot:
6 | - This Codebase requires [OpenAI API Key](https://openai.com/blog/openai-api), kindly make sure you get one.
7 | - Install Necessary Dependencies from 'requirements.txt'.
8 | - Run : pip install -r requirements.txt
9 | - Open and Store your API Key in "Code/BotDefinition.py".
10 | - Run "Code/ChatBot.py" for Terminal Based ChatBot.
11 | - Run "Code/GuiChatBot.py" for WebApp Based ChatBot.
12 | - That's it! Just enter your prompts and get the responses.
13 | - If you want to terminate the chat, just enter 'End Chat'.
14 |
15 | ### How to get the Best out of this Piece of Code:
16 | - Use it in Terminal/CMD for quick response.
17 | - Use it in Jupyter Notebook / Google Colab for more interactive and complex prompts.
18 | - Use it in the BackEnd of any WebApp for enhanced functionality.
19 | - Whatever and However you wish to use it!
20 |
21 | ### Here is a Sample of How it Works in the Terminal:
22 | 
23 |
--------------------------------------------------------------------------------
/Code/BotDefinition.py:
--------------------------------------------------------------------------------
1 | import openai
2 |
3 | openai.api_key = "Enter Your API Key Here"
4 |
5 | class OpenAIBot:
6 | def __init__(self,engine):
7 | # Initialize conversation with a system message
8 | self.conversation = [{"role": "system", "content": "You are a helpful assistant."}]
9 | self.engine = engine
10 | def add_message(self, role, content):
11 | # Adds a message to the conversation.
12 | self.conversation.append({"role": role, "content": content})
13 | def generate_response(self, prompt):
14 | # Add user prompt to conversation
15 | self.add_message("user", prompt)
16 | try:
17 | # Make a request to the API using the chat-based endpoint with conversation context
18 | response = openai.ChatCompletion.create( model=self.engine, messages=self.conversation)
19 | # Extract the response
20 | assistant_response = response['choices'][0]['message']['content'].strip()
21 | # Add assistant response to conversation
22 | self.add_message("assistant", assistant_response)
23 | # Return the response
24 | return assistant_response
25 | except:
26 | print('Error Generating Response!')
--------------------------------------------------------------------------------
/Code/templates/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |