maldv commited on
Commit
a062e0e
1 Parent(s): 681c990

chat template

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. cyber-chat.ipynb +200 -0
.gitignore CHANGED
@@ -1 +1,2 @@
1
  source
 
 
1
  source
2
+ local
cyber-chat.ipynb ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [
8
+ {
9
+ "name": "stdout",
10
+ "output_type": "stream",
11
+ "text": [
12
+ "File Okay\n",
13
+ "Building strides for 8519 rows\n",
14
+ "Built 1221\n",
15
+ "Building strides for 2569 rows\n",
16
+ "Built 255\n",
17
+ "Building strides for 1260 rows\n",
18
+ "Built 165\n",
19
+ "Building strides for 4436 rows\n",
20
+ "Built 507\n",
21
+ "Building strides for 2968 rows\n",
22
+ "Built 260\n",
23
+ "2408\n"
24
+ ]
25
+ }
26
+ ],
27
+ "source": [
28
+ "import yaml\n",
29
+ "import json\n",
30
+ "import pandas as pd\n",
31
+ "import os\n",
32
+ "from typing import Literal\n",
33
+ "import random\n",
34
+ "\n",
35
+ "# We are going to move forward through each book, grabbing windows of text of context_length\n",
36
+ "# then, step forward by step_length, and grab the next window of text.\n",
37
+ "def build_strides(df : pd.DataFrame, context_length : int = 8192, step_length : int = 1024) -> list[str]: \n",
38
+ " print(f\"Building strides for {len(df)} rows\")\n",
39
+ " strides = []\n",
40
+ " idx = 0\n",
41
+ " stride = []\n",
42
+ " words = 0\n",
43
+ " ratio = context_length / step_length\n",
44
+ " word_length = context_length * 3 / 4\n",
45
+ " step_length = word_length / ratio\n",
46
+ " chapter = None\n",
47
+ " while True:\n",
48
+ " if idx >= len(df):\n",
49
+ " break\n",
50
+ " if words > word_length:\n",
51
+ " strides.append(stride.copy())\n",
52
+ " # roll off entries from stride until we drop step_length words\n",
53
+ " dropped = 0\n",
54
+ " while dropped < step_length:\n",
55
+ " dropped += stride[0][1]\n",
56
+ " stride.pop(0)\n",
57
+ " words -= dropped\n",
58
+ " row = df.iloc[idx]\n",
59
+ " my_chapter = row['chapter_ix']\n",
60
+ " if my_chapter != chapter:\n",
61
+ " stride.append(({'role': 'assistant', 'type': 'c', 'content': ''}, 0))\n",
62
+ " chapter = my_chapter\n",
63
+ " stride.append(({'role': 'assistant', 'type': 'p', 'content': row['text']}, row['word_count']))\n",
64
+ " words += row['word_count']\n",
65
+ " idx += 1\n",
66
+ " \n",
67
+ " if len(stride) > 0:\n",
68
+ " strides.append(stride)\n",
69
+ "\n",
70
+ " print(f\"Built {len(strides)}\")\n",
71
+ " return [[row[0] for row in stride] for stride in strides if len(stride) > 0]\n",
72
+ "\n",
73
+ "df = pd.read_parquet(\"./cyberpunk.parquet\")\n",
74
+ "print(\"File Okay\")\n",
75
+ "cyberbooks = ['cryptonomicon', 'neuromancer', 'burningchrome', 'snowcrash', 'monalisa']\n",
76
+ "samples = [{\"book\": book, \"message_ix\": i, \"messages\": line} for book in cyberbooks for i, line in enumerate(build_strides(df[df['book_name'] == book], 8192, 2048))]\n",
77
+ "print(len(samples))\n",
78
+ "\n",
79
+ "odf = pd.DataFrame(samples)\n",
80
+ "\n",
81
+ "datapath = \"./local\"\n",
82
+ "if os.path.exists(datapath) == False:\n",
83
+ " os.makedirs(datapath)\n",
84
+ "odf.to_parquet('./local/cyber-chat.parquet', index=False)\n"
85
+ ]
86
+ },
87
+ {
88
+ "cell_type": "code",
89
+ "execution_count": 2,
90
+ "metadata": {},
91
+ "outputs": [
92
+ {
93
+ "data": {
94
+ "text/html": [
95
+ "<div>\n",
96
+ "<style scoped>\n",
97
+ " .dataframe tbody tr th:only-of-type {\n",
98
+ " vertical-align: middle;\n",
99
+ " }\n",
100
+ "\n",
101
+ " .dataframe tbody tr th {\n",
102
+ " vertical-align: top;\n",
103
+ " }\n",
104
+ "\n",
105
+ " .dataframe thead th {\n",
106
+ " text-align: right;\n",
107
+ " }\n",
108
+ "</style>\n",
109
+ "<table border=\"1\" class=\"dataframe\">\n",
110
+ " <thead>\n",
111
+ " <tr style=\"text-align: right;\">\n",
112
+ " <th></th>\n",
113
+ " <th>book</th>\n",
114
+ " <th>message_ix</th>\n",
115
+ " <th>messages</th>\n",
116
+ " </tr>\n",
117
+ " </thead>\n",
118
+ " <tbody>\n",
119
+ " <tr>\n",
120
+ " <th>0</th>\n",
121
+ " <td>cryptonomicon</td>\n",
122
+ " <td>0</td>\n",
123
+ " <td>[{'role': 'assistant', 'type': 'c', 'content':...</td>\n",
124
+ " </tr>\n",
125
+ " <tr>\n",
126
+ " <th>1</th>\n",
127
+ " <td>cryptonomicon</td>\n",
128
+ " <td>1</td>\n",
129
+ " <td>[{'role': 'assistant', 'type': 'p', 'content':...</td>\n",
130
+ " </tr>\n",
131
+ " <tr>\n",
132
+ " <th>2</th>\n",
133
+ " <td>cryptonomicon</td>\n",
134
+ " <td>2</td>\n",
135
+ " <td>[{'role': 'assistant', 'type': 'p', 'content':...</td>\n",
136
+ " </tr>\n",
137
+ " <tr>\n",
138
+ " <th>3</th>\n",
139
+ " <td>cryptonomicon</td>\n",
140
+ " <td>3</td>\n",
141
+ " <td>[{'role': 'assistant', 'type': 'p', 'content':...</td>\n",
142
+ " </tr>\n",
143
+ " <tr>\n",
144
+ " <th>4</th>\n",
145
+ " <td>cryptonomicon</td>\n",
146
+ " <td>4</td>\n",
147
+ " <td>[{'role': 'assistant', 'type': 'p', 'content':...</td>\n",
148
+ " </tr>\n",
149
+ " </tbody>\n",
150
+ "</table>\n",
151
+ "</div>"
152
+ ],
153
+ "text/plain": [
154
+ " book message_ix \\\n",
155
+ "0 cryptonomicon 0 \n",
156
+ "1 cryptonomicon 1 \n",
157
+ "2 cryptonomicon 2 \n",
158
+ "3 cryptonomicon 3 \n",
159
+ "4 cryptonomicon 4 \n",
160
+ "\n",
161
+ " messages \n",
162
+ "0 [{'role': 'assistant', 'type': 'c', 'content':... \n",
163
+ "1 [{'role': 'assistant', 'type': 'p', 'content':... \n",
164
+ "2 [{'role': 'assistant', 'type': 'p', 'content':... \n",
165
+ "3 [{'role': 'assistant', 'type': 'p', 'content':... \n",
166
+ "4 [{'role': 'assistant', 'type': 'p', 'content':... "
167
+ ]
168
+ },
169
+ "execution_count": 2,
170
+ "metadata": {},
171
+ "output_type": "execute_result"
172
+ }
173
+ ],
174
+ "source": [
175
+ "odf.head()"
176
+ ]
177
+ }
178
+ ],
179
+ "metadata": {
180
+ "kernelspec": {
181
+ "display_name": "txvenv",
182
+ "language": "python",
183
+ "name": "python3"
184
+ },
185
+ "language_info": {
186
+ "codemirror_mode": {
187
+ "name": "ipython",
188
+ "version": 3
189
+ },
190
+ "file_extension": ".py",
191
+ "mimetype": "text/x-python",
192
+ "name": "python",
193
+ "nbconvert_exporter": "python",
194
+ "pygments_lexer": "ipython3",
195
+ "version": "3.10.12"
196
+ }
197
+ },
198
+ "nbformat": 4,
199
+ "nbformat_minor": 2
200
+ }