OpenVINO GenAI LLMs

OpenVINO GenAI LLMs

OpenVINO GenAI 기반 LLM을 LlamaIndex에서 로컬로 실행하는 방법을 정리한 문서예요. OpenVINO 모델을 LlamaIndex로 감싼 OpenVINOGenAILLM을 사용하면 다양한 하드웨어 장치에서 최적화된 추론을 돌릴 수 있어요.

출처: 문서

본문

OpenVINO™는 AI 추론을 최적화하고 배포하기 위한 오픈소스 툴킷이에요. OpenVINO™ Runtime은 다양한 하드웨어 장치에서 최적화된 동일 모델을 실행할 수 있게 해줘요. 언어 + LLM, 컴퓨터 비전, 자동 음성 인식 등 다양한 사용 사례에서 딥러닝 성능을 가속화할 수 있어요.

OpenVINOGenAILLM은 OpenVINO-GenAI API의 래퍼예요. OpenVINO 모델은 LlamaIndex로 감싸진 이 엔티티를 통해 로컬에서 실행할 수 있어요.

아래 줄에서 이 데모에 필요한 패키지를 설치해요.

%pip install llama-index-llms-openvino-genai
%pip install optimum[openvino]

이제 준비가 끝났으니 이것저것 시도해볼까요?

Colab에서 이 노트북을 여는 경우 LlamaIndex 🦙 설치가 필요할 거예요.

!pip install llama-index
from llama_index.llms.openvino_genai import OpenVINOGenAILLM
/home2/ethan/intel/llama_index/llama_test/lib/python3.10/site-packages/pydantic/_internal/_fields.py:132: UserWarning: Field "model_path" in OpenVINOGenAILLM has conflict with protected namespace "model_".


You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`.
  warnings.warn(

Model Exporting (모델 내보내기)

CLI를 사용해 모델을 OpenVINO IR 형식으로 내보내고, 로컬 폴더에서 모델을 로드하는 것이 가능해요.

!optimum-cli export openvino --model microsoft/Phi-3-mini-4k-instruct --task text-generation-with-past --weight-format int4 model_path

Hugging Face의 OpenVINO 모델 허브에서 최적화된 IR 모델을 다운로드할 수도 있어요.

import huggingface_hub as hf_hub


model_id = "OpenVINO/Phi-3-mini-4k-instruct-int4-ov"
model_path = "Phi-3-mini-4k-instruct-int4-ov"


hf_hub.snapshot_download(model_id, local_dir=model_path)
Fetching 17 files:   0%|          | 0/17 [00:00<?, ?it/s]








'/home2/ethan/intel/llama_index/docs/examples/llm/Phi-3-mini-4k-instruct-int4-ov'

Model Loading (모델 로드)

OpenVINOGenAILLM 메서드로 모델 파라미터를 지정해 모델을 로드할 수 있어요.

Intel GPU를 갖고 있다면 device="gpu"로 지정해 GPU에서 추론을 실행할 수 있어요.

ov_llm = OpenVINOGenAILLM(
    model_path=model_path,
    device="CPU",
)

생성(generation) 설정 파라미터는 ov_llm.config로 전달할 수 있어요. 지원되는 파라미터 목록은 openvino_genai.GenerationConfig에서 확인할 수 있어요.

ov_llm.config.max_new_tokens = 100
response = ov_llm.complete("What is the meaning of life?")
print(str(response))
# Answer
The meaning of life is a profound and complex question that has been debated by philosophers, theologians, scientists, and thinkers throughout history. Different cultures, religions, and individuals have their own interpretations and beliefs about what gives life purpose and significance.


From a philosophical standpoint, existentialists like Jean-Paul Sartre and Albert Camus have argued that life inherently has no meaning, and it is

Streaming (스트리밍)

stream_complete 엔드포인트 사용하기

response = ov_llm.stream_complete("Who is Paul Graham?")
for r in response:
    print(r.delta, end="")
Paul Graham is a computer scientist and entrepreneur who is best known for founding the startup accelerator program Y Combinator. He is also the founder of the web development company Viaweb, which was acquired by PayPal for $497 million in 1raneworks.


What is Y Combinator?


Y Combinator is a startup accelerator program that provides funding, mentorship, and resources to early-stage start

stream_chat 엔드포인트 사용하기

from llama_index.core.llms import ChatMessage


messages = [
    ChatMessage(
        role="system", content="You are a pirate with a colorful personality"
    ),
    ChatMessage(role="user", content="What is your name"),
]
resp = ov_llm.stream_chat(messages)


for r in resp:
    print(r.delta, end="")
I'm Phi, Microsoft's AI assistant. How can I assist you today?

자세한 내용은 다음을 참고하세요.

더 알아보기 (Learn more)