Journalism AI - v3.0.7
    Preparing search index...

    Function askOllama

    • Interacts with a local Ollama model to perform a wide range of tasks.

      Ollama must be running on the machine. Set the AI_MODEL environment variable or pass model directly.

      Pass a custom Ollama instance via the ollama option to target a non-default host.

      File handling: pass local files via files: [{ path, type }]. Only "image" and "text" types are supported for now.

      Caching: model calls can be slow and costly, so original responses are cached in .journalism-cache by default. processResponse reruns on every call, including cache hits, and its output is never cached. Keep it lightweight and optimize heavier processing, especially for large workloads. Set cache: false to disable caching.

      Temperature defaults to 0 for deterministic responses.

      Type Parameters

      • TResponse = unknown

      Parameters

      • prompt: string

        The primary text prompt.

      • Optionaloptions: {
            cache?: boolean;
            contextWindow?: number;
            files?: { path: string; type: "image" | "text" }[];
            model?: string;
            ollama?: unknown;
            ollamaParameters?: Partial<ChatRequest>;
            processResponse?: (response: unknown) => TResponse | Promise<TResponse>;
            schemaJson?: unknown;
            systemPrompt?: string;
            temperature?: number;
            thinkingLevel?: boolean | "low" | "medium" | "high";
        }
        • Optionalcache?: boolean

          Cache the model response before user processing, not the callback output. Defaults to true.

        • OptionalcontextWindow?: number

          Override the model's context window size.

        • Optionalfiles?: { path: string; type: "image" | "text" }[]

          Files to send alongside the prompt. Only "image" and "text" types are supported (local paths only; no GCS, audio, video, or PDF). Text files are sent as separate user messages after the prompt; images are sent as attachments to the prompt message.

        • Optionalmodel?: string

          Model name; defaults to AI_MODEL env var.

        • Optionalollama?: unknown

          Custom Ollama instance targeting a specific host.

        • OptionalollamaParameters?: Partial<ChatRequest>

          Extra params merged into client.chat.

        • OptionalprocessResponse?: (response: unknown) => TResponse | Promise<TResponse>

          Transform or validate the model response on every call, including cache hits. The callback output is never cached; rejected cached responses are removed.

        • OptionalschemaJson?: unknown

          JSON schema for structured output.

        • OptionalsystemPrompt?: string

          Optional system prompt.

        • Optionaltemperature?: number

          Sampling temperature (default 0).

        • OptionalthinkingLevel?: boolean | "low" | "medium" | "high"

          Enables reasoning. Pass true for models that only support on/off, or "low", "medium", or "high" for granular control.

      Returns Promise<
          {
              contextWindow: number
              | null;
              durationMs: number;
              files: { path: string; type: "image" | "text" }[];
              fromCache: boolean;
              model: string;
              outputTokenCount: number;
              prompt: string;
              promptTokenCount: number;
              response: TResponse;
              systemPrompt: string | null;
              temperature: number;
              thinkingLevel: boolean | "low" | "medium" | "high" | null;
              thoughts: string | null;
              tokensPerSecond: number | null;
              totalTokens: number;
          },
      >

      // Assumes AI_MODEL is set in environment variables.
      const result = await askOllama("What is the capital of France?");
      console.log(result.response); // "Paris"
      // Use a custom Ollama instance.
      import { Ollama } from "ollama";
      const client = new Ollama({ host: "http://127.0.0.1:11434" });
      const result = await askOllama("What is the capital of France?", { ollama: client });
      // Analyse a local image.
      const result = await askOllama("Describe this image.", {
      files: [{ path: "./photo.jpg", type: "image" }],
      });
      // Structured JSON output.
      import * as z from "zod";
      const schema = z.toJSONSchema(z.object({ country: z.string(), capital: z.string() }));
      const result = await askOllama(
      "What is the capital of France?",
      { schemaJson: schema },
      );
      // Enable thinking / reasoning.
      const result = await askOllama(
      "What is 17 * 23?",
      { thinkingLevel: "low" },
      );
      // Access token usage directly from the response.
      const result = await askOllama("What is the capital of France?");
      console.log(result.response); // "Paris"
      console.log(`${result.totalTokens} tokens in ${result.durationMs}ms`);