├── .DS_Store ├── .dockerignore ├── .env ├── .gitignore ├── Dockerfile ├── README.md ├── docker-compose.yaml ├── main.py ├── requirements.txt ├── screenshots ├── .DS_Store └── sample.png ├── src ├── __init__.py ├── chatgpt_api.py └── gradio_bot.py └── utils ├── __init__.py └── constants.py /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/di37/chatbot-chatgpt-api/952e5144e5b569934bc341fb311e6ff0ca259ca6/.DS_Store -------------------------------------------------------------------------------- /.dockerignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/di37/chatbot-chatgpt-api/952e5144e5b569934bc341fb311e6ff0ca259ca6/.dockerignore -------------------------------------------------------------------------------- /.env: -------------------------------------------------------------------------------- 1 | export OPENAI_API_KEY=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 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | **/*__pycache__ 2 | flagged 3 | items 4 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.10.8 2 | 3 | WORKDIR /app 4 | 5 | RUN apt-get update 6 | RUN apt-get install gcc g++ libsm6 libxext6 -y 7 | RUN pip install --upgrade pip 8 | 9 | COPY requirements.txt ./ 10 | RUN pip install -r requirements.txt 11 | 12 | COPY . /app 13 | 14 | ENTRYPOINT [ "python", "-u", "main.py"] -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Creating custom Chatbot with ChatGPT API 2 | 3 | ## Motivation 4 | 5 | The launch of ChatGPT had a profound impact on the world, as it enabled countless individuals to benefit from its ability - to simplify complex concepts and solve sophisticated problems, ranging from assisting students to improving the operations of large businesses. Now, OpenAI has introduced the ChatGPT API, which allows organizations to seamlessly integrate this technology into their products, that will allow to unlock exponential improvements in functionality and performance of these products. 6 | 7 | ## Task 8 | 9 | In this project, we will use ChatGPT API and Gradio to provide Chatbot User Interface. Gradio is library that allows easy-to-use quick demo for using Machine Learning models. 10 | 11 | ## Running API on Docker Container 12 | 13 | - Sign up in OpenAI website and get your API key. 14 | - `.env` must contain the OPENAPI KEY and paths. Sample included. For production purpose, .env file should be included in .gitignore file. 15 | - Run the docker container: 16 | 17 | ```bash 18 | docker compose up 19 | ``` 20 | 21 | - If any changes made to the code, then run following command for the code changes to be reflected in the docker container.Then, we can run the above command. 22 | 23 | ```bash 24 | docker compose build 25 | ``` 26 | 27 | - On your web browser, go to `http://localhost:9001` and the Chatbot Gradio Interface will appear. 28 | 29 | - Sample as follows. 30 | 31 |  32 | 33 | ## Resources 34 | 35 | - ChatGPT API Documentation: https://platform.openai.com/docs/guides/chat 36 | - Gradio Chatbot Documentation: https://gradio.app/creating-a-chatbot/ 37 | - ChatGPT Clone Tutorial: https://www.youtube.com/watch?v=n5nn3mQxrE8&ab_channel=1littlecoder 38 | 39 | ## Important Note 40 | 41 | This project is created exclusively for educational purpose. Not to be used for commercial purposes. 42 | -------------------------------------------------------------------------------- /docker-compose.yaml: -------------------------------------------------------------------------------- 1 | services: 2 | pythonapp: 3 | build: ./ 4 | image: chatgpt_chatbot 5 | ports: 6 | - "9001:7000" 7 | volumes: 8 | - "./items:/app/items" -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import os, sys 2 | 3 | sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) 4 | 5 | from src import gradio_helper_chatbot, gradio_interface 6 | 7 | demo = gradio_interface(gradio_helper_chatbot) 8 | # Use this config when running on Docker 9 | demo.launch(server_name="0.0.0.0", server_port=7000) 10 | # To run on local machine 11 | # demo.launch() -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | openai 2 | gradio 3 | python-dotenv -------------------------------------------------------------------------------- /screenshots/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/di37/chatbot-chatgpt-api/952e5144e5b569934bc341fb311e6ff0ca259ca6/screenshots/.DS_Store -------------------------------------------------------------------------------- /screenshots/sample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/di37/chatbot-chatgpt-api/952e5144e5b569934bc341fb311e6ff0ca259ca6/screenshots/sample.png -------------------------------------------------------------------------------- /src/__init__.py: -------------------------------------------------------------------------------- 1 | from src.chatgpt_api import * 2 | from src.gradio_bot import * 3 | -------------------------------------------------------------------------------- /src/chatgpt_api.py: -------------------------------------------------------------------------------- 1 | import os, sys 2 | 3 | sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) 4 | 5 | import openai 6 | from utils import * 7 | 8 | from dotenv import load_dotenv 9 | 10 | load_dotenv() 11 | 12 | openai.api_key = os.getenv("OPENAI_API_KEY") 13 | 14 | 15 | def openai_chatgpt(prompt: str): 16 | """ 17 | This function allows to make api call to OpenAI. 18 | 19 | Args: 20 | prompt (str): The prompt passed to the OpenAI API. 21 | 22 | Returns: 23 | response (str): The response from OpenAI. 24 | """ 25 | completion = openai.ChatCompletion.create( 26 | model=MODEL, # Model to use. 27 | temperature=TEMPERATURE, # This parameter controls variability of the response. 28 | messages=[ 29 | { 30 | "role": ROLE, # whether we will act as user or assistant. 31 | "content": prompt, 32 | } 33 | ], 34 | ) 35 | # The above parameters is to be changed as per the business needs. 36 | return completion["choices"][0]["message"]["content"] 37 | 38 | 39 | # if __name__ == "__main__": 40 | # print(openai_chatgpt("Complete the sentence for me: My name is Monkey D. Luffy. I will be...")) 41 | # print(os.getenv("OPENAI_API_KEY")) 42 | -------------------------------------------------------------------------------- /src/gradio_bot.py: -------------------------------------------------------------------------------- 1 | import os, sys 2 | 3 | sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) 4 | 5 | from src.chatgpt_api import openai_chatgpt 6 | import gradio as gr 7 | 8 | 9 | def gradio_helper_chatbot(input: str, history=[]): 10 | """ 11 | Function that is used for connecting the chatbot interface, user input and the response that will be received from OpenAI. 12 | 13 | Args: 14 | input (str): user input 15 | history (list): history of user input and responses from OpenAI API. It represents the state. 16 | 17 | Returns: 18 | history, history (list, list): It is in this way that gradio demos will take in update as end of the function is reached. 19 | """ 20 | # Combining current and previous conversation both input and response. 21 | s = list(sum(history, ())) 22 | s.append(input) 23 | inp = " ".join(s) 24 | # Output result from OpenAI API 25 | output = openai_chatgpt(inp) 26 | # This will allow to save the previous chat and allow to continue the conversation 27 | # with the chatbot 28 | history.append((input, output)) 29 | return history, history 30 | 31 | 32 | def gradio_interface(helper_function): 33 | """ 34 | This function allows implementation of fully functional chatbot interface. 35 | 36 | Args: 37 | helper_function (function): This is helper function to be passed to gradio demo. 38 | """ 39 | with gr.Blocks() as demo: 40 | gr.Markdown(""" 41 |