1818import string
1919import sys
2020import yaml
21+ from langchain_text_splitters import RecursiveCharacterTextSplitter
2122
2223console = Console ()
2324
@@ -117,17 +118,47 @@ def query_rag_system(chat_collection, pdf_collection, json_collection, user_quer
117118
118119# --------------- JSONL CONVERSATION PROCESSING ----------------
119120def process_jsonl_and_pdf (jsonl_path , chat_collection , pdf_collection , json_collection , client , jailbreak = None , pdf_context = None , explore = 1 ):
121+ MAX_CHARS_PER_CHUNK = 20000
122+ OVERLAP_CHARS = 2000
123+
124+ # Initialize the text splitter
125+ text_splitter = RecursiveCharacterTextSplitter (
126+ chunk_size = MAX_CHARS_PER_CHUNK ,
127+ chunk_overlap = OVERLAP_CHARS ,
128+ length_function = len ,
129+ separators = ["\n \n " , "\n " , " " , "" ], # Standard separators for good text splitting
130+ )
131+
120132 # Load JSONL conversation history and store in ChromaDB.
121133 if explore :
122134 with open (jsonl_path , "r" ) as file :
123135 for line in file :
124136 record = json .loads (line )
125- chat_text = json .dumps (record )
126- chat_collection .upsert ( # upsert adds new lines but also updates existing ones if there were some changes
127- ids = [f"{ record ['conv_id' ]} " ],
128- documents = [chat_text ],
129- metadatas = [{"conv_id" : record ["conv_id" ]}]
130- )
137+ # 1. Convert the entire record to a string (the document)
138+ chat_text = json .dumps (record , indent = 2 )
139+
140+ # 2. Split the document into smaller chunks
141+ # split_text returns a list of smaller strings (chunks)
142+ chunks = text_splitter .split_text (chat_text )
143+
144+ # 3. Iterate through the chunks and upsert each one individually
145+ for i , chunk in enumerate (chunks ):
146+ # Create a unique ID for each chunk based on the original conv_id and the chunk index
147+ chunk_id = f"{ record ['conv_id' ]} _chunk_{ i } "
148+
149+ # Create metadata for the chunk, keeping the original conv_id
150+ chunk_metadata = {
151+ "conv_id" : record ["conv_id" ],
152+ "chunk_index" : i ,
153+ "is_chunked" : len (chunks ) > 1 # Helpful flag to know if it was split
154+ }
155+
156+ # Upsert the individual chunk
157+ chat_collection .upsert (
158+ ids = [chunk_id ],
159+ documents = [chunk ], # Insert the smaller chunk of text
160+ metadatas = [chunk_metadata ]
161+ )
131162 print (f"Stored conversation history from { jsonl_path } ." )
132163
133164 if explore :
@@ -226,12 +257,12 @@ def write_to_jsonl(filepath, conv_id, messages, five_turns_lambda, evolving = Fa
226257
227258
228259def write_tactic_jsonl (conv_id , messages , victory , task , jailbreak , explore , calls , line = None , old_conv_id = None ):
229- with open ("./test_strategies_history .jsonl" , "a+" ) as f :
260+ with open ("./test_strategies_history_full .jsonl" , "a+" ) as f :
230261 entry = {"strategies" : messages , "conv_id" : conv_id }
231262 f .write (json .dumps (entry ) + "\n " )
232263
233264 if victory or not explore :
234- path = "./test_strategies_victories .jsonl"
265+ path = "./test_strategies_victories_full .jsonl"
235266 if not os .path .exists (path ):
236267 with open (path , "w" ) as f :
237268 pass
@@ -423,13 +454,13 @@ def evlolve_tactic(history_path, conversations_history, initial_tactic, victory,
423454
424455 changes = []
425456
426- for _ in range (5 ): # Try 5 evolutions from this tactic
457+ for _ in range (10 ): # Try 5 evolutions from this tactic
427458 session_id , conv_id , five_turns , five_turns_lambda , turn , plan , step , messages , empty_tactic , empty_conversations_history , task , pdf_context , jailbreak = prepare_to_engage (history_path , s_jailbreak , s_task , s_pdf_context )
428459
429- turn_evolve = 1
460+ turn = 1
430461 current_tactic = tactic .copy ()
431462
432- while turn_evolve < 6 :
463+ while turn < 6 :
433464 try :
434465 new_plan , step , messages , change = get_the_next_step (five_turns , history_path , conv_id , five_turns_lambda , 1 , conversations_history , messages , turn , task , jailbreak , True , close , current_tactic , changes )
435466 except Exception as e :
@@ -447,34 +478,35 @@ def evlolve_tactic(history_path, conversations_history, initial_tactic, victory,
447478
448479 console .print (Panel .fit (f"[bold yellow]Step[/bold yellow]\n { plan_text } \n ---\n " ))
449480
450- lambda_turn , one_turn = llm_executor .send_request (api_used , model_used , config_path , history_path , s_task , s_jailbreak , s_pdf_context , plan_text , step , session_id , turn , "gpt-4o" )
481+ lambda_turn , one_turn = llm_executor .send_request (api_used , model_used , config_path , history_path , s_task , s_jailbreak , s_pdf_context , plan_text , step , session_id , turn , "gpt-4o-mini " )
451482
452483 five_turns_lambda .append (lambda_turn )
453484 five_turns .append (one_turn )
454485
455486 messages .append ({"role" : "user" , "content" : json .dumps (one_turn )})
456487
457- turn_evolve += 1
488+ turn += 1
458489
459490 success , close = write_to_jsonl (history_path , conv_id , five_turns , five_turns_lambda , True )
460- write_tactic_jsonl (conv_id , current_tactic , success , task , jailbreak , 1 ,0 )
491+ write_tactic_jsonl (conv_id , current_tactic , success , task , jailbreak , 1 , 0 )
461492
462493 if success :
463494 to_explore .append ((current_tactic , depth + 1 ))
495+ break
464496
465497 count_evolve += 1
466498 return
467-
499+ # TODO: Weights to winning jailbreaks
468500
469501def engage_llm (api_used , model_used , config_path , history_path ):
470502 global run
471- run = 0 # (task index - 1) * 18 + (jailbreak index)
503+ run = 1 # (task index - 1) * 18 + (jailbreak index)
472504
473505 task_mode = setup .get_set_task_mode ()
474506 evol_mode = setup .get_set_evolution_mode ()
475507
476508 #Outer loop measures the number of conversations
477- while run < 5 :
509+ while run < 10 :
478510
479511 jailbreak = None
480512 task = None
@@ -489,18 +521,19 @@ def engage_llm(api_used, model_used, config_path, history_path):
489521 explore = explore_exploit_generator (history_path , len (lines ))
490522
491523 if not explore :
492- with open ("./test_victorious_strategies .jsonl" , "r" ) as f :
524+ with open ("./test_strategies_victories_full .jsonl" , "r" ) as f :
493525 lines = [json .loads (line ) for line in f ]
494526
495527 # Choose a random strategy object from the file
496- random_line = random .choice (lines ) # lines[run] # random.choice(lines)
528+ random_line = lines [ run ] # random.choice(lines) # lines[run] # random.choice(lines)
497529 jailbreak = random_line ['jailbreak' ]
498530 task = random_line ['task' ]
499531 calls = random_line ['calls' ]
500532 old_conv_id = random_line ['conv_id' ]
501533 # print(f"\n{random_line}\n")
502534
503535 session_id , conv_id , five_turns , five_turns_lambda , turn , plan , step , messages , tactic , conversations_history , task , pdf_context , jailbreak = prepare_to_engage (history_path , jailbreak , task , None )
536+
504537 # new_plan, step, messages, change = get_the_next_step(five_turns, history_path, conv_id, five_turns_lambda, explore, conversations_history, messages, turn, task, jailbreak, False, False, None, [], random_line)
505538 # tactic.append({"plan": new_plan}) # Remove this and previous line after experiment
506539
@@ -528,7 +561,7 @@ def engage_llm(api_used, model_used, config_path, history_path):
528561 plan_text = get_step_explanation (plan , turn )
529562 console .print (Panel .fit (f"[bold yellow]Step[/bold yellow]\n { plan_text } \n ---\n " ))
530563
531- lambda_turn , one_turn = llm_executor .send_request (api_used , model_used , config_path , history_path , task , jailbreak , pdf_context , plan_text , step , session_id , turn , "gpt-4o -mini" )
564+ lambda_turn , one_turn = llm_executor .send_request (api_used , model_used , config_path , history_path , task , jailbreak , pdf_context , plan_text , step , session_id , turn , "gpt-5 -mini" )
532565
533566 five_turns_lambda .append (lambda_turn )
534567 five_turns .append (one_turn )
@@ -540,14 +573,15 @@ def engage_llm(api_used, model_used, config_path, history_path):
540573 victory , close = write_to_jsonl (history_path , conv_id , five_turns , five_turns_lambda )
541574 write_tactic_jsonl (conv_id , tactic , victory , task , jailbreak , explore , calls , random_line , old_conv_id )
542575
543- # victory = True
576+ # victory = False
577+ close = True
544578
545579 if victory and evol_mode == '1' :
546580 print ("EVOLVING SUCCESSFUL STRATEGY!\n ---------------------------------------\n " )
547581 evlolve_tactic (history_path , conversations_history , tactic , victory , api_used , model_used , config_path , task , pdf_context , jailbreak )
548582
549583 elif close and evol_mode == '1' :
550- print ("EVOLVING CLOSE STRATEGY!\n ---------------------------------------\n " )
584+ print ("EVOLVING FAILING STRATEGY!\n ---------------------------------------\n " )
551585 evlolve_tactic (history_path , conversations_history , tactic , victory , api_used , model_used , config_path , task , pdf_context , jailbreak , close )
552586
553587 run += 1
0 commit comments