[GenAI]

import { ollama } from "ollama-ai-provider";
import { generateObject, generateText } from "ai";
import { z } from "zod";
import { globby } from "globby";
import { parseArgs } from "node:util";
import { readFile } from "node:fs/promises";

const files = await globby("**/*.json", { gitignore: true });

const isNonEmpty = (files: string[]): files is [string, ...string[]] => {
  return files.length > 0;
};

if (!isNonEmpty(files)) {
  console.error("No files found");
  process.exit(1);
}

const { positionals } = parseArgs({
  allowPositionals: true,
});

const model = ollama("gemma3:latest");

const objectPrompt = positionals[0];

const { object } = await generateObject({
  model,
  schema: z.object({
    command: z
      .string()
      .describe("The command to execute, e.g. 'summarize' or 'translate'"),
    filePath: z.enum(files).describe("The path to the files to prcess"),
  }),
  prompt: objectPrompt,
});

const content = await readFile(object.filePath, "utf-8");

const textPrompt = `
<command>
${object.command}
</command>

<content>
${content}
</content>
`;

const { text } = await generateText({
  model,
  prompt: textPrompt,
});
console.log(text);

 

Example

bun index.ts "Summarize package.json file, what packages has been installed"

posted @ 2025-07-05 19:06  Zhentiw  阅读(8)  评论(0)    收藏  举报