Wednesday, June 18, 2025

Optimizing LLM Queries for CSV Files to Minimize Token Usage: A Beginner's Guide

When working with large CSV files and querying them using a Language Model (LLM), optimizing your approach to minimize token usage is crucial. This helps reduce costs, improve performance, and make your system more efficient. Here’s a beginner-friendly guide to help you understand how to achieve this.


What Are Tokens, and Why Do They Matter?

Tokens are the building blocks of text that LLMs process. A single word like "cat" or punctuation like "." counts as a token. Longer texts mean more tokens, which can lead to higher costs and slower query responses. By optimizing how you query CSV data, you can significantly reduce token usage.


Key Strategies to Optimize LLM Queries for CSV Files

1. Preprocess and Filter Data

Before sending data to the LLM, filter and preprocess it to retrieve only the relevant rows and columns. This minimizes the size of the input text.

How to Do It:

  • Use Python or database tools to preprocess the CSV file.
  • Filter for only the rows and columns necessary for your query.
    import pandas as pd
    
    # Load CSV file
    df = pd.read_csv("data.csv")
    
    # Filter relevant data
    filtered_df = df[df["Category"] == "AI"]
    filtered_df = filtered_df[["Name", "Score"]]
    
    # Save filtered data to a smaller CSV file
    filtered_df.to_csv("filtered_data.csv", index=False)
    

Benefit: Instead of sending an entire CSV file, you send only the required subset of data.


2. Summarize Data

Use aggregation or summarization techniques to condense the data before passing it to the LLM.

Example Use Case:
Instead of sending 1,000 rows of sales data, aggregate it at a higher level:

  • Compute totals, averages, or other metrics.
  • Pass only the summary (e.g., "Total sales for 2025: $1,000,000").
summary = df.groupby("Year")["Sales"].sum()
summary_text = f"Total sales by year: {summary.to_dict()}"

Benefit: A single line of summary text replaces thousands of rows, saving tokens.


3. Use Metadata for Efficient Queries

Add metadata to your CSV file, such as tags or categories, to make filtering easier and faster.

Example:
If your CSV contains transaction logs, include metadata like:

  • Date
  • Category
  • Priority

Instead of passing raw logs, query the metadata first:

  • Query: "Show me high-priority transactions from June 2025."
  • Result: Filtered data with only relevant rows.

4. Chunk Large Data

If your CSV file is too large to process at once, split it into smaller chunks and query the LLM sequentially.

How to Do It:

  • Divide the file into multiple parts.
  • Process each chunk individually.
  • Combine the results.
# Split CSV into chunks
chunk_size = 100  # Number of rows per chunk
for i, chunk in enumerate(pd.read_csv("large_file.csv", chunksize=chunk_size)):
    chunk.to_csv(f"chunk_{i}.csv", index=False)

Benefit: Smaller chunks fit within the LLM’s context window, reducing token usage.


5. Use Embedding-Based Search

Convert your CSV data into embeddings (vector representations). Store these embeddings in a vector database and perform similarity searches to retrieve the most relevant rows for your query.

Tools to Use:

  • Use libraries like sentence-transformers to generate embeddings.
  • Store them in vector databases like Pinecone or Weaviate.

Example:

  • Query: "Find rows similar to 'AI projects with a score above 90'."
  • Result: Only the top 5–10 most relevant rows are sent to the LLM.

6. Pre-Format Data

Format your data into a compact structure, like JSON or a short table, before sending it to the LLM.

Example:
Instead of sending raw CSV rows:

Name, Score, Category
Alice, 95, AI
Bob, 88, Data Science

Send this:

[{"Name": "Alice", "Score": 95, "Category": "AI"}]

Benefit: Structured data reduces unnecessary tokens and improves query clarity.


7. Leverage Caching

Cache the results of frequently used queries. If a query has already been processed, return the cached result instead of re-querying the LLM.

How to Implement:

  • Use a key-value store (e.g., Redis) to save query results.
  • Check the cache before querying the LLM.

Example:

  • Query: "Summarize sales data for 2025."
  • Cache lookup: Return the previously computed summary if available.

8. Ask Specific Questions

Frame your queries to be as specific as possible. Avoid open-ended or vague queries, as they may require processing more data.

Example:

  • Instead of: "Tell me about the CSV file."
  • Ask: "What are the top 5 products by sales in 2025?"

A Practical Workflow for CSV Optimization

  1. Load and Preprocess Data

    • Use Python or database tools to filter and preprocess the CSV.
  2. Summarize or Chunk Data

    • Aggregate or split the data into smaller sets.
  3. Query Efficiently

    • Use metadata or embeddings to retrieve only relevant rows.
  4. Send Compact Data to LLM

    • Format the filtered data into a compact structure.
  5. Cache Results

    • Store frequently queried results to avoid redundant token usage.

Example Query Optimization

Scenario: You have a CSV file with 1,000 rows of sales data. You want to know the top 3 products by sales.

Non-Optimized Query:

"Here is my CSV file: [entire 1,000 rows]. What are the top 3 products by sales?"

Optimized Query:

  1. Preprocess the CSV:
    top_products = df.groupby("Product")["Sales"].sum().nlargest(3)
    
  2. Send the result to the LLM:
    "These are the top 3 products by sales: {'Product A': 500, 'Product B': 450, 'Product C': 400}. Provide insights on these."
    

Conclusion

By filtering, summarizing, and structuring your data before querying an LLM, you can significantly reduce token usage and costs. Start small, experiment with these techniques, and gradually build an efficient workflow for handling large CSV files. Happy optimizing! 🚀

Friday, June 13, 2025

Enhancing Prompt Engineering Accuracy in Software Requirements Processing with Multi-Agent Systems and Multiple LLMs

When processing software requirements or testing documents using large language models (LLMs), ensuring the accuracy and reliability of the results is a critical challenge. One effective approach is using a multi-agent system that leverages multiple LLMs to cross-validate outputs, compare results, and improve overall accuracy. This approach reduces reliance on a single model and ensures robustness in processing, especially for tasks like test case generation, requirement validation, and data summarization.

This blog explores how to use multi-agent systems with different LLMs to process software requirements, compare results, and measure accuracy.


What is a Multi-Agent System?

