reflexion-human-eval/hotpotqa_runs/notebooks/CotQA_no_context.ipynb
2023-05-21 16:39:38 -04:00

191 lines
4.6 KiB
Plaintext

{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Notebook for running Chain-of-Thought with no supporting context experiments"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"import sys, os\n",
"sys.path.append('..')\n",
"root = '../root/'"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"from util import summarize_trial, log_trial, save_agents\n",
"import joblib\n",
"from agents import CoTAgent, ReflexionStrategy"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Load the HotPotQA Sample"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"hotpot = joblib.load('../data/hotpot-qa-distractor-sample.joblib').reset_index(drop = True)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Define the Reflexion Strategy"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
" NONE: No reflection\n",
" LAST_ATTEMPT: Use last reasoning trace in context \n",
" REFLEXION: Apply reflexion to the next reasoning trace \n",
" LAST_ATTEMPT_AND_REFLEXION: Use last reasoning trace in context and apply reflexion to the next reasoning trace \n",
" \n"
]
}
],
"source": [
"print(ReflexionStrategy.__doc__)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"strategy: ReflexionStrategy = ReflexionStrategy.REFLEXION"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Initialize a CoTAgent for each question"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"from prompts import cot_simple_reflect_agent_prompt, cot_simple_reflect_prompt, cot_simple_agent_prompt\n",
"from fewshots import COTQA_SIMPLE6, COT_SIMPLE_REFLECTION\n",
"\n",
"agents = [CoTAgent(question = row['question'],\n",
" context = '',\n",
" key = row['answer'],\n",
" agent_prompt=cot_simple_agent_prompt if strategy == ReflexionStrategy.NONE else cot_simple_reflect_agent_prompt,\n",
" cot_examples = COTQA_SIMPLE6,\n",
" reflect_prompt = cot_simple_reflect_prompt,\n",
" reflect_examples = COT_SIMPLE_REFLECTION,\n",
" ) for _, row in hotpot.iterrows()]"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Run `n` trials"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"n = 5\n",
"trial = 0\n",
"log = ''\n",
"for i in range(n):\n",
" for agent in [a for a in agents if not a.is_correct()]:\n",
" agent.run(reflexion_strategy = strategy)\n",
" print(f'Answer: {agent.key}')\n",
" trial += 1\n",
" log += log_trial(agents, trial)\n",
" correct, incorrect = summarize_trial(agents)\n",
" print(f'Finished Trial {trial}, Correct: {len(correct)}, Incorrect: {len(incorrect)}')"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Save the result log"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"with open(os.path.join(root, 'CoT', 'no_context', strategy.value, f'{len(agents)}_questions_{trial}_trials.txt'), 'w') as f:\n",
" f.write(log)\n",
"save_agents(agents, os.path.join(root, 'CoT', 'no_context', strategy.value, 'agents'))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "env",
"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.9.16"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "e23f799cbd2581634725fbf6ce3480ae26192d78438dfafc8efe944acd6490d5"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}