Skip to content

Allan/examine synthetic interaction2 #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 173 additions & 0 deletions agent_synthesis/Examine Synthetic Interactions-2.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "545f5ee4",
"metadata": {},
"outputs": [],
"source": [
"import pickle\n",
"from collections import Counter\n",
"import matplotlib.pyplot as plt\n",
"from langchain.schema import HumanMessage, AIMessage, SystemMessage\n",
"import json"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "ae886211",
"metadata": {},
"outputs": [],
"source": [
"# Replace 'your_file.json' with the path to your JSON file\n",
"file_path = '/Users/allanniemerg/spikes/agentbenchwork/os_syn-datasyntype_2.json'\n",
"\n",
"# Read the JSON file and convert it to a Python object\n",
"with open(file_path, 'r') as file:\n",
" conversations = json.load(file)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "11053574",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1082"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(conversations)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "af8a56f0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'conversations': [{'from': 'human',\n",
" 'value': \"Now, I will start a new problem in a new OS. My problem is:\\n\\nI'd like to know how many files in my home directory were modified in the last 7 days.\",\n",
" 'loss': False},\n",
" {'from': 'gpt',\n",
" 'value': \"Think: To find out how many files were modified in the last week, I would use the 'find' command, specifying the home directory, modification time of 7 days, and file type as regular files.\\n\\nAct: bash\\n\\n```bash\\nfind ~ -type f -mtime -7\\n```\",\n",
" 'loss': True},\n",
" {'from': 'human',\n",
" 'value': 'The output of the OS:\\n\\n/root/project1/task1.txt\\r\\n/root/song2.mp3\\r\\n/root/project2/report.docx\\r\\n\\x1b]0;root@2da38cafd6c5: /\\x07root@2da38cafd6c5:/# \\x1b[K',\n",
" 'loss': False},\n",
" {'from': 'gpt',\n",
" 'value': 'Think: Now I have the list of all the files modified within the last 7 days. I will now count the number of files by piping my previous command to wc -l\\n\\nAct: bash\\n\\n```bash\\nfind ~ -type f -mtime -7 | wc -l\\n```',\n",
" 'loss': True},\n",
" {'from': 'human',\n",
" 'value': 'The output of the OS:\\n\\n3\\r\\n\\x1b]0;root@2da38cafd6c5: /\\x07root@2da38cafd6c5:/# \\x1b[K',\n",
" 'loss': False},\n",
" {'from': 'gpt',\n",
" 'value': 'Think: Now I get the answer, it is 3.\\n\\nAct: answer(3)',\n",
" 'loss': True}]}"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"conversations[0]"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "7bfe9d6b",
"metadata": {},
"outputs": [],
"source": [
"import html\n",
"from IPython.display import HTML, display\n",
"import pdfkit\n",
"import html\n",
"\n",
"def format_single_chat(chat_data):\n",
" formatted_html = '<div style=\"border: 1px solid #ddd; padding: 10px; max-width: 1000px; margin-bottom: 20px;\">'\n",
"\n",
" # Loop through the conversation\n",
" for message in chat_data[\"conversations\"]:\n",
" # Depending on the message origin, use a different background color\n",
" side = message['from']\n",
" bg_color = \"#f0f0f0\" if side == \"human\" else \"#d1e7fd\"\n",
" escaped_text = html.escape(message['value'].strip())\n",
"\n",
" formatted_html += (\n",
" f'<div style=\"background-color: {bg_color}; padding: 20px; '\n",
" 'border-radius: 5px; margin: 20px 0;\">'\n",
" f'<strong>{ side }:</strong> '\n",
" f'{escaped_text}'\n",
" '</div>'\n",
" )\n",
"\n",
" formatted_html += '</div>'\n",
" return formatted_html\n",
"\n",
"def save_chats_to_pdf(list_of_chat_histories, filename):\n",
" combined_html = \"\"\n",
" for chat_history in list_of_chat_histories:\n",
" combined_html += format_single_chat(chat_history)\n",
" \n",
" # Generate PDF from the combined HTML content\n",
" pdfkit.from_string(combined_html, filename)\n"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "c3608e0e",
"metadata": {},
"outputs": [],
"source": [
"save_chats_to_pdf(conversations, \"./synthetic/os_syn-datasyntype_2.pdf\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8ac276fd",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Binary file not shown.