AutoGen > User Guide > Using Non-OopenAI Models > vLLM
https://microsoft.github.io/autogen/0.2/docs/topics/non-openai-models/local-vllm
vLLM은 로컬에서 실행되는 프록시 및 추론(inference) 서버로, OpenAI 호환 API를 제공한다. 프록시와 추론 기능을 모두 수행하기 때문에 별도의 추론 서버를 설치할 필요가 없다.
참고: 현재 기준 vLLM은 현재 OpenAI의 Function Calling(AutoGen과 함께 사용 가능)을 지원하지 않는다. 하지만 해당 기능이 개발 중이라 해당 Docs를 읽는 시점에 지원되고 있을 수도 있다고 한다.
vLLM github에 들어가보니까 내가 아래에서 사용하려고 하는 mistral function calling 이 되도록 되어 있는 PR을 보니까 아마 될거라 생각된다.
vLLM mistral function calling format
https://github.com/vllm-project/vllm/pull/8515
아무튼 이 스택을 사용하려면 다음을 설치해야 한다.
(1) AutoGen
(2) vLLM
AutoGen install
https://microsoft.github.io/autogen/0.2/docs/installation/
참고! 스택 구성을 위해 가상 환경 (virtual environment)을 사용하는 것을 권장한다. 가상 환경 설치 관련 가이드는 아래의 article 참고!
virtual environment article
https://microsoft.github.io/autogen/0.2/docs/installation/
터미널에서 아래와 같은 명령어로 vLLM을 설치한다.
pip install vllm
vLLM은 서버를 실행할 때 새로운 모델을 다운로드 한다.
모델은 Hugging Face에서 제공되며, 필터링된 텍스트 생성 모델 목록은 아래에서 확인 가능하다.
Hugging face
https://huggingface.co/
텍스트 생성 모델
https://huggingface.co/models?pipeline_tag=text-generation&sort=trending
또한 vLLM에서 자주 사용하는 모델 목록도 제공하고 있다.
모델 이름은 전체 이름을 사용해야 하며, 예를 들어 mistralai/Mistral-7B-Instruct-v0.2와 같이 작성한다.
vLLM에서 자주 사용하는 목록
https://docs.vllm.ai/en/latest/models/supported_models.html
vLLM은 기보적으로 사전에 정의된 chat template을 사용한다.
다만, Hugging Face의 모델 설정 파일(config 파일)에 chat template이 정의되어 있다면 해당 템플릿을 사용한다. 이 떄문에 AutoGen에서 사용하는 'role' : 'system' 메시지를 허용하지 않는 템플릿일 경우 문제가 발생할 수 있다. 아래에서 사용해야할 Misral.AI의 Mistral 7B 모델에 대해, 'user', 'assistant', 'system' 역할을 모두 허용하는 채팅 템플릿을 직접 만ㄷ르어야 한다.
autogemistraltemplate.jinja라는 파일 이름을 만들고, 아래와 같은 내용을 입력해야 한다.
{{ bos_token }}
{% for message in messages %}
{% if ((message['role'] == 'user' or message['role'] == 'system') != (loop.index0 % 2 == 0)) %}
{{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}
{% endif %}
{% if (message['role'] == 'user' or message['role'] == 'system') %}
{{ '[INST] ' + message['content'] + ' [/INST]' }}
{% elif message['role'] == 'assistant' %}
{{ message['content'] + eos_token}}
{% else %}
{{ raise_exception('Only system, user and assistant roles are supported!') }}
{% endif %}
{% endfor %}
참고
채팅 템플릿은 모델 또는 모델 계열에 따라 다르다. 여기에서 보여주는 예시는 Mistral 7B 및 Mistral 8x7B와 같은 Mistral 기반 모델에 해당된다.
vLLm에는 다양한 모델용 예제 템플릿이 준비되어 있고, 이를 기반으로 자신의 채팅 템플릿을 만들 수 있다. 단, 'sysytem 역할 메시지를 지원하도록 템플릿을 수정해야 할 수도 있다 는 점을 인지해야 한다.
선택한 모델과 autogenmistraltemplate.jinja 채팅 템플릿을 사용해서 vLLM을 실행하려면 아래와 같이 명령어를 입력한다.
python -m vllm.entrypoints.openai.api_server --model mistralai/Mistral-7B-Instruct-v0.2 --chat-template (autogenmistraltemplate.jinja의 경로)
이때 autogenmistraltemplate.jinja 가 있는 경로를 넣어줘야 한다.
즉, 나는
python -m vllm.entrypoints.openai.api_server \
--model mistralai/Mistral-7B-Instruct-v0.2 \
--chat-template /Users/geonheekim/Desktop/python_interview/autoGen/User_guide/Using_Non_OpenAI_Models/autogenmistraltemplate.jinja
로 해줬으니 참고
이대로 실행했더니PyTorch의 c10d 분산 통신 모듈에서 TCP 연결에 실패해서 vLLM proxy server 실행을 못하고 있어서 vLLM을 upgrade 해서 설치하고
오류 회피용 스크립트를 정리해서 켜보도록 했다.
# 🧹 환경 변수 초기화 (PyTorch Distributed 관련 문제 방지)
unset MASTER_ADDR
unset MASTER_PORT
unset RANK
unset WORLD_SIZE
# ✅ 사용자 설정: 모델 이름과 Chat Template 경로
MODEL_NAME="mistralai/Mistral-7B-Instruct-v0.2"
CHAT_TEMPLATE="/Users/geonheekim/Desktop/python_interview/autoGen/User_guide/Using_Non_OpenAI_Models/autogenmistraltemplate.jinja"
# 🚀 vLLM OpenAI 서버 실행
python -m vllm.entrypoints.openai.api_server \
--model "$MODEL_NAME" \
--chat-template "$CHAT_TEMPLATE"
위 스크립트를 만들고 저장하고 실행하는 방법은
터미널에서 nano run.sh 를 만들고 위의 스크립트를 작성하고 맥 기준 ctrl + O 그리고 엔터 치기 ctrl + x로 나가기 chmod+x run.sh로 실행 권한 부여하기.
그러면 run.sh 스크립트 파일이 저장되는데 ./run.sh로 실행한다.
아무튼 로컬에서 실행하려니까 안돼서 회사 개발 OCI 서버에서 켰다. 에효
근데... 자꾸 커넥션 에러남 base_url 때문인거 같은데,,
이것 때문에 하루 삽질해서 이거는 차후에 하고 수정해야겠음.. 아오 빡쳐라..