mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-05 17:35:28 +03:00
8.8 KiB
8.8 KiB
In [ ]:
!pip install aim
!pip install langchain
!pip install openai
!pip install google-search-resultsIn [ ]:
import os
from datetime import datetime
from langchain.llms import OpenAI
from langchain.callbacks.base import CallbackManager
from langchain.callbacks import AimCallbackHandler, StdOutCallbackHandlerIn [ ]:
os.environ["OPENAI_API_KEY"] = "..."
os.environ["SERPAPI_API_KEY"] = "..."In [ ]:
session_group = datetime.now().strftime("%m.%d.%Y_%H.%M.%S")
aim_callback = AimCallbackHandler(
repo=".",
experiment_name="scenario 1: OpenAI LLM",
)
manager = CallbackManager([StdOutCallbackHandler(), aim_callback])
llm = OpenAI(temperature=0, callback_manager=manager, verbose=True)In [ ]:
# scenario 1 - LLM
llm_result = llm.generate(["Tell me a joke", "Tell me a poem"] * 3)
aim_callback.flush_tracker(
langchain_asset=llm,
experiment_name="scenario 2: Chain with multiple SubChains on multiple generations",
)
In [ ]:
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChainIn [ ]:
# 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": "the phenomenon behind the remarkable speed of cheetahs"},
{"title": "the best in class mlops tooling"},
]
synopsis_chain.apply(test_prompts)
aim_callback.flush_tracker(
langchain_asset=synopsis_chain, experiment_name="scenario 3: Agent with Tools"
)In [ ]:
from langchain.agents import initialize_agent, load_tools
from langchain.agents import AgentTypeIn [ ]:
# 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?"
)
aim_callback.flush_tracker(langchain_asset=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;3mLeonardo DiCaprio seemed to prove a long-held theory about his love life right after splitting from girlfriend Camila Morrone just months ...[0m Thought:[32;1m[1;3m I need to find out Camila Morrone's age Action: Search Action Input: "Camila Morrone age"[0m Observation: [36;1m[1;3m25 years[0m Thought:[32;1m[1;3m I need to calculate 25 raised to the 0.43 power Action: Calculator Action Input: 25^0.43[0m Observation: [33;1m[1;3mAnswer: 3.991298452658078 [0m Thought:[32;1m[1;3m I now know the final answer Final Answer: Camila Morrone is Leo DiCaprio's girlfriend and her current age raised to the 0.43 power is 3.991298452658078.[0m [1m> Finished chain.[0m

