머신러닝AI2024. 4. 27. 17:16

오픈소스 LLM 모델을 local에서 띄워서 구동하는 가장 손쉬운 방법은 ollama 이고 지난번에 소개한 적이 있다.

https://infoengineer.tistory.com/135

macos에서도 구동되기 때문에 너무나 간단하게 llama3, gemma, phi-3, command-r 등 어느정도 한글이 되는 모델들을 다운로드 받아서 구동시킬 수 있다.

 

그리고 일단 이렇게 구동되면 langchain과도 바로 연결된다.

 

$ ollama run llama3:instruct
>>> give me a joke
Here's one:
Why don't eggs tell jokes?
(wait for it...)
Because they'd crack each other up!
Hope that made you smile!
>>> Send a message (/? for help)

 

이렇게 구동이 되면 내부에 API 서버가 이미 구동되어 대기 상태가 된다(ollama serve 명령으로도 띄우는 것이 가능하다)

이후에는 아래와 같이 시험해볼 수 있다.

 

$ curl http://localhost:11434/api/generate -d '{
  "model": "llama3",
  "prompt":"Why is the sky blue?"
}'
{"model":"llama3","created_at":"2024-04-27T08:10:00.736586071Z","response":"What","done":false}
{"model":"llama3","created_at":"2024-04-27T08:10:00.746743823Z","response":" a","done":false}
{"model":"llama3","created_at":"2024-04-27T08:10:00.757109205Z","response":" great","done":false}
{"model":"llama3","created_at":"2024-04-27T08:10:00.768475258Z","response":" question","done":false}
....

{"model":"llama3","created_at":"2024-04-27T08:10:04.094431458Z","response":".","done":false}
{"model":"llama3","created_at":"2024-04-27T08:10:04.104777568Z","response":"","done":true,"context":[128006,.....,128009],"total_duration":3490085755,"load_duration":2410324,"prompt_eval_count":11,"prompt_eval_duration":75044000,"eval_count":327,"eval_duration":3368118000}

 

아니면 이제 langchain을 사용할 수도 있다.

 

$ cat > langchain_ollama_stream.py 
from langchain.callbacks.manager import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain_community.llms import Ollama

llm = Ollama(
    model="llama3", callback_manager=CallbackManager([StreamingStdOutCallbackHandler()])
)
llm("The first man on the summit of Mount Everest, the highest peak on Earth, was ...")

$ python3 langchain_ollama_stream.py 

....

....

 

그 외에도 다양한 방식의 langchain사용이 가능하다. 잘 응용해서 사용해보자.

 

 

1) 간단한 invoke

 

from langchain_community.llms import Ollama

llm = Ollama(model="llama3")

llm.invoke("Tell me a joke")

 

 

2) 간단한 stream 형식의 출력

 

from langchain_community.llms import Ollama

llm = Ollama(model="llama3")

query = "Tell me a joke"

for chunks in llm.stream(query):
    print(chunks, end="")

 

반응형
Posted by 작동미학