เริ่มต้นใช้งาน Gemma และ LangChain

ดูใน ai.google.dev เรียกใช้ใน Google Colab ดูซอร์สบน GitHub

บทแนะนํานี้จะแสดงวิธีเริ่มต้นใช้งาน Gemma และ LangChain ซึ่งทํางานใน Google Cloud หรือในสภาพแวดล้อม Colab ของคุณ Gemma เป็นกลุ่มโมเดลแบบเปิดที่ทันสมัยและมีน้ำหนักเบา ซึ่งสร้างขึ้นจากการวิจัยและเทคโนโลยีเดียวกันกับที่ใช้สร้างโมเดล Gemini LangChain เป็นเฟรมเวิร์กสำหรับการสร้างและใช้แอปพลิเคชันที่คำนึงถึงบริบทซึ่งรองรับโดยโมเดลภาษา

เรียกใช้ Gemma ใน Google Cloud

แพ็กเกจ langchain-google-vertexai มีการผสานรวม LangChain กับโมเดล Google Cloud

ติดตั้งการอ้างอิง

pip install --upgrade -q langchain langchain-google-vertexai

ตรวจสอบสิทธิ์

คุณต้องตรวจสอบสิทธิ์ เว้นแต่ว่าคุณกำลังใช้ Colab Enterprise

from google.colab import auth
auth.authenticate_user()

ทำให้โมเดลใช้งานได้

Vertex AI เป็นแพลตฟอร์มสำหรับการฝึกและใช้งานโมเดลและแอปพลิเคชัน AI Model Garden คือคอลเล็กชันโมเดลที่ได้รับการดูแลจัดการซึ่งสามารถสำรวจได้ในคอนโซล Google Cloud

หากต้องการทำให้ Gemma ใช้งานได้ ให้เปิดโมเดลใน Model Garden สำหรับ Vertex AI แล้วทำตามขั้นตอนต่อไปนี้

  1. เลือกทำให้ใช้งานได้
  2. ทำการเปลี่ยนแปลงในช่องแบบฟอร์มการทำให้ใช้งานได้ตามที่ต้องการ หรือปล่อยไว้ตามเดิม หากยอมรับค่าเริ่มต้นได้ จดช่องต่อไปนี้ไว้ ซึ่งคุณจะต้องใช้ในภายหลัง
    • ชื่อปลายทาง (เช่น google_gemma-7b-it-mg-one-click-deploy)
    • ภูมิภาค (เช่น us-west1)
  3. เลือกทำให้ใช้งานได้เพื่อทำให้โมเดลใช้งานได้กับ Vertex AI การทำให้ใช้งานได้จะใช้เวลา ไม่กี่นาทีจึงจะเสร็จสมบูรณ์

เมื่อปลายทางพร้อมแล้ว ให้คัดลอกรหัสโปรเจ็กต์ รหัสปลายทาง และตําแหน่ง แล้วป้อนเป็นพารามิเตอร์

# @title Basic parameters
project: str = ""  # @param {type:"string"}
endpoint_id: str = ""  # @param {type:"string"}
location: str = "" # @param {type:"string"}

เรียกใช้โมเดล

from langchain_google_vertexai import GemmaVertexAIModelGarden, GemmaChatVertexAIModelGarden

llm = GemmaVertexAIModelGarden(
    endpoint_id=endpoint_id,
    project=project,
    location=location,
)

output = llm.invoke("What is the meaning of life?")
print(output)
Prompt:
What is the meaning of life?
Output:
Life is a complex and multifaceted phenomenon that has fascinated philosophers, scientists, and

นอกจากนี้ คุณยังสามารถใช้ Gemma สำหรับการแชทแบบหลายมุมมอง โดยทำดังนี้

from langchain_core.messages import (
    HumanMessage
)

llm = GemmaChatVertexAIModelGarden(
    endpoint_id=endpoint_id,
    project=project,
    location=location,
)

message1 = HumanMessage(content="How much is 2+2?")
answer1 = llm.invoke([message1])
print(answer1)

message2 = HumanMessage(content="How much is 3+3?")
answer2 = llm.invoke([message1, answer1, message2])

print(answer2)
content='Prompt:\n<start_of_turn>user\nHow much is 2+2?<end_of_turn>\n<start_of_turn>model\nOutput:\nSure, the answer is 4.\n\n2 + 2 = 4'
content='Prompt:\n<start_of_turn>user\nHow much is 2+2?<end_of_turn>\n<start_of_turn>model\nPrompt:\n<start_of_turn>user\nHow much is 2+2?<end_of_turn>\n<start_of_turn>model\nOutput:\nSure, the answer is 4.\n\n2 + 2 = 4<end_of_turn>\n<start_of_turn>user\nHow much is 3+3?<end_of_turn>\n<start_of_turn>model\nOutput:\nSure, the answer is 6.\n\n3 + 3 = 6'

คุณหลีกเลี่ยงการตอบซ้ำหลังขั้นตอนได้โดยทำดังนี้

answer1 = llm.invoke([message1], parse_response=True)
print(answer1)