A multi-agent system involves multiple independent AI agents (in this case, LLMs) working together to:

  1. Perform the same task independently.
  2. Cross-validate or refine each other’s outputs.
  3. Provide diverse perspectives to improve the quality and reliability of the final result.

In our case, the agents will be different LLMs (e.g., GPT-4, Claude, Cohere, or Google Bard).


Use Case Overview

We’ll process a software requirements CSV file to:

  1. Extract high-priority functional requirements.
  2. Generate test cases for these requirements.
  3. Use multiple LLMs to process the data and compare their outputs.

Finally, we’ll measure accuracy by comparing the results and identifying discrepancies.


Step-by-Step Guide

Step 1: Setup and Prepare the Data

We’ll begin by preparing the software requirements data in a CSV format.

Example CSV File (requirements.csv):

ID,Requirement,Type,Priority,Status
1,Users must be able to register and log in using their email and password,Functional,High,Approved
2,Search functionality must return relevant results within 2 seconds,Functional,Medium,Pending
3,The platform must handle 500 concurrent users,Non-Functional,High,Approved
4,Payment processing must support credit cards and PayPal securely,Functional,High,Approved
5,Daily backups of all data must be performed automatically,Non-Functional,Medium,Pending

Load the CSV Data in Python

import pandas as pd

# Load the CSV file
csv_file = 'requirements.csv'
df = pd.read_csv(csv_file)

# Convert the data to JSON format
json_data = df.to_json(orient='records')
print("JSON Data for AI Query:")
print(json_data)

Step 2: Define the Prompt

We’ll use the following prompt to query multiple LLMs. The task involves extracting high-priority functional requirements and generating test cases.

Prompt Template:

Here is the software requirements data in JSON format:

{json_data}

Task:
1. Extract all "High Priority" functional requirements.
2. Generate 2 test cases for each extracted requirement.
3. Return the output in a structured JSON format.

Step 3: Query Multiple LLMs

We’ll use Python to query multiple LLMs (e.g., GPT-4, Claude, Cohere) simultaneously. Here’s how you can set up the multi-agent system.

Install Required Libraries

pip install openai anthropic cohere

Python Code for Multi-Agent Queries

import openai
import requests
import json

# Set API Keys for different LLMs
openai.api_key = 'your-openai-api-key'
anthropic_api_key = 'your-anthropic-api-key'
cohere_api_key = 'your-cohere-api-key'

# Define the prompt
prompt = f"""
Here is the software requirements data in JSON format:

{json_data}

Task:
1. Extract all "High Priority" functional requirements.
2. Generate 2 test cases for each extracted requirement.
3. Return the output in a structured JSON format.
"""

# Query GPT (OpenAI)
def query_gpt(prompt):
    response = openai.Completion.create(
        engine="text-davinci-003",
        prompt=prompt,
        max_tokens=500,
        temperature=0
    )
    return response.choices[0].text.strip()

# Query Claude (Anthropic)
def query_claude(prompt):
    url = "https://api.anthropic.com/v1/complete"
    headers = {
        "x-api-key": anthropic_api_key,
        "Content-Type": "application/json"
    }
    data = {
        "prompt": prompt,
        "model": "claude-v1",
        "max_tokens_to_sample": 500,
        "temperature": 0
    }
    response = requests.post(url, headers=headers, json=data)
    return response.json()['completion'].strip()

# Query Cohere
def query_cohere(prompt):
    url = "https://api.cohere.ai/generate"
    headers = {"Authorization": f"Bearer {cohere_api_key}"}
    data = {
        "model": "command-xlarge-nightly",
        "prompt": prompt,
        "max_tokens": 500,
        "temperature": 0
    }
    response = requests.post(url, headers=headers, json=data)
    return response.json()['generations'][0]['text'].strip()

# Execute queries
gpt_output = query_gpt(prompt)
claude_output = query_claude(prompt)
cohere_output = query_cohere(prompt)

# Display outputs
print("GPT Output:")
print(gpt_output)

print("\nClaude Output:")
print(claude_output)

print("\nCohere Output:")
print(cohere_output)

Step 4: Compare and Validate Outputs

To measure the accuracy of the outputs, we’ll compare the results from all LLMs and identify discrepancies. This can be done by:

  1. Manual Comparison: Reviewing outputs side-by-side.
  2. Automated Comparison: Using Python to check for differences.

Automated Comparison Code

# Parse outputs into JSON
gpt_data = json.loads(gpt_output)
claude_data = json.loads(claude_output)
cohere_data = json.loads(cohere_output)

# Compare outputs
def compare_outputs(data1, data2):
    discrepancies = []
    for key in data1.keys():
        if data1[key] != data2[key]:
            discrepancies.append({
                "Key": key,
                "Data1": data1[key],
                "Data2": data2[key]
            })
    return discrepancies

# Compare GPT vs Claude
discrepancies_gpt_claude = compare_outputs(gpt_data, claude_data)

# Compare GPT vs Cohere
discrepancies_gpt_cohere = compare_outputs(gpt_data, cohere_data)

# Display discrepancies
print("Discrepancies between GPT and Claude:")
print(discrepancies_gpt_claude)

print("\nDiscrepancies between GPT and Cohere:")
print(discrepancies_gpt_cohere)

Step 5: Measure Accuracy

To evaluate the accuracy of each model:

  1. Define a ground truth (a set of manually verified correct outputs).
  2. Compare each model’s output against the ground truth using metrics like:
    • Precision: Correct results out of all results generated by the model.
    • Recall: Correct results out of all possible correct results.
    • F1-Score: Harmonic mean of precision and recall.

Accuracy Calculation Code

from sklearn.metrics import precision_score, recall_score, f1_score

def calculate_accuracy(predicted, ground_truth):
    # Convert results to sets for comparison
    predicted_set = set(predicted)
    ground_truth_set = set(ground_truth)
    
    # Calculate metrics
    tp = len(predicted_set & ground_truth_set)  # True positives
    fp = len(predicted_set - ground_truth_set)  # False positives
    fn = len(ground_truth_set - predicted_set)  # False negatives
    
    precision = tp / (tp + fp) if tp + fp > 0 else 0
    recall = tp / (tp + fn) if tp + fn > 0 else 0
    f1 = 2 * (precision * recall) / (precision + recall) if precision + recall > 0 else 0
    
    return {"Precision": precision, "Recall": recall, "F1-Score": f1}

