### Generate Recommendation Training Samples Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/goodreads/design_questions.ipynb Iterates through user history to construct training examples. It selects the last few books as context and the final book as the target, formatting them into a question-answer structure. ```python random.seed(2046) question = "What book should be recommended to the user based on his history: {book_titles}?" answer = "{targe_book_title}" generated_data = [] user_ids = list(user_history.keys()) random.shuffle(user_ids) for user_id in user_ids: tmp_history = user_history[user_id] tmp_history.sort(key=lambda x: x[1]) if len(tmp_history) < 2: continue book_titles = [graph['book_nodes']['book-'+idd[-1]]['features']['title'] for idd in tmp_history[-6:-1]] targe_book_title = graph['book_nodes']['book-'+tmp_history[-1][-1]]['features']['title'] generated_data.append({"book_titles": book_titles, "targe_book_title": targe_book_title}) if len(generated_data) == k: break all_generated_data[(question, answer)] = generated_data ``` -------------------------------- ### Generate 'Start Date' Questions (Python) Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/legal/design_questions.ipynb Generates `k` question-answer pairs about the start date of courts. It shuffles court IDs, extracts court names and start dates, and stores them if the start date is a string. Includes random seeding for reproducibility. ```python ## question (easy): what is the start date of court xxx? random.seed(2023) question = "what is the start date of court {court_name}?" answer = "{start_date}" generated_data = [] court_ids = list(graph['court_nodes'].keys()) random.shuffle(court_ids) for court_id in court_ids: court_name = graph['court_nodes'][court_id]['features']['full_name'] start_date = graph['court_nodes'][court_id]['features']['start_date'] if isinstance(start_date, str): generated_data.append({"court_name":court_name, "start_date": start_date}) if len(generated_data) == k: break assert len(generated_data) == k all_generated_data[(question, answer)] = generated_data ``` -------------------------------- ### Initialize and Run GraphAgent Source: https://context7.com/petergriffinjin/graph-cot/llms.txt Demonstrates how to configure and instantiate the GraphAgent class to perform iterative reasoning over a graph dataset. It shows the setup of arguments, agent initialization, and executing a question-answering task. ```python from GraphAgent import GraphAgent from graph_prompts import graph_agent_prompt import argparse args = argparse.Namespace( dataset="dblp", ref_dataset="dblp", llm_version="gpt-3.5-turbo-16k", max_steps=15, graph_dir="/path/to/data/processed_data/dblp/graph.json", embedder_name="sentence-transformers/all-mpnet-base-v2", faiss_gpu=False, embed_cache=True, embed_cache_dir="/path/to/data/processed_data/dblp", node_text_keys={'paper': ['title'], 'author': ['name', 'organization'], 'venue': ['name']} ) agent = GraphAgent(args, graph_agent_prompt) question = "What organization is researcher Greg Daville from?" expected_answer = "Hove East Sussex, United Kingdom" agent.run(question, expected_answer) print(f"Agent's answer: {agent.answer}") print(f"Is correct: {agent.is_correct()}") print(f"Reasoning trace:\n{agent.scratchpad}") ``` -------------------------------- ### Load Shopping Queries Datasets Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/amazon/design_questions.ipynb Loads and merges example and product data from parquet files for use in retrieval-based tasks. This involves joining two datasets based on product locale and ID. ```python import pandas as pd df_examples = pd.read_parquet('/shared/data3/bowenj4/llm-graph-plugin/data/raw_data/amazon/shopping_queries_dataset/shopping_queries_dataset_examples.parquet') df_products = pd.read_parquet('/shared/data3/bowenj4/llm-graph-plugin/data/raw_data/amazon/shopping_queries_dataset/shopping_queries_dataset_products.parquet') df_examples_products = pd.merge( df_examples, df_products, how='left', left_on=['product_locale','product_id'], right_on=['product_locale', 'product_id'] ) ``` -------------------------------- ### Initialize Biological Node Dictionaries Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/raw_data/biomedical/raw_process.ipynb Creates empty dictionaries to store node information for various biological entities such as Anatomy, Genes, and Diseases. This setup is a prerequisite for mapping graph nodes during data processing. ```python Anatomy_nodes = {} Biological_Process_nodes = {} Cellular_Component_nodes = {} Compound_nodes = {} Disease_nodes = {} Gene_nodes = {} Molecular_Function_nodes = {} Pathway_nodes = {} Pharmacologic_Class_nodes = {} Side_Effect_nodes = {} Symptom_nodes = {} id_set = set() ``` -------------------------------- ### Execute Graph Operations with graph_funcs Source: https://context7.com/petergriffinjin/graph-cot/llms.txt Provides examples of using the graph_funcs API to perform core graph operations such as retrieving node attributes, exploring neighbors, and calculating node degrees. ```python from tools.graph_funcs import graph_funcs import json graph = json.load(open("/path/to/graph.json")) gf = graph_funcs(graph) paper_id = "53f4a7c2dabfae963d25d100" title = gf.check_nodes(paper_id, "title") abstract = gf.check_nodes(paper_id, "abstract") authors = gf.check_neighbours(paper_id, "author") citation_count = gf.check_degree(paper_id, "cited_by") ``` -------------------------------- ### Initialize OpenAI Client and Environment Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/generate_sample.ipynb Sets up the necessary imports and initializes the OpenAI API client for subsequent model interactions. ```python import os import pickle import json import random from tqdm import tqdm from openai import OpenAI import time client = OpenAI(api_key="xxx") ``` -------------------------------- ### Initialize Graph Data Environment Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/goodreads/design_questions.ipynb Sets up the necessary imports and loads the graph data from a JSON file to prepare for data extraction. ```python import os import json import pickle import random from collections import Counter, defaultdict k = 10 graph = json.load(open(os.path.join('/shared/data3/bowenj4/llm-graph-plugin/data/processed_data/goodreads', 'graph.json'))) all_generated_data = {} ``` -------------------------------- ### Load and Preprocess User Reviews Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/amazon/design_questions.ipynb Loads user review data from a CSV file and preprocesses it to create a history of items interacted with by each user. This is a foundational step for generating recommendation-based training data. ```python user_history = load_reviews('/shared/data3/bowenj4/llm-graph-plugin/data/raw_data/amazon/item_dedup.csv') ``` -------------------------------- ### Initialize Data Storage (Python) Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/legal/design_questions.ipynb Initializes an empty dictionary `all_generated_data` to store generated question-answer data pairs. It also sets an integer variable `k` to 10, likely for limiting the number of generated samples. ```python all_generated_data = {} # key: triple (question (str), answer (str)), value: generated data (List) k = 10 ``` -------------------------------- ### Data Schema Definition for Legal Entities Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/raw_data/legal/raw_process.ipynb Defines the features and potential relationships for various legal document entities including opinions, opinion clusters, courts, and dockets. This schema guides the structuring and processing of the raw data. ```python ## nodes: opinion, opinion_cluster, court, docket ## opinion features: plain_text ## opinion_cluster features: syllabus, judges, case_name, attorneys ## court: full_name, start_date, end_date, citation_string ## docket: pacer_case_id, case_name_full ## (parenthetical): text ``` -------------------------------- ### Generate Multi-hop Biological Relationship Data Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/biomedical/design_questions.ipynb Uses the four_hop function to traverse a knowledge graph starting from symptoms to identify associated cellular components or pathways. It validates the output length and stores the generated question-answer pairs in a dictionary. ```python question = "Which cellular component is participated by most genes that are upregulated in disease with {symptom_name}?" answer = "{cellular_component_name}" generated_data = four_hop('Symptom_nodes', 'Disease-presents-Symptom', 'Disease_nodes', 'Disease-upregulates-Gene', "Gene_nodes", "Gene-participates-Cellular Component", "Cellular_Component_nodes", "symptom_name", "cellular_component_name") assert len(generated_data) == k all_generated_data[(question, answer)] = generated_data ``` -------------------------------- ### Define Data Directories based on Domain Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/raw_data/maple/raw_process.ipynb Sets up the raw data and save directories dynamically based on a specified domain. This allows for organized storage and retrieval of data for different research domains. ```python domain="Materials_Science" # Medicine, Chemistry, Biology, Physics, Materials_Science raw_data_dir=f"/home/ec2-user/quic-efs/user/bowenjin/llm-graph-plugin/data/raw_data/maple/MAPLE/{domain}" save_dir=f"/home/ec2-user/quic-efs/user/bowenjin/llm-graph-plugin/data/processed_data/maple/{domain}" ``` -------------------------------- ### Graph Data JSON Format Example Source: https://context7.com/petergriffinjin/graph-cot/llms.txt This JSON structure defines the format for graph data used by GRBench. It includes typed nodes with features and neighbor relationships, such as 'paper_nodes', 'author_nodes', and 'venue_nodes'. Each node type has a 'features' object and a 'neighbors' object specifying connections to other nodes. ```json { "paper_nodes": { "paper_id_1": { "features": { "title": "Graph Neural Networks: A Review", "abstract": "This paper surveys graph neural network architectures...", "keywords": ["graph neural networks", "deep learning"], "year": "2021" }, "neighbors": { "author": ["author_id_1", "author_id_2"], "venue": ["venue_id_1"], "reference": ["paper_id_2", "paper_id_3"], "cited_by": ["paper_id_4", "paper_id_5"] } } }, "author_nodes": { "author_id_1": { "features": { "name": "John Smith", "organization": "MIT, Cambridge, MA" }, "neighbors": { "paper": ["paper_id_1", "paper_id_6"] } } }, "venue_nodes": { "venue_id_1": { "features": { "name": "NeurIPS" }, "neighbors": { "paper": ["paper_id_1", "paper_id_7"] } } } } ``` -------------------------------- ### Run Graph-CoT Experiments via Bash Source: https://context7.com/petergriffinjin/graph-cot/llms.txt Executes the Graph-CoT pipeline on academic datasets like DBLP and MAPLE. It configures the LLM backend, dataset paths, and output files for result logging. ```bash #!/bin/bash OPENAI_KEY=your_openai_key GPT_version=gpt-3.5-turbo-16k max_steps=10 DATASET=dblp DATA_PATH=/path/to/data/processed_data/$DATASET SAVE_FILE=/path/to/results/$GPT_version/$DATASET/results.jsonl CUDA_VISIBLE_DEVICES=0,1 python Graph-CoT/code/run.py --dataset $DATASET --path $DATA_PATH --save_file $SAVE_FILE --llm_version $GPT_version --openai_api_key $OPENAI_KEY --max_steps $max_steps ``` -------------------------------- ### One-Hop Graph Traversal Function Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/biomedical/design_questions.ipynb Implements a function `one_hop` to retrieve related entities from a graph based on a starting node type, center node, neighbor node type, and edge type. It randomly samples center nodes and collects a specified number (k) of neighbor names, ensuring reproducibility with random seeding. ```python def one_hop(graph, center_node_type, center_node_save_key, neighbor_node_type, neighbor_node_save_key, edge_type, k): generated_data = [] cnt = 0 center_ids = list(graph[center_node_type].keys()) random.shuffle(center_ids) for center_id in center_ids: center_name = graph[center_node_type][center_id]['features']['name'] if edge_type not in graph[center_node_type][center_id]['neighbors']: continue neighbor_ids = graph[center_node_type][center_id]['neighbors'][edge_type] neighbor_names = [graph[neighbor_node_type][neighbor_id]['features']['name'] for neighbor_id in neighbor_ids] if len(neighbor_names) > 5: continue generated_data.append({center_node_save_key:center_name, neighbor_node_save_key: ', '.join(neighbor_names)}) cnt += 1 if cnt == k: break return generated_data ``` -------------------------------- ### Graph Construction Data Structures Initialization Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/raw_data/legal/raw_process.ipynb Initializes dictionaries and defaultdicts to store nodes and edges for constructing a graph representation of legal documents. This includes structures for opinions, clusters, dockets, and courts, along with their relationships. ```python ## construct book node dictionary ## opinion features: date_created, date_modified, text (plain_text, html, html_lawbox, html_columbia, html_anon_2020, xml_harvard, html_with_citations) ## opinion edges: citing opinions, cited opinions, opinion cluster ## opinion_cluster features: date_created, date_modified, judges, date_filed, slug, case_name_short, case_name, case_name_full, scdb_id, scdb_decision_direction, scdb_votes_majority, scdb_votes_minority, attorneys, syllabus, headnotes, summary ## opinion_cluster edges: opinions, docket ## docket features: date_created, date_modified, date_last_index, date_filed, date_last_filing, case_name_short case_name, case_name_full, slug, docket_number, docket_number_core, pacer_case_id, ia_date_first_change, date_blocked ## docket edges: opinion cluster, court ## court: date_modified, position, citation_string, short_name, full_name, url, start_date, end_date, jurisdiction, notes ## court edges: docket opinion_nodes = {} opinion_cluster_nodes = {} docket_nodes = {} court_nodes = {} opinion_cluster2opinions = defaultdict(list) docket2opinion_clusters = defaultdict(list) court2docket = defaultdict(list) ``` -------------------------------- ### Save Preprocessed Samples Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/amazon/design_questions.ipynb Saves the generated training data (all_generated_data) to a pickle file. This allows for persistence and reuse of the preprocessed samples for model training. ```python import json import os import pickle pickle.dump(all_generated_data, open(os.path.join(f'preprocess_samples.pkl'), 'wb')) ``` -------------------------------- ### Initialize Data Structure for Generated Questions Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/biomedical/design_questions.ipynb Initializes an empty dictionary `all_generated_data` to store question-answer pairs and their corresponding generated data. The keys are tuples of (question, answer) strings, and the values are lists of generated data. ```python k = 10 all_generated_data = {} # key: triple (question (str), answer (str)), value: generated data (List) ``` -------------------------------- ### Define Data Directories (Python) Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/legal/design_questions.ipynb Sets up the directory paths for processed and raw data related to the LLM-graph plugin. These variables are used later for loading and saving data files. ```python data_dir=f"/shared/data3/bowenj4/llm-graph-plugin/data/processed_data/legal" downstream_dir=f"/shared/data3/bowenj4/llm-graph-plugin/data/raw_data/legal" ``` -------------------------------- ### Load Graph Data Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/amazon/design_questions.ipynb Initializes the environment and loads a graph structure from a JSON file. It assumes the existence of a 'graph.json' file within the specified data directory. ```python import os import json data_dir = f"/shared/data3/bowenj4/llm-graph-plugin/data/processed_data/amazon" graph = json.load(open(os.path.join(data_dir, 'graph.json'))) print(graph.keys()) ``` -------------------------------- ### Run Open-Source LLM Baseline Source: https://context7.com/petergriffinjin/graph-cot/llms.txt Uses HuggingFace transformers to run inference with open-source models like Llama-2. It initializes the model pipeline and generates answers for a set of benchmark questions. ```python from transformers import AutoTokenizer, pipeline import torch model_name = "meta-llama/Llama-2-13b-chat-hf" tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=True) llm_pipeline = pipeline("text-generation", model=model_name, torch_dtype=torch.float16, device_map="auto") output = llm_pipeline(prompt, do_sample=True, top_k=10, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id, max_length=1000) ``` -------------------------------- ### List First 10 Opinion Node Keys (Python) Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/legal/design_questions.ipynb Retrieves and displays the keys of the first 10 opinion nodes in the graph. This provides a quick overview of the available opinion nodes. ```python list(graph['opinion_nodes'].keys())[:10] ``` -------------------------------- ### Construct Item and Brand Graph Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/raw_data/amazon/raw_process.ipynb Iterates through raw product data to build item and brand nodes. It maps features, establishes neighbor relationships, and ensures bidirectional consistency for related items. ```python item_nodes = {} brand_nodes = {} brand_name2id = {} for item_id in tqdm(item_raw_data): if 'brand' in item_raw_data[item_id] and item_raw_data[item_id]['brand'] != '': if item_raw_data[item_id]['brand'] not in brand_name2id: idd = f'brand_{len(brand_nodes)}' brand_name2id[item_raw_data[item_id]['brand']] = idd brand_nodes[idd] = {'features': {}, 'neighbors': {}} brand_nodes[idd]['features']['name'] = item_raw_data[item_id]['brand'] brand_nodes[idd]['neighbors']['item'] = [item_id] else: brand_nodes[brand_name2id[item_raw_data[item_id]['brand']]]['neighbors']['item'].append(item_id) item_nodes[item_id] = {'features': {}, 'neighbors': {}} item_nodes[item_id]['features']['title'] = item_raw_data[item_id].get('title', '') # ... (feature extraction logic) for item_id in tqdm(item_nodes): for rel in ['also_viewed_item', 'also_bought_item', 'bought_together_item']: for nid in item_nodes[item_id]['neighbors'][rel]: if nid in item_nodes and item_id not in item_nodes[nid]['neighbors'][rel]: item_nodes[nid]['neighbors'][rel].append(item_id) ``` -------------------------------- ### Load Raw Data Files Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/raw_data/maple/raw_process.ipynb Loads raw data for papers, authors, venues, and categories from their respective files using the previously defined reading functions. This step prepares the data for graph construction. ```python book_raw_data = read_json_lines(os.path.join(raw_data_dir, 'papers.json'), 'paper') author_raw_data = read_txt_lines(os.path.join(raw_data_dir, 'authors.txt'), 2) venue_raw_data = read_txt_lines(os.path.join(raw_data_dir, 'venues.txt'), 1) category_raw_data = read_txt_lines(os.path.join(raw_data_dir, 'labels.txt'), 1) ``` -------------------------------- ### Create Docket Nodes and Features (Python) Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/raw_data/legal/raw_process.ipynb This Python code processes raw docket data to construct graph nodes. For each docket, it initializes a node with empty features and neighbors. It then populates the node's features with information such as 'case_name_full' and 'pacer_case_id'. Neighbor relationships for 'opinion_cluster' and 'court' are established, linking dockets to their corresponding opinion clusters and courts. ```Python for idd, docket_row in tqdm(dockets_raw_data.iterrows()): docket_nodes[str(docket_row['id'])] = {'features': {}, 'neighbors': {}} ## add features docket_nodes[str(docket_row['id'])]['features']['case_name_full'] = docket_row['case_name_full'] docket_nodes[str(docket_row['id'])]['features']['pacer_case_id'] = docket_row['pacer_case_id'] ## add neighbors docket_nodes[str(docket_row['id'])]['neighbors']['opinion_cluster'] = docket2opinion_clusters[str(docket_row['id'])] if str(docket_row['id']) in docket2opinion_clusters else [] docket_nodes[str(docket_row['id'])]['neighbors']['court'] = [str(docket_row['court_id'])] court2docket[str(docket_row['court_id'])].append(str(docket_row['id'])) ``` -------------------------------- ### Serialize Processed Data Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/goodreads/design_questions.ipynb Saves the generated recommendation dataset to a file using the pickle module for later use in model training. ```python pickle.dump(all_generated_data, open(os.path.join(f'preprocess_samples.pkl'), 'wb')) print('Saving file of #questions, ', len(all_generated_data)) ``` -------------------------------- ### Data Directory Configuration Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/raw_data/legal/raw_process.ipynb Sets the paths for raw data input and processed data output directories. These variables are used throughout the script to manage data storage and retrieval. ```python raw_data_dir="/home/ubuntu/quic-efs/user/bowenjin/llm-graph-plugin/data/raw_data/legal" save_dir="/home/ubuntu/quic-efs/user/bowenjin/llm-graph-plugin/data/processed_data/legal" ``` -------------------------------- ### Generate Recommendation Training Data Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/amazon/design_questions.ipynb Generates training data for a recommendation system by creating question-answer pairs based on user history. It filters users with insufficient history or missing item titles and formats the data for training. ```python random.seed(2039) question = "What next item should be recommended to the user based on his history: {item_titles}?" answer = "{targe_item_title}" generated_data = [] user_ids = list(user_history.keys()) random.shuffle(user_ids) for user_id in user_ids: tmp_history = user_history[user_id] tmp_history.sort(key=lambda x: x[0]) if len(tmp_history) < 2 or tmp_history[-1][-1] not in graph['item_nodes'] or graph['item_nodes'][tmp_history[-1][-1]]['features']['title'] == '': continue item_titles = [graph['item_nodes'][idd[-1]]['features']['title'] for idd in tmp_history[-8:-1] if idd[-1] in graph['item_nodes'] and graph['item_nodes'][idd[-1]]['features']['title'] != ''] targe_item_title = graph['item_nodes'][tmp_history[-1][-1]]['features']['title'] if targe_item_title != '' and len(item_titles) >= 5: generated_data.append({"item_titles": item_titles, "targe_item_title": targe_item_title}) if len(generated_data) == k: break all_generated_data[(question, answer)] = generated_data ``` -------------------------------- ### Loading LegalBench Datasets for Citation Prediction (Python) Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/legal/design_questions.ipynb This snippet demonstrates how to load the 'citation_prediction_classification' and 'citation_prediction_open' datasets from the Hugging Face 'nguha/legalbench' dataset. It also initializes a set to store unique case names extracted from a graph structure, which will be used for filtering. ```python # download data from datasets import load_dataset lp_cls_dataset = load_dataset("nguha/legalbench", 'citation_prediction_classification', split="test") lp_open_dataset = load_dataset("nguha/legalbench", 'citation_prediction_open', split="test") case_name_set = set() docket_ids_list = list(graph['docket_nodes'].keys()) for docket_id in tqdm(docket_ids_list): case_name = graph['docket_nodes'][docket_id]['features']['case_name'] case_name_set.add(case_name) ``` -------------------------------- ### Generate Multi-hop Reasoning Questions Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/goodreads/design_questions.ipynb Performs complex queries by identifying books sharing the same author and publisher. It uses pre-computed hash maps for efficient neighbor lookups and filters results to ensure valid multi-hop relationships. ```python random.seed(2032) author_to_books = {} publisher_to_books = {} for book_id, book in graph['book_nodes'].items(): for author_id in book['neighbors']['author']: author_to_books.setdefault(author_id, []).append(book_id) if book['neighbors']['publisher']: publisher_id = book['neighbors']['publisher'][0] publisher_to_books.setdefault(publisher_id, []).append(book_id) question = "Find books written by the same author and published by the same publisher as the book {book_title}?" answer = "{matching_book_titles}" generated_data = [] book_ids = list(graph['book_nodes'].keys()) random.shuffle(book_ids) for book_id in book_ids: target_book = graph['book_nodes'][book_id] target_book_title = target_book['features']['title'] target_author_ids = target_book['neighbors']['author'] target_publisher_ids = target_book['neighbors']['publisher'] if not target_publisher_ids: continue matching_book_titles = [] for author_id in target_author_ids: for other_book_id in author_to_books.get(author_id, []): other_book_title = graph['book_nodes'][other_book_id]['features']['title'] if other_book_title != target_book_title and other_book_id in publisher_to_books.get(target_publisher_ids[0], []): matching_book_titles.append(other_book_title) matching_book_titles = list(set(matching_book_titles)) if len(matching_book_titles) == 0 or len(matching_book_titles) > 30: continue if matching_book_titles: generated_data.append({"book_title": target_book_title, "matching_book_titles": ', '.join(matching_book_titles)}) if len(generated_data) == k: break all_generated_data[(question, answer)] = generated_data ``` -------------------------------- ### Saving Processed Data using Pickle (Python) Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/legal/design_questions.ipynb This code snippet saves the `all_generated_data` dictionary, which contains all the generated question-answer pairs, to a file named 'preprocess_samples.pkl' using Python's pickle module. It also prints the total number of questions saved. ```python ## save import pickle import os # Assume 'data_dir' is a defined path for saving the file. # Assume 'all_generated_data' is the dictionary containing the processed data. pickle.dump(all_generated_data, open(os.path.join(data_dir, 'preprocess_samples.pkl'), 'wb')) print('Saving file of #questions, ', len(all_generated_data)) ``` -------------------------------- ### Displaying DataFrame Head (Parentheticals) Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/raw_data/legal/raw_process.ipynb Displays the first 5 rows of the 'parentheticals_raw_data' DataFrame, offering a preview of parenthetical data. ```python parentheticals_raw_data.head(n=5) ``` -------------------------------- ### Serialize Generated Data Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/dblp/design_questions.ipynb Saves the dictionary of generated questions and answers to a local file using the pickle module for later use in training or evaluation. ```python import pickle import os pickle.dump(all_generated_data, open(os.path.join(f'preprocess_samples.pkl'), 'wb')) print('Saving file of #questions, ', len(all_generated_data)) ``` -------------------------------- ### Save Processed Data using Pickle Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/maple/design_questions.ipynb This Python code snippet demonstrates how to save the generated training data (stored in the 'all_generated_data' dictionary) to a file using Python's pickle module. It specifies the output file path and prints a confirmation message indicating the number of questions saved. ```python import pickle import os # Assuming 'all_generated_data' is populated from the previous step # and 'domain' is defined elsewhere. # For demonstration, let's mock them: domain = '.' # Current directory all_generated_data = {("Question Template", "Answer Template"): [{"paper1_title": "T1", "paper2_title": "T2", "paper_target_title": "T1"}]} output_dir = os.path.join(domain) if not os.path.exists(output_dir): os.makedirs(output_dir) output_path = os.path.join(output_dir, 'preprocess_samples.pkl') with open(output_path, 'wb') as f: pickle.dump(all_generated_data, f) print(f'Saving file of #questions, {len(all_generated_data)}') ``` -------------------------------- ### Displaying DataFrame Head (Courts) Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/raw_data/legal/raw_process.ipynb Presents the first 5 rows of the 'court_raw_data' DataFrame, providing a glimpse into the court information. ```python court_raw_data.head(n=5) ``` -------------------------------- ### Load Graph Data from JSON Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/dblp/design_questions.ipynb Initializes the graph structure by loading data from a local JSON file. This is a prerequisite for all subsequent data generation tasks. ```python import json with open('./graph.json', 'r') as fp: graph = json.load(fp) print(graph.keys()) ``` -------------------------------- ### Date Parsing and Review Loading Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/goodreads/design_questions.ipynb Converts string-based dates into numeric format for sorting and loads compressed JSON review files into a user history dictionary. ```python def date2num(date): MONTH2NUM = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07', 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'} _, month, day, time, _, year = date.split(' ') tmp_str = year + MONTH2NUM[month] + day + ''.join(time.split(':')) return int(tmp_str) def load_reviews(file_path): user_history = defaultdict(list) with gzip.open(file_path, 'rb') as f: readin = f.readlines() for line in tqdm(readin): review = json.loads(line.decode('utf-8')) user_history[review['user_id']].append((review['date_added'], date2num(review['date_added']), review['book_id'])) return user_history ``` -------------------------------- ### Generate 1-Hop Question-Answer Pairs Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/dblp/design_questions.ipynb Generates synthetic datasets for simple queries such as identifying paper authors, author affiliations, or paper venues. It uses random sampling to ensure variety in the generated data. ```python random.seed(2023) question = 'Who are the authors of paper "{paper_title}?" ' answer = "{authors}" generated_data = [] paper_ids = list(graph['paper_nodes'].keys()) random.shuffle(paper_ids) for paper_id in paper_ids: paper_title = graph['paper_nodes'][paper_id]['features']['title'] author_ids = graph['paper_nodes'][paper_id]['neighbors']['author'] author_names = [graph['author_nodes'][author_id]['features']['name'] for author_id in author_ids] generated_data.append({"paper_title":paper_title, "authors": ', '.join(author_names)}) if(len(generated_data) == k): break all_generated_data[(question, answer)] = generated_data ``` -------------------------------- ### Generate K-Hop Author Collaboration Questions Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/dblp/design_questions.ipynb Calculates the shortest hop distance between two authors in a graph to generate questions about collaboration degrees. It uses a BFS approach to find k-hop neighbors and validates organization metadata before storing the result. ```python def get_k_hop_neighbor(cur_author, hop, dist): queue = [cur_author] dist[cur_author] = 0 while(len(queue)): cia = queue.pop(0) cur_papers = graph['author_nodes'][cia]['neighbors']['paper'] cur_nids = [] for pid in cur_papers: nids = graph['paper_nodes'][pid]['neighbors']['author'] cur_nids.extend(nids) for cin in cur_nids: if cin in dist: continue dist[cin] = dist[cia] + 1 if dist[cin] == hop: return cin queue.append(cin) return -1 ``` -------------------------------- ### Construct Graph Nodes and Neighbors Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/raw_data/maple/raw_process.ipynb Iterates through the loaded paper data to construct dictionaries for paper, author, and venue nodes. It populates node features and establishes neighbor relationships (e.g., papers citing other papers, authors of papers, papers in venues). ```python paper_nodes = {} author_nodes = {} venue_nodes = {} venue_name2id = {} for paper_id in tqdm(book_raw_data): paper = book_raw_data[paper_id] # venue nodes if paper['venue'] != '': if paper['venue'] not in venue_nodes: venue_nodes[paper['venue']] = {'features': {}, 'neighbors': {}} venue_nodes[paper['venue']]['features']['name'] = venue_raw_data[paper['venue']] venue_nodes[paper['venue']]['neighbors']['paper'] = [paper["paper"]] else: venue_nodes[paper['venue']]['neighbors']['paper'].append(paper["paper"]) # paper nodes paper_nodes[paper_id] = {'features': {}, 'neighbors': {}} ## add features paper_nodes[paper_id]['features']['title'] = paper['title'] paper_nodes[paper_id]['features']['abstract'] = paper['abstract'] paper_nodes[paper_id]['features']['year'] = paper['year'] paper_nodes[paper_id]['features']['label'] = [category_raw_data[lb] for lb in paper['label']] ## add neighbors paper_nodes[paper_id]['neighbors']['author'] = paper['author'] paper_nodes[paper_id]['neighbors']['venue'] = [paper['venue']] if paper['venue'] != '' else [] paper_nodes[paper_id]['neighbors']['reference'] = paper['reference'] paper_nodes[paper_id]['neighbors']['cited_by'] = [] # author nodes for aid in paper['author']: if aid not in author_nodes: author_nodes[aid] = {'features': {}, 'neighbors': {}} author_nodes[aid]['features']['name'] = author_raw_data[aid] author_nodes[aid]['neighbors']['paper'] = [paper["paper"]] else: author_nodes[aid]['neighbors']['paper'].append(paper["paper"]) ## add 'cited_by' for paper nodes for paper_id in tqdm(paper_nodes): for ref_pid in paper_nodes[paper_id]['neighbors']['reference']: if ref_pid not in paper_nodes: continue paper_nodes[ref_pid]['neighbors']['cited_by'].append(paper_id) ``` -------------------------------- ### Generate Paper Recommendation Data Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/maple/design_questions.ipynb This Python code snippet reads paper data from a file, generates candidate paper titles for recommendations, and formats this information into question-answer pairs. It shuffles paper IDs to create diverse candidate lists and stores the generated data in a dictionary keyed by question-answer tuples. The process is controlled by a parameter 'k'. ```python import os import random # Assuming 'graph' and 'downstream_dir' are defined elsewhere # For demonstration, let's mock them: graph = { 'paper_nodes': { 'paper1': {'features': {'title': 'Title of Paper 1'}}, 'paper2': {'features': {'title': 'Title of Paper 2'}}, 'paper3': {'features': {'title': 'Title of Paper 3'}}, 'paper4': {'features': {'title': 'Title of Paper 4'}}, 'paper5': {'features': {'title': 'Title of Paper 5'}}, 'paper6': {'features': {'title': 'Title of Paper 6'}}, 'paper7': {'features': {'title': 'Title of Paper 7'}}, 'paper8': {'features': {'title': 'Title of Paper 8'}}, 'paper9': {'features': {'title': 'Title of Paper 9'}}, 'paper10': {'features': {'title': 'Title of Paper 10'}}, 'paper11': {'features': {'title': 'Title of Paper 11'}}, 'paper12': {'features': {'title': 'Title of Paper 12'}} } } downstream_dir = './data' # Mocking the file content for demonstration if not os.path.exists(downstream_dir): os.makedirs(downstream_dir) with open(os.path.join(downstream_dir, 'PaperRecommendations.txt'), 'w') as f: f.write('paper1\tpaper2\n') f.write('paper3\tpaper4\n') f.write('paper5\tpaper6\n') f.write('paper7\tpaper8\n') f.write('paper9\tpaper10\n') f.write('paper11\tpaper12\n') k = 3 question_template = "Which paper should be recommended to the reader of paper {paper1_title}? Please select from the candidate list {paper2_title}, {paper3_title}, {paper4_title}, {paper5_title}, {paper6_title}, {paper7_title}, {paper8_title}, {paper9_title}, {paper10_title}, {paper11_title}. Please answer the paper title rather than ID." answer_template = "{paper_target_title}" generated_data = [] raw_pair = [] curr_set = set() with open(os.path.join(downstream_dir, 'PaperRecommendations.txt')) as f: while True: line = f.readline() if not line: # Break if end of file is reached break tmp = line.strip().split('\t') if len(tmp) == 2 and tmp[0] in graph['paper_nodes'] and tmp[1] in graph['paper_nodes'] and tmp[0] not in curr_set and tmp[1] not in curr_set: raw_pair.append((tmp[0], tmp[1])) curr_set.add(tmp[0]) curr_set.add(tmp[1]) if len(raw_pair) == k * 5: # Adjusted condition to ensure enough pairs are collected break random.shuffle(raw_pair) paper_ids = list(graph['paper_nodes'].keys()) all_generated_data = {} for pair in raw_pair: candidate_titles = [] # Ensure the target paper is one of the candidates candidate_titles.append(graph['paper_nodes'][pair[1]]['features']['title']) # Collect other candidate titles, ensuring no duplicates and sufficient count other_paper_ids = [pid for pid in paper_ids if pid != pair[1]] random.shuffle(other_paper_ids) num_needed = 10 # We need 10 candidates in total (1 target + 9 others) for i in range(min(num_needed - 1, len(other_paper_ids))): candidate_titles.append(graph['paper_nodes'][other_paper_ids[i]]['features']['title']) # If we still don't have enough candidates (e.g., very small graph), fill with duplicates or handle error while len(candidate_titles) < num_needed: # This part might need adjustment based on desired behavior for small graphs # For now, let's just repeat the last added title or a default one if candidate_titles: candidate_titles.append(candidate_titles[-1]) else: candidate_titles.append("Default Candidate Title") random.shuffle(candidate_titles) # Ensure we have exactly 10 candidates after shuffling for indexing candidate_titles = candidate_titles[:num_needed] generated_data.append({ "paper1_title": graph['paper_nodes'][pair[0]]['features']['title'], "paper2_title": candidate_titles[0], "paper3_title": candidate_titles[1], "paper4_title": candidate_titles[2], "paper5_title": candidate_titles[3], "paper6_title": candidate_titles[4], "paper7_title": candidate_titles[5], "paper8_title": candidate_titles[6], "paper9_title": candidate_titles[7], "paper10_title": candidate_titles[8], "paper11_title": candidate_titles[9], "paper_target_title": graph['paper_nodes'][pair[1]]['features']['title'] }) if len(generated_data) == k: break all_generated_data[(question_template, answer_template)] = generated_data ``` -------------------------------- ### Count Similar Books for a Book (Python) Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/goodreads/design_questions.ipynb Generates question-answer pairs to determine the number of similar books associated with a given book. It iterates through books and counts the entries in their 'similar_books' neighbor list. ```Python random.seed(2037) question = "How many similar books does the book {book_title} have?" answer = "{number_of_similar_books}" generated_data = [] book_ids = list(graph['book_nodes'].keys()) random.shuffle(book_ids) for book_id in book_ids: book_title = graph['book_nodes'][book_id]['features']['title'] similar_books = graph['book_nodes'][book_id]['neighbors'].get('similar_books') if not similar_books: continue number_of_similar_books = len(similar_books) generated_data.append({"book_title": book_title, "number_of_similar_books": number_of_similar_books}) if len(generated_data) == k: break all_generated_data[(question, answer)] = generated_data ``` -------------------------------- ### Load User Review History Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/amazon/design_questions.ipynb Parses a CSV-formatted review file to build a mapping of users to their historical item interactions. It uses a defaultdict to group data by user ID. ```python import json import gzip from collections import defaultdict from tqdm import tqdm def load_reviews(file_path): user_history = defaultdict(list) with open(file_path, 'r') as f: readin = f.readlines() for line in tqdm(readin): tmp = line.strip().split(',') user_history[tmp[0]].append((tmp[-1], tmp[1])) return user_history ``` -------------------------------- ### Display First Four Book IDs Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/raw_data/goodreads/raw_process.ipynb Retrieves and displays the first four book IDs from the loaded `book_raw_data` dictionary. This is a simple check to verify data loading and inspect the keys. ```python list(book_raw_data.keys())[:4] ``` -------------------------------- ### Displaying DataFrame Head (Opinions) Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/raw_data/legal/raw_process.ipynb Shows the first 5 rows of the 'opinion_raw_data' DataFrame. This is useful for inspecting the structure and content of the opinion data. ```python opinion_raw_data.head(n=5) ``` -------------------------------- ### Construct Graph Nodes and Edges Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/raw_data/dblp/raw_process.ipynb Processes the raw DBLP paper data to construct nodes for papers, authors, and venues. It extracts features for each node and defines their relationships (neighbors), including papers citing other papers and authors contributing to papers. The 'cited_by' relationship is also populated by iterating through references. ```python ## construct node dictionary ## paper features: title, abstract, keywords, lang, year ## paper neighbors: paper, author, venue ## author features: name, org ## author neighbors: paper ## venue features: name ## venue neighbors: paper paper_nodes = {} author_nodes = {} venue_nodes = {} venue_name2id = {} for paper in tqdm(paper_raw_data): # venue nodes if paper['venue']['raw'] != '': if paper['venue']['raw'] not in venue_name2id: idd = f'pub_{len(venue_nodes)}' venue_name2id[paper['venue']['raw']] = idd venue_nodes[idd] = {'features': {}, 'neighbors': {}} venue_nodes[idd]['features']['name'] = paper['venue']['raw'] venue_nodes[idd]['neighbors']['paper'] = [paper["id"]] else: venue_nodes[venue_name2id[paper['venue']['raw']]]['neighbors']['paper'].append(paper["id"]) # paper nodes paper_nodes[paper["id"]] = {'features': {}, 'neighbors': {}} ## add features paper_nodes[paper["id"]]['features']['title'] = paper['title'] paper_nodes[paper["id"]]['features']['abstract'] = paper['abstract'] paper_nodes[paper["id"]]['features']['keywords'] = paper['keywords'] paper_nodes[paper["id"]]['features']['lang'] = paper['lang'] paper_nodes[paper["id"]]['features']['year'] = paper['year'] ## add neighbors paper_nodes[paper["id"]]['neighbors']['author'] = [a['id'] for a in paper['authors']] paper_nodes[paper["id"]]['neighbors']['venue'] = [venue_name2id[paper['venue']['raw']]] if paper['venue']['raw'] != '' else [] paper_nodes[paper["id"]]['neighbors']['reference'] = paper['references'] if 'references' in paper else [] paper_nodes[paper["id"]]['neighbors']['cited_by'] = [] # author nodes for a in paper['authors']: if a["id"] not in author_nodes: author_nodes[a["id"]] = {'features': {}, 'neighbors': {}} author_nodes[a["id"]]['features']['name'] = a['name'] author_nodes[a["id"]]['features']['organization'] = a['org'] author_nodes[a["id"]]['neighbors']['paper'] = [paper["id"]] else: author_nodes[a["id"]]['neighbors']['paper'].append(paper["id"]) ## add 'cited_by' for paper nodes for paper_id in tqdm(paper_nodes): for ref_pid in paper_nodes[paper_id]['neighbors']['reference']: if ref_pid not in paper_nodes: continue paper_nodes[ref_pid]['neighbors']['cited_by'].append(paper_id) ``` -------------------------------- ### Print Sample Count Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/amazon/design_questions.ipynb Prints the total number of generated data samples stored in `all_generated_data`. This is a simple utility to verify the quantity of data processed. ```python print(len(all_generated_data)) ``` -------------------------------- ### Generate Reasoning Questions Source: https://github.com/petergriffinjin/graph-cot/blob/main/data/processed_data/amazon/design_questions.ipynb Iterates through item nodes in the graph to generate question-answer pairs based on item features like brand, category, and price. It uses random sampling to select items and enforces a limit on the number of generated samples. ```python import random random.seed(2023) all_generated_data = {} k = 10 item_ids = list(graph['item_nodes'].keys()) random.shuffle(item_ids) # Example: Generate brand questions question = "What is the brand of item {item_title}?" answer = "{brand_name}" generated_data = [] for item_id in item_ids: item_title = graph['item_nodes'][item_id]['features']['title'] brand_ids = graph['item_nodes'][item_id]['neighbors']['brand'] if len(brand_ids) == 1 and item_title != '': brand_names = [graph['brand_nodes'][bid]['features']['name'] for bid in brand_ids] generated_data.append({"item_title": item_title, "brand_name": ', '.join(brand_names)}) if len(generated_data) == k: break all_generated_data[(question, answer)] = generated_data ``` -------------------------------- ### Run GPT Baseline Inference Source: https://context7.com/petergriffinjin/graph-cot/llms.txt Performs batch asynchronous inference using OpenAI models without graph augmentation. It processes input questions from a JSONL file and collects model responses for performance comparison. ```python import asyncio from openai import AsyncOpenAI async def dispatch_openai_requests(client, messages_list, model="gpt-3.5-turbo", temperature=0.01, max_tokens=2048, top_p=1.0): async_responses = [client.chat.completions.create(model=model, messages=x, temperature=temperature, max_tokens=max_tokens, top_p=top_p) for x in messages_list] return await asyncio.gather(*async_responses) client = AsyncOpenAI(api_key="your_openai_key") responses = asyncio.run(dispatch_openai_requests(client, query_messages[:20])) ```