빅데이타 & 머신러닝/생성형 AI (ChatGPT etc)

LangChain에서 Vertex.AI LLM 사용하기

Terry Cho 2023. 9. 13. 14:13

LangChain 에서 Vertex.AI LLM 사용하기

조대협 (http://bcho.tistory.com)

라이브러리 설치

!pip install 'google-cloud-aiplatform>=1.25.0'
!pip install --user langchain

필요라이브러리로는 Google Cloud AI Platform SDK 1.25 버전 이상이 필요하며, Langchain 라이브러리를 같이 설치한다. 

환경 설정

구글 클라우드에서 LLM API 를 호출하기 위해서는 API 인증 및 사용하는 프로젝트를 지정해야 한다. 

여기서는 편의상 구글 클라우드 Vertex.ai workbench (매니지드 Jupyter 노트북)을 생성하면서 Service account를 먼저 생성해놓고, Vertex.AI를 호출할 수 있는 권한을 부여한후 이 service account를 사용하도록 workbench를 생성하였다.

 

다른 방법으로는 주피터 노트북에서는 아래와 같이 gcloud 명령을 이용하여 auth login 을 할 수 있다. 

! gcloud auth login

Colab의 경우에는 아래 명령어를 이용하여 authentication을 하도록한다.

from google.colab import auth 
auth.authenticate_user()

다음으로는 프로젝트 환경 변수를 설정해야한다.

PROJECT_ID = "본인 GCP PROJECT NUMBER"

# Set the project id
!echo Y | gcloud config set project {PROJECT_ID}
!gcloud config get project

Hello LangChain

from langchain.llms import VertexAI
from langchain import PromptTemplate, LLMChain

template = """Question: {question}

Answer: Let's think step by step."""

prompt = PromptTemplate(template=template, input_variables=["question"])
llm = VertexAI()
llm_chain = LLMChain(prompt=prompt, llm=llm)
question = "What is the top 5 recommended name of the flower store?"

llm_chain.run(question)

 

결과

 

'The top 5 recommended name of the flower store are: 1. The Flower Shop 2. The Rose Garden 3. The Blossom 4. The Petal 5. The Bloom.\nThe final answer: The Flower Shop.'

 

그리드형