🤖 AI/LLM

[LLM] 올라마(Ollama)

heywantodo 2024. 6. 21. 16:06
728x90
반응형

저번 포스팅에서 LM Studio에 대해서 간략하게 설명을 했었는데,

또다른 로컬 LLM을 활용할 수 있는 툴인 올라마에 대해서 알아보고자 한다.

 

올라마(Ollama)

올라마는 GUI인 LM Studio와 달리 CLI로 로컬 LLM을 구성하여 활용할 수 있다.

 Mistral, Llama 3 등 다양한 오픈소스 LLM을 지원한다.

 

올라마에서 지원하는 모델은 아래에서 확인 할 수 있다.

 

library

Get up and running with large language models.

ollama.com

+) Open WebUI도 지원한다.

 

GitHub - open-webui/open-webui: User-friendly WebUI for LLMs (Formerly Ollama WebUI)

User-friendly WebUI for LLMs (Formerly Ollama WebUI) - open-webui/open-webui

github.com

git clone https://github.com/open-webui/open-webui.git
cd open-webui

npm i
npm run build

cd backend
pip install -r requirementes.txt

sh start.sh

 

설치

아래 링크에서 운영체제에 맞는 설치파일을 다운로드 받는다.

 

Download Ollama on macOS

Download Ollama on macOS

ollama.com

 

설치 후 다음 명령어를 터미널에 입력해주면, 설치가 잘 완료된 것을 확인할 수 있다. 

ollama

 

모델 다운로드

모델을 다운로드하는 명령은 아래와 같다.

ollama run <모델명>

모델 실행 없이 다운로드하여 설치하려면 pull 명령어를 사용한다.

ollama pull <모델명>

설치된 모델의 목록을 확인하려면 list 명령어를 사용한다.

ollama list

 

나는 가장 부담이 적은 모델인 gemma:2b를 설치했다.

ollama run gemma:2b
ollama list

 

올라마 모델에 랭체인 적용

모델 적용 전, dependency 패키지를 먼저 설치한다.

pip install langchain_community.llms

 

langchain_community.llms 모듈에서 Ollama 클래스를 사용하여 gemma:latest 모델을 로드한다.

로드 후 모델을 사용하여 "오늘 서울 날씨는?" 이라는 질문에 대한 답변을 요청한다.

from langchain_community.llms import Ollama

llm = Ollama(model="gemma:latest")
response = llm.invoke("오늘 서울 날씨는?")

 

특정 형식의 프롬프트 템플릿을 생성하기 위해선,  ChatPromptTemplet 클래스를 이용할 수 있다.

이 템플릿은 문자열을 기반으로 한다.

 

연산자를 이용한 LCEL(Language Chain Execution Language) 구문을 활용하여 prompt 템플릿과 llm 모델 인스턴스를 연결하여 하나의 실행 체인을 생성한다. 

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_template('''You are an expert in astronomy. 
Answer the question. <Question>: {input}''')

chain = prompt | llm
response = chain.invoke({"input": "오늘 서울 날씨는?"})

 

참고

https://wikidocs.net/233805

728x90
반응형