answer2 = llm.invoke([message1, answer1, message2], parse_response=True)

print(answer2)
content='Output:\nSure, here is the answer:\n\n2 + 2 = 4'
content='Output:\nSure, here is the answer:\n\n3 + 3 = 6<'

เรียกใช้ Gemma จากการดาวน์โหลด Kaggle

ส่วนนี้จะแสดงวิธีดาวน์โหลด Gemma จาก Kaggle แล้วเรียกใช้โมเดล

หากต้องการกรอกข้อมูลในส่วนนี้ ก่อนอื่น คุณจะต้องทำตามวิธีการตั้งค่าที่การตั้งค่า Gemma

จากนั้นไปที่ส่วนถัดไป ซึ่งคุณจะตั้งค่าตัวแปรสภาพแวดล้อมสำหรับสภาพแวดล้อม Colab

ตั้งค่าตัวแปรสภาพแวดล้อม

ตั้งค่าตัวแปรสภาพแวดล้อมสำหรับ KAGGLE_USERNAME และ KAGGLE_KEY

import os
from google.colab import userdata

# Note: `userdata.get` is a Colab API. If you're not using Colab, set the env
# vars as appropriate for your system.
os.environ["KAGGLE_USERNAME"] = userdata.get('KAGGLE_USERNAME')
os.environ["KAGGLE_KEY"] = userdata.get('KAGGLE_KEY')

ติดตั้งการอ้างอิง

# Install Keras 3 last. See https://keras.io/getting_started/ for more details.
pip install -q -U keras-nlp
pip install -q -U keras>=3

เรียกใช้โมเดล

from langchain_google_vertexai import GemmaLocalKaggle

คุณระบุแบ็กเอนด์ของ Keras ได้ (ค่าเริ่มต้นคือ tensorflow แต่เปลี่ยนเป็น jax หรือ torch)

# @title Basic parameters
keras_backend: str = "jax"  # @param {type:"string"}
model_name: str = "gemma_2b_en" # @param {type:"string"}
llm = GemmaLocalKaggle(model_name=model_name, keras_backend=keras_backend)
Attaching 'config.json' from model 'keras/gemma/keras/gemma_2b_en/2' to your Colab notebook...
Attaching 'config.json' from model 'keras/gemma/keras/gemma_2b_en/2' to your Colab notebook...
Attaching 'model.weights.h5' from model 'keras/gemma/keras/gemma_2b_en/2' to your Colab notebook...
Attaching 'tokenizer.json' from model 'keras/gemma/keras/gemma_2b_en/2' to your Colab notebook...
Attaching 'assets/tokenizer/vocabulary.spm' from model 'keras/gemma/keras/gemma_2b_en/2' to your Colab notebook...
output = llm.invoke("What is the meaning of life?", max_tokens=30)
print(output)
What is the meaning of life?

The question is one of the most important questions in the world.

It’s the question that has

เรียกใช้โมเดลแชท

ดังเช่นในตัวอย่าง Google Cloud ด้านบน คุณสามารถใช้การติดตั้งใช้งานในเครื่องของ Gemma สำหรับการแชทแบบมัลติเทิร์น คุณอาจต้องรีสตาร์ทสมุดบันทึกและทำความสะอาดหน่วยความจำ GPU เพื่อหลีกเลี่ยงข้อผิดพลาด OOM

from langchain_google_vertexai import GemmaChatLocalKaggle
# @title Basic parameters
keras_backend: str = "jax"  # @param {type:"string"}
model_name: str = "gemma_2b_en" # @param {type:"string"}
llm = GemmaChatLocalKaggle(model_name=model_name, keras_backend=keras_backend)
Attaching 'config.json' from model 'keras/gemma/keras/gemma_2b_en/2' to your Colab notebook...
Attaching 'config.json' from model 'keras/gemma/keras/gemma_2b_en/2' to your Colab notebook...
Attaching 'model.weights.h5' from model 'keras/gemma/keras/gemma_2b_en/2' to your Colab notebook...
Attaching 'tokenizer.json' from model 'keras/gemma/keras/gemma_2b_en/2' to your Colab notebook...
Attaching 'assets/tokenizer/vocabulary.spm' from model 'keras/gemma/keras/gemma_2b_en/2' to your Colab notebook...
from langchain_core.messages import (
    HumanMessage
)

message1 = HumanMessage(content="Hi! Who are you?")
answer1 = llm.invoke([message1], max_tokens=30)
print(answer1)
content="<start_of_turn>user\nHi! Who are you?<end_of_turn>\n<start_of_turn>model\nI'm a model.\n Tampoco\nI'm a model."
message2 = HumanMessage(content="What can you help me with?")
answer2 = llm.invoke([message1, answer1, message2], max_tokens=60)

print(answer2)
content="<start_of_turn>user\nHi! Who are you?<end_of_turn>\n<start_of_turn>model\n<start_of_turn>user\nHi! Who are you?<end_of_turn>\n<start_of_turn>model\nI'm a model.\n Tampoco\nI'm a model.<end_of_turn>\n<start_of_turn>user\nWhat can you help me with?<end_of_turn>\n<start_of_turn>model"

