SageMaker 追蹤
Amazon SageMaker 是一項全受管服務,用於快速且輕鬆地建置、訓練和部署機器學習 (ML) 模型。
Amazon SageMaker Experiments 是
Amazon SageMaker
的一項功能,可讓您組織、追蹤、比較和評估 ML 實驗和模型版本。
此筆記本示範如何使用 LangChain 回呼將提示詞和其他 LLM 超參數記錄和追蹤到 SageMaker Experiments
中。在此,我們使用不同的情境來展示此功能
- 情境 1:單一 LLM - 使用單一 LLM 模型根據給定提示詞產生輸出的案例。
- 情境 2:循序鏈 - 使用兩個 LLM 模型的循序鏈的案例。
- 情境 3:具有工具的代理程式 (思維鏈) - 除了 LLM 之外,還使用多種工具(搜尋和數學)的案例。
在此筆記本中,我們將建立單一實驗來記錄每個情境的提示詞。
安裝與設定
%pip install --upgrade --quiet sagemaker
%pip install --upgrade --quiet langchain-openai
%pip install --upgrade --quiet google-search-results
首先,設定所需的 API 金鑰
- OpenAI:https://platform.openai.com/account/api-keys (適用於 OpenAI LLM 模型)
- Google SERP API:https://serpapi.com/manage-api-key (適用於 Google 搜尋工具)
import os
## Add your API keys below
os.environ["OPENAI_API_KEY"] = "<ADD-KEY-HERE>"
os.environ["SERPAPI_API_KEY"] = "<ADD-KEY-HERE>"
from langchain_community.callbacks.sagemaker_callback import SageMakerCallbackHandler
API 參考:SageMakerCallbackHandler
from langchain.agents import initialize_agent, load_tools
from langchain.chains import LLMChain, SimpleSequentialChain
from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAI
from sagemaker.analytics import ExperimentAnalytics
from sagemaker.experiments.run import Run
from sagemaker.session import Session
LLM 提示詞追蹤
# LLM Hyperparameters
HPARAMS = {
"temperature": 0.1,
"model_name": "gpt-3.5-turbo-instruct",
}
# Bucket used to save prompt logs (Use `None` is used to save the default bucket or otherwise change it)
BUCKET_NAME = None
# Experiment name
EXPERIMENT_NAME = "langchain-sagemaker-tracker"
# Create SageMaker Session with the given bucket
session = Session(default_bucket=BUCKET_NAME)
情境 1 - LLM
RUN_NAME = "run-scenario-1"
PROMPT_TEMPLATE = "tell me a joke about {topic}"
INPUT_VARIABLES = {"topic": "fish"}
with Run(
experiment_name=EXPERIMENT_NAME, run_name=RUN_NAME, sagemaker_session=session
) as run:
# Create SageMaker Callback
sagemaker_callback = SageMakerCallbackHandler(run)
# Define LLM model with callback
llm = OpenAI(callbacks=[sagemaker_callback], **HPARAMS)
# Create prompt template
prompt = PromptTemplate.from_template(template=PROMPT_TEMPLATE)
# Create LLM Chain
chain = LLMChain(llm=llm, prompt=prompt, callbacks=[sagemaker_callback])
# Run chain
chain.run(**INPUT_VARIABLES)
# Reset the callback
sagemaker_callback.flush_tracker()
情境 2 - 循序鏈
RUN_NAME = "run-scenario-2"
PROMPT_TEMPLATE_1 = """You are a playwright. Given the title of play, it is your job to write a synopsis for that title.
Title: {title}
Playwright: This is a synopsis for the above play:"""
PROMPT_TEMPLATE_2 = """You are a play critic from the New York Times. Given the synopsis of play, it is your job to write a review for that play.
Play Synopsis: {synopsis}
Review from a New York Times play critic of the above play:"""
INPUT_VARIABLES = {
"input": "documentary about good video games that push the boundary of game design"
}
with Run(
experiment_name=EXPERIMENT_NAME, run_name=RUN_NAME, sagemaker_session=session
) as run:
# Create SageMaker Callback
sagemaker_callback = SageMakerCallbackHandler(run)
# Create prompt templates for the chain
prompt_template1 = PromptTemplate.from_template(template=PROMPT_TEMPLATE_1)
prompt_template2 = PromptTemplate.from_template(template=PROMPT_TEMPLATE_2)
# Define LLM model with callback
llm = OpenAI(callbacks=[sagemaker_callback], **HPARAMS)
# Create chain1
chain1 = LLMChain(llm=llm, prompt=prompt_template1, callbacks=[sagemaker_callback])
# Create chain2
chain2 = LLMChain(llm=llm, prompt=prompt_template2, callbacks=[sagemaker_callback])
# Create Sequential chain
overall_chain = SimpleSequentialChain(
chains=[chain1, chain2], callbacks=[sagemaker_callback]
)
# Run overall sequential chain
overall_chain.run(**INPUT_VARIABLES)
# Reset the callback
sagemaker_callback.flush_tracker()
情境 3 - 具有工具的代理程式
RUN_NAME = "run-scenario-3"
PROMPT_TEMPLATE = "Who is the oldest person alive? And what is their current age raised to the power of 1.51?"
with Run(
experiment_name=EXPERIMENT_NAME, run_name=RUN_NAME, sagemaker_session=session
) as run:
# Create SageMaker Callback
sagemaker_callback = SageMakerCallbackHandler(run)
# Define LLM model with callback
llm = OpenAI(callbacks=[sagemaker_callback], **HPARAMS)
# Define tools
tools = load_tools(["serpapi", "llm-math"], llm=llm, callbacks=[sagemaker_callback])
# Initialize agent with all the tools
agent = initialize_agent(
tools, llm, agent="zero-shot-react-description", callbacks=[sagemaker_callback]
)
# Run agent
agent.run(input=PROMPT_TEMPLATE)
# Reset the callback
sagemaker_callback.flush_tracker()
載入記錄資料
提示詞記錄後,我們可以輕鬆載入並將其轉換為 Pandas DataFrame,如下所示。
# Load
logs = ExperimentAnalytics(experiment_name=EXPERIMENT_NAME)
# Convert as pandas dataframe
df = logs.dataframe(force_refresh=True)
print(df.shape)
df.head()
如上所示,實驗中有三個執行 (列),對應於每個情境。每個執行都會將提示詞和相關的 LLM 設定/超參數記錄為 json,並儲存在 s3 儲存貯體中。歡迎隨時載入並探索每個 json 路徑的記錄資料。