From 53ffe5bf219b4af8a305a4670556c4fa8f0af972 Mon Sep 17 00:00:00 2001 From: paus Date: Wed, 11 Oct 2023 11:50:57 -0400 Subject: [PATCH] CP instance of a2rchi. --- .github/workflows/prod-cp-ci-cd.yaml | 70 +++++++++++++++++ config/prod-cp-config.yaml | 112 +++++++++++++++++++++++++++ config/prompts/cp.prompt | 16 ++++ deploy/prod-cp/prod-cp-compose.yaml | 81 +++++++++++++++++++ deploy/prod-cp/prod-cp-install.sh | 21 +++++ deploy/prod-cp/prod-cp-stop.sh | 5 ++ deploy/ssh_config | 8 ++ 7 files changed, 313 insertions(+) create mode 100644 .github/workflows/prod-cp-ci-cd.yaml create mode 100644 config/prod-cp-config.yaml create mode 100644 config/prompts/cp.prompt create mode 100644 deploy/prod-cp/prod-cp-compose.yaml create mode 100644 deploy/prod-cp/prod-cp-install.sh create mode 100644 deploy/prod-cp/prod-cp-stop.sh diff --git a/.github/workflows/prod-cp-ci-cd.yaml b/.github/workflows/prod-cp-ci-cd.yaml new file mode 100644 index 0000000..7c34c97 --- /dev/null +++ b/.github/workflows/prod-cp-ci-cd.yaml @@ -0,0 +1,70 @@ +name: Deploy A2rchi Prod for cp +run-name: ${{ github.actor }} deploys A2rchi for cp to prod +on: + push: + branches: + - release-cp +jobs: + deploy-prod-system: + runs-on: ubuntu-latest + env: + SSH_AUTH_SOCK: /tmp/ssh_agent.sock + steps: + # boilerplate message and pull repository to CI runner + - run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event." + - uses: actions/checkout@v3 + - run: echo "The ${{ github.repository }} repository has been cloned to the runner." + + # setup SSH + - name: Setup SSH + run: | + mkdir -p /home/runner/.ssh/ + echo "${{ secrets.SSH_PRIVATE_KEY_MDRUSSO }}" > /home/runner/.ssh/id_rsa_submit + chmod 600 /home/runner/.ssh/id_rsa_submit + echo "${{ secrets.SSH_SUBMIT_KNOWN_HOSTS }}" > ~/.ssh/known_hosts + cp ${{ github.workspace }}/deploy/ssh_config /home/runner/.ssh/config + ssh-agent -a $SSH_AUTH_SOCK > /dev/null + ssh-add /home/runner/.ssh/id_rsa_submit + + # create secrets files for docker-compose + - name: Create Secrets Files + run: | + mkdir -p ${{ github.workspace }}/deploy/prod-cp/secrets/ + touch ${{ github.workspace }}/deploy/prod-cp/secrets/flask_uploader_app_secret_key.txt + echo "${{ secrets.PROD_FLASK_UPLOADER_APP_SECRET_KEY }}" >> ${{ github.workspace }}/deploy/prod-cp/secrets/flask_uploader_app_secret_key.txt + chmod 400 ${{ github.workspace }}/deploy/prod-cp/secrets/flask_uploader_app_secret_key.txt + touch ${{ github.workspace }}/deploy/prod-cp/secrets/uploader_salt.txt + echo "${{ secrets.PROD_UPLOADER_SALT }}" >> ${{ github.workspace }}/deploy/prod-cp/secrets/uploader_salt.txt + chmod 400 ${{ github.workspace }}/deploy/prod-cp/secrets/uploader_salt.txt + touch ${{ github.workspace }}/deploy/prod-cp/secrets/openai_api_key.txt + echo "${{ secrets.OPENAI_API_KEY }}" >> ${{ github.workspace }}/deploy/prod-cp/secrets/openai_api_key.txt + chmod 400 ${{ github.workspace }}/deploy/prod-cp/secrets/openai_api_key.txt + touch ${{ github.workspace }}/deploy/prod-cp/secrets/hf_token.txt + echo "${{ secrets.HF_TOKEN }}" >> ${{ github.workspace }}/deploy/prod-cp/secrets/hf_token.txt + chmod 400 ${{ github.workspace }}/deploy/prod-cp/secrets/hf_token.txt + + # stop any existing docker compose that's running + - name: Stop Docker Compose + run: | + ssh ppc 'bash -s' < ${{ github.workspace }}/deploy/prod-cp/prod-cp-stop.sh + + # copy repository to machine + - name: Copy Repository + run: | + rsync -e ssh -r ${{ github.workspace}}/* --exclude .git/ --delete ppc:~/A2rchi-prod-cp/ + + # run deploy script + - name: Run Deploy Script + run: | + ssh ppc 'bash -s' < ${{ github.workspace }}/deploy/prod-cp/prod-cp-install.sh + + # clean up secret files + - name: Remove Secrets from Runner + run: | + rm ${{ github.workspace }}/deploy/prod-cp/secrets/flask_uploader_app_secret_key.txt + rm ${{ github.workspace }}/deploy/prod-cp/secrets/uploader_salt.txt + rm ${{ github.workspace }}/deploy/prod-cp/secrets/openai_api_key.txt + rm ${{ github.workspace }}/deploy/prod-cp/secrets/hf_token.txt + + # print job status + - run: echo "🍏 This job's status is ${{ job.status }}." \ No newline at end of file diff --git a/config/prod-cp-config.yaml b/config/prod-cp-config.yaml new file mode 100644 index 0000000..7edb824 --- /dev/null +++ b/config/prod-cp-config.yaml @@ -0,0 +1,112 @@ +global: + TRAINED_ON: "CP" #used to create name of the specific version of a2rchi we're using + DATA_PATH: "/root/data/" + ACCOUNTS_PATH: "/root/.accounts/" + LOCAL_VSTORE_PATH: "/root/data/vstore/" + ACCEPTED_FILES: + -".txt" + -".html" + -".pdf" + +interfaces: + chat_app: + PORT: 7861 + EXTERNAL_PORT: 7683 + HOST: "0.0.0.0" # either "0.0.0.0" (for public) or "127.0.0.1" (for internal) + HOSTNAME: "t3desk019.mit.edu" # careful, this is used for the chat service + template_folder: "/root/A2rchi/A2rchi/interfaces/chat_app/templates" + static_folder: "/root/A2rchi/A2rchi/interfaces/chat_app/static" + num_responses_until_feedback: 3 #the number of responses given by A2rchi until she asks for feedback. + uploader_app: + PORT: 5001 + HOST: "0.0.0.0" # either "0.0.0.0" (for public) or "127.0.0.1" (for internal) + template_folder: "/root/A2rchi/A2rchi/interfaces/uploader_app/templates" + +chains: + input_lists: + - empty.list +# - miscellanea.list +# - cp-textbook.list + base: + # roles that A2rchi knows about + ROLES: + - User + - A2rchi + - Expert + prompts: + # prompt that serves to condense a history and a question into a single question + CONDENSING_PROMPT: config/prompts/condense.prompt + # main prompt which takes in a single question and a context. + MAIN_PROMPT: config/prompts/cp.prompt + chain: + # pick one of the models listed in the model class map below + MODEL_NAME: OpenAILLM + # map of all the class models and their keyword arguments + MODEL_CLASS_MAP: + OpenAILLM: + class: OpenAILLM + kwargs: + model_name: gpt-4 + temperature: 1 + DumbLLM: + class: DumbLLM + kwargs: + filler: null + LlamaLLM: + class: LlamaLLM + kwargs: + base_model: "meta-llama/Llama-2-7b-chat-hf" #the location of the model (ex. meta-llama/Llama-2-70b) + peft_model: null #the location of the finetuning of the model. Can be none + enable_salesforce_content_safety: True # Enable safety check with Salesforce safety flan t5 + quantization: True #enables 8-bit quantization + max_new_tokens: 4096 #The maximum numbers of tokens to generate + seed: null #seed value for reproducibility + do_sample: True #Whether or not to use sampling ; use greedy decoding otherwise. + min_length: null #The minimum length of the sequence to be generated, input prompt + min_new_tokens + use_cache: True #[optional] Whether or not the model should use the past last key/values attentions Whether or not the model should use the past last key/values attentions (if applicable to the model) to speed up decoding. + top_p: .9 # [optional] If set to float < 1, only the smallest set of most probable tokens with probabilities that add up to top_p or higher are kept for generation. + temperature: .6 # [optional] The value used to modulate the next token probabilities. + top_k: 50 # [optional] The number of highest probability vocabulary tokens to keep for top-k-filtering. + repetition_penalty: 1.0 #The parameter for repetition penalty. 1.0 means no penalty. + length_penalty: 1 #[optional] Exponential penalty to the length that is used with beam-based generation. + max_padding_length: null # the max padding length to be used with tokenizer padding the prompts. + chain_update_time: 10 # the amount of time (in seconds) which passes between when the chain updates to the newest version of the vectorstore +utils: + cleo: + cleo_update_time: 10 + mailbox: + IMAP4_PORT: 143 + mailbox_update_time: 10 + data_manager: + CHUNK_SIZE: 1000 + CHUNK_OVERLAP: 0 + use_HTTP_chromadb_client: True # recommended: True (use http client for the chromadb vectorstore?) + # use_HTTP_chromadb_client: False + vectordb_update_time: 10 + chromadb_host: chromadb-prod-cp + chromadb_port: 8000 + collection_name: "prod_cp_collection" #unique in case vector stores are ever combined. + reset_collection: True # reset the entire collection each time it is accessed by a new data manager instance + embeddings: + # choose one embedding from list below + EMBEDDING_NAME: OpenAIEmbeddings + # list of possible embeddings to use in vectorstore + EMBEDDING_CLASS_MAP: + OpenAIEmbeddings: + class: OpenAIEmbeddings + kwargs: + model: text-embedding-ada-002 + similarity_score_reference: 0.4 + HuggingFaceEmbeddings: + class: HuggingFaceEmbeddings + kwargs: + model_name: "sentence-transformers/all-mpnet-base-v2" + model_kwargs: + device: 'cpu' + encode_kwargs: + normalize_embeddings: True + similarity_score_reference: 0.9 + scraper: + reset_data: True # delete websites and sources.yml in data folder + verify_urls: False # should be true when possible + enable_warnings: False # keeps output clean if verify == False diff --git a/config/prompts/cp.prompt b/config/prompts/cp.prompt new file mode 100644 index 0000000..f33be0d --- /dev/null +++ b/config/prompts/cp.prompt @@ -0,0 +1,16 @@ +# Prompt used to qurery LLM with appropriate context and question. +# This prompt is specific to 8.01 taught at MIT and likely will not perform well for other applications, where it is recommeneded to write your own prompt and change it in the config +# +# All final promptsd must have the following tags in them, which will be filled with the appropriate information: +# {question} +# {context} +# +You are a conversational chatbot and teaching assisitant named A2rchi who helps students taking Classical Mechanics 1 at MIT (also called 8.01). You will be provided context to help you answer their questions. +Using your physics, math, and problem solving knowledge, answer the question at the end. Unless otherwise indicated, assume the users know high school level physics. +Since you are a teaching assisitant, please try to give throughou answers to questions with explanations, instead of just giving the answer. +If you don't know, say "I don't know". It is extremely important you only give correct answers. If you need to ask a follow up question, please do. + +Context: {context} + +Question: {question} +Helpful Answer: \ No newline at end of file diff --git a/deploy/prod-cp/prod-cp-compose.yaml b/deploy/prod-cp/prod-cp-compose.yaml new file mode 100644 index 0000000..8d3cf65 --- /dev/null +++ b/deploy/prod-cp/prod-cp-compose.yaml @@ -0,0 +1,81 @@ +services: + chat-prod-cp: + build: + context: ../.. + dockerfile: deploy/dockerfiles/Dockerfile-chat + depends_on: + chromadb-prod-cp: + condition: service_healthy + environment: + RUNTIME_ENV: prod-cp + OPENAI_API_KEY_FILE: /run/secrets/openai_api_key + HUGGING_FACE_HUB_TOKEN_FILE: /run/secrets/hf_token + secrets: + - openai_api_key + - hf_token + volumes: + - a2rchi-prod-cp-data:/root/data/ + ports: + - 7683:7861 # host:container + restart: always + + data-manager-prod-cp: + build: + context: ../.. + dockerfile: deploy/dockerfiles/Dockerfile-data-manager + args: + BUILD_ENV: prod-cp + depends_on: + chromadb-prod-cp: + condition: service_healthy + environment: + RUNTIME_ENV: prod-cp + FLASK_UPLOADER_APP_SECRET_KEY_FILE: /run/secrets/flask_uploader_app_secret_key + UPLOADER_SALT_FILE: /run/secrets/uploader_salt + OPENAI_API_KEY_FILE: /run/secrets/openai_api_key + HUGGING_FACE_HUB_TOKEN_FILE: /run/secrets/hf_token + secrets: + - flask_uploader_app_secret_key + - uploader_salt + - openai_api_key + - hf_token + ports: + - 5004:5001 # host:container + volumes: + - a2rchi-prod-cp-data:/root/data/ + - /home/tier3/a2rchi/cp-content/:/root/data/cp-content/ + restart: always + + chromadb-prod-cp: + build: + context: ../.. + dockerfile: deploy/dockerfiles/Dockerfile-chroma + environment: + RUNTIME_ENV: prod-cp + ports: + - 8003:8000 # host:container + volumes: + - a2rchi-prod-cp-data:/chroma/chroma/ + restart: always + # healthcheck originates from inside container; so use container port + healthcheck: + test: ["CMD", "curl", "-f", "http://0.0.0.0:8000/api/v1/heartbeat"] + interval: 15s + timeout: 10s + retries: 3 + start_period: 10s + start_interval: 5s + +volumes: + a2rchi-prod-cp-data: + external: true + +secrets: + flask_uploader_app_secret_key: + file: secrets/flask_uploader_app_secret_key.txt + uploader_salt: + file: secrets/uploader_salt.txt + openai_api_key: + file: secrets/openai_api_key.txt + hf_token: + file: secrets/hf_token.txt diff --git a/deploy/prod-cp/prod-cp-install.sh b/deploy/prod-cp/prod-cp-install.sh new file mode 100644 index 0000000..1e72c07 --- /dev/null +++ b/deploy/prod-cp/prod-cp-install.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +# create volume if it doesn't already exist +exists=`docker volume ls | awk '{print $2}' | grep a2rchi-prod-cp-data` +if [[ $exists != 'a2rchi-prod-cp-data' ]]; then + docker volume create --name a2rchi-prod-cp-data +fi + +# start services +echo "Starting docker compose" +cd A2rchi-prod-cp/deploy/prod-cp/ +docker compose -f prod-cp-compose.yaml up -d --build --force-recreate --always-recreate-deps + +# # secrets files are created by CI pipeline and destroyed here +# rm secrets/cleo_*.txt +# rm secrets/imap_*.txt +# rm secrets/sender_*.txt +# rm secrets/flask_uploader_app_secret_key.txt +# rm secrets/uploader_salt.txt +# rm secrets/openai_api_key.txt +# rm secrets/hf_token.txt diff --git a/deploy/prod-cp/prod-cp-stop.sh b/deploy/prod-cp/prod-cp-stop.sh new file mode 100644 index 0000000..a1699df --- /dev/null +++ b/deploy/prod-cp/prod-cp-stop.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +echo "Stop running docker compose" +cd A2rchi-prod-cp/deploy/prod-cp/ +docker compose -f prod-cp-compose.yaml down diff --git a/deploy/ssh_config b/deploy/ssh_config index c75b42e..540603a 100644 --- a/deploy/ssh_config +++ b/deploy/ssh_config @@ -16,3 +16,11 @@ Host submit-t3desk IdentityFile ~/.ssh/id_rsa_submit StrictHostKeyChecking no +Host ppc + HostName ppc.mit.edu + User a2rchi + IdentitiesOnly=yes + PreferredAuthentications publickey + PasswordAuthentication no + IdentityFile ~/.ssh/id_rsa_submit + StrictHostKeyChecking no