귀엽고 편하고 짱이고 레릿고 🦙Ollama🦙 사용해보기(feat. llama3.2)

Joshua·2024년 9월 27일
2

LLM

목록 보기
2/6
post-thumbnail

오늘도 열심히 LLM 공부를 해보자🧐 !
어렴풋하게 알고는 있었지만 잘 몰랐던 Ollama에 대해서 알아보려 한다
요즘 LLM 공부를 한다고 했더니 동료분이 Ollama도 공부해보라며 주셨다
이런 공부거리 주는거 넘 좋아 감사합니다🤗
홈페이지 보자마자 너무 해보고 싶어 ! 내가 직접 만져볼래🦙🦙🦙 !


1. Ollama 란?

Ollama는 온프레미스(On-Premises) AI 모델을 쉽게 실행하고 관리할 수 있게 해주는 LLMOps 플랫폼이다.
Ollama를 사용하면 클라우드가 아닌 로컬 환경에서 LLM을 실행할 수 있어 데이터 프라이버시와 보안 요구 사항을 충족하는 데 유용하다.
AI 모델을 설치, 관리, 업데이트하는 과정을 간소화해주는 도구를 제공하며,
특정 AI 모델을 사용하는 애플리케이션 개발이나 실험을 쉽게 할 수 있다.

1) 주요 특징은 ?

  • 로컬에서 AI 모델 실행: 클라우드 환경에 의존하지 않고, 자신의 로컬 컴퓨터나 서버에서 모델을 실행할 수 있다.
  • 보안 및 프라이버시 강화: 민감 데이터를 외부 클라우드로 보내지 않으므로, 데이터 보안이 중요한 프로젝트에서 유용.
    -> 특히 데이터 프라이버시가 중요한 산업에서 안전하게 AI를 활용하려는 경우에 적합✨ !!
  • 쉬운 설치 및 관리: 복잡한 설치 과정 없이 AI 모델을 쉽게 사용할 수 있다.

2) 어떤 모델을 쓸 수 있어 ?

llama 모델만 쓸 수 있을 줄 알았는데

https://ollama.com/library
생각보다 다양한 모델들이 많다 !
보니까 또 엄청 쓰고 싶어졌음 🫡

어디 얼마나 간편하고 쉬운지 직접 알아봐야지 !!

2. Ollama을 사용해보자 !!

1) Ollama 설치

Ollama는 Mac, Windows, Linux에서 사용이 가능하다
기본적으로 터미널 명령어를 통해 설치함 !!

  • MacOS

    brew install ollama
  • Windows 및 Linux
    현재 Ollama는 Windows에서 직접적으로 지원되지 않는다 ㅠ 그래서 wsl을 이용해서 설치하면 된다 !
    둘 다 명령어는 같음

curl -fsSL https://ollama.com/install.sh | sh


window 환경인 나는 wsl 부터 깔고.... ^ ^;;
없는줄 몰랐다 헿🤭

두근두근 열심히 까는 중
용량이 커서 인지 설치하는데 시간이 꽤 걸린다

이런 화면이 나오면 설치 완료 !


2) 모델 다운로드

ollama pull <model_name>

드디어 !!!! LLM 모델을 사용해볼 수 있는 기회가 !!!
어디 한번 직접 해볼까나요✊
일단 내가 써보고 싶었던 llama2 모델을 로컬 환경으로 다운로드 해본다

model pulling success!!


3) wsl에서 모델 실행

바아아아로 실행 가즈아ㅏㅏㅏ🚗🚙🛻🚌🚎

ollama run llama2

어떻게 사용하는 거냐면요

?

아 뭐야
메모리 용량 부족 ㅠㅠ

좀 더 작고 어차피 !!!!! 쓰고 싶었던건 llama3.2였다 이말이야 !!!!! 소중한 llama3.2 를 설치해본다


모델을 사용하는 방법은 2가지가 있는데,

ollama run llama3.2 "what is love?"

이렇게 입력하면

