Skip to content

Commit

Permalink
Merge pull request #317 from ohdearquant/merge_direct
Browse files Browse the repository at this point in the history
chain of react notebook
  • Loading branch information
ohdearquant authored Mar 30, 2024
2 parents 0485fdc + eb2e4ea commit 3db720a
Showing 1 changed file with 152 additions and 0 deletions.
152 changes: 152 additions & 0 deletions notebooks/chain_of_react.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from lionagi.core.direct.cot import chain_of_thoughts\n",
"\n",
"\n",
"sentence = \"a big white shark is being chased by a blue whale, the shark is exhausted and the whale is about to catch it, the shark cries for help, a group of dolphins come to help the shark, which made the blue whale confused. now the shark, dolphins and blue whale are negotiating to solve their different preferences. If you are the blue whale, you want to give the shark a good pounch without upsetting the dolphins how would you do it?.\"\n",
"\n",
"out_ = await chain_of_thoughts(sentence, num_steps=2)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"The blue whale, seeing the need for peace, suggests to the shark and the dolphins that they all come together to discuss and find a peaceful resolution to their conflict.During the meeting, the whale presents a proposition where the shark would agree to steer clear of the whale's preferred areas, in return for a guarantee of safety and no harm from the whale.The whale then asks the dolphins to oversee the agreement, trusting their fairness and judgment to ensure both the shark and the whale adhere to their promises, thereby securing a peaceful coexistence.\""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"out_.chain_answer"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def multiply(number1: float, number2: float):\n",
" \"\"\"\n",
" Perform multiplication on two numbers.\n",
"\n",
" Args:\n",
" number1: First number to multiply.\n",
" number2: Second number to multiply.\n",
"\n",
" Returns:\n",
" The product of number1 and number2.\n",
" \n",
" \"\"\"\n",
" return number1 * number2\n",
"\n",
"sys_mul = \"you are asked to perform as a function picker and parameter provider\"\n",
"instruction = \"Think step by step, understand the following basic math question and provide parameters (number1, number2) for function calling\"\n",
"question1 = \"A school is ordering laptops for its students. If each classroom has 27 students and the school wants to provide a laptop for each student in its 131 classrooms, how many laptops in total does the school need to order? if each computer needs 7 USB cables, each USB cable costs $5.1795, how much does the school need to spend in USB cables?\""
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from lionagi.core.direct.cot import chain_of_react\n",
"\n",
"\n",
"out_ = await chain_of_react(\n",
" question1, instruction=instruction, tools=multiply, num_steps=3, directive_system=sys_mul\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'step_1': {'plan': 'Calculate the total number of laptops needed by multiplying the number of students per classroom by the number of classrooms.',\n",
" 'reason': 'To find out how many laptops the school needs to order, we need to know the total number of students across all classrooms.'},\n",
" 'step_2': {'plan': 'Multiply the total number of laptops by the number of USB cables needed per laptop to find out the total number of USB cables required.',\n",
" 'reason': 'Each laptop requires a specific number of USB cables, so to find the total USB cables needed, we multiply the total laptops by the USB cables per laptop.'},\n",
" 'step_3': {'plan': 'Multiply the total number of USB cables by the cost of each USB cable to find out the total cost for USB cables.',\n",
" 'reason': 'To calculate the total expenditure on USB cables, we need to know the cost per cable and the total number of cables required.'}}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"out_.plan"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"To calculate the total number of laptops needed, we need to multiply the number of students per classroom by the number of classrooms.\n",
"{'function': 'multiply', 'arguments': {'number1': 27, 'number2': 131}}\n",
"{'function': 'multiply', 'arguments': {'number1': 27, 'number2': 131}, 'output': 3537}\n",
"\n",
"To find out the total number of USB cables needed, we need to multiply the total number of laptops by the number of USB cables required per laptop.\n",
"{'function': 'multiply', 'arguments': {'number1': 3537, 'number2': 7}}\n",
"{'function': 'multiply', 'arguments': {'number1': 3537, 'number2': 7}, 'output': 24759}\n",
"\n",
"To find out the total cost for USB cables, we need to multiply the total number of USB cables by the cost of each USB cable.\n",
"{'function': 'multiply', 'arguments': {'number1': 24759, 'number2': 5.1795}}\n",
"{'function': 'multiply', 'arguments': {'number1': 24759, 'number2': 5.1795}, 'output': 128239.2405}\n",
"\n"
]
}
],
"source": [
"for i,j,z in zip(out_.chain_reason, out_.chain_actions, out_.chain_action_response):\n",
" print(f\"{i}\\n{j}\\n{z}\\n\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit 3db720a

Please sign in to comment.