본문 바로가기
서버구축 (WEB,DB)

로컬 머신(Local Machine, PC) 환경에서 ChatBot 구축 및 실행 방법

by 날으는물고기 2024. 9. 11.

로컬 머신(Local Machine, PC) 환경에서 ChatBot 구축 및 실행 방법

Build A ChatBot That Runs Purely On Your Local Machine (Using Llama 2 + Epsilla + LangChain + Streamlit) by Richard Song - Epsilla

로컬 머신에서 실행되는 챗봇(ChatBot)을 구축하려면, 다음의 도구들을 사용할 수 있습니다.

  • Llama 2: Meta의 오픈 소스 언어 모델
  • Epsilla: 비슷한 맥락에서 사용될 수 있는 임의의 도구명으로 추정됨
  • LangChain: 다양한 언어 모델을 체인으로 연결해주는 프레임워크
  • Streamlit: 파이썬 애플리케이션을 웹 앱으로 빠르게 전환해주는 도구

아래는 Llama 2, LangChain, Streamlit을 사용하여 로컬에서 실행 가능한 ChatBot을 구축하는 방법입니다.

1. 환경 설정

필요한 라이브러리를 설치합니다. pip을 사용하여 다음과 같이 설치할 수 있습니다.

pip install torch transformers langchain streamlit

2. Llama 2 모델 다운로드 및 설정

Llama 2 모델을 다운로드하고 설정합니다. Meta의 Llama 2는 Hugging Face의 Transformers 라이브러리를 통해 사용할 수 있습니다.

from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "meta-llama/Llama-2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

3. LangChain 설정

LangChain을 사용하여 모델을 체인으로 연결합니다. LangChain을 통해 모델의 출력과 입력을 보다 유연하게 관리할 수 있습니다.

from langchain.llms import HuggingFaceLLM
from langchain.chains import ConversationChain

llm = HuggingFaceLLM(model=model, tokenizer=tokenizer)
conversation_chain = ConversationChain(llm=llm)

4. Streamlit을 사용한 인터페이스 구성

Streamlit을 사용하여 웹 인터페이스를 구성합니다. 다음은 간단한 Streamlit 앱의 예제입니다.

import streamlit as st

st.title("ChatBot with Llama 2, LangChain, and Streamlit")

if 'conversation' not in st.session_state:
    st.session_state.conversation = []

def chat(user_input):
    st.session_state.conversation.append(("User", user_input))
    response = conversation_chain.run(input=user_input)
    st.session_state.conversation.append(("Bot", response))

user_input = st.text_input("You: ", "")
if st.button("Send"):
    chat(user_input)

for role, text in st.session_state.conversation:
    st.write(f"{role}: {text}")

5. 전체 코드 통합

위의 모든 코드를 통합하여 최종 ChatBot을 완성합니다.

import streamlit as st
from transformers import AutoModelForCausalLM, AutoTokenizer
from langchain.llms import HuggingFaceLLM
from langchain.chains import ConversationChain

# 모델 및 토크나이저 설정
model_name = "meta-llama/Llama-2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

# LangChain 설정
llm = HuggingFaceLLM(model=model, tokenizer=tokenizer)
conversation_chain = ConversationChain(llm=llm)

# Streamlit 설정
st.title("ChatBot with Llama 2, LangChain, and Streamlit")

if 'conversation' not in st.session_state:
    st.session_state.conversation = []

def chat(user_input):
    st.session_state.conversation.append(("User", user_input))
    response = conversation_chain.run(input=user_input)
    st.session_state.conversation.append(("Bot", response))

user_input = st.text_input("You: ", "")
if st.button("Send"):
    chat(user_input)

for role, text in st.session_state.conversation:
    st.write(f"{role}: {text}")

6. 실행 방법

위의 코드를 app.py 파일로 저장한 후, 다음 명령어를 통해 Streamlit 서버를 실행할 수 있습니다.

streamlit run app.py

이제 브라우저에서 http://localhost:8501로 접속하면 ChatBot을 사용할 수 있습니다.

이와 같이 로컬 머신에서 Llama 2, LangChain, Streamlit을 사용한 ChatBot을 구성할 수 있습니다. 필요한 추가 설정이나 최적화는 사용자의 환경과 필요에 따라 조정할 수 있습니다.

728x90

댓글