mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-08 19:05:20 +03:00
19 KiB
19 KiB
In [ ]:
!pip install wandb
!pip install pandas
!pip install textstat
!pip install spacy
!python -m spacy download en_core_web_smIn [1]:
import os
os.environ["WANDB_API_KEY"] = ""
# os.environ["OPENAI_API_KEY"] = ""
# os.environ["SERPAPI_API_KEY"] = ""In [2]:
from datetime import datetime
from langchain.callbacks import WandbCallbackHandler, StdOutCallbackHandler
from langchain.callbacks.base import CallbackManager
from langchain.llms import OpenAIIn [3]:
"""Main function.
This function is used to try the callback handler.
Scenarios:
1. OpenAI LLM
2. Chain with multiple SubChains on multiple generations
3. Agent with Tools
"""
session_group = datetime.now().strftime("%m.%d.%Y_%H.%M.%S")
wandb_callback = WandbCallbackHandler(
job_type="inference",
project="langchain_callback_demo",
group=f"minimal_{session_group}",
name="llm",
tags=["test"],
)
manager = CallbackManager([StdOutCallbackHandler(), wandb_callback])
llm = OpenAI(temperature=0, callback_manager=manager, verbose=True)[34m[1mwandb[0m: Currently logged in as: [33mharrison-chase[0m. Use [1m`wandb login --relogin`[0m to force relogin
Tracking run with wandb version 0.14.0
Run data is saved locally in
/Users/harrisonchase/workplace/langchain/docs/ecosystem/wandb/run-20230318_150408-e47j1914 View project at https://wandb.ai/harrison-chase/langchain_callback_demo
[34m[1mwandb[0m: [33mWARNING[0m The wandb callback is currently in beta and is subject to change based on updates to `langchain`. Please report any issues to https://github.com/wandb/wandb/issues with the tag `langchain`.
In [4]:
# SCENARIO 1 - LLM
llm_result = llm.generate(["Tell me a joke", "Tell me a poem"] * 3)
wandb_callback.flush_tracker(llm, name="simple_sequential")Waiting for W&B process to finish... (success).
View run llm at: https://wandb.ai/harrison-chase/langchain_callback_demo/runs/e47j1914
Synced 5 W&B file(s), 2 media file(s), 5 artifact file(s) and 0 other file(s)
Synced 5 W&B file(s), 2 media file(s), 5 artifact file(s) and 0 other file(s)
Find logs at:
./wandb/run-20230318_150408-e47j1914/logsVBox(children=(Label(value='Waiting for wandb.init()...\r'), FloatProgress(value=0.016745895149999985, max=1.0…
Tracking run with wandb version 0.14.0
Run data is saved locally in
/Users/harrisonchase/workplace/langchain/docs/ecosystem/wandb/run-20230318_150534-jyxma7hu View project at https://wandb.ai/harrison-chase/langchain_callback_demo
In [5]:
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChainIn [6]:
# SCENARIO 2 - Chain
template = """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 = PromptTemplate(input_variables=["title"], template=template)
synopsis_chain = LLMChain(llm=llm, prompt=prompt_template, callback_manager=manager)
test_prompts = [
{
"title": "documentary about good video games that push the boundary of game design"
},
{"title": "cocaine bear vs heroin wolf"},
{"title": "the best in class mlops tooling"},
]
synopsis_chain.apply(test_prompts)
wandb_callback.flush_tracker(synopsis_chain, name="agent")Waiting for W&B process to finish... (success).
View run simple_sequential at: https://wandb.ai/harrison-chase/langchain_callback_demo/runs/jyxma7hu
Synced 4 W&B file(s), 2 media file(s), 6 artifact file(s) and 0 other file(s)
Synced 4 W&B file(s), 2 media file(s), 6 artifact file(s) and 0 other file(s)
Find logs at:
./wandb/run-20230318_150534-jyxma7hu/logsVBox(children=(Label(value='Waiting for wandb.init()...\r'), FloatProgress(value=0.016736786816666675, max=1.0…
Tracking run with wandb version 0.14.0
Run data is saved locally in
/Users/harrisonchase/workplace/langchain/docs/ecosystem/wandb/run-20230318_150550-wzy59zjq View project at https://wandb.ai/harrison-chase/langchain_callback_demo
In [7]:
from langchain.agents import initialize_agent, load_tools
from langchain.agents import AgentTypeIn [8]:
# SCENARIO 3 - Agent with Tools
tools = load_tools(["serpapi", "llm-math"], llm=llm, callback_manager=manager)
agent = initialize_agent(
tools,
llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
callback_manager=manager,
verbose=True,
)
agent.run(
"Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?"
)
wandb_callback.flush_tracker(agent, reset=False, finish=True)[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m I need to find out who Leo DiCaprio's girlfriend is and then calculate her age raised to the 0.43 power. Action: Search Action Input: "Leo DiCaprio girlfriend"[0m Observation: [36;1m[1;3mDiCaprio had a steady girlfriend in Camila Morrone. He had been with the model turned actress for nearly five years, as they were first said to be dating at the end of 2017. And the now 26-year-old Morrone is no stranger to Hollywood.[0m Thought:[32;1m[1;3m I need to calculate her age raised to the 0.43 power. Action: Calculator Action Input: 26^0.43[0m Observation: [33;1m[1;3mAnswer: 4.059182145592686 [0m Thought:[32;1m[1;3m I now know the final answer. Final Answer: Leo DiCaprio's girlfriend is Camila Morrone and her current age raised to the 0.43 power is 4.059182145592686.[0m [1m> Finished chain.[0m
Waiting for W&B process to finish... (success).
View run agent at: https://wandb.ai/harrison-chase/langchain_callback_demo/runs/wzy59zjq
Synced 5 W&B file(s), 2 media file(s), 7 artifact file(s) and 0 other file(s)
Synced 5 W&B file(s), 2 media file(s), 7 artifact file(s) and 0 other file(s)
Find logs at:
./wandb/run-20230318_150550-wzy59zjq/logsIn [ ]: