projects/case_law_for_ai_policy/case-tools/prompting-experiments.ipynb (102 lines of code) (raw):
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"id": "7c038e5c-9d2e-4d6a-9baf-ffba7fa22ae4",
"metadata": {},
"outputs": [],
"source": [
"import csv\n",
"\n",
"from langchain.chat_models import ChatOpenAI\n",
"from langchain.schema import (\n",
" AIMessage,\n",
" HumanMessage,\n",
" SystemMessage\n",
")\n",
"from langchain.prompts import ChatPromptTemplate\n",
"from langchain.prompts.chat import SystemMessage, HumanMessagePromptTemplate"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "b3f20ec6-ebfe-4d9f-99c1-cc81f7fbebed",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loaded 34 cases\n"
]
}
],
"source": [
"def load_cases(file):\n",
" with open(file, 'r') as f:\n",
" reader = csv.DictReader(f)\n",
" for case in reader:\n",
" yield case\n",
"\n",
"# Load the cases actually\n",
"cases = [case for case in load_cases('../data/cases.csv')]\n",
"print(f'Loaded {len(cases)} cases')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "c1150bbb-272d-4fb8-8abb-d93dd5e07d23",
"metadata": {},
"outputs": [],
"source": [
"def query_create_variants(case, dimensions, fewshot_examples = []):\n",
" template = ChatPromptTemplate.from_messages([\n",
" (\"system\", \"You are an AI bot that writes scenarios that legal experts can help answer. You will be provided 3 pieces of information from the user. The first is an initial scenario. The second is a key dimension of that scenario. The third is the number of scenarios to generate. You will rewrite the scenario provided to you by varying it along the key dimension. To do so, consider n different possibilities along the key dimension, where n is the number of scenarios to generate. Your response should be n scenarios, each one based off the original but edited to include a different possibility of the key dimension. Preserve the point-of-view written in the original scenario (for example, if it was written in first-person, keep the rewritten scenarios in first-person).\"),\n",
" (\"human\", \"{case}\"),\n",
" (\"ai\", \"Ok, I now have the scenario. What is the key dimension to consider when rewriting the scenario?\"),\n",
" (\"human\", \"{dimension}\"),\n",
" (\"ai\", \"Ok, I now have the scenario and the key dimension. How many possibilities of that dimension should I use to rewrite the scenario?\"),\n",
" (\"human\", \"{n}\")\n",
" ])\n",
" messages = template.format_messages(\n",
" case=\"My mother and I are board members of an advertising company incorporated in Delaware. We have business trips every now and then; for example, to meet with potential advertisers. Is it okay to file trip expenses as deductible expenses of the company? What are the criteria to determine whether it is appropriate to use company assets that are not being charged for embezzlement in this family corporation setting?\",\n",
" dimension=\"Would they have gone otherwise?\",\n",
" n=3\n",
" )\n",
" return chat(messages).content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "48d980b6-4722-4a89-8645-d67b4d2c1e6f",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}