이런식으로 bash로 결과를 확인할 수 있다


ollama run llama3.2 

이렇게 입력하면

메모장에서 대화하는 느낌으로 대화할 수 dlT다
편한 방법으로 하면 되는데,
한국어는 왜 저렇게 이상해..? 하고 모델을 좀 찾아보니까

아하... 한국어가 지원이 안되는거였다😥
그냥 열심히 번역을 돌리는 거였음 ㅠㅠ
한국어보다 영어로 대화하는게 흐름이 자연스럽다

/?를 입력해서 사용할 수 있는 명령어도 볼 수 있다

/?
Available Commands:
/set Set session variables
/show Show model information
/load [model] Load a session or model
/save [model] Save your current session
/clear Clear session context
/bye Exit
/?, /help Help for a command
/? shortcuts Help for keyboard shortcuts

내가 대화하던 모델을 저장했다

'>>> /save engchat
Created new model 'engchat'

그럼 모델 리스트에 저장되고,

ollama run engchat

을 입력하면 내가 대화했던 내용이 나오면서 모델과 대화할 수 있게 된다

  • 몇번 테스트 해보니, 첫 번째 방법은 문맥유지가 안되는 걸 발견했다. 하긴 그렇겠지?

4) python에서 모델 실행

물론, 파이썬에서도 가능하다📑

  • langchain 사용
    ollama와 모델을 저장했다면, langchain을 이용하여 바로 사용할 수 있다.

https://brunch.co.kr/@b2439ea8fc654b8/28

이 분의 코드를 참고했다🥰 !!

from langchain_community.llms import Ollama
from langchain import PromptTemplate
llm = Ollama(model="llama3.2", stop=["<|eot_id|>"])
def get_model_response(user_prompt, system_prompt):
    template = """
        <|begin_of_text|>
		<|start_hearder_id|>system<|end_hearder_id|>
        {system_prompt}
        <|eot_id|>
        <|start_hearder_id|>user<|end_hearder_id|>
        {user_prompt}
        <|eot_id|>
		<|start_hearder_id|>assistant<|end_hearder_id|>
    """

    prompt = PromptTemplate(
        input_variables=["system_prompt", "user_prompt"],
        template=template
    )

    response = llm(prompt.format(system_prompt=system_prompt, user_prompt = user_prompt))

        # 결과 출력
    print("===== 시스템 프롬프트 =====")
    print(system_prompt)
    print("\n===== 사용자 질문 =====")
    print(user_prompt)
    print("\n===== 모델 응답 =====")
    print(response)

    return response

system_prompt = "당신은 초등학교 과학 교사입니다. 현상에 대해 친절하게 이론과 예시를 설명해주세요"
user_prompt = "수증기가 뭔가요?"
get_model_response(user_prompt, system_prompt)

이 코드에 대한 답변이다

===== 시스템 프롬프트 =====
당신은 초등학교 과학 교사입니다. 현상에 대해 친절하게 이론과 예시를 설명해주세요
===== 사용자 질문 =====
수증기가 뭔가요?
===== 모델 응답 =====
수증기란 물과 공기를 결합한 상태의 가스입니다. 수증기는 생명체에 essential하다는 뜻으로 중요합니다.
수증기와 물은 상호작용을 하면서 물에서 수분기가 분리되어 수증기로 만든다.
수증기는 아름답고 깨끗한 공기를 주는气성으로, 기온이 0°C 이내가 되면 물을 가스화시켜서 수증기로 만듭니다.
수증기가 가득한 환경은 건강하다는 의미가 아니라, 불ruh하고 아름답다.
'수증기란 물과 공기를 결합한 상태의 가스입니다. 수증기는 생명체에 essential하다는 뜻으로 중요합니다.\n\n수증기와 물은 상호작용을 하면서 물에서 수분기가 분리되어 수증기로 만든다.

역시 한국어는 좀 어색하다 ^^;;ㅎㅎ
영어로 질문하면 엄청 깔끔한 답변을 받을 수 있다!

