-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_gpt2_dataset.rs
More file actions
392 lines (341 loc) · 13.1 KB
/
search_gpt2_dataset.rs
File metadata and controls
392 lines (341 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
use zeroentropy_community::Client;
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use serde_json::Value;
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[command(name = "gpt2-search")]
#[command(about = "Search GPT-2 dataset using ZeroEntropy semantic search", long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,
/// Path to the GPT-2 dataset directory
#[arg(short, long, default_value = "../gpt-2-output-dataset/data")]
dataset: PathBuf,
}
#[derive(Subcommand)]
enum Commands {
/// Index dataset into ZeroEntropy collections
Index {
/// Number of documents to index per collection (0 = all documents)
#[arg(short, long, default_value = "0")]
limit: usize,
/// Collections to index (comma-separated: webtext,gpt2_small,gpt2_medium,gpt2_large,gpt2_xl)
#[arg(short, long, value_delimiter = ',')]
collections: Option<Vec<String>>,
},
/// Search indexed collections
Search {
/// Search query
query: String,
/// Number of results per collection
#[arg(short, long, default_value = "5")]
limit: usize,
/// Collections to search (comma-separated)
#[arg(short, long, value_delimiter = ',')]
collections: Option<Vec<String>>,
},
/// Run predefined code search queries
CodeSearch {
/// Collections to search (comma-separated)
#[arg(short, long, value_delimiter = ',')]
collections: Option<Vec<String>>,
},
/// Interactive search mode
Interactive,
}
fn expand_tilde(path: &Path) -> PathBuf {
if let Some(path_str) = path.to_str() {
if path_str.starts_with("~") {
if let Some(home) = std::env::var("USERPROFILE").ok().or_else(|| std::env::var("HOME").ok()) {
return PathBuf::from(path_str.replacen("~", &home, 1));
}
}
}
path.to_path_buf()
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load .env file if it exists
dotenv::dotenv().ok();
let cli = Cli::parse();
// Create client from ZEROENTROPY_API_KEY environment variable
let client = Client::from_env()?;
// Dataset path - expand tilde if present
let dataset_path = expand_tilde(&cli.dataset);
// Available collections
let all_collections = vec![
("webtext", "webtext.valid.jsonl"),
("gpt2_small", "small-117M.valid.jsonl"),
("gpt2_medium", "medium-345M.valid.jsonl"),
("gpt2_large", "large-762M.valid.jsonl"),
("gpt2_xl", "xl-1542M.valid.jsonl"),
];
match cli.command {
Commands::Index { limit, collections: selected } => {
let collections_to_index = filter_collections(&all_collections, selected);
index_collections(&client, &dataset_path, &collections_to_index, limit).await?;
}
Commands::Search { query, limit, collections: selected } => {
let collections_to_search = filter_collections(&all_collections, selected);
search_collections(&client, &collections_to_search, &query, limit).await?;
}
Commands::CodeSearch { collections: selected } => {
let collections_to_search = filter_collections(&all_collections, selected);
code_search(&client, &collections_to_search).await?;
}
Commands::Interactive => {
interactive_search(&client, &all_collections).await?;
}
}
Ok(())
}
fn filter_collections<'a>(
all_collections: &'a [(&'a str, &'a str)],
selected: Option<Vec<String>>,
) -> Vec<(&'a str, &'a str)> {
match selected {
Some(names) => all_collections
.iter()
.filter(|(name, _)| names.contains(&name.to_string()))
.copied()
.collect(),
None => all_collections.to_vec(),
}
}
async fn index_collections(
client: &Client,
dataset_path: &Path,
collections: &[(&str, &str)],
limit: usize,
) -> Result<(), Box<dyn std::error::Error>> {
println!("{}", "=".repeat(60));
println!("Indexing GPT-2 Dataset");
println!("{}", "=".repeat(60));
if limit == 0 {
println!("Indexing all documents");
} else {
println!("Limit: {} documents per collection", limit);
}
println!();
for (collection_name, filename) in collections {
println!("\nProcessing {}...", collection_name);
// Create collection
match client.collections().add(*collection_name).await {
Ok(response) => println!(" {}", response.message),
Err(zeroentropy_community::Error::Conflict(_)) => {
println!(" Collection already exists");
}
Err(e) => return Err(e.into()),
}
// Load and index samples
let file_path = dataset_path.join(filename);
if !file_path.exists() {
println!(" File not found: {}", file_path.display());
continue;
}
let file = File::open(&file_path)?;
let reader = BufReader::new(file);
if limit == 0 {
println!(" Indexing all samples...");
} else {
println!(" Indexing up to {} samples...", limit);
}
let mut count = 0;
for (idx, line) in reader.lines().enumerate() {
if limit > 0 && idx >= limit {
break;
}
let line = line?;
if let Ok(json) = serde_json::from_str::<Value>(&line) {
if let Some(text) = json.get("text").and_then(|v| v.as_str()) {
// Add metadata
let mut metadata = HashMap::new();
metadata.insert(
"source".to_string(),
zeroentropy_community::MetadataValue::String(collection_name.to_string()),
);
metadata.insert(
"index".to_string(),
zeroentropy_community::MetadataValue::String(idx.to_string()),
);
// Index the document
let doc_path = format!("{}_{}.txt", collection_name, idx);
match client.documents().add_text(
*collection_name,
&doc_path,
text,
Some(metadata),
).await {
Ok(_) => count += 1,
Err(zeroentropy_community::Error::Conflict(_)) => {
// Document already exists, skip silently
continue;
}
Err(e) => eprintln!(" Error adding document {}: {}", idx, e),
}
if count % 10 == 0 {
print!(".");
std::io::Write::flush(&mut std::io::stdout())?;
}
}
}
}
println!("\n Indexed {} documents from {}", count, collection_name);
}
Ok(())
}
async fn code_search(
client: &Client,
collections: &[(&str, &str)],
) -> Result<(), Box<dyn std::error::Error>> {
println!("\n{}", "=".repeat(60));
println!("Code Search Mode");
println!("{}", "=".repeat(60));
println!();
// Search queries for code
let code_queries = vec![
"function definition programming code",
"import module python java javascript",
"class method implementation",
"for loop while loop iteration",
"if else conditional statement",
"return variable assignment",
"API endpoint HTTP request",
"database query SQL",
];
for query in code_queries {
println!("\nSearching for: \"{}\"", query);
println!("{}", "-".repeat(60));
// Search each collection
for (collection_name, _) in collections {
let results = match client.queries().top_snippets(
*collection_name,
query,
3, // Top 3 results per collection
None,
Some(true), // include metadata
None,
None,
).await {
Ok(r) => r,
Err(e) => {
println!(" Error searching {}: {}", collection_name, e);
continue;
}
};
if !results.results.is_empty() {
println!("\n {} ({} results):", collection_name, results.results.len());
for (i, result) in results.results.iter().take(2).enumerate() {
println!("\n {}. {} (score: {:.4})", i + 1, result.path, result.score);
// Show first 200 chars of content
let preview = if result.content.len() > 200 {
format!("{}...", &result.content[..200])
} else {
result.content.clone()
};
println!(" {}", preview.replace('\n', "\n "));
}
}
}
println!(); // Blank line between queries
}
Ok(())
}
async fn search_collections(
client: &Client,
collections: &[(&str, &str)],
query: &str,
limit: usize,
) -> Result<(), Box<dyn std::error::Error>> {
println!("{}", "=".repeat(60));
println!("Searching: \"{}\"", query);
println!("{}", "=".repeat(60));
println!();
for (collection_name, _) in collections {
let results = match client.queries().top_snippets(
*collection_name,
query,
limit as u32,
None,
Some(true),
None,
None,
).await {
Ok(r) => r,
Err(e) => {
println!("Error searching {}: {}", collection_name, e);
continue;
}
};
if !results.results.is_empty() {
println!("{} - Found {} matches:", collection_name, results.results.len());
for (i, result) in results.results.iter().enumerate() {
println!("\n {}. {} (score: {:.4})", i + 1, result.path, result.score);
let preview = if result.content.len() > 300 {
format!("{}...", &result.content[..300])
} else {
result.content.clone()
};
println!(" {}", preview.replace('\n', "\n "));
}
println!();
}
}
Ok(())
}
async fn interactive_search(
client: &Client,
collections: &[(&str, &str)],
) -> Result<(), Box<dyn std::error::Error>> {
println!("\n{}", "=".repeat(60));
println!("Interactive Search (type 'quit' to exit)");
println!("{}", "=".repeat(60));
loop {
println!("\nEnter search query: ");
let mut query = String::new();
std::io::stdin().read_line(&mut query)?;
let query = query.trim();
if query.is_empty() {
continue;
}
if query.eq_ignore_ascii_case("quit") || query.eq_ignore_ascii_case("exit") {
break;
}
println!("\nSearching all collections for: \"{}\"", query);
println!("{}", "-".repeat(60));
// Search all collections
for (collection_name, _) in collections {
let results = match client.queries().top_snippets(
*collection_name,
query,
5,
None,
Some(true),
None,
None,
).await {
Ok(r) => r,
Err(e) => {
println!(" Error: {}", e);
continue;
}
};
if !results.results.is_empty() {
println!("\n {} - Found {} matches:", collection_name, results.results.len());
for (i, result) in results.results.iter().take(3).enumerate() {
println!("\n {}. {} (score: {:.4})", i + 1, result.path, result.score);
let preview = if result.content.len() > 300 {
format!("{}...", &result.content[..300])
} else {
result.content.clone()
};
println!(" {}", preview.replace('\n', "\n "));
}
}
}
}
Ok(())
}