기타 (Other)

LibreChat 사용기

Wibaek 2024. 5. 8. 15:29
728x90

GPT4를 사용하고 싶은데, 월 20달러를 내고 사용할정도로 자주 사용하지는 않아 API를 통해 사용하기 위해 찾아보던 중, LibreChat이라는 프로젝트를 찾았습니다.

 

https://github.com/danny-avila/LibreChat

 

GitHub - danny-avila/LibreChat: Enhanced ChatGPT Clone: Features OpenAI, Assistants API, Azure, Groq, GPT-4 Vision, Mistral, Bin

Enhanced ChatGPT Clone: Features OpenAI, Assistants API, Azure, Groq, GPT-4 Vision, Mistral, Bing, Anthropic, OpenRouter, Vertex AI, Gemini, AI model switching, message search, langchain, DALL-E-3,...

github.com

 

스타수 11.3k의 유명한 프로젝트로 로컬로 Chat GPT와 비슷한 UI에서 GPT부터 Anthropic사의 LLM까지 API KEY만 있다면 자유롭게 사용 가능한 서비스입니다.

 

https://docs.librechat.ai/install/installation/docker_compose_install.html

 

🐳 Docker Compose ✨(Recommended)

Docker Compose Installation Guide: Docker Compose installation is recommended for most use cases. It's the easiest, simplest, and most reliable method to get started.

docs.librechat.ai

설치는 매우 간단히 진행됩니다. 저는 MacOS 환경을 사용하지만, MongoDB등을 설치해주어야 해서 docker를 이용한 방식으로 사용했습니다.

 

git clone https://github.com/danny-avila/LibreChat.git

cd LibreChat
  
cp .env.example .env

위와 같이 .env파일을 만들어 내부에 API KEY등을 세팅해주고(또는 실행한 후 매번 입력해줄 수도 있습니다)

docker compose up -d

Docker compose로 쉽게 사용할 수 있습니다.

 

이메일이나 OAuth를 통해 계정을 여러개 만들 수 있어, 서버를 띄워 지인들과 사용하기에도 좋아 보입니다.

 

사용 세팅

MongoDB 설정

저는 사용을 위해 클라우드 서버와 온라인 MongoDB를 이용해 실행했습니다.

https://docs.librechat.ai/install/configuration/mongodb.html

 

🍃 Online MongoDB

This guide teaches you how to set up an online MongoDB database for LibreChat using MongoDB Atlas, a cloud-based service. You will learn how to create an account, a project, and a cluster, as well as how to configure your database credentials, network acce

docs.librechat.ai

해당 내용에 따라 데이터베이스를 생성해주고, MONGO_URI를 생성된 DB에 맞춰 수정해줍니다.

인스턴스에 의존성 설치

https://docs.librechat.ai/deployment/docker_ubuntu_deploy.html

 

🐳 Ubuntu Docker Deployment

These instructions are designed for someone starting from scratch for a Docker Installation on a remote Ubuntu server

docs.librechat.ai

이후 클라우드에 인스턴스를 생성하고 연결했습니다. 환경은 Ubuntu 22.04 LTS ARM를 사용했습니다.

 

우선 https 연결을 위한 인증서를 받기 위해 인스턴스를 도메인과 연결시켜준 후, 아래와 같이 진행하여 lets encrypt 서비스를 이용해 인증서를 받습니다.

sudo apt update
sudo apt-get install certbot python3-certbot-nginx -y

sudo certbot --nginx
# 이후 instruction에 맞추어 진행

 

이후 도커를 설치해줍니다

sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
apt-cache policy docker-ce
sudo apt install docker-ce -y
sudo usermod -aG docker $USER

# 실행 테스트
sudo systemctl status docker

 

Docker compose를 설치해줍니다

sudo curl -L https://github.com/docker/compose/releases/download/v2.26.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

# 설치 확인
docker-compose -v

 

이후 앱을 받고 .env를 추가해주고, 적절히 수정해줍니다

git clone https://github.com/danny-avila/LibreChat.git
cd LibreChat/

cp .env.example .env

.env 수정, librechat.yaml 생성

이때 보안을 위해 .env에 아래와 같이 임의의 값을 추가해주는 것을 권장합니다

vi .env

# FIND THESE VARIABLES AND REPLACE THEIR DEFAULT VALUES!

# Must be a 16-byte IV (32 characters in hex)

