The method leverages a query string provided by the user to generate a filter condition based on the columns of the CSV file. This condition is used to extract relevant data from the CSV and return it in a structured format. The filtering logic is generated by an external LLM (Large Language Model) client, which receives a prompt based on the CSV’s structure.

Example

This method is part of the CsvHandler class, which handles CSV file operations, and is specifically designed to search through the data in the CSV file based on a user-defined query.

To use OpenAI models, you’ll need to create an OpenAI account and get an API key. For more details refer here.

Set up a OpenAI API key as an environmental variable and run the following code.

 export OPENAI_API_KEY = "**************************"
csv_handler.py
import asyncio

from superagentx_handlers.csv_cli import CsvHandler
from superagentx.llm import LLMClient


async def search(query):
    input_path = "<File_path>"
    llm_config = {'llm_type': 'openai'}
    llm_client = LLMClient(llm_config=llm_config)
    csv_handler = CsvHandler(
        file_path=input_path,
        llm_client=llm_client
    )
    return await csv_handler.search(query)


async def main(query):
    res = await search(query)
    print(res)

if __name__ == '__main__':
    query="who are all Petroleum engineer"
    asyncio.run(main(query))

Result

csv cli
{
    "Index": {
        "59": 60
    },
    "User Id": {
        "59": "0f8deedb629A5f6"
    },
    "First Name": {
        "59": "Grace"
    },
    "Last Name": {
        "59": "Phelps"
    },
    "Sex": {
        "59": "Male"
    },
    "Email": {
        "59": "clarkeangela@example.net"
    },
    "Phone": {
        "59": "(034)867-8827x6777"
    },
    "Date of birth": {
        "59": "1909-10-15"
    },
    "Job Title": {
        "59": "Petroleum engineer"
    }
}