# Example ground truth and predicted outputs
ground_truth = [
    "Users must be able to register with valid credentials.",
    "Payment can be processed securely via credit card."
]

predicted_gpt = [
    "Users must be able to register with valid credentials.",
    "Payment can be processed securely via credit card."
]

predicted_claude = [
    "Users must be able to register with valid credentials.",
    "Payments can be processed using PayPal securely."
]

# Calculate accuracy for GPT
accuracy_gpt = calculate_accuracy(predicted_gpt, ground_truth)
print("GPT Accuracy:", accuracy_gpt)

# Calculate accuracy for Claude
accuracy_claude = calculate_accuracy(predicted_claude, ground_truth)
print("Claude Accuracy:", accuracy_claude)

Benefits of Multi-Agent Systems

  1. Increased Reliability: Cross-validating outputs from multiple models ensures higher accuracy.
  2. Error Detection: Discrepancies highlight potential errors or ambiguities in the data or model behavior.
  3. Diverse Perspectives: Different LLMs may interpret prompts differently, providing complementary insights.

Conclusion

Using a multi-agent system with multiple LLMs (e.g., GPT, Claude, Cohere), you can improve the accuracy and reliability of software requirement processing tasks. By comparing outputs, identifying discrepancies, and calculating metrics like precision and recall, you can ensure robust results for tasks like test case generation, requirement extraction, and validation.

This approach is ideal for critical projects where accuracy is paramount, and errors can have significant consequences. With Python and Generative AI, you can build a scalable and reliable pipeline for processing technical documents. 🚀

Processing Software Requirements or Testing Documents in CSV Format with Python and Generative AI

 Software requirements or testing documents often contain structured data, and querying or processing these documents effectively can make tasks like test case generation, requirement analysis, and data summarization much easier. In this blog, we’ll explore how to use Python and Generative AI to process software requirements or testing documents stored in CSV files.

We’ll cover:

  1. Reading and preparing the CSV file.
  2. Writing queries for Generative AI using prompt engineering.
  3. Using Generative AI to extract, process, and generate additional data based on the CSV.
  4. Saving results back to a CSV file.

1. Understanding the Example CSV

Here’s an example of a software requirements CSV file (requirements.csv):

ID,Requirement,Type,Priority,Status
1,Users must be able to register and log in using their email and password,Functional,High,Approved
2,Search functionality must return relevant results within 2 seconds,Functional,Medium,Pending
3,The platform must handle 500 concurrent users,Non-Functional,High,Approved
4,Payment processing must support credit cards and PayPal securely,Functional,High,Approved
5,Daily backups of all data must be performed automatically,Non-Functional,Medium,Pending

2. Python Code to Read and Process the CSV

We’ll use Python to:

  1. Read the CSV file into a DataFrame.
  2. Convert it to JSON for Generative AI queries.
  3. Write prompts to query the data.
  4. Save the AI-generated results back to a CSV.

Setup

Install Required Libraries:

pip install pandas openai

Step 1: Read the CSV File

We’ll use pandas to read the CSV file into a DataFrame and convert it into JSON format for use with Generative AI.

import pandas as pd

# Read the CSV file
csv_file = 'requirements.csv'
df = pd.read_csv(csv_file)

# Display the DataFrame
print("Original DataFrame:")
print(df)

# Convert the DataFrame into JSON for AI queries
json_data = df.to_json(orient='records')
print("\nData in JSON format:")
print(json_data)

Output:

Original DataFrame:
   ID                                          Requirement           Type  \
0   1  Users must be able to register and log in using...    Functional   
1   2  Search functionality must return relevant resul...    Functional   
2   3     The platform must handle 500 concurrent users  Non-Functional   
3   4  Payment processing must support credit cards an...    Functional   
4   5  Daily backups of all data must be performed aut...  Non-Functional   

  Priority    Status  
0     High  Approved  
1   Medium   Pending  
2     High  Approved  
3     High  Approved  
4   Medium   Pending  

Data in JSON format:
[{"ID":1,"Requirement":"Users must be able to register and log in using their email and password","Type":"Functional","Priority":"High","Status":"Approved"}, ...]

Step 2: Write a Query Using Prompt Engineering

To interact with the JSON data, we’ll craft a prompt for Generative AI. For this example, we’ll filter all high-priority functional requirements and generate test cases for each.

Prepare the Prompt

import openai

# Set your OpenAI API key
openai.api_key = 'your-openai-api-key'

# Define the prompt
prompt = f"""
Here is the software requirements data in JSON format:

{json_data}

Task:
1. Extract all "High Priority" functional requirements.
2. Generate 2 test cases for each extracted requirement.
3. Return the output in a structured JSON format.
"""

# Display the prompt
print("Generated Prompt:")
print(prompt)

Step 3: Query the Generative AI Model

We’ll send the prompt to OpenAI’s API (e.g., GPT-4) and process the output.

# Query the OpenAI API
response = openai.Completion.create(
    engine="text-davinci-003",  # Use the appropriate engine
    prompt=prompt,
    max_tokens=500,
    temperature=0
)

# Extract the response text
ai_output = response.choices[0].text.strip()

# Display AI's Output
print("\nAI Output:")
print(ai_output)

Expected AI Output:

{
  "High Priority Requirements": [
    {
      "Requirement": "Users must be able to register and log in using their email and password",
      "Test Cases": [
        "Verify that the user can register with a valid email and password.",
        "Verify that the user cannot register with an invalid email format."
      ]
    },
    {
      "Requirement": "Payment processing must support credit cards and PayPal securely",
      "Test Cases": [
        "Verify that payment can be processed securely via credit card.",
        "Verify that payment can be processed securely via PayPal."
      ]
    }
  ]
}

Step 4: Save the Results Back to a CSV

Now, we’ll parse the AI’s JSON output and save the results in a structured CSV file.

Parse and Save the Results

import json

# Parse the AI output (assumes it is valid JSON)
output_data = json.loads(ai_output)

# Flatten the data for CSV storage
flattened_data = []
for item in output_data['High Priority Requirements']:
    for test_case in item['Test Cases']:
        flattened_data.append({
            "Requirement": item['Requirement'],
            "Test Case": test_case
        })

# Convert to a DataFrame
output_df = pd.DataFrame(flattened_data)

# Save to a new CSV file
output_file = 'high_priority_test_cases.csv'
output_df.to_csv(output_file, index=False)

print(f"\nGenerated test cases saved to {output_file}")

Generated CSV (high_priority_test_cases.csv):

Requirement,Test Case
Users must be able to register and log in using their email and password,Verify that the user can register with a valid email and password.
Users must be able to register and log in using their email and password,Verify that the user cannot register with an invalid email format.
Payment processing must support credit cards and PayPal securely,Verify that payment can be processed securely via credit card.
Payment processing must support credit cards and PayPal securely,Verify that payment can be processed securely via PayPal.

Complete Python Script

Here’s the complete code for the workflow:

import pandas as pd
import openai
import json

# Set OpenAI API key
openai.api_key = 'your-openai-api-key'

# Step 1: Read the CSV file
csv_file = 'requirements.csv'
df = pd.read_csv(csv_file)
json_data = df.to_json(orient='records')

# Step 2: Define the prompt
prompt = f"""
Here is the software requirements data in JSON format:

{json_data}

Task:
1. Extract all "High Priority" functional requirements.
2. Generate 2 test cases for each extracted requirement.
3. Return the output in a structured JSON format.
"""

# Step 3: Query the OpenAI API
response = openai.Completion.create(
    engine="text-davinci-003",
    prompt=prompt,
    max_tokens=500,
    temperature=0
)

# Extract and parse the AI output
ai_output = response.choices[0].text.strip()
output_data = json.loads(ai_output)

# Step 4: Flatten the data for saving to CSV
flattened_data = []
for item in output_data['High Priority Requirements']:
    for test_case in item['Test Cases']:
        flattened_data.append({
            "Requirement": item['Requirement'],
            "Test Case": test_case
        })

# Convert to DataFrame and save to CSV
output_df = pd.DataFrame(flattened_data)
output_file = 'high_priority_test_cases.csv'
output_df.to_csv(output_file, index=False)

print(f"Generated test cases saved to {output_file}")

Conclusion

With this workflow, you can:

  1. Load and process CSV data for software requirements or testing documents.
  2. Use Generative AI with prompt engineering to extract, analyze, or generate additional information (e.g., test cases).
  3. Save AI-generated results into a structured CSV file for further use.

This approach is highly modular and can be adapted for different tasks, such as summarizing requirements, identifying gaps, or validating completeness. Happy engineering! 🚀

Detailed Prompt Engineering for Processing Software Requirements or Testing Documents in CSV Format

When working with software requirements or testing documents in a CSV format, crafting detailed queries or instructions using prompt engineering becomes crucial. These documents often have structured data (e.g., requirements, test cases, priorities) that require targeted processing, such as extracting specific information, analyzing gaps, or generating additional data like test scenarios.

In this blog, we’ll explore how to write detailed queries and instructions to process software requirements or testing documents stored in CSV format using prompt engineering.

Understanding the CSV Structure

Before writing prompts, it’s essential to understand the structure of the CSV file. Here’s an example of how software requirements or testing data might look in a CSV:

Example CSV Data: requirements.csv

ID,Requirement,Type,Priority,Status
1,Users must be able to register and log in using their email and password,Functional,High,Approved
2,Search functionality must return relevant results within 2 seconds,Functional,Medium,Pending
3,The platform must handle 500 concurrent users,Non-Functional,High,Approved
4,Payment processing must support credit cards and PayPal securely,Functional,High,Approved
5,Daily backups of all data must be performed automatically,Non-Functional,Medium,Pending

In this structure:

  • ID: Unique identifier for each requirement.
  • Requirement: The actual description of the requirement.
  • Type: Whether it’s a functional or non-functional requirement.
  • Priority: Priority level (High, Medium, Low).
  • Status: Current status of the requirement (e.g., Approved, Pending).

Step-by-Step Guide for Writing Queries

Step 1: Define the Task

Clearly specify the goal of your query. Examples include:

  • Extracting requirements based on specific criteria (e.g., type, priority, status).
  • Generating test cases for functional requirements.
  • Analyzing requirements for missing details or ambiguities.
  • Summarizing the document for a quick overview.

Step 2: Include the Relevant CSV Data

Provide a subset of the CSV data in your prompt to help the AI understand the structure and context. If the CSV is large, include only a few rows as examples.

Example CSV Excerpt for Prompt:

Here is a sample of the software requirements document in CSV format:

ID,Requirement,Type,Priority,Status
1,Users must be able to register and log in using their email and password,Functional,High,Approved
2,Search functionality must return relevant results within 2 seconds,Functional,Medium,Pending
3,The platform must handle 500 concurrent users,Non-Functional,High,Approved
4,Payment processing must support credit cards and PayPal securely,Functional,High,Approved
5,Daily backups of all data must be performed automatically,Non-Functional,Medium,Pending

Step 3: Write a Detailed Query or Instruction

1. Extracting Specific Requirements

If you want to filter the requirements based on certain conditions (e.g., "High Priority" functional requirements), write the query like this:

Prompt:

Here is a sample of the software requirements document in CSV format:

ID,Requirement,Type,Priority,Status
1,Users must be able to register and log in using their email and password,Functional,High,Approved
2,Search functionality must return relevant results within 2 seconds,Functional,Medium,Pending
3,The platform must handle 500 concurrent users,Non-Functional,High,Approved
4,Payment processing must support credit cards and PayPal securely,Functional,High,Approved
5,Daily backups of all data must be performed automatically,Non-Functional,Medium,Pending

Query: Extract all "High Priority" functional requirements. Return them as a numbered list.

Expected Output:

1. Users must be able to register and log in using their email and password.
2. Payment processing must support credit cards and PayPal securely.

2. Generating Test Scenarios

To generate test cases for functional requirements, include an instruction for the AI to create test scenarios:

Prompt:

Here is a sample of the software requirements document in CSV format:

ID,Requirement,Type,Priority,Status
1,Users must be able to register and log in using their email and password,Functional,High,Approved
2,Search functionality must return relevant results within 2 seconds,Functional,Medium,Pending
3,The platform must handle 500 concurrent users,Non-Functional,High,Approved
4,Payment processing must support credit cards and PayPal securely,Functional,High,Approved

Query: Generate 2 test scenarios for each functional requirement. Return them as a list.

Expected Output:

1. Test Case for Requirement 1:
   a. Verify that the user can register with a valid email and password.
   b. Verify that the user cannot register with an invalid email format.
2. Test Case for Requirement 2:
   a. Verify that searching for a keyword returns relevant results.
   b. Verify that searching by category displays all matching results.
3. Test Case for Requirement 4:
   a. Verify that payment can be processed securely via credit card.
   b. Verify that payment can be processed securely via PayPal.

3. Summarizing Requirements

Sometimes, you may need a quick summary of all requirements.

Prompt:

Here is a sample of the software requirements document in CSV format:

ID,Requirement,Type,Priority,Status
1,Users must be able to register and log in using their email and password,Functional,High,Approved
2,Search functionality must return relevant results within 2 seconds,Functional,Medium,Pending
3,The platform must handle 500 concurrent users,Non-Functional,High,Approved
4,Payment processing must support credit cards and PayPal securely,Functional,High,Approved
5,Daily backups of all data must be performed automatically,Non-Functional,Medium,Pending

Query: Summarize the requirements by their type (Functional vs Non-Functional).

Expected Output:

Functional Requirements:
1. Users must be able to register and log in using their email and password.
2. Search functionality must return relevant results within 2 seconds.
3. Payment processing must support credit cards and PayPal securely.

Non-Functional Requirements:
1. The platform must handle 500 concurrent users.
2. Daily backups of all data must be performed automatically.

4. Analyzing for Missing Details

To identify missing or incomplete requirements:

Prompt:

Here is a sample of the software requirements document in CSV format:

ID,Requirement,Type,Priority,Status
1,Users must be able to register and log in using their email and password,Functional,High,Approved
2,Search functionality must return relevant results within 2 seconds,Functional,Medium,Pending
3,The platform must handle 500 concurrent users,Non-Functional,High,Approved
4,Payment processing must support credit cards and PayPal securely,Functional,High,Approved

Query: Analyze the requirements and identify any missing or incomplete details. Suggest improvements to make the requirements more comprehensive.

Expected Output:

1. Requirement 1: Add details about password complexity requirements (e.g., minimum length, special characters).
2. Requirement 2: Specify how relevance will be determined for search results (e.g., ranking algorithm).
3. Requirement 3: Include performance benchmarks for the 500 concurrent users (e.g., response time under load).

Step 4: Define the Output Format

Clearly specify the format for the output, such as lists, JSON, or plain text.

Example: JSON Format

Query: Extract all "High Priority" requirements and return them in JSON format.

Expected Output:
{
  "High Priority Requirements": [
    {"ID": 1, "Requirement": "Users must be able to register and log in using their email and password", "Type": "Functional", "Status": "Approved"},
    {"ID": 3, "Requirement": "The platform must handle 500 concurrent users", "Type": "Non-Functional", "Status": "Approved"},
    {"ID": 4, "Requirement": "Payment processing must support credit cards and PayPal securely", "Type": "Functional", "Status": "Approved"}
  ]
}

Tips for Writing Effective Prompts

  1. Be Specific:
    • Use clear and concise instructions, such as "Extract all high-priority functional requirements."
  2. Provide Context:
    • Include a sample of the CSV data to help the AI understand the structure.
  3. Break Down Complex Tasks:
    • For tasks like generating test cases, split the query into multiple steps if needed.
  4. Define Output Format:
    • Specify whether you want the output as plain text, a list, or JSON.
  5. Iterate and Refine:
    • Test your prompts with different queries and refine them to improve accuracy.

Conclusion

Using prompts to process software requirements or testing documents in CSV format requires a clear understanding of the data structure and the task at hand. By following the steps outlined above, you can:

  • Extract relevant information.
  • Generate test cases.
  • Analyze requirements for gaps.
  • Summarize large documents.

With practice, you’ll be able to write detailed and effective queries that leverage the full power of Generative AI to streamline your software development and testing workflows. 🚀

Thursday, June 12, 2025

From CSV to JSON: Querying Data Using Prompt Engineering and Storing Results in a CSV


In this guide, we'll walk through the full process of reading data from a CSV file, converting it into JSON, using prompt engineering to query that JSON data, retrieving a single-row answer, and finally storing the result back into another CSV file. This workflow combines data processing and the power of Generative AI to interact with tabular data in a human-friendly way.

Overview of Steps

  1. Read the CSV file and convert it into JSON format.
  2. Use prompt engineering to query the JSON data.
  3. Retrieve a single-row answer based on the user’s query.
  4. Append the result as a new row to another CSV file.

Step 1: Read the CSV File and Convert It into JSON

Before querying the data, we’ll start by reading the data from the CSV file and converting it into a JSON format that the AI model can interpret.

Sample CSV File (data.csv)

Here’s an example of the data we’ll use:

Name,Age,Department,Salary
Alice,30,HR,50000
Bob,45,IT,70000
Charlie,28,Finance,60000
Diana,35,Marketing,65000

Python Code to Read and Convert CSV to JSON

We’ll use pandas to load the CSV file and convert it into JSON.

import pandas as pd

# Step 1: Read the CSV file
csv_file = 'data.csv'
df = pd.read_csv(csv_file)

# Convert the DataFrame to JSON format (records orientation)
json_data = df.to_json(orient='records')

# Display the JSON
print(json_data)

JSON Output:

json

[
  {"Name":"Alice","Age":30,"Department":"HR","Salary":50000},
  {"Name":"Bob","Age":45,"Department":"IT","Salary":70000},
  {"Name":"Charlie","Age":28,"Department":"Finance","Salary":60000},
  {"Name":"Diana","Age":35,"Department":"Marketing","Salary":65000}
]

Step 2: Craft the Prompt to Query JSON Data

The next step is to ask questions about the JSON data using prompt engineering. A well-crafted prompt ensures that the AI can interpret the data and return the desired row correctly.

Crafting the Prompt

We’ll include the JSON data in the prompt and ask a specific question to retrieve a single row.

Example Prompt

Here is the data in JSON format:

[
  {"Name":"Alice","Age":30,"Department":"HR","Salary":50000},
  {"Name":"Bob","Age":45,"Department":"IT","Salary":70000},
  {"Name":"Charlie","Age":28,"Department":"Finance","Salary":60000},
  {"Name":"Diana","Age":35,"Department":"Marketing","Salary":65000}
]

Query: Find the row where the 'Department' is 'IT'. Return the output as a single row in JSON format.

Expected AI Output

{"Name":"Bob","Age":45,"Department":"IT","Salary":70000}

Step 3: Use Generative AI to Query the Data

To execute the query, we’ll use an AI model via an API (e.g., OpenAI’s GPT API). The model will process the JSON data and return the requested row based on the query.

Python Code to Query the Data

Here’s how you can integrate a generative AI model to query the JSON data:

import openai
import json

# Step 1: Set up OpenAI API key
openai.api_key = 'your-openai-api-key'

# Step 2: Define the JSON data and the query
json_data = [
    {"Name": "Alice", "Age": 30, "Department": "HR", "Salary": 50000},
    {"Name": "Bob", "Age": 45, "Department": "IT", "Salary": 70000},
    {"Name": "Charlie", "Age": 28, "Department": "Finance", "Salary": 60000},
    {"Name": "Diana", "Age": 35, "Department": "Marketing", "Salary": 65000}
]
query = "Find the row where the 'Department' is 'IT'. Return the output as a single row in JSON format."

# Step 3: Craft the prompt
prompt = f"""
Here is the data in JSON format:

{json.dumps(json_data, indent=2)}

Query: {query}
"""

# Step 4: Use OpenAI's GPT API to process the prompt
response = openai.Completion.create(
    engine="text-davinci-003",
    prompt=prompt,
    max_tokens=100,
    temperature=0
)

# Step 5: Extract the AI's response
result = response.choices[0].text.strip()
print("AI Response (Row):", result)

# Convert the response back to a Python dictionary (JSON object)
row_data = json.loads(result)

Result:

{"Name": "Bob", "Age": 45, "Department": "IT", "Salary": 70000}

Step 4: Store the Result in a New CSV File

Finally, we’ll save the retrieved row into another CSV file (output.csv). If the file already exists, we’ll append the result as a new row.

Python Code to Save the Result

# Step 1: Convert the row data into a DataFrame
result_df = pd.DataFrame([row_data])

# Step 2: Append the row to 'output.csv'
output_file = 'output.csv'

# If the file exists, append without writing the header
try:
    result_df.to_csv(output_file, mode='a', index=False, header=False)
except FileNotFoundError:
    # If the file doesn't exist, write with the header
    result_df.to_csv(output_file, mode='w', index=False, header=True)

print(f"Row saved to {output_file}")

Output in output.csv

After running the script, the output.csv file will contain the following content:

Name,Age,Department,Salary
Bob,45,IT,70000

If you run additional queries, the new rows will be appended to the file.


Step 5: Automate the Workflow

To handle multiple queries or automate the workflow, you can wrap the steps into a function and call it with different queries.

Complete Workflow Script

def query_and_save_row(csv_file, query, output_file):
    # Read the CSV file
    df = pd.read_csv(csv_file)
    json_data = df.to_json(orient='records')

    # Craft the prompt
    prompt = f"""
    Here is the data in JSON format:

    {json_data}

    Query: {query}
    """
    
    # Query the AI model
    response = openai.Completion.create(
        engine="text-davinci-003",
        prompt=prompt,
        max_tokens=100,
        temperature=0
    )
    
    # Extract the AI's response
    row_data = json.loads(response.choices[0].text.strip())
    
    # Save the result to output CSV
    result_df = pd.DataFrame([row_data])
    try:
        result_df.to_csv(output_file, mode='a', index=False, header=not pd.io.common.file_exists(output_file))
    except FileNotFoundError:
        result_df.to_csv(output_file, mode='w', index=False, header=True)
    
    print(f"Row saved to {output_file}")


# Example usage
query_and_save_row('data.csv', "Find the row where the 'Department' is 'IT'. Return the output as a single row in JSON format.", 'output.csv')

Conclusion

This workflow demonstrates how to:

  1. Read data from a CSV file and convert it into JSON format.
  2. Ask questions using prompt engineering to query the JSON data.
  3. Retrieve a single-row answer from the AI model.
  4. Store the result back into another CSV file.

This method is versatile and can be adapted for various use cases, including data analysis, reporting, and automation. By combining the power of Generative AI and Python, you can create intelligent, human-like workflows for your data-driven tasks! 🚀

Guidelines for Prompt Engineering: Taking Your Skills to the Next Level

Prompt engineering is at the heart of leveraging Generative AI models effectively. Whether you're working with tools like GPT, Claude, or foundation models on AWS Bedrock, mastering prompt engineering can make the difference between mediocre and outstanding results. As an intermediate prompt engineer, you likely already know the basics—crafting clear instructions, testing outputs, and iterating on prompts. Now, it's time to level up.

This blog explores advanced guidelines for prompt engineering, offering strategies, techniques, and best practices to help you optimize your prompts and achieve more consistent, nuanced, and effective outputs.


1. Think Like the AI: Context is King

Why Context Matters

AI models rely heavily on the context you provide in your prompt. They don’t "think" like humans—they predict the next word or token based on the input. Including relevant information in your prompt can help the model generate more accurate and targeted responses.

Best Practices

  • Include Specific Details: If you're asking the model to draft an email, specify the recipient, tone, and purpose. For example:
    • Basic Prompt: "Write an email about a meeting."
    • Improved Prompt: "Write a professional email to Sarah confirming a project meeting on Friday at 3 PM, emphasizing the importance of her input on the budget."
  • Set the Scene: For storytelling or creative tasks, provide background information or establish a setting to guide the model.
  • Use Few-Shot Learning: Include examples in your prompt to show the model what kind of output you expect. For instance:
    Example 1: The cat sat on the mat.
    Example 2: The dog slept under the tree.
    Now complete this sentence: The bird...
    

2. Leverage the Structure of Prompts

Why Structure Helps

A well-structured prompt gives the model clear instructions and boundaries. It prevents confusion and improves the quality of the output.

Best Practices

  • Break Down Complex Tasks: Instead of asking for everything in one go, split the task into smaller parts. For example:
    • Basic Prompt: "Write a summary of this article and explain its implications."
    • Improved Prompt:
      1. "Summarize this article in 3-4 sentences."
      2. "Explain the implications of the article in 2-3 points."
  • Use Lists or Numbered Steps: When expecting structured outputs, indicate this in the prompt. Example:
    List three benefits of exercise:
    1.
    2.
    3.
    
  • Ask for Formats: Clearly specify the format you need, such as JSON, bullet points, or paragraphs. Example:
    Generate a response in JSON format:
    {
      "name": "",
      "age": "",
      "occupation": ""
    }
    

3. Control Output Length and Style

Why It’s Important

Without constraints, AI models may produce outputs that are too short, too verbose, or off-style. Controlling length and style ensures the response aligns with your needs.

Best Practices

  • Specify Word or Sentence Limits: Example: "Summarize this article in 50 words or less."
  • Define Tone and Style: Use clear instructions like "Write in a formal tone," "Be conversational," or "Use simple language for a 10-year-old."
  • Use Role-Playing: Ask the model to "act" like a specific persona to guide tone and expertise. Example:
    You are an experienced software engineer. Explain recursion to a beginner programmer in simple terms.
    

4. Use Constraints to Guide the Model

Why Constraints Work

AI models are probabilistic, meaning they explore a range of possible outputs. Adding constraints helps the model stay focused and avoid irrelevant or incorrect responses.

Best Practices

  • Define What NOT to Do: Explicitly state what the response should avoid. Example:
    • "Explain quantum computing in simple terms, but do not use math-heavy jargon."
  • Ask for Specific Perspectives: Example:
    • "Explain the benefits of recycling from an environmentalist's perspective."
  • Limit Creativity When Necessary: Set parameters like "Be factual and concise" for technical or research-based tasks.

5. Iterate and Refine Your Prompts

Why Iteration is Key

Even the most well-crafted prompts may not produce perfect results on the first attempt. Refining your prompts based on the output can significantly improve performance.

Best Practices

  • Analyze Outputs: Look for patterns in the model’s behavior. Does it misunderstand part of the task? Is it too verbose? Adjust accordingly.
  • Experiment with Variations: Test different phrasings of the same prompt. For example:
    • Version 1: "Write a summary of this document."
    • Version 2: "Summarize this document in plain language for a general audience."
  • Use Feedback Loops: Provide corrections or feedback in subsequent prompts to guide the model. Example:
    Your previous answer was too general. Focus on the economic impact of the topic.
    

6. Incorporate Inference Parameters

Why Parameters Matter

Inference parameters like temperature, top-p, and max tokens (discussed in our previous blog) can greatly influence the model’s behavior. Adjusting these allows you to control randomness, creativity, and response length.

Best Practices

  • For factual and precise outputs, set:
    • Temperature: 0.2
    • Top-p: 0.3
  • For creative outputs, set:
    • Temperature: 0.8–1.0
    • Top-p: 0.9
  • Use max tokens to limit response length and avoid overly long answers.

7. Use Chain-of-Thought (CoT) Prompts

What is CoT?

Chain-of-Thought (CoT) prompting involves asking the model to "think step by step" to improve reasoning and problem-solving.

Best Practices

  • Ask for Step-by-Step Reasoning: Example:
    Solve this math problem step by step: A train travels 60 miles in 1 hour. How far will it travel in 3 hours?
    
  • Break Down Logical Tasks: For decision-making or analysis, use CoT to guide the model. Example:
    Analyze the pros and cons of remote work. Start with the benefits, then discuss the drawbacks.
    

8. Combine Prompts for Advanced Use Cases

Why Combine Prompts?

Complex tasks often require multiple prompts or stages. Combining prompts allows you to break down workflows into manageable parts.

Best Practices

  • Multi-Step Prompts: Execute tasks in stages. Example:
    1. "Generate a list of 5 ideas for a blog post about AI ethics."
    2. "Expand on the second idea with an outline."
    3. "Write an introduction for the chosen topic based on the outline."
  • Iterative Refinement: Use the output of one prompt as the input for the next. Example:
    • Prompt 1: "Summarize this article."
    • Prompt 2: "Rewrite the summary to make it more engaging."

9. Balance Creativity and Consistency

Why It’s Important

AI models can produce wildly creative outputs or stick rigidly to safe, predictable answers. Balancing these traits ensures that the output meets your requirements.

Best Practices

  • Use temperature and top-p to balance creativity and focus.
  • Include examples to provide consistency in tone and style.
  • Use role-playing to establish consistency across multiple responses.

10. Test Across Different Scenarios

Why Testing Matters

AI models may behave differently depending on the task or domain. Testing your prompts across various scenarios ensures robustness.

Best Practices

  • Test prompts with different types of content (e.g., technical, creative, conversational).
  • Evaluate outputs for quality, relevance, and consistency.
  • Adjust prompts to generalize them for broader use cases.

Conclusion

Mastering prompt engineering is a journey, and as an intermediate practitioner, you’re now equipped to tackle more complex challenges. By focusing on context, structure, constraints, and inference parameters, you can craft prompts that consistently deliver high-quality results. Don’t forget to experiment, iterate, and refine—prompt engineering is as much an art as it is a science.

With these advanced strategies, you’re ready to unlock the full potential of AI and create outputs that are not just good but exceptional. Happy prompting! 😊

A Beginner’s Guide to Inference Parameters in Prompt Engineering

 Artificial Intelligence (AI), particularly Generative AI, has revolutionized the way we interact with technology. From chatbots and content generation to code assistance and creative outputs, models like OpenAI’s GPT, Google’s Bard, and Amazon’s Bedrock foundation models are capable of performing incredible tasks. A key part of using these models effectively is prompt engineering, which involves crafting prompts (or instructions) to generate the desired outputs.

However, what many beginners overlook is the role of inference parameters—special settings that can fine-tune how the AI responds. Understanding these parameters can take your results from "okay" to "amazing."

In this blog, we’ll break down inference parameters in prompt engineering and explain how to use them to improve AI-generated results.


What Are Inference Parameters?

Inference parameters are settings that control how an AI model generates outputs when given a prompt. These parameters influence the creativity, consistency, and quality of the responses.