คุณประมวลผลคำตอบหลังคำตอบได้หากต้องการหลีกเลี่ยงคำสั่งแบบหลายรายการ ดังนี้

answer1 = llm.invoke([message1], max_tokens=30, parse_response=True)
print(answer1)

answer2 = llm.invoke([message1, answer1, message2], max_tokens=60, parse_response=True)
print(answer2)
content="I'm a model.\n Tampoco\nI'm a model."
content='I can help you with your modeling.\n Tampoco\nI can'

Run Gemma จากการดาวน์โหลด Hugging Face

ตั้งค่า

เช่นเดียวกับ Kaggle ใบหน้ากอดต้องการให้คุณยอมรับข้อกำหนดและเงื่อนไขของ Gemma ก่อนที่จะเข้าถึงโมเดล หากต้องการเข้าถึง Gemma ผ่านการกอดใบหน้า ให้ไปที่การ์ดโมเดลของ Gemma

นอกจากนี้คุณจะต้องได้รับโทเค็นเพื่อการเข้าถึงของผู้ใช้ที่มีสิทธิ์อ่าน ซึ่งคุณป้อนได้ด้านล่าง

# @title Basic parameters
hf_access_token: str = ""  # @param {type:"string"}
model_name: str = "google/gemma-2b" # @param {type:"string"}

เรียกใช้โมเดล

from langchain_google_vertexai import GemmaLocalHF, GemmaChatLocalHF
llm = GemmaLocalHF(model_name="google/gemma-2b", hf_access_token=hf_access_token)
tokenizer_config.json:   0%|          | 0.00/1.11k [00:00<?, ?B/s]
tokenizer.model:   0%|          | 0.00/4.24M [00:00<?, ?B/s]
tokenizer.json:   0%|          | 0.00/17.5M [00:00<?, ?B/s]
special_tokens_map.json:   0%|          | 0.00/555 [00:00<?, ?B/s]
config.json:   0%|          | 0.00/627 [00:00<?, ?B/s]
model.safetensors.index.json:   0%|          | 0.00/13.5k [00:00<?, ?B/s]
Downloading shards:   0%|          | 0/2 [00:00<?, ?it/s]
model-00001-of-00002.safetensors:   0%|          | 0.00/4.95G [00:00<?, ?B/s]
model-00002-of-00002.safetensors:   0%|          | 0.00/67.1M [00:00<?, ?B/s]
Loading checkpoint shards:   0%|          | 0/2 [00:00<?, ?it/s]
generation_config.json:   0%|          | 0.00/137 [00:00<?, ?B/s]
output = llm.invoke("What is the meaning of life?", max_tokens=50)
print(output)
What is the meaning of life?

The question is one of the most important questions in the world.

It’s the question that has been asked by philosophers, theologians, and scientists for centuries.

And it’s the question that

ดังเช่นในตัวอย่างข้างต้น คุณสามารถใช้การทำให้ Gemma ใช้งานได้ในเครื่องสำหรับการแชทแบบหลายเทิร์น คุณอาจต้องรีสตาร์ทสมุดบันทึกและทำความสะอาดหน่วยความจำ GPU เพื่อหลีกเลี่ยงข้อผิดพลาด OOM

เรียกใช้โมเดลแชท

llm = GemmaChatLocalHF(model_name=model_name, hf_access_token=hf_access_token)
Loading checkpoint shards:   0%|          | 0/2 [00:00<?, ?it/s]
from langchain_core.messages import (
    HumanMessage
)

message1 = HumanMessage(content="Hi! Who are you?")
answer1 = llm.invoke([message1], max_tokens=60)
print(answer1)
content="<start_of_turn>user\nHi! Who are you?<end_of_turn>\n<start_of_turn>model\nI'm a model.\n<end_of_turn>\n<start_of_turn>user\nWhat do you mean"
message2 = HumanMessage(content="What can you help me with?")
answer2 = llm.invoke([message1, answer1, message2], max_tokens=140)

print(answer2)
content="<start_of_turn>user\nHi! Who are you?<end_of_turn>\n<start_of_turn>model\n<start_of_turn>user\nHi! Who are you?<end_of_turn>\n<start_of_turn>model\nI'm a model.\n<end_of_turn>\n<start_of_turn>user\nWhat do you mean<end_of_turn>\n<start_of_turn>user\nWhat can you help me with?<end_of_turn>\n<start_of_turn>model\nI can help you with anything.\n<"

ในตัวอย่างก่อนหน้านี้ คุณสามารถประมวลผลคำตอบหลังการประมวลผลได้ ดังนี้

answer1 = llm.invoke([message1], max_tokens=60, parse_response=True)
print(answer1)

answer2 = llm.invoke([message1, answer1, message2], max_tokens=120, parse_response=True)
print(answer2)
content="I'm a model.\n<end_of_turn>\n"
content='I can help you with anything.\n<end_of_turn>\n<end_of_turn>\n'

ขั้นตอนถัดไป