CREDS_IV=e2341419ec3dd3d19b13a1a87fafcbfb

# Must be 32-byte keys (64 characters in hex)

CREDS_KEY=f34be427ebb29de8d88c107a71546019685ed8b241d8f2ed00c3df97ad2566f0
JWT_SECRET=16f8c0ef4a5d391b26034086c628469d3f9f497f08163ab9b40137092f2909ef
JWT_REFRESH_SECRET=eaa5191f2914e30b9387fd84e254e4ba6fc51b4654968a9b0803b456a54b8418

임의의 값은 다음 링크에서 생성할 수도 있습니다

https://replit.com/@daavila/crypto#index.js

 

MongoDB 연결을 위해 .env에서 아래와 같은 값을 수정해줍니다

MONGO_URI=

 

저는 Open API의 API Key를 설정해줬습니다

OPENAI_API_KEY=

또한 아무나 가입할 수 없게 가입을 막았습니다

ALLOW_REGISTRATION=false

 

librechat.yaml을 생성해줍니다

vi librechat.yaml
# For more information, see the Configuration Guide:
# https://docs.librechat.ai/install/configuration/custom_config.html

# Configuration version (required)
version: 1.0.5
# This setting caches the config file for faster loading across app lifecycle
cache: true

https 사용을 위한 설정

deploy-compose.yml에서 다음과 같이 설정해 인증서에 접근할 수 있게 했습니다

client:
  build:
    context: .
    dockerfile: Dockerfile.multi
    target: prod-stage
  container_name: LibreChat-NGINX
  ports:
    - 80:80
    - 443:443
  depends_on:
    - api
  restart: always
  volumes:
    - ./client/nginx.conf:/etc/nginx/conf.d/default.conf
      - /etc/letsencrypt/live/<put.here.your.domain.name>:/etc/letsencrypt/live/<put.here.your.domain.name>
      - /etc/letsencrypt/archive/<put.here.your.domain.name>:/etc/letsencrypt/archive/<put.here.your.domain.name>
      - /etc/letsencrypt/options-ssl-nginx.conf:/etc/letsencrypt/options-ssl-nginx.conf
      - /etc/letsencrypt/ssl-dhparams.pem:/etc/letsencrypt/ssl-dhparams.pem

 

그리고 client/nginx.conf도 적절히 수정해줍니다.

vi client/nginx.conf
# Secure default configuration generated by Mozilla SSL Configuration Generator
# generated 2024-01-21, Mozilla Guideline v5.7, nginx 1.24.0, OpenSSL 3.1.4, intermediate configuration
# https://ssl-config.mozilla.org/#server=nginx&version=1.24.0&config=intermediate&openssl=3.1.4&guideline=5.7

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # To Configure SSL, comment all lines within the Non-SSL section and uncomment all lines under the SSL section. 
    ########################################  Non-SSL  ########################################
    server_name <put.here.your.domain.name>;
 
    # Increase the client_max_body_size to allow larger file uploads
    # The default limits for image uploads as of 11/22/23 is 20MB/file, and 25MB/request
    client_max_body_size 25M;

    ########################################  SSL  ########################################
#    # Redirect all http traffic to https
    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    ssl_certificate /etc/letsencrypt/live/<put.here.your.domain.name>/fullchain.pem; # Use your cert paths
    ssl_certificate_key /etc/letsencrypt/live/<put.here.your.domain.name>/privkey.pem; # Use your cert paths
    ssl_session_timeout 1d;
    ssl_session_cache shared:MozSSL:10m;  # about 40000 sessions
    ssl_session_tickets off;

    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    server_name <put.here.your.domain.name>;


    location /api {
        proxy_pass http://api:3080/api;
    }

    location / {
        proxy_pass http://api:3080;
    }
}

추가 작업, 실행

저는 MongoDB를 온라인에 있는 별도의 db를 사용할 예정임으로 deploy-compose.yml 에서

      - MONGO_URI=mongodb://mongodb:27017/LibreChat

위의 environment 부분을 주석처리 해주었습니다. 이를 주석처리 해주어야 .env에 있는 값을 사용하는 것으로 보입니다.

 

또한 https 인증서를 받을때의 문제인지, nginx가 켜져있어 꺼주었습니다.

sudo systemctl stop nginx

 

모든 설정이 완료되었으면 앱을 실행해줍니다.

sudo docker-compose -f ./deploy-compose.yml up -d

 

728x90