Dynamically Inferring File Types from User Prompts in Local AI Scripts

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.

Scripting Local Language Models with Ollama and the Vercel AI SDK
Lesson 6 of 7

Scripting Local Language Models with Ollama and the Vercel AI SDK

Build a local AI-powered command-line assistant from scratch. Learn to use Ollama, Vercel AI SDK & Zod to parse input, automate tasks, and run scripts offline.

Share with a coworker

Transcript

[00:00] So let's refactor the file type out of here. We're going to move all of this beneath the object prompt so that we can pull the file type out of there. Meaning we're going to use generate object to pull out the file type. And this will look something just like this using the same model. And this already added in a default.

[00:21] We're going to do just MD and actually make a list of file types. This looks fine. Let's just do md and text so that in our enums we can have file types here and that our default can be file types, the first one in the list. And I'm actually going to cast this as const so that it matches the type here. Otherwise this expects that the first value in this array is always available so saying as const will force that to be true.

[00:53] And then I'm going to rename this to file type object just so that once we get the file type as suggested here I'll hit tab and now we'll have asterisk and then our file type. And this is still working in the current directory. And kind of while we're here, speaking of enums, so this is restricted to markdown and txt. I'm going to come down to our commands and use an enum here as well so that we are forced to only use the available commands. And we can remove this and use string literals to get rid of the red squigglies.

[01:25] And I'm going to pull this out and give it a name of const valid commands and then just drop that in there. We can't use as const this time because it's possible that our command map doesn't have a key. So now our keys of summarize and translate are in there. And because we're using the default up here, and the default is MD, If I invoke this, we'll clear out the previous run, say bun index, and then just run a please summarize my profile. Once I hit enter this will error out because I totally forgot to pass in my object prompt.

[02:02] And then just run this again. And now this error is out. We are back into prompt engineering land to tell this prompt to extract a file type. So I'm going to wrap this prompt in something like this, where it's essentially saying determine the most relevant file type in the description. I'm going to also add in that only these file types are allowed.

[02:24] So now when you run this it's going to default to Markdown and use Markdown as the file type. Let's actually add to our logs the file type object just so we can see this again. And you'll see it found a file type of MD based on the default and based on the fact that we used enum. Now the enum is definitely forcing this to find a file type. So if we change this back to just a string and then run this, this will default to markdown.

[02:56] But this allows us to pass in like please summarize my profile from my PDF, hit enter, and the file type it found was PDF and no files are found. Again this enum, if we use it here, is going to force, even if we say PDF in here, it's going to force it into either Markdown or TXT. So you have to determine the behavior you want. Whether you want it to always give you one of these, whether you want it to fall back if this is just a string and the file type isn't found, and it even looks like an optional snuck in through autocomplete where I can attempt to say if this is a string. So we remove the file types, remove this language to try not to use the default, then run it again without a file type.

[03:40] In this scenario it still fell back to markdown, but we can remove default here, run it again, and now it's finding profile. And this is where I would say my available file types are our current file types in the description itself, and this time it shouldn't find profile. And it still found markdown. I'm going to say please return an empty string. And hopefully this combination of string, not enums, passing the file types into the description with optional on asking it to please return an empty string, hopefully this time we'll finally get an empty object with no files found.

[04:18] So as you can see it is quite the juggling act based on the specific behavior that you want. For this tool I'm just going to land on this for now so that if a user requests in here a PDF file we can tell them if there's not a file type I can say no file type found, only one of the following file types is allowed. So we give the user good messaging with MD and text. So now I can say profile markdown, hit enter, it finds the profile markdown file and summarizes it.