langchain 을 이용해서 Llama 를 API 키 없이, 인터넷 연결이 없이 AI 생성 챗봇을 실행하는 방법에 대한 내용을 정리 하였습니다.
Step 1. LLM 다운로드 하기
Llama 2 모델을 다운로드 합니다.
https://huggingface.co/meta-llama/Llama-2-7b-chat-hf
meta-llama/Llama-2-7b-chat-hf · Hugging Face
This is a form to enable access to Llama 2 on Hugging Face after you have been granted access from Meta. Please visit the Meta website and accept our license terms and acceptable use policy before submitting this form. Requests will be processed in 1-2 day
huggingface.co
https://huggingface.co/TheBloke/Llama-2-7B-Chat-GGML/blob/main/llama-2-7b-chat.ggmlv3.q8_0.bin
llama-2-7b-chat.ggmlv3.q8_0.bin · TheBloke/Llama-2-7B-Chat-GGML at main
huggingface.co
** 위의 파일이 동작을 하지 않아서 검색해본 결과 여기서 gguf 파일을 다운로드 하였습니다.
https://huggingface.co/devilteo911/selfrag_llama2_7b-q8_0/tree/main
devilteo911/selfrag_llama2_7b-q8_0 at main
huggingface.co
Step 2. Python 환경 설정하기
pip install llama-cpp-python
다운로드 한 gguf 파일을 python 생성 디렉토리에 넣코 아래와 같이 입력해서 결과를 확인합니다.
from llama_cpp import Llama
llm = Llama(model_path="./selfrag_llama2_7b-q8_0.gguf")
output = llm(
"Question: What are the names of the planets in the solar system? Answer: ",
max_tokens=32,
stop=["Q:", "\n"],
echo=True,
)
print(output)
여기서 결과가 BLAS = 1 이 되어야 GPU 를 사용하며, 0 이면 CPU 를 사용하는 것입니다.
만일 GPU 를 쓰고 싶다면 아래와 같이 삭제 후 재 실행을 해야 한다.
set CMAKE_ARGS="-DLLAMA_CUBLAS=on"
set FORCE_CMAKE=1
pip install llama-cpp-python
Step 3. LangChain 이용하기
pip install langchain
pip install -U langchain-community
아래 코드를 입력하고 실행을 하면 아래 이미지와 같은 답변을 준다.
from langchain.llms import LlamaCpp
llm = LlamaCpp(
model_path="./selfrag_llama2_7b-q8_0.gguf",
n_ctx=512,
n_batch=512,
n_gpu_layers=35,
verbose=True,
)
prompt = """
Question: What are the names of the planets in the solar system? Answer:
"""
print(llm(prompt))
'Development > AI' 카테고리의 다른 글
[AI] Colab 에서 Hugging Face 토큰 오류 해결 (0) | 2024.02.13 |
---|---|
[AI] Google Gemini API Key 발급 방법 (0) | 2024.01.30 |
[AI] OpenAI Key 발급 하기 (0) | 2024.01.30 |
댓글