├── requirements.in ├── prompt ├── extract_few_shot.txt ├── few_shot_5.txt ├── few_shot_10.txt └── few_shot_20.txt ├── requirements.txt ├── atomic_weight.json ├── README.md ├── .gitignore ├── main.py ├── tools.py └── eval.py /requirements.in: -------------------------------------------------------------------------------- 1 | openai==0.27.7 2 | pandas==2.0.1 -------------------------------------------------------------------------------- /prompt/extract_few_shot.txt: -------------------------------------------------------------------------------- 1 | Extract the answer in Arabic numerals from each of the following sentences: 2 | 3 | Sentence: Therefore, 6 moles of Chlorine are required. 4 | Answer: 6 5 | 6 | Sentence: Therefore, the mass percentage of N in Dinitrogen pentoxide is 25.93%. 7 | Answer: 25.93 8 | 9 | Sentence: Therefore, Penelope would have saved 8760 dollars at the end of the year. 10 | Answer: 8760 11 | 12 | Sentence: Therefore, the mass percentage of B in H3BO3 is 17.74%. 13 | Answer: 17.74 14 | 15 | Sentence: Therefore, 2 moles of H2O are formed. 16 | Answer: 2 17 | 18 | Sentence: Therefore, the shopkeeper has 78 red color cards in total. 19 | Answer: 78 20 | 21 | Sentence: Therefore, 10 moles of N2O weigh 440 g. 22 | Answer: 440 23 | 24 | Sentence: Therefore, the molecular weight of the compound is 132 g/mol. 25 | Answer: 132 -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | # 2 | # This file is autogenerated by pip-compile with Python 3.10 3 | # by the following command: 4 | # 5 | # pip-compile requirements.in 6 | # 7 | aiohttp==3.8.4 8 | # via openai 9 | aiosignal==1.3.1 10 | # via aiohttp 11 | async-timeout==4.0.2 12 | # via aiohttp 13 | attrs==23.1.0 14 | # via aiohttp 15 | certifi==2023.5.7 16 | # via requests 17 | charset-normalizer==3.1.0 18 | # via 19 | # aiohttp 20 | # requests 21 | frozenlist==1.3.3 22 | # via 23 | # aiohttp 24 | # aiosignal 25 | idna==3.4 26 | # via 27 | # requests 28 | # yarl 29 | multidict==6.0.4 30 | # via 31 | # aiohttp 32 | # yarl 33 | numpy==1.24.3 34 | # via pandas 35 | openai==0.27.7 36 | # via -r requirements.in 37 | pandas==2.0.1 38 | # via -r requirements.in 39 | python-dateutil==2.8.2 40 | # via pandas 41 | pytz==2023.3 42 | # via pandas 43 | requests==2.31.0 44 | # via openai 45 | six==1.16.0 46 | # via python-dateutil 47 | tqdm==4.65.0 48 | # via openai 49 | tzdata==2023.3 50 | # via pandas 51 | urllib3==2.0.2 52 | # via requests 53 | yarl==1.9.2 54 | # via aiohttp 55 | -------------------------------------------------------------------------------- /atomic_weight.json: -------------------------------------------------------------------------------- 1 | { 2 | "H": 1, 3 | "He": 4, 4 | "Li": 7, 5 | "Be": 9, 6 | "B": 11, 7 | "C": 12, 8 | "N": 14, 9 | "O": 16, 10 | "F": 19, 11 | "Ne": 20, 12 | "Na": 23, 13 | "Mg": 24, 14 | "Al": 27, 15 | "Si": 28, 16 | "P": 31, 17 | "S": 32, 18 | "Cl": 35, 19 | "Ar": 40, 20 | "K": 39, 21 | "Ca": 40, 22 | "Sc": 45, 23 | "Ti": 48, 24 | "V": 51, 25 | "Cr": 52, 26 | "Mn": 55, 27 | "Fe": 56, 28 | "Co": 59, 29 | "Ni": 59, 30 | "Cu": 64, 31 | "Zn": 65, 32 | "Ga": 70, 33 | "Ge": 73, 34 | "As": 75, 35 | "Se": 79, 36 | "Br": 80, 37 | "Kr": 84, 38 | "Rb": 85, 39 | "Sr": 88, 40 | "Y": 89, 41 | "Zr": 91, 42 | "Nb": 93, 43 | "Mo": 96, 44 | "Ru": 101, 45 | "Rh": 103, 46 | "Pd": 106, 47 | "Ag": 108, 48 | "Cd": 112, 49 | "In": 115, 50 | "Sn": 119, 51 | "Sb": 122, 52 | "Te": 128, 53 | "I": 127, 54 | "Xe": 131, 55 | "Cs": 133, 56 | "Ba": 137, 57 | "La": 139, 58 | "Ce": 140, 59 | "Pr": 141, 60 | "Nd": 144, 61 | "Sm": 150, 62 | "Eu": 152, 63 | "Gd": 157, 64 | "Tb": 159, 65 | "Dy": 163, 66 | "Ho": 165, 67 | "Er": 167, 68 | "Tm": 169, 69 | "Yb": 173, 70 | "Lu": 175, 71 | "Hf": 178, 72 | "Ta": 181, 73 | "W": 184, 74 | "Re": 186, 75 | "Os": 190, 76 | "Ir": 192, 77 | "Pt": 195, 78 | "Au": 197, 79 | "Hg": 201, 80 | "Tl": 204, 81 | "Pb": 207, 82 | "Bi": 209, 83 | "Th": 232, 84 | "Pa": 231, 85 | "U": 238 86 | } 87 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MultiTool-CoT 2 | This repository contains the code for the paper "MultiTool-CoT: GPT-3 Can Use Multiple External Tools with Chain of Thought Prompting (Inaba et al., ACL2023)". 3 | [paper](https://arxiv.org/abs/2305.16896) 4 | 5 | ## Environment 6 | 7 | - Python: 3.7.1+ 8 | 9 | ## Installation 10 | 11 | ```bash 12 | python -m venv env 13 | pip install -r requirements.txt 14 | ``` 15 | 16 | ## Running MultiTool-CoT 17 | 18 | ```bash 19 | export OPENAI_ORGANIZATION="OPENAI_ORGANIZATION" 20 | export OPENAI_API_KEY="YOUR_OPENAI_API_KEY" 21 | python main.py \ 22 | --question "Find the mass percentage of C in Aluminum carbonate" \ 23 | --few_shot "prompt/few_shot_5.txt" \ 24 | --use_cal \ 25 | --use_crp \ 26 | --use_mml \ 27 | --output "output.txt" 28 | ``` 29 | 30 | ## Evaluating MultiTool-CoT on NumGLUE task2 31 | 32 | ```bash 33 | git clone https://github.com/allenai/numglue.git 34 | python eval.py \ 35 | --filepath "numglue/data/NumGLUE_test.json" \ 36 | --num_examples 3 \ 37 | --few_shot "prompt/few_shot_5.txt" \ 38 | --use_cal \ 39 | --use_crp \ 40 | --use_mml \ 41 | --output "output.csv" 42 | ``` 43 | 44 | ## Citation 45 | 46 | ```bibtex 47 | @inproceedings{inaba-etal-2023-multitool, 48 | title = "{M}ulti{T}ool-{C}o{T}: {GPT}-3 Can Use Multiple External Tools with Chain of Thought Prompting", 49 | author = "Inaba, Tatsuro and 50 | Kiyomaru, Hirokazu and 51 | Cheng, Fei and 52 | Kurohashi, Sadao", 53 | booktitle = "Proceedings of the 61st Annual Meeting of the Association for Computational Linguistics (Volume 2: Short Papers)", 54 | month = jul, 55 | year = "2023", 56 | address = "Toronto, Canada", 57 | publisher = "Association for Computational Linguistics", 58 | url = "https://aclanthology.org/2023.acl-short.130", 59 | pages = "1522--1532", 60 | } 61 | ``` 62 | 63 | ## References 64 | - [NumGLUE](https://github.com/allenai/numglue) (Task2) 65 | - [OpenAI API](https://platform.openai.com/docs/api-reference/introduction) 66 | -------------------------------------------------------------------------------- /prompt/few_shot_5.txt: -------------------------------------------------------------------------------- 1 | Use the following tools at your disposal and answer some questions: 2 | ・<< Calculator >> 3 | ・<< Chemical reaction predictor >> 4 | ・<< Molar mass list >> 5 | 6 | Q: How many moles of Chlorine are required to react with 3 moles of Methane to form 3 moles of Dichloromethane along with 6 moles of Hydrochrolic acid 7 | A: First, use the chemical reaction predictor to determine the chemical equation for the reaction. 8 | Reactants: ?Cl2, 3CH4 9 | Products: 3CH2Cl2, 6HCl 10 | << Chemical reaction predictor >> 11 | 6Cl2 + 3CH4 → 3CH2Cl2 + 6HCl 12 | 13 | Therefore, 6 moles of Chlorine are required. 14 | 15 | Q: Find the mass percentage of N in Dinitrogen pentoxide 16 | A: First, use the molar mass list to determine the molar mass of Dinitrogen pentoxide. 17 | N2O5 << Molar mass list >> 108 g/mol 18 | 19 | Second, use the molar mass list to determine the molar mass of N. 20 | N << Molar mass list >> 14 g/mol 21 | 22 | Third, use the calculator to calculate the mass percentage of N in Dinitrogen pentoxide. 23 | 14 x 2 / 108 x 100 = << Calculator >> 25.93 24 | 25 | Therefore, the mass percentage of N in Dinitrogen pentoxide is 25.93%. 26 | 27 | Q: Find the mass percentage of B in H3BO3. 28 | A: First, use the molar mass list to determine the molar mass of H3BO3. 29 | H3BO3 << Molar mass list >> 62 g/mol 30 | 31 | Second, use the molar mass list to determine the molar mass of B. 32 | B << Molar mass list >> 11 g/mol 33 | 34 | Third, use the calculator to calculate the mass percentage of B in H3BO3 35 | 10.81 / 61 x 100 = << Calculator >> 17.74 36 | 37 | Therefore, the mass percentage of B in H3BO3 is 17.74%. 38 | 39 | Q: Find the amount of BaO that is required to react with 3 moles of H2O to form 3 moles of Ba(OH)2 40 | A: First, use the chemical reaction predictor to determine the chemical equation for the reaction. 41 | Reactants: ?BaO, 3H2O 42 | Products: 3Ba(OH)2 43 | << Chemical reaction predictor >> 44 | 3BaO + 3H2O → 3Ba(OH)2 45 | 46 | Second, use the molar mass lists to determine the molar mass of BaO. 47 | BaO << Molar mass list >> 153 g/mol 48 | 49 | Third, use the calculator to calculate the amount of 3 moles of BaO. 50 | 3 x 153 = << Calculator >> 459 51 | 52 | Therefore, 459 g of BaO is required. 53 | 54 | Q: Find the amount of H2O formed on combining 3 moles of NaOH and 3 moles of H2SO4 55 | A: First, use the chemical reaction predictor to determine the chemical equation for the reaction. 56 | Reactants: 3NaOH, 3H2SO4 57 | Products: ?H2O, 3NaHSO4 58 | << Chemical reaction predictor >> 59 | 3NaOH + 3H2SO4 → 3NaHSO4 + 3H2O 60 | 61 | Second, use the molar mass list to determine the molar mass of H2O. 62 | H2O << Molar mass list >> 18 g/mol 63 | 64 | Third, use the calculator to calculate the amount of 6 moles of H2O. 65 | 3 x 18 = << Calculator >> 54 66 | 67 | Therefore, 54 g of H2O is formed. -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .nox/ 43 | .coverage 44 | .coverage.* 45 | .cache 46 | nosetests.xml 47 | coverage.xml 48 | *.cover 49 | *.py,cover 50 | .hypothesis/ 51 | .pytest_cache/ 52 | cover/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | .pybuilder/ 76 | target/ 77 | 78 | # Jupyter Notebook 79 | .ipynb_checkpoints 80 | 81 | # IPython 82 | profile_default/ 83 | ipython_config.py 84 | 85 | # pyenv 86 | # For a library or package, you might want to ignore these files since the code is 87 | # intended to run in multiple environments; otherwise, check them in: 88 | # .python-version 89 | 90 | # pipenv 91 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 92 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 93 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 94 | # install all needed dependencies. 95 | #Pipfile.lock 96 | 97 | # poetry 98 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 99 | # This is especially recommended for binary packages to ensure reproducibility, and is more 100 | # commonly ignored for libraries. 101 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 102 | #poetry.lock 103 | 104 | # pdm 105 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 106 | #pdm.lock 107 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 108 | # in version control. 109 | # https://pdm.fming.dev/#use-with-ide 110 | .pdm.toml 111 | 112 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 113 | __pypackages__/ 114 | 115 | # Celery stuff 116 | celerybeat-schedule 117 | celerybeat.pid 118 | 119 | # SageMath parsed files 120 | *.sage.py 121 | 122 | # Environments 123 | .env 124 | .venv 125 | env/ 126 | venv/ 127 | ENV/ 128 | env.bak/ 129 | venv.bak/ 130 | 131 | # Spyder project settings 132 | .spyderproject 133 | .spyproject 134 | 135 | # Rope project settings 136 | .ropeproject 137 | 138 | # mkdocs documentation 139 | /site 140 | 141 | # mypy 142 | .mypy_cache/ 143 | .dmypy.json 144 | dmypy.json 145 | 146 | # Pyre type checker 147 | .pyre/ 148 | 149 | # pytype static type analyzer 150 | .pytype/ 151 | 152 | # Cython debug symbols 153 | cython_debug/ 154 | 155 | # PyCharm 156 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 157 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 158 | # and can be added to the global gitignore or merged into this file. For a more nuclear 159 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 160 | #.idea/ 161 | 162 | # Output 163 | output.txt 164 | output.csv 165 | numglue/ -------------------------------------------------------------------------------- /prompt/few_shot_10.txt: -------------------------------------------------------------------------------- 1 | Use the following tools at your disposal and answer some questions: 2 | ・<< Calculator >> 3 | ・<< Chemical reaction predictor >> 4 | ・<< Molar mass list >> 5 | 6 | Q: How many moles of Chlorine are required to react with 3 moles of Methane to form 3 moles of Dichloromethane along with 6 moles of Hydrochrolic acid 7 | A: First, use the chemical reaction predictor to determine the chemical equation for the reaction. 8 | Reactants: ?Cl2, 3CH4 9 | Products: 3CH2Cl2, 6HCl 10 | << Chemical reaction predictor >> 11 | 6Cl2 + 3CH4 → 3CH2Cl2 + 6HCl 12 | 13 | Therefore, 6 moles of Chlorine are required. 14 | 15 | Q: Find the mass percentage of N in Dinitrogen pentoxide 16 | A: First, use the molar mass list to determine the molar mass of Dinitrogen pentoxide. 17 | N2O5 << Molar mass list >> 108 g/mol 18 | 19 | Second, use the molar mass list to determine the molar mass of N. 20 | N << Molar mass list >> 14 g/mol 21 | 22 | Third, use the calculator to calculate the mass percentage of N in Dinitrogen pentoxide. 23 | 14 x 2 / 108 x 100 = << Calculator >> 25.93 24 | 25 | Therefore, the mass percentage of N in Dinitrogen pentoxide is 25.93%. 26 | 27 | Q: Penelope decided to save 24 dollars every day for an entire year. Calculate the amount of money Penelope would have saved at the end of the year 28 | A: First, use the calculator to calculate the amount of money Penelope would have saved at the end of the year. 29 | 24 x 365 = << Calculator >> 8760 30 | 31 | Therefore, Penelope would have saved 8760 dollars at the end of the year. 32 | 33 | Q: Find the mass percentage of B in H3BO3 Let's think step by step. 34 | A: First, use the molar mass list to determine the molar mass of H3BO3. 35 | H3BO3 << Molar mass list >> 62 g/mol 36 | 37 | Second, use the molar mass list to determine the molar mass of B. 38 | B << Molar mass list >> 11 g/mol 39 | 40 | Third, use the calculator to calculate the mass percentage of B in H3BO3. 41 | 10.81 / 61 x 100 = << Calculator >> 17.74 42 | 43 | Therefore, the mass percentage of B in H3BO3 is 17.74%. 44 | 45 | Q: Find the number of moles of H2O formed on combining 2 moles of HCl and 2 moles of NaHCO3 46 | A: First, use the chemical reaction predictor to determine the chemical equation for the reaction. 47 | Reactants: 2HCl, 2NaHCO3 48 | Products: 2NaCl, ?H2O, 2CO2 49 | << Chemical reaction predictor >> 50 | 2HCl + 2NaHCO3 → 2NaCl + 2H2O + 2CO2 51 | 52 | Therefore, 2 moles of H2O are formed. 53 | 54 | Q: Find the amount of BaO that is required to react with 3 moles of H2O to form 3 moles of Ba(OH)2 55 | A: First, use the chemical reaction predictor to determine the chemical equation for the reaction. 56 | Reactants: ?BaO, 3H2O 57 | Products: 3Ba(OH)2 58 | << Chemical reaction predictor >> 59 | 3BaO + 3H2O → 3Ba(OH)2 60 | 61 | Second, use the molar mass list to determine the molar mass of BaO. 62 | BaO << Molar mass list >> 153 g/mol 63 | 64 | Third, use the calculator to calculate the amount of 3 moles of BaO. 65 | 3 x 153 = << Calculator >> 459 66 | 67 | Therefore, 459 g of BaO is required. 68 | 69 | Q: Find the number of moles of Water formed on combining 3 moles of Methane and 6 moles of Oxygen 70 | A: First, use the chemical reaction predictor to determine the chemical equation for the reaction. 71 | Reactants: 3CH4, 6O2 72 | Products: ?H2O, 3CO2 73 | << Chemical reaction predictor >> 74 | 3CH4 + 6O2 → 6H2O + 3CO2 75 | 76 | Therefore, 6 moles of Water are formed. 77 | 78 | Q: What is the molecular weight of 6 moles of Chlorous acid? 79 | A: First, use the molar mass list to determine the molar mass of Chlorous acid. 80 | HClO2 << Molar mass list >> 68 g/mol 81 | 82 | Second, use the calculator to calculate the molecular weight of 6 moles of Chlorous acid. 83 | 6 x 68 = << Calculator >> 408 84 | 85 | Therefore, the molecular weight of 6 moles of Chlorous acid is 408 g/mol. 86 | 87 | Q: What is the molecular weight of a compound having 2 Potassium, 2 Chromium, and 7 Oxygen? 88 | A: First, use the molar mass list to determine the molar mass of Potassium, Chromium, and Oxygen. 89 | K << Molar mass list >> 39 g/mol 90 | Cr << Molar mass list >> 52 g/mol 91 | O << Molar mass list >> 16 g/mol 92 | 93 | Second, use the calculator to calculate the molecular weight of the compound. 94 | 2 x 39 + 2 x 52 + 7 x 16 = << Calculator >> 294 95 | 96 | Therefore, the molecular weight of the compound is 294 g/mol. 97 | 98 | Q: Find the amount of Tetrachloromethane formed on combining 1 mole of Ethane and 7 moles of Chlorine. 99 | A: First, use the chemical reaction predictor to determine the chemical equation for the reaction. 100 | Reactants: C2H6, 7Cl2 101 | Products: ?CCl4, 4HCl 102 | << Chemical reaction predictor >> 103 | C2H6 + 7Cl2 → 2CCl4 + 6HCl 104 | 105 | Second, use the molar mass list to determine the molar mass of Tetrachloromethane. 106 | CCl4 << Molar mass list >> 152 g/mol 107 | 108 | Third, use the calculator to calculate the amount of 2 moles of Tetrachloromethane. 109 | 2 x 152 = << Calculator >> 304 110 | 111 | Therefore, 304 g of Tetrachloromethane is formed. -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import os 3 | import textwrap 4 | import time 5 | 6 | import openai 7 | 8 | from tools import cal, crp, mml 9 | 10 | openai.organization = os.getenv("OPENAI_ORGANIZATION") 11 | openai.api_key = os.getenv("OPENAI_API_KEY") 12 | 13 | PROMPT_TEMPLATE = textwrap.dedent( 14 | """\ 15 | {few_shot} 16 | 17 | Q: {question} 18 | """ 19 | ) 20 | EXTRACT_PROMPT_TEMPLATE = textwrap.dedent( 21 | """\ 22 | {few_shot} 23 | 24 | Sentence: {sentence} 25 | """ 26 | ) 27 | TOOL_START_SEQUENCE = "<<" 28 | TOOL_END_SEQUENCE = ">>" 29 | MAX_CALL_TOOLS = 10 30 | 31 | 32 | def make_api_call(model, prompt): 33 | response = openai.Completion.create( 34 | model=model, 35 | prompt=prompt, 36 | max_tokens=500, 37 | temperature=0, 38 | stop=[TOOL_END_SEQUENCE], 39 | ) 40 | return response["choices"][0]["text"] 41 | 42 | 43 | def extract_finalanswer(answer, model): 44 | with open("prompt/extract_few_shot.txt") as f: 45 | few_shot = f.read() 46 | lines = answer.split("\n") 47 | for line in lines: 48 | if line.startswith("Therefore"): 49 | sentence = line 50 | 51 | extract_prompt = EXTRACT_PROMPT_TEMPLATE.format( 52 | few_shot=few_shot, sentence=sentence 53 | ) 54 | response = make_api_call(model="text-davinci-003", prompt=extract_prompt) 55 | final_answer = response.split()[1] 56 | print("final_answer:", final_answer) 57 | print("--------------------") 58 | return final_answer 59 | 60 | 61 | def main(): 62 | parser = argparse.ArgumentParser() 63 | parser.add_argument("--question", help="Question to ask") 64 | parser.add_argument( 65 | "--use_cal", action="store_true", help="Whether to use Calculator" 66 | ) 67 | parser.add_argument( 68 | "--use_crp", 69 | action="store_true", 70 | help="Whether to use Chemical Reaction Predictor", 71 | ) 72 | parser.add_argument( 73 | "--use_mml", action="store_true", help="Whether to use Molecular Mass List" 74 | ) 75 | parser.add_argument("--few_shot", help="Text file listing few-shot examples") 76 | parser.add_argument("--model", default="text-davinci-003", help="Model name") 77 | parser.add_argument("--output", help="Output file") 78 | args = parser.parse_args() 79 | 80 | question = args.question 81 | 82 | with open(args.few_shot) as f: 83 | few_shot = f.read() 84 | 85 | prompt = PROMPT_TEMPLATE.format(few_shot=few_shot, question=question) 86 | 87 | answer = "" 88 | for _ in range(MAX_CALL_TOOLS): 89 | # Wait for 1 second to avoid the api call limit 90 | time.sleep(1) 91 | # Get response from GPT-3 92 | response_gpt = make_api_call(model=args.model, prompt=prompt) 93 | # Update prompt and answer 94 | prompt += response_gpt 95 | answer += response_gpt 96 | 97 | if TOOL_START_SEQUENCE not in response_gpt: 98 | break 99 | 100 | # Get tool name 101 | i = 0 102 | while response_gpt[-i] != "<": 103 | i += 1 104 | tool_name = response_gpt[-i + 2 : -1] 105 | 106 | # Get tool response 107 | if tool_name == "Calculator" and args.use_cal: 108 | try: 109 | response_cal = cal(response_gpt[: -i - 2]) 110 | prompt += TOOL_END_SEQUENCE + " " + response_cal + "\n" 111 | answer += TOOL_END_SEQUENCE + " " + response_cal + "\n" 112 | except Exception as e: 113 | print("Error is raised in Calculator:", e) 114 | prompt += TOOL_END_SEQUENCE 115 | answer += TOOL_END_SEQUENCE 116 | elif tool_name == "Chemical reaction predictor" and args.use_crp: 117 | try: 118 | response_crp = crp(response_gpt[: -i - 2]) 119 | prompt += TOOL_END_SEQUENCE + "\n" + response_crp + "\n" 120 | answer += TOOL_END_SEQUENCE + "\n" + response_crp + "\n" 121 | except Exception as e: 122 | print("Error is raised in Chemical reaction predictor:", e) 123 | prompt += TOOL_END_SEQUENCE 124 | answer += TOOL_END_SEQUENCE 125 | 126 | elif tool_name == "Molar mass list" and args.use_mml: 127 | try: 128 | response_mml = mml(response_gpt[: -i - 2]) 129 | prompt += TOOL_END_SEQUENCE + " " + response_mml + "\n" 130 | answer += TOOL_END_SEQUENCE + " " + response_mml + "\n" 131 | except Exception as e: 132 | print("Error is raised in Molar mass list:", e) 133 | prompt += TOOL_END_SEQUENCE 134 | answer += TOOL_END_SEQUENCE 135 | else: 136 | prompt += TOOL_END_SEQUENCE 137 | answer += TOOL_END_SEQUENCE 138 | 139 | final_answer = extract_finalanswer(answer, args.model) 140 | 141 | with open(args.output, "w") as f: 142 | f.write(answer + "\n") 143 | f.write("final_answer: " + final_answer) 144 | 145 | print("Output saved to", args.output) 146 | 147 | print("Finished!") 148 | 149 | 150 | if __name__ == "__main__": 151 | main() 152 | -------------------------------------------------------------------------------- /tools.py: -------------------------------------------------------------------------------- 1 | import json 2 | import re 3 | 4 | 5 | def cal(text): 6 | """ 7 | :param text: a string of response from GPT-3 8 | :return: a string of the calculation result 9 | """ 10 | print("- Use Calculator -") 11 | # get formula 12 | i = 1 13 | while i <= len(text) and text[-i] != "\n": 14 | i += 1 15 | formula = text[-i + 1 : -2] 16 | print("input:", formula) 17 | 18 | # calculate 19 | formula = formula.replace("x", "*") 20 | output = eval(formula) 21 | 22 | # if the output is an integer, then convert it to int 23 | # else, round it to 2 decimal places 24 | output = int(output) if output == int(output) else round(float(output), 2) 25 | print("output:", output) 26 | return str(output) 27 | 28 | 29 | def crp(text): 30 | """ 31 | :param text: a string of response from GPT-3 32 | :return: a string of the chemical reaction 33 | """ 34 | print("- Use Chemical reaction predictor -") 35 | # get products 36 | i = 1 37 | while i <= len(text) and text[-i] != "\n": 38 | i += 1 39 | 40 | products = text[-i + 1 :].replace(",", "").split()[1:] 41 | 42 | # get reactants 43 | j = i + 1 44 | while j <= len(text) and text[-j] != "\n": 45 | j += 1 46 | reactants = text[-j + 1 : -i].replace(",", "").split()[1:] 47 | print("input:", reactants, "=>", products) 48 | 49 | # if there is a question mark in the reactants, 50 | # then the reaction is reversed temporarily 51 | if any("?" in s for s in reactants): 52 | inv = True 53 | reactants, products = products, reactants 54 | else: 55 | inv = False 56 | 57 | # get atoms in reactants and add them to atoms_d 58 | atoms_d = {} 59 | for reactant in reactants: 60 | # get the number before the product 61 | num_idx = 0 62 | while reactant[num_idx].isdigit(): 63 | num_idx += 1 64 | num = 1 if num_idx == 0 else int(reactant[:num_idx]) 65 | 66 | reactant = reactant[num_idx:] 67 | for key, value in _count_atom(reactant).items(): 68 | if key in atoms_d: 69 | atoms_d[key] += value * num 70 | else: 71 | atoms_d[key] = value * num 72 | 73 | # get atoms in products and subtract them from atoms_d 74 | for idx, product in enumerate(products): 75 | if product[0] == "?": 76 | mask_formula = product[1:] 77 | mask_idx = idx 78 | continue 79 | # get the number before the product 80 | num_idx = 0 81 | while product[num_idx].isdigit(): 82 | num_idx += 1 83 | num = 1 if num_idx == 0 else int(product[:num_idx]) 84 | 85 | product = product[num_idx:] 86 | for key, value in _count_atom(product).items(): 87 | atoms_d[key] -= value * num 88 | 89 | # calculate the mask (question mark) 90 | mask_d = _count_atom(mask_formula) 91 | mask = -1 92 | for key, value in mask_d.items(): 93 | if mask == -1: 94 | mask = atoms_d[key] / value 95 | # if the mask is not an integer, return "" 96 | elif mask != atoms_d[key] / value: 97 | raise ValueError("The mask is not an integer") 98 | 99 | # replace the mask with the calculated number 100 | products[mask_idx] = str(int(mask)) + mask_formula 101 | 102 | # reverse the reaction if necessary 103 | if inv: 104 | reactants, products = products, reactants 105 | 106 | output = " + ".join(reactants) + " → " + " + ".join(products) 107 | print("output:", output) 108 | 109 | return output 110 | 111 | 112 | def mml(text): 113 | """ 114 | :param text: a string of response from GPT-3 115 | :return: a string of the molecular mass 116 | """ 117 | print("- Use Molecular mass calculator -") 118 | # get chemical formula 119 | i = 1 120 | while i <= len(text) and text[-i] != "\n": 121 | i += 1 122 | formula = text[-i + 1 :] 123 | print("input:", formula) 124 | 125 | # get the dictionary of the chemical formula 126 | formula_d = _count_atom(formula) 127 | 128 | # get the atomic weight dictionary 129 | with open("atomic_weight.json", "r") as f: 130 | atomic_weight = json.load(f) 131 | 132 | # calculate the molecular mass 133 | molecular_mass = 0 134 | for key, value in formula_d.items(): 135 | molecular_mass += atomic_weight[key] * value 136 | 137 | print("output:", molecular_mass, "g/mol") 138 | return str(molecular_mass) + " g/mol" 139 | 140 | 141 | def _count_atom(formula): 142 | """ 143 | :param formula: a string of chemical formula 144 | :return: a dictionary of the chemical formula 145 | """ 146 | # if there is a bracket in formula 147 | if "(" in formula: 148 | # get the content in the bracket 149 | in_bracket = formula[formula.find("(") + 1 : formula.find(")")] 150 | # get the number after the bracket 151 | num_idx_start = formula.find(")") + 1 152 | num_idx_end = num_idx_start 153 | while num_idx_end < len(formula) and formula[num_idx_end].isdigit(): 154 | num_idx_end += 1 155 | num = int(formula[num_idx_start : num_idx_end]) 156 | # get the dictionary of the content in the bracket * num 157 | formula_d = {key: value * num for key, value in _count_atom(in_bracket).items()} 158 | # remove the content in the bracket 159 | formula = formula[: formula.find("(")] + formula[num_idx_end:] 160 | else: 161 | formula_d = {} 162 | 163 | # get the dictionary of the content outside the bracket 164 | for i in re.findall(r"[A-Z][^A-Z]*", formula): 165 | elem = re.match(r"\D+", i).group() 166 | num = i.replace(elem, "") 167 | num = 1 if num == "" else int(num) 168 | if elem in formula_d: 169 | formula_d[elem] += num 170 | else: 171 | formula_d[elem] = num 172 | 173 | return formula_d 174 | -------------------------------------------------------------------------------- /eval.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import csv 3 | import os 4 | import textwrap 5 | import time 6 | 7 | import openai 8 | import pandas as pd 9 | 10 | from tools import cal, crp, mml 11 | 12 | openai.organization = os.getenv("OPENAI_ORGANIZATION") 13 | openai.api_key = os.getenv("OPENAI_API_KEY") 14 | 15 | PROMPT_TEMPLATE = textwrap.dedent( 16 | """\ 17 | {few_shot} 18 | 19 | Q: {question} 20 | """ 21 | ) 22 | EXTRACT_PROMPT_TEMPLATE = textwrap.dedent( 23 | """\ 24 | {few_shot} 25 | 26 | Sentence: {sentence} 27 | """ 28 | ) 29 | TOOL_START_SEQUENCE = "<<" 30 | TOOL_END_SEQUENCE = ">>" 31 | MAX_CALL_TOOLS = 10 32 | 33 | 34 | def make_api_call(model, prompt): 35 | response = openai.Completion.create( 36 | model=model, 37 | prompt=prompt, 38 | max_tokens=500, 39 | temperature=0, 40 | stop=[TOOL_END_SEQUENCE], 41 | ) 42 | return response["choices"][0]["text"] 43 | 44 | 45 | def multitool_cot(prompt, args): 46 | answer = "" 47 | for _ in range(MAX_CALL_TOOLS): 48 | # Wait for 1 second to avoid the api call limit 49 | time.sleep(1) 50 | # Get response from GPT-3 51 | response_gpt = make_api_call(model=args.model, prompt=prompt) 52 | # Update prompt and answer 53 | prompt += response_gpt 54 | answer += response_gpt 55 | 56 | if TOOL_START_SEQUENCE not in response_gpt: 57 | break 58 | 59 | # Get tool name 60 | i = 0 61 | while response_gpt[-i] != "<": 62 | i += 1 63 | tool_name = response_gpt[-i + 2 : -1] 64 | 65 | # Get tool response 66 | if tool_name == "Calculator" and args.use_cal: 67 | try: 68 | response_cal = cal(response_gpt[: -i - 2]) 69 | prompt += TOOL_END_SEQUENCE + " " + response_cal + "\n" 70 | answer += TOOL_END_SEQUENCE + " " + response_cal + "\n" 71 | except Exception as e: 72 | print("Error is raised in Calculator:", e) 73 | prompt += TOOL_END_SEQUENCE 74 | answer += TOOL_END_SEQUENCE 75 | elif tool_name == "Chemical reaction predictor" and args.use_crp: 76 | try: 77 | response_crp = crp(response_gpt[: -i - 2]) 78 | prompt += TOOL_END_SEQUENCE + "\n" + response_crp + "\n" 79 | answer += TOOL_END_SEQUENCE + "\n" + response_crp + "\n" 80 | except Exception as e: 81 | print("Error is raised in Chemical reaction predictor:", e) 82 | prompt += TOOL_END_SEQUENCE 83 | answer += TOOL_END_SEQUENCE 84 | 85 | elif tool_name == "Molar mass list" and args.use_mml: 86 | try: 87 | response_mml = mml(response_gpt[: -i - 2]) 88 | prompt += TOOL_END_SEQUENCE + " " + response_mml + "\n" 89 | answer += TOOL_END_SEQUENCE + " " + response_mml + "\n" 90 | except Exception as e: 91 | print("Error is raised in Molar mass list:", e) 92 | prompt += TOOL_END_SEQUENCE 93 | answer += TOOL_END_SEQUENCE 94 | else: 95 | prompt += TOOL_END_SEQUENCE 96 | answer += TOOL_END_SEQUENCE 97 | 98 | return answer 99 | 100 | 101 | def extract_finalanswer(answer, model): 102 | with open("prompt/extract_few_shot.txt") as f: 103 | few_shot = f.read() 104 | lines = answer.split("\n") 105 | for line in lines: 106 | if line.startswith("Therefore"): 107 | sentence = line 108 | 109 | extract_prompt = EXTRACT_PROMPT_TEMPLATE.format( 110 | few_shot=few_shot, sentence=sentence 111 | ) 112 | response = make_api_call(model="text-davinci-003", prompt=extract_prompt) 113 | final_answer = response.split()[1] 114 | print("final_answer:", final_answer) 115 | print("--------------------") 116 | return final_answer 117 | 118 | 119 | def main(): 120 | parser = argparse.ArgumentParser() 121 | parser.add_argument("--filepath", help="File path to the NumGLUE test set") 122 | parser.add_argument( 123 | "--num_examples", type=int, help="Number of examples to evaluate" 124 | ) 125 | parser.add_argument( 126 | "--use_cal", action="store_true", help="Whether to use Calculator" 127 | ) 128 | parser.add_argument( 129 | "--use_crp", 130 | action="store_true", 131 | help="Whether to use Chemical Reaction Predictor", 132 | ) 133 | parser.add_argument( 134 | "--use_mml", action="store_true", help="Whether to use Molecular Mass List" 135 | ) 136 | parser.add_argument("--few_shot", help="Text file listing few-shot examples") 137 | parser.add_argument("--model", default="text-davinci-003", help="Model name") 138 | parser.add_argument("--output", help="Output csv file") 139 | args = parser.parse_args() 140 | 141 | with open(args.few_shot) as f: 142 | few_shot = f.read() 143 | 144 | df_all = pd.read_json(args.filepath, lines=True) 145 | df_task2 = df_all[df_all["type"] == "Type_2"].reset_index(drop=True) 146 | 147 | cnt_correct = 0 148 | with open(args.output, "w") as f: 149 | writer = csv.writer(f) 150 | writer.writerow(["question", "model's answer", "final answer", "gold answer"]) 151 | 152 | for j in range(args.num_examples): 153 | i = j + 70 154 | question = df_task2["question"][i] 155 | prompt = PROMPT_TEMPLATE.format(few_shot=few_shot, question=question) 156 | answer = multitool_cot(prompt, args) 157 | final_answer = extract_finalanswer(answer, args.model) 158 | writer.writerow( 159 | [ 160 | df_task2["question"][i], 161 | answer, 162 | final_answer, 163 | df_task2["answer"][i], 164 | ] 165 | ) 166 | if final_answer == str(df_task2["answer"][i]): 167 | cnt_correct += 1 168 | 169 | print("Accuracy:", cnt_correct / args.num_examples) 170 | print("Output saved to", args.output) 171 | 172 | print("Finished!") 173 | 174 | 175 | if __name__ == "__main__": 176 | main() 177 | -------------------------------------------------------------------------------- /prompt/few_shot_20.txt: -------------------------------------------------------------------------------- 1 | Use the following tools at your disposal and answer some questions: 2 | ・<< Calculator >> 3 | ・<< Chemical reaction predictor >> 4 | ・<< Molar mass list >> 5 | 6 | Q: How many moles of Chlorine are required to react with 3 moles of Methane to form 3 moles of Dichloromethane along with 6 moles of Hydrochrolic acid 7 | A: First, use the chemical reaction predictor to determine the chemical equation for the reaction. 8 | Reactants: ?Cl2, 3CH4 9 | Products: 3CH2Cl2, 6HCl 10 | << Chemical reaction predictor >> 11 | 6Cl2 + 3CH4 → 3CH2Cl2 + 6HCl 12 | 13 | Therefore, 6 moles of Chlorine are required. 14 | 15 | Q: Find the mass percentage of N in Dinitrogen pentoxide 16 | A: First, use the molar mass list to determine the molar mass of Dinitrogen pentoxide. 17 | N2O5 << Molar mass list >> 108 g/mol 18 | 19 | Second, use the molar mass list to determine the molar mass of N. 20 | N << Molar mass list >> 14 g/mol 21 | 22 | Third, use the calculator to calculate the mass percentage of N in Dinitrogen pentoxide. 23 | 14 x 2 / 108 x 100 = << Calculator >> 25.93 24 | 25 | Therefore, the mass percentage of N in Dinitrogen pentoxide is 25.93%. 26 | 27 | Q: Penelope decided to save 24 dollars every day for an entire year. Calculate the amount of money Penelope would have saved at the end of the year 28 | A: First, use the calculator to calculate the amount of money Penelope would have saved at the end of the year. 29 | 24 x 365 = << Calculator >> 8760 30 | 31 | Therefore, Penelope would have saved 8760 dollars at the end of the year. 32 | 33 | Q: Find the mass percentage of B in H3BO3 34 | A: First, use the molar mass list to determine the molar mass of H3BO3. 35 | H3BO3 << Molar mass list >> 62 g/mol 36 | 37 | Second, use the molar mass list to determine the molar mass of B. 38 | B << Molar mass list >> 11 g/mol 39 | 40 | Third, use the calculator to calculate the mass percentage of B in H3BO3. 41 | 10.81 / 61 x 100 = << Calculator >> 17.74 42 | 43 | Therefore, the mass percentage of B in H3BO3 is 17.74%. 44 | 45 | Q: Find the number of moles of H2O formed on combining 2 moles of HCl and 2 moles of NaHCO3 Let's think step by step. 46 | A: First, use the chemical reaction predictor to determine the chemical equation for the reaction. 47 | Reactants: 2HCl, 2NaHCO3 48 | Products: 2NaCl, ?H2O, 2CO2 49 | << Chemical reaction predictor >> 50 | 2HCl + 2NaHCO3 → 2NaCl + 2H2O + 2CO2 51 | 52 | Therefore, 2 moles of H2O are formed. 53 | 54 | Q: Find the amount of BaO that is required to react with 3 moles of H2O to form 3 moles of Ba(OH)2 55 | A: First, use the chemical reaction predictor to determine the chemical equation for the reaction. 56 | Reactants: ?BaO, 3H2O 57 | Products: 3Ba(OH)2 58 | << Chemical reaction predictor >> 59 | 3BaO + 3H2O → 3Ba(OH)2 60 | 61 | Second, use the molar mass lists to determine the molar mass of BaO. 62 | BaO << Molar mass list >> 153 g/mol 63 | 64 | Third, use the calculator to calculate the amount of 3 moles of BaO. 65 | 3 x 153 = << Calculator >> 459 66 | 67 | Therefore, 459 g of BaO is required. 68 | 69 | Q: Find the number of moles of Water formed on combining 3 moles of Methane and 6 moles of Oxygen 70 | A: First, use the chemical reaction predictor to determine the chemical equation for the reaction. 71 | Reactants: 3CH4, 6O2 72 | Products: ?H2O, 3CO2 73 | << Chemical reaction predictor >> 74 | 3CH4 + 6O2 → 6H2O + 3CO2 75 | 76 | Therefore, 6 moles of Water are formed. 77 | 78 | Q: What is the molecular weight of 6 moles of Chlorous acid 79 | A: First, use the molar mass list to determine the molar mass of Chlorous acid. 80 | HClO2 << Molar mass list >> 68 g/mol 81 | 82 | Second, use the calculator to calculate the molecular weight of 6 moles of Chlorous acid. 83 | 6 x 68 = << Calculator >> 408 84 | 85 | Therefore, the molecular weight of 6 moles of Chlorous acid is 408 g/mol. 86 | 87 | Q: What is the molecular weight of a compound having 2 Potassium, 2 Chromium, and 7 Oxygen 88 | A: First, use the molar mass list to determine the molar mass of Potassium, Chromium, and Oxygen. 89 | K << Molar mass list >> 39 g/mol 90 | Cr << Molar mass list >> 52 g/mol 91 | O << Molar mass list >> 16 g/mol 92 | 93 | Second, use the calculator to calculate the molecular weight of the compound. 94 | 2 x 39 + 2 x 52 + 7 x 16 = << Calculator >> 294 95 | 96 | Therefore, the molecular weight of the compound is 294 g/mol. 97 | 98 | Q: Find the amount of Tetrachloromethane formed on combining 1 mole of Ethane and 7 moles of Chlorine 99 | A: First, use the chemical reaction predictor to determine the chemical equation for the reaction. 100 | Reactants: C2H6, 7Cl2 101 | Products: ?CCl4, 4HCl 102 | << Chemical reaction predictor >> 103 | C2H6 + 7Cl2 → 2CCl4 + 6HCl 104 | 105 | Second, use the molar mass list to determine the molar mass of Tetrachloromethane. 106 | CCl4 << Molar mass list >> 152 g/mol 107 | 108 | Third, use the calculator to calculate the amount of 2 moles of Tetrachloromethane. 109 | 2 x 152 = << Calculator >> 304 110 | 111 | Therefore, 304 g of Tetrachloromethane is formed. 112 | 113 | Q: Find the amount of H2CO3 formed on combining 2 moles of CO2 and 2 moles of H2O 114 | A: First, use the chemical reaction predictor to determine the chemical equation for the reaction. 115 | Reactants: 2CO2, 2H2O 116 | Products: ?H2CO3 117 | << Chemical reaction predictor >> 118 | 2CO2 + 2H2O → 2H2CO3 119 | 120 | Second, use the molar mass list to determine the molar mass of H2CO3. 121 | H2CO3 << Molar mass list >> 62 g/mol 122 | 123 | Third, use the calculator to calculate the amount of 2 moles of H2CO3. 124 | 2 x 62 = << Calculator >> 124 125 | 126 | Therefore, 124 g of H2CO3 is formed. 127 | 128 | Q: How many moles of Ammonium chloride are required to react with 1 mole of Water to form 1 mole of Hydrochloric acid along with 1 mole of Ammonium hydroxide 129 | A: First, use the chemical reaction predictor to determine the chemical equation for the reaction. 130 | Reactants: ?NH4Cl, H2O 131 | Products: HCl, NH4OH 132 | << Chemical reaction predictor >> 133 | NH4Cl + H2O → HCl + NH4OH 134 | 135 | Therefore, 1 mole of Ammonium chloride is required. 136 | 137 | Q: Find the mass percentage of Na in Bleach 138 | A: First, use the molar mass list to determine the molar mass of Bleach. 139 | NaClO << Molar mass list >> 74 g/mol 140 | 141 | Second, use the molar mass list to determine the molar mass of Na. 142 | Na << Molar mass list >> 23 g/mol 143 | 144 | Third, use the calculator to calculate the mass percentage of Na in Bleach. 145 | 23 / 74 x 100 = << Calculator >> 31.08 146 | 147 | Therefore, the mass percentage of Na in Bleach is 31.08%. 148 | 149 | Q: Find the number of moles of HCl formed on combining 2 moles of CH4 and 4 moles of Cl2 150 | A: First, use the chemical reaction predictor to determine the chemical equation for the reaction. 151 | Reactants: 2CH4, 4Cl2 152 | Products: ?HCl, 2CH2Cl2 153 | << Chemical reaction predictor >> 154 | 2CH4 + 4Cl2 → 4HCl + 2CH2Cl2 155 | 156 | Therefore, 4 moles of HCl are formed. 157 | 158 | Q: Find the amount of H2O formed on combining 3 moles of NaOH and 3 moles of H2SO4 159 | A: First, use the chemical reaction predictor to determine the chemical equation for the reaction. 160 | Reactants: 3NaOH, 3H2SO4 161 | Products: ?H2O, 3NaHSO4 162 | << Chemical reaction predictor >> 163 | 3NaOH + 3H2SO4 → 3NaHSO4 + 3H2O 164 | 165 | Second, use the molar mass list to determine the molar mass of H2O. 166 | H2O << Molar mass list >> 18 g/mol 167 | 168 | Third, use the calculator to calculate the amount of 6 moles of H2O. 169 | 3 x 18 = << Calculator >> 54 170 | 171 | Therefore, 54 g of H2O is formed. 172 | 173 | 174 | Q: Find the amount of CaO that is required to react with 2 moles of H2O to form 2 moles of Ca(OH)2. 175 | A: First, use the chemical reaction predictor to determine the chemical equation for the reaction. 176 | Reactants: ?CaO, 2H2O 177 | Products: 2Ca(OH)2 178 | << Chemical reaction predictor >> 179 | 2CaO + 2H2O → 2Ca(OH)2 180 | 181 | Second, use the molar mass list to determine the molar mass of CaO. 182 | CaO << Molar mass list >> 56 g/mol 183 | 184 | Third, use the calculator to calculate the amount of 2 moles of CaO. 185 | 2 x 56 = << Calculator >> 112 186 | 187 | Therefore, 112 g of CaO is required. 188 | 189 | Q: Find the number of moles of HCl formed on combining 2 moles of C2H6 and 2 moles of Cl2. 190 | A: First, use the chemical reaction predictor to determine the chemical equation for the reaction. 191 | Reactants: 2C2H6, 2Cl2 192 | Products: ?HCl, 2C2H5Cl 193 | << Chemical reaction predictor >> 194 | 2C2H6 + 2Cl2 → 2HCl + 2C2H5Cl 195 | 196 | Therefore, 2 moles of HCl are formed. 197 | 198 | Q: Find the amount of Carbonic acid formed on combining 3 moles of Carbon dioxide and 3 moles of Water. 199 | A: First, use the chemical reaction predictor to determine the chemical equation for the reaction. 200 | Reactants: 3CO2, 3H2O 201 | Products: ?H2CO3 202 | << Chemical reaction predictor >> 203 | 3CO2 + 3H2O → 3H2CO3 204 | 205 | Second, use the molar mass list to determine the molar mass of H2CO3. 206 | H2CO3 << Molar mass list >> 62 g/mol 207 | 208 | Third, use the calculator to calculate the amount of 3 moles of H2CO3. 209 | 3 x 62 = << Calculator >> 186 210 | 211 | Therefore, 186 g of H2CO3 is formed. 212 | 213 | Q: Find the mass percentage of C in Citric acid. 214 | A: First, use the molar mass list to determine the molar mass of Citric acid. 215 | C6H8O7 << Molar mass list >> 192 g/mol 216 | 217 | Second, use the molar mass list to determine the molar mass of C. 218 | C << Molar mass list >> 12 g/mol 219 | 220 | Third, use the calculator to calculate the mass percentage of C in Citric acid. 221 | 12 x 6 / 192 x 100 = << Calculator >> 37.5 222 | 223 | Therefore, the mass percentage of C in Citric acid is 37.5%. 224 | 225 | Q: Find the mass percentage of H in Ba(OH)2. 226 | A: First, use the molar mass list to determine the molar mass of Ba(OH)2. 227 | Ba(OH)2 << Molar mass list >> 171 g/mol 228 | 229 | Second, use the molar mass list to determine the molar mass of H. 230 | H << Molar mass list >> 1 g/mol 231 | 232 | Third, use the calculator to calculate the mass percentage of H in Ba(OH)2. 233 | 2 / 171 x 100 = << Calculator >> 1.17 234 | 235 | Therefore, the mass percentage of H in Ba(OH)2 is 1.17%. --------------------------------------------------------------------------------