mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-05 09:25:14 +03:00
Updates docs and cookbooks to import ChatOpenAI, OpenAI, and OpenAI Embeddings from `langchain_openai` There are likely more --------- Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
30 KiB
30 KiB
In [1]:
from IPython.display import SVG
from langchain_experimental.cpal.base import CPALChain
from langchain_experimental.pal_chain import PALChain
from langchain_openai import OpenAI
llm = OpenAI(temperature=0, max_tokens=512)
cpal_chain = CPALChain.from_univariate_prompt(llm=llm, verbose=True)
pal_chain = PALChain.from_math_prompt(llm=llm, verbose=True)In [2]:
question = (
"Tim buys the same number of pets as Cindy and Boris."
"Cindy buys the same number of pets as Bill plus Bob."
"Boris buys the same number of pets as Ben plus Beth."
"Bill buys the same number of pets as Obama."
"Bob buys the same number of pets as Obama."
"Ben buys the same number of pets as Obama."
"Beth buys the same number of pets as Obama."
"If Obama buys one pet, how many pets total does everyone buy?"
)In [3]:
pal_chain.run(question)Out [3]:
[1m> Entering new chain...[0m
[32;1m[1;3mdef solution():
"""Tim buys the same number of pets as Cindy and Boris.Cindy buys the same number of pets as Bill plus Bob.Boris buys the same number of pets as Ben plus Beth.Bill buys the same number of pets as Obama.Bob buys the same number of pets as Obama.Ben buys the same number of pets as Obama.Beth buys the same number of pets as Obama.If Obama buys one pet, how many pets total does everyone buy?"""
obama_pets = 1
tim_pets = obama_pets
cindy_pets = obama_pets + obama_pets
boris_pets = obama_pets + obama_pets
total_pets = tim_pets + cindy_pets + boris_pets
result = total_pets
return result[0m
[1m> Finished chain.[0m
'5'
In [4]:
cpal_chain.run(question)Out [4]:
[1m> Entering new chain...[0m
[32;1m[1;3mstory outcome data
name code value depends_on
0 obama pass 1.0 []
1 bill bill.value = obama.value 1.0 [obama]
2 bob bob.value = obama.value 1.0 [obama]
3 ben ben.value = obama.value 1.0 [obama]
4 beth beth.value = obama.value 1.0 [obama]
5 cindy cindy.value = bill.value + bob.value 2.0 [bill, bob]
6 boris boris.value = ben.value + beth.value 2.0 [ben, beth]
7 tim tim.value = cindy.value + boris.value 4.0 [cindy, boris][0m
[36;1m[1;3mquery data
{
"question": "how many pets total does everyone buy?",
"expression": "SELECT SUM(value) FROM df",
"llm_error_msg": ""
}[0m
[1m> Finished chain.[0m
13.0
In [5]:
# wait 20 secs to see display
cpal_chain.draw(path="web.svg")
SVG("web.svg")Out [5]:
In [6]:
question = (
"Jan has three times the number of pets as Marcia."
"Marcia has two more pets than Cindy."
"If Cindy has ten pets, how many pets does Barak have?"
)In [7]:
pal_chain.run(question)Out [7]:
[1m> Entering new chain...[0m
[32;1m[1;3mdef solution():
"""Jan has three times the number of pets as Marcia.Marcia has two more pets than Cindy.If Cindy has ten pets, how many pets does Barak have?"""
cindy_pets = 10
marcia_pets = cindy_pets + 2
jan_pets = marcia_pets * 3
result = jan_pets
return result[0m
[1m> Finished chain.[0m
'36'
In [8]:
try:
cpal_chain.run(question)
except Exception as e_msg:
print(e_msg)
[1m> Entering new chain...[0m
[32;1m[1;3mstory outcome data
name code value depends_on
0 cindy pass 10.0 []
1 marcia marcia.value = cindy.value + 2 12.0 [cindy]
2 jan jan.value = marcia.value * 3 36.0 [marcia][0m
[36;1m[1;3mquery data
{
"question": "how many pets does barak have?",
"expression": "SELECT name, value FROM df WHERE name = 'barak'",
"llm_error_msg": ""
}[0m
unanswerable, query and outcome are incoherent
outcome:
name code value depends_on
0 cindy pass 10.0 []
1 marcia marcia.value = cindy.value + 2 12.0 [cindy]
2 jan jan.value = marcia.value * 3 36.0 [marcia]
query:
{'question': 'how many pets does barak have?', 'expression': "SELECT name, value FROM df WHERE name = 'barak'", 'llm_error_msg': ''}
In [9]:
question = (
"Jan has three times the number of pets as Marcia. "
"Marcia has two more pets than Cindy. "
"If Cindy has four pets, how many total pets do the three have?"
)In [10]:
pal_chain.run(question)Out [10]:
[1m> Entering new chain...[0m
[32;1m[1;3mdef solution():
"""Jan has three times the number of pets as Marcia. Marcia has two more pets than Cindy. If Cindy has four pets, how many total pets do the three have?"""
cindy_pets = 4
marcia_pets = cindy_pets + 2
jan_pets = marcia_pets * 3
total_pets = cindy_pets + marcia_pets + jan_pets
result = total_pets
return result[0m
[1m> Finished chain.[0m
'28'
In [11]:
cpal_chain.run(question)Out [11]:
[1m> Entering new chain...[0m
[32;1m[1;3mstory outcome data
name code value depends_on
0 cindy pass 4.0 []
1 marcia marcia.value = cindy.value + 2 6.0 [cindy]
2 jan jan.value = marcia.value * 3 18.0 [marcia][0m
[36;1m[1;3mquery data
{
"question": "how many total pets do the three have?",
"expression": "SELECT SUM(value) FROM df",
"llm_error_msg": ""
}[0m
[1m> Finished chain.[0m
28.0
In [12]:
# wait 20 secs to see display
cpal_chain.draw(path="web.svg")
SVG("web.svg")Out [12]:
In [13]:
question = (
"Jan has the number of pets as Marcia plus the number of pets as Cindy. "
"Marcia has no pets. "
"If Cindy has four pets, how many total pets do the three have?"
)In [14]:
cpal_chain.run(question)Out [14]:
[1m> Entering new chain...[0m
[32;1m[1;3mstory outcome data
name code value depends_on
0 marcia pass 0.0 []
1 cindy pass 4.0 []
2 jan jan.value = marcia.value + cindy.value 4.0 [marcia, cindy][0m
[36;1m[1;3mquery data
{
"question": "how many total pets do the three have?",
"expression": "SELECT SUM(value) FROM df",
"llm_error_msg": ""
}[0m
[1m> Finished chain.[0m
8.0
In [15]:
# wait 20 secs to see display
cpal_chain.draw(path="web.svg")
SVG("web.svg")Out [15]:
In [16]:
question = (
"Jan has the number of pets as Marcia plus the number of pets as Cindy. "
"Marcia has two more pets than Cindy. "
"If Cindy has four pets, how many total pets do the three have?"
)In [17]:
cpal_chain.run(question)Out [17]:
[1m> Entering new chain...[0m
[32;1m[1;3mstory outcome data
name code value depends_on
0 cindy pass 4.0 []
1 marcia marcia.value = cindy.value + 2 6.0 [cindy]
2 jan jan.value = cindy.value + marcia.value 10.0 [cindy, marcia][0m
[36;1m[1;3mquery data
{
"question": "how many total pets do the three have?",
"expression": "SELECT SUM(value) FROM df",
"llm_error_msg": ""
}[0m
[1m> Finished chain.[0m
20.0
In [18]:
# wait 20 secs to see display
cpal_chain.draw(path="web.svg")
SVG("web.svg")Out [18]:
In [19]:
%load_ext autoreload
%autoreload 2