├── .dockerignore ├── .gitignore ├── requirements.txt ├── utils ├── __init__.py └── constants.py ├── .DS_Store ├── .env ├── src ├── __init__.py ├── chatgpt_api.py └── gradio_bot.py ├── screenshots ├── .DS_Store └── sample.png ├── docker-compose.yaml ├── Dockerfile ├── main.py └── README.md /.dockerignore: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | **/*__pycache__ 2 | flagged 3 | items 4 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | openai 2 | gradio 3 | python-dotenv -------------------------------------------------------------------------------- /utils/__init__.py: -------------------------------------------------------------------------------- 1 | from utils.constants import * 2 | -------------------------------------------------------------------------------- /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/di37/chatbot-chatgpt-api/HEAD/.DS_Store -------------------------------------------------------------------------------- /.env: -------------------------------------------------------------------------------- 1 | export OPENAI_API_KEY=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 -------------------------------------------------------------------------------- /src/__init__.py: -------------------------------------------------------------------------------- 1 | from src.chatgpt_api import * 2 | from src.gradio_bot import * 3 | -------------------------------------------------------------------------------- /screenshots/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/di37/chatbot-chatgpt-api/HEAD/screenshots/.DS_Store -------------------------------------------------------------------------------- /screenshots/sample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/di37/chatbot-chatgpt-api/HEAD/screenshots/sample.png -------------------------------------------------------------------------------- /docker-compose.yaml: -------------------------------------------------------------------------------- 1 | services: 2 | pythonapp: 3 | build: ./ 4 | image: chatgpt_chatbot 5 | ports: 6 | - "9001:7000" 7 | volumes: 8 | - "./items:/app/items" -------------------------------------------------------------------------------- /utils/constants.py: -------------------------------------------------------------------------------- 1 | import os, sys 2 | 3 | sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))) 4 | 5 | MODEL = "gpt-3.5-turbo" 6 | TEMPERATURE = 1.0 7 | ROLE = "user" -------------------------------------------------------------------------------- /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"] -------------------------------------------------------------------------------- /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() -------------------------------------------------------------------------------- /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 |