├── .dockerignore ├── Dockerfile ├── LICENSE ├── README.md ├── app.py ├── gunicorn_config.py ├── requirements.txt └── utils.py /.dockerignore: -------------------------------------------------------------------------------- 1 | .git 2 | Dockerfile 3 | .DS_Store 4 | .gitignore 5 | .dockerignore 6 | 7 | # PyCharm specific folder 8 | 9 | .idea 10 | 11 | # Environments 12 | 13 | .env 14 | .venv 15 | env/ 16 | venv/ 17 | ENV/ 18 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.11 2 | # Use the python latest image 3 | COPY . ./ 4 | # Copy the current folder content into the docker image 5 | RUN pip install flask gunicorn langchain openai 6 | # Install the required packages of the application 7 | CMD gunicorn --bind :$PORT app:app 8 | # Bind the port and refer to the app.py app -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Ismail Pelaseyed 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 | # digitalocean-langchain 2 | 3 | Boilerplate for deploying LangChain on Digitalocean App Platform 4 | 5 | ## Installation and running locally 6 | 7 | 1. Create and activate a virtual environment 8 | 9 | ```sh 10 | virtualenv MY_ENV 11 | source MY_ENV/bin/activate 12 | ``` 13 | 14 | 1. Install packages with pip 15 | 16 | ```sh 17 | cd ad-gpt 18 | pip install -r requirements.txt 19 | ``` 20 | 21 | 1. Set up your .env file 22 | 23 | - Duplicate `.env.example` to `.env` 24 | 25 | 1. Run the project 26 | 27 | ```sh 28 | flask --app run app 29 | ``` 30 | 31 | ## Deploying to Google Cloud Run, requires CloudSDK and Google Cloud Project with billing enabled 32 | 33 | 1. Use Google builds command to create the docker image in the container registry 34 | 35 | ```sh 36 | gcloud builds submit --tag gcr.io/PROJECT_ID/langchain 37 | ``` 38 | 39 | 1. Create a Cloud Run service 40 | 41 | ```sh 42 | gcloud run deploy --image gcr.io/PROJECT_ID/langchain --timeout=300 --platform managed 43 | ``` 44 | 45 | 1. Verify the deployed cloud run service in the Google Cloud Console 46 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | 2 | from flask import Flask, jsonify, make_response, request 3 | from utils import create_chain 4 | 5 | 6 | app = Flask(__name__) 7 | 8 | @app.route("/", methods = ['POST']) 9 | def chat(): 10 | if not request.is_json: 11 | return make_response( 12 | jsonify( 13 | {"success": False, 14 | "error": "Unexpected error, request is not in JSON format"}), 15 | 400) 16 | 17 | try: 18 | data = request.json 19 | message = data["message"] 20 | chatgpt_chain = create_chain() 21 | prediction = chatgpt_chain.predict(human_input=message) 22 | 23 | return jsonify({"success": True, "data": prediction}) 24 | except: 25 | return make_response( 26 | jsonify( 27 | {"success": False, 28 | "error": "Unexpected error: failed to send the message"}), 29 | 400) -------------------------------------------------------------------------------- /gunicorn_config.py: -------------------------------------------------------------------------------- 1 | bind = "0.0.0.0:8080" 2 | workers = 2 -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Flask 2 | gunicorn 3 | langchain 4 | openai 5 | python-dotenv -------------------------------------------------------------------------------- /utils.py: -------------------------------------------------------------------------------- 1 | from typing import Any 2 | from langchain.chains import LLMChain 3 | from langchain.chat_models import ChatOpenAI 4 | from langchain.prompts.prompt import PromptTemplate 5 | 6 | 7 | template = """Assistant is a large language model trained by OpenAI. 8 | Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand. 9 | Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics. 10 | Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist. 11 | 12 | Human: {human_input} 13 | Assistant:""" 14 | 15 | prompt = PromptTemplate( 16 | input_variables=["human_input"], 17 | template=template 18 | ) 19 | 20 | def create_chain() -> Any: 21 | chatgpt_chain = LLMChain( 22 | llm=ChatOpenAI(temperature=0), 23 | prompt=prompt, 24 | verbose=False, 25 | ) 26 | 27 | return chatgpt_chain 28 | --------------------------------------------------------------------------------