Think of it like adjusting the dials on a radio. With the right settings, you can tune the AI model to produce exactly what you’re looking for—whether that's creative storytelling, concise answers, or highly factual content.


Key Inference Parameters and What They Do

Here are the most important inference parameters you’ll encounter while working with AI models:

1. Temperature

  • What it does: Controls the randomness of the output.
    • A low temperature (e.g., 0.1) makes the model more focused and deterministic. It will stick closely to the most probable output.
    • A high temperature (e.g., 1.0) makes the model more creative and diverse, introducing randomness into its responses.
  • Use cases:
    • Low temperature: Fact-based tasks like coding, summarization, or generating precise answers.
    • High temperature: Creative tasks like storytelling, poetry, or brainstorming ideas.

Example:

  • Prompt: "Write a description of the night sky."
    • Temperature = 0.2 → "The night sky is dark, with stars scattered across it like dots of light."
    • Temperature = 1.0 → "The night sky unfurls like a velvet canvas, adorned with shimmering jewels that dance and twinkle in the infinite expanse."

2. Top-p (Nucleus Sampling)

  • What it does: Controls how much of the probability distribution the model considers when generating a response. Instead of choosing from all possible words, it limits the choices to the most likely ones until their combined probability reaches a threshold.
    • Top-p = 0.1: The model considers only the top 10% of the most likely words.
    • Top-p = 1.0: The model considers all possible words (maximum randomness).
  • Use cases:
    • Low top-p: Ensures focused and highly relevant outputs.
    • High top-p: Encourages more diverse and creative responses.

Example:

  • Prompt: "Write a greeting for a birthday card."
    • Top-p = 0.2 → "Happy Birthday! Wishing you a wonderful year ahead."
    • Top-p = 0.9 → "Happy Birthday! May your day be filled with laughter, love, and all the cake you can eat!"

3. Max Tokens

  • What it does: Determines the maximum length of the output created by the AI. A token is typically a word or part of a word, and models have a limit on how many tokens they can process in total (input + output).
  • Use cases:
    • Short max tokens: For concise answers like tweets, summaries, or headlines.
    • Long max tokens: For detailed essays, stories, or explanations.

Tip: If your outputs are being cut off mid-sentence, increase the max tokens!


4. Frequency Penalty

  • What it does: Adjusts how much the model avoids repeating the same words or phrases within the response.
    • A higher frequency penalty discourages repetition.
    • A lower frequency penalty allows the model to repeat words when necessary.
  • Use cases:
    • High penalty: Creative writing or brainstorming to avoid repetitive outputs.
    • Low penalty: Technical writing or code generation where repetition might be necessary.

Example:

  • Prompt: "Describe a beautiful garden."
    • Low frequency penalty (0) → "The garden is full of flowers, flowers everywhere, with colorful flowers."
    • High frequency penalty (2.0) → "The garden is vibrant, filled with blossoms of every hue, each petal unique and radiant."

5. Presence Penalty

  • What it does: Encourages the model to introduce new topics or ideas that haven’t been mentioned before in the response.
    • A higher presence penalty pushes the model to explore diverse content.
    • A lower presence penalty keeps the response more focused on the initial topic.
  • Use cases:
    • High penalty: Brainstorming, idea generation, or creative writing.
    • Low penalty: Focused responses, such as answering a specific question.

6. Stop Sequences

  • What it does: Defines specific words or phrases that signal the AI to stop generating output. This is useful for controlling the structure of the response.
  • Use cases:
    • Structured outputs like Q&A pairs, JSON, or code snippets.
    • Ensuring the AI doesn’t continue beyond a desired point.

Example:

  • Prompt: "List three benefits of exercise:"
    • Stop sequence: "\n" → "1. Improves physical health.\n2. Boosts mental well-being.\n3. Enhances energy levels."

How These Parameters Work Together

While each parameter has a distinct role, they often work best when adjusted together. Here’s how they interact:

  • Temperature + Top-p: Combine these to balance randomness and relevance. For example, setting temperature = 0.7 and top-p = 0.8 can produce creative yet coherent outputs.
  • Frequency Penalty + Presence Penalty: Use these together to manage repetition and encourage new ideas. For brainstorming, you might set both penalties higher.
  • Max Tokens + Stop Sequences: Control the length and structure of your output by setting appropriate max tokens and defining clear stop points.

Practical Examples

Here are a few real-world examples of how inference parameters can be applied:

1. Writing a Product Description

Prompt: "Write a product description for a smartwatch."

  • Temperature = 0.8, Top-p = 0.9: Generates a creative and engaging description.
  • Temperature = 0.2, Top-p = 0.5: Produces a factual and straightforward description.

2. Creating a Chatbot Response

Prompt: "How can I reset my password?"

  • Temperature = 0.2, Top-p = 0.3: Ensures the response is accurate and to the point.
  • Frequency Penalty = 0.5, Presence Penalty = 0.5: Reduces repetitive phrasing while maintaining relevance.

3. Brainstorming Ideas

Prompt: "List unique ideas for a sci-fi novel."

  • Temperature = 1.0, Top-p = 0.9: Encourages highly creative responses.
  • Presence Penalty = 1.5: Ensures the ideas are diverse and non-redundant.

Tips for Beginners

  1. Experiment: Start with default values and tweak one parameter at a time to see how it affects the output.
  2. Balance Creativity and Accuracy: Use a moderate temperature (0.7) and top-p (0.8) for most tasks until you’re more comfortable fine-tuning.
  3. Test for Specific Use Cases: Adjust parameters based on the type of output you want—whether it’s creative, technical, or concise.
  4. Combine Parameters Thoughtfully: Think about how each parameter interacts with others to create the desired result.

Conclusion

Inference parameters are the secret sauce of prompt engineering, giving you control over how AI models generate responses. By understanding and adjusting parameters like temperature, top-p, max tokens, and penalties, you can tailor AI outputs to suit a wide range of use cases—from creative writing to highly technical tasks.

As a beginner, don’t be afraid to experiment! With practice, you’ll develop an intuition for fine-tuning inference parameters and unlocking the full potential of Generative AI. Happy prompt engineering! 😊

AI's Impact on the IT Industry 2026