import initialize_agent, load_toolsfrom langchain.agents import Toolfrom langchain.callbacks import SageMakerCallbackHandlerfrom sagemaker.analytics import ExperimentAnalyticsfrom sagemaker.session import Sessionfrom sagemaker.experiments.run import RunLLM Prompt Tracking‚Äã#LLM HyperparametersHPARAMS = {    "temperature": 0.1,    "model_name": "text-davinci-003",}#Bucket used to save prompt logs (Use `None` is used to save the default bucket or otherwise change it)BUCKET_NAME = None#Experiment nameEXPERIMENT_NAME = "langchain-sagemaker-tracker"#Create SageMaker Session with the given bucketsession = Session(default_bucket=BUCKET_NAME)Scenario 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()Scenario 2 - Sequential Chain‚Äã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,
This notebook shows how LangChain Callback can be used to log and track prompts and other LLM hyperparameters into SageMaker Experiments. Here, we use different scenarios to showcase the capability:
This notebook shows how LangChain Callback can be used to log and track prompts and other LLM hyperparameters into SageMaker Experiments. Here, we use different scenarios to showcase the capability: ->: import initialize_agent, load_toolsfrom langchain.agents import Toolfrom langchain.callbacks import SageMakerCallbackHandlerfrom sagemaker.analytics import ExperimentAnalyticsfrom sagemaker.session import Sessionfrom sagemaker.experiments.run import RunLLM Prompt Tracking‚Äã#LLM HyperparametersHPARAMS = {    "temperature": 0.1,    "model_name": "text-davinci-003",}#Bucket used to save prompt logs (Use `None` is used to save the default bucket or otherwise change it)BUCKET_NAME = None#Experiment nameEXPERIMENT_NAME = "langchain-sagemaker-tracker"#Create SageMaker Session with the given bucketsession = Session(default_bucket=BUCKET_NAME)Scenario 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()Scenario 2 - Sequential Chain‚Äã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,