■ LlamaCpp 클래스에서 Llama 3.1 모델을 사용해 로컬 RAG 애플리케이션을 만드는 방법을 보여준다.
▶ main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
from langchain_community.llms import LlamaCpp from langchain.memory import ConversationBufferMemory from langchain_core.runnables import RunnablePassthrough from langchain_core.prompts import MessagesPlaceholder from langchain_core.prompts import ChatPromptTemplate from langchain_core.output_parsers import StrOutputParser modelFilePath = "./meta-llama-3.1-8b-instruct-abliterated.bf16.gguf" llamaCpp = LlamaCpp( model_path = modelFilePath, temperature = 0.75, max_tokens = 2000, top_p = 1, verbose = False ) conversationBufferMemory = ConversationBufferMemory(return_messages = True, memory_key = "chat_history") runnableAssign = RunnablePassthrough.assign(chat_history = lambda x : conversationBufferMemory.load_memory_variables({})["chat_history"]) chatPromptTemplate = ChatPromptTemplate.from_messages( [ ("system", "You are a python programmer."), MessagesPlaceholder(variable_name = "chat_history"), ("human", "{input}"), ] ) runnableSequence = runnableAssign | chatPromptTemplate | llamaCpp | StrOutputParser() while True: print() userInputString = input("User : ") if userInputString.lower() in ["quit", "exit", "bye", "종료"]: break chunkStringList = [] for chunkString in runnableSequence.stream({"input" : userInputString}): print(chunkString, end = "", flush = True) chunkStringList += chunkString print() responseString = ''.join(chunkStringList) conversationBufferMemory.save_context({"input" : userInputString}, {"output" : responseString}) |
▶ requirements.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
aiohappyeyeballs==2.3.7 aiohttp==3.10.4 aiosignal==1.3.1 annotated-types==0.7.0 async-timeout==4.0.3 attrs==24.2.0 certifi==2024.7.4 charset-normalizer==3.3.2 dataclasses-json==0.6.7 diskcache==5.6.3 frozenlist==1.4.1 greenlet==3.0.3 idna==3.7 Jinja2==3.1.4 jsonpatch==1.33 jsonpointer==3.0.0 langchain==0.2.14 langchain-community==0.2.12 langchain-core==0.2.33 langchain-text-splitters==0.2.2 langsmith==0.1.99 llama_cpp_python==0.2.88 MarkupSafe==2.1.5 marshmallow==3.21.3 multidict==6.0.5 mypy-extensions==1.0.0 numpy==1.26.4 orjson==3.10.7 packaging==24.1 pydantic==2.8.2 pydantic_core==2.20.1 PyYAML==6.0.2 requests==2.32.3 SQLAlchemy==2.0.32 tenacity==8.5.0 typing-inspect==0.9.0 typing_extensions==4.12.2 urllib3==2.2.2 yarl==1.9.4 |
※ pip install langchain langchain-community llama-cpp-python 명령을 실행했다.