I have a series of documents that need their content classified with multiple AI models. What data structure should I use to store the classifications?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
When dealing with multiple AI models classifying the content of documents, you need a data structure that can efficiently store and manage the classifications from each model. Here are a few options, each with its own advantages:
1. Nested Dictionary
A nested dictionary can be a straightforward way to store the classifications. Each document can be a key in the outer dictionary, and the value can be another dictionary where each key is the model name and the value is the classification result.
If you are working in a Python environment, using a pandas DataFrame can be very powerful, especially if you need to perform data analysis or manipulation.
Nested Dictionary: Simple and easy to use for small to medium-sized datasets.
List of Dictionaries: Good for iterating over documents.
DataFrame: Ideal for data analysis and manipulation.
Custom Class: Best for complex scenarios where you need encapsulation and methods.
JSON: Great for storage and transmission.
Choose the structure that best fits your needs based on the size of your dataset, the operations you need to perform, and the environment in which you are working.
User
Let’s say each document needs multiple passes from an LLM the first is instructed to return json classification of its content, the second enforces the json schema.
Assistant
Given the requirement to process each document with multiple passes from a Language Model (LLM), where the first pass returns a JSON classification and the second pass enforces the JSON schema, you can structure your data and processing pipeline as follows:
Data Structure
You can use a combination of nested dictionaries and lists to store the classifications and the results of each pass. Here's an example structure:
python
documents = [
{
"document_id": "document_1",
"content": "Content of document 1",
"pass_1_result": None, # Result from the first pass
"pass_2_result": None # Result from the second pass
},
{
"document_id": "document_2",
"content": "Content of document 2",
"pass_1_result": None, # Result from the first pass
"pass_2_result": None # Result from the second pass
}
]
Processing Pipeline
First Pass: Instruct the LLM to return a JSON classification of the document's content.
Second Pass: Enforce the JSON schema on the result from the first pass.
Here's a sample implementation in Python:
python
import json
from some_llm_library import LLM # Replace with your actual LLM library
# Initialize the LLM
llm = LLM()
# Define the JSON schema for validation
json_schema = {
"type": "object",
"properties": {
"category": {"type": "string"},
"subcategory": {"type": "string"},
"confidence": {"type": "number"}
},
"required": ["category", "subcategory", "confidence"]
}
def validate_json_schema(data, schema):
# Implement JSON schema validation logic here
# This is a placeholder for actual validation
try:
jsonschema.validate(instance=data, schema=schema)
return True
except jsonschema.exceptions.ValidationError as err:
return False
def process_document(document):
# First Pass: Get JSON classification from LLM
prompt_1 = f"Classify the following content and return the result in JSON format: {document['content']}"
pass_1_result = llm.generate(prompt_1)
document["pass_1_result"] = json.loads(pass_1_result)
# Second Pass: Enforce JSON schema
if validate_json_schema(document["pass_1_result"], json_schema):
document["pass_2_result"] = document["pass_1_result"]
else:
prompt_2 = f"Enforce the following JSON schema on the given data: {json.dumps(document['pass_1_result'])} with schema {json.dumps(json_schema)}"
pass_2_result = llm.generate(prompt_2)
document["pass_2_result"] = json.loads(pass_2_result)
# Process each document
for document in documents:
process_document(document)
# Output the results
for document in documents:
print(f"Document ID: {document['document_id']}")
print(f"Pass 1 Result: {document['pass_1_result']}")
print(f"Pass 2 Result: {document['pass_2_result']}")
Explanation
Data Structure: Each document is represented as a dictionary with fields for the document ID, content, and results from the two passes.
First Pass: The LLM is prompted to classify the document's content and return the result in JSON format.
Second Pass: The result from the first pass is validated against the JSON schema. If it is valid, it is used as is; otherwise, the LLM is prompted to enforce the schema.
Validation: A placeholder function validate_json_schema is used to validate the JSON against the schema. You can replace this with actual validation logic using a library like jsonschema.
This approach ensures that each document is processed in two passes, with the second pass enforcing the JSON schema, and the results are stored in a structured manner.