===== 시스템 프롬프트 =====
You are an elementary school science teacher. Please kindly explain the theory and examples of the phenomenon.
===== 사용자 질문 =====
What is water vapor?
===== 모델 응답 =====
Hello, young scientists! Today we're going to explore one of the most fascinating substances in our world: water vapor!
What is Water Vapor?
Water vapor is a type of gas that is formed when liquid water (like the water you drink or the water in your aquarium) evaporates. Evaporation is the process by which a liquid changes state to become a gas.
When sunlight hits the surface of a body of water, such as a lake or ocean, it heats up the water molecules and causes them to move faster and faster. As they gain energy, some of the water molecules break free from the surface tension of the water and turn into vapor. This process is called evaporation.
Examples of Water Vapor:
1. Condensation: When you breathe onto a cold window or mirror, you'll often see your breath fog up. This is because the warm air from your breath contains water vapor that condenses onto the cool surface, forming tiny droplets of water.
2. Steam: Have you ever taken a hot shower and seen steam rising from the drain? That's water vapor! When the hot water hits the cold air, it turns back into liquid water, creating steam.
3. Clouds: Clouds are made up of water vapor suspended in the air. When the air is cool enough, the water vapor in the clouds condenses onto tiny particles in the air, forming visible clouds.


pip install ollama

일단 ollama 라이브러리를 install 하고

import ollama
response = ollama.chat(model = 'llama3.2', messages=[
    {
        'role': 'user',
        'content' : 'What is LLM?'
    },
])
print(response['message']['content'])

기본적인 코드를 작성한 뒤 질문을 해본다

LLM stands for Large Language Model. It's a type of artificial intelligence (AI) model that's designed to process and understand human language at a massive scale.

Large Language Models are trained on vast amounts of text data, which allows them to learn patterns and relationships in language. This training enables the model to generate human-like text, respond to questions, and even engage in conversations.

꽤 괜찮은 답변이 나왔다 ! (답변이 너무 길어서 1/3만 가져왔다)
티스토리를 보다가 stream 방식으로도 구현이 가능한 걸 보고 바로 적용해봤다😎

import ollama
# 스트림 방식 구현(한글자씩 출력)
stream = ollama.chat(model = 'llama3.2', messages=[
                        {
                            'role': 'user',
                            'content' : 'What is LLM?'
                        }],
                    stream = True                 
                    )

for chunk in stream:
    print(chunk['message']['content'], end='', flush=True)

사진이라 보여줄 수 없지만 잘 구현되는 걸 보니 괜시리 뿌듯했다 ㅠㅠ


5) 번외

Ollama를 설치하고 사용 중일 때

http://localhost:11434/

들어가면 이런 화면이 나온다. 그냥 귀여워서🤭


3. 모델 관리

Ollama는 모델을 쉽게 관리하고 업데이트할 수 있도록 다양한 명령어를 제공한다.

1) 모델 업데이트

ollama update llama2 

2) 모델 리스트 확인

ollama list

3) 모델 삭제

ollama rm llama2

4. 느낀점

일단 엄청 편하다 !!!
AWS 환경에서도 물론 편하게 사용할 수 있지만
초보자가 쓰기엔 알아야 할 내용이 많은데
이건 바로 로컬에서 설치해서 여러가지 모델을 해볼 수 있다니
배포까지 학습하려고 했으나 머리가 터질 것 같아서
배포 부분은 나중에 따로 포스팅하려고 한다 !

뭐 이건 개인적인 거지만 이렇게 하나씩 알아갈수록 더 좋은 성능의 장비들을 가지고 싶다 ㅠㅠ
메모리 다 쓸까봐 조마조마해하면서 했음🤣🤣🤣ㅋㅋㅋㅋㅋㅋ
명령어 몇개로도 모델을 쉽게 테스트할 수 있다는 점에서 많이 애용할 것 같다

profile
🐥삐약이 개발자🐤

0개의 댓글

관련 채용 정보