Lesson

Dynamically Inferring File Types from User Prompts in Local AI Scripts

Access
Included
Transcript
Needs source

Hardcoding your script to search for only one type of file (like .md) is a major limitation. This lesson shows you how to make your script smarter by teaching it to infer the desired file type directly from the user's natural language prompt.

You'll implement a two-stage AI process. The first stage analyzes the user's request to determine the file extension they're interested in (e.g., md, txt). The second stage then uses that file type to dynamically search the file system, find the relevant files, and execute the command.

Workflow demonstrated in this lesson:

  • Add a preliminary generateObject call to extract a fileType from the user's initial prompt.
  • Define a list of supported fileTypes (e.g., ['md', 'txt']) and use a detailed prompt to guide the AI, instructing it to choose from the list or use a default.
  • Dynamically construct the globby pattern using the inferred fileType (e.g., *.$[fileType]).
  • Implement error handling to inform the user if they request an unsupported file type or if no files of the specified type are found.
  • Demonstrate how the script defaults to searching for Markdown files but can correctly search for other types when specified (e.g., "summarize my profile from my pdf").

This technique adds a significant layer of intelligence to your script, allowing it to adapt its behavior based on the user's intent and making it a far more flexible and intuitive command-line tool.