LangChain Math Agent¶
This notebook shows how to evaluate and track a langchain math agent with TruLens.
Import from Langchain and TruLens¶
In [ ]:
Copied!
# !pip install trulens trulens-apps-langchain langchain==0.0.283
# !pip install trulens trulens-apps-langchain langchain==0.0.283
In [ ]:
Copied!
from langchain import LLMMathChain
from langchain.agents import AgentType
from langchain.agents import Tool
from langchain.agents import initialize_agent
from langchain.chat_models import ChatOpenAI
from trulens.core import TruSession
from trulens.apps.langchain import TruChain
session = TruSession()
from langchain import LLMMathChain
from langchain.agents import AgentType
from langchain.agents import Tool
from langchain.agents import initialize_agent
from langchain.chat_models import ChatOpenAI
from trulens.core import TruSession
from trulens.apps.langchain import TruChain
session = TruSession()
Add API keys¶
For this example you will need an Open AI key
In [ ]:
Copied!
import os
os.environ["OPENAI_API_KEY"] = "..."
import os
os.environ["OPENAI_API_KEY"] = "..."
Create the application and wrap with TruLens¶
In [ ]:
Copied!
llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613")
llm_math_chain = LLMMathChain.from_llm(llm, verbose=True)
tools = [
Tool(
name="Calculator",
func=llm_math_chain.run,
description="useful for when you need to answer questions about math",
),
]
agent = initialize_agent(
tools, llm, agent=AgentType.OPENAI_FUNCTIONS, verbose=True
)
tru_agent = TruChain(agent)
llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613")
llm_math_chain = LLMMathChain.from_llm(llm, verbose=True)
tools = [
Tool(
name="Calculator",
func=llm_math_chain.run,
description="useful for when you need to answer questions about math",
),
]
agent = initialize_agent(
tools, llm, agent=AgentType.OPENAI_FUNCTIONS, verbose=True
)
tru_agent = TruChain(agent)
Run the app¶
In [ ]:
Copied!
with tru_agent as recording:
agent(inputs={"input": "how much is Euler's number divided by PI"})
with tru_agent as recording:
agent(inputs={"input": "how much is Euler's number divided by PI"})
Start the TruLens dashboard to explore¶
In [ ]:
Copied!
from trulens.dashboard import run_dashboard
run_dashboard(session)
from trulens.dashboard import run_dashboard
run_dashboard(session)