Skip to content

The bare essense of Agentic AI in 75 lines of code #31

Description

@niloy

Below is the code snippet which implements the minimum code required to get an agentic AI with tool call usage that runs locally.

import ollama from "ollama";

type Message = {
    role: "system" | "user" | "assistant" | "tool";
    content: string;
};

const system = `
You are a genius problem solver. Provide clear and concise answers to the user's questions.
`;
const user = `GOAL: How many r in the word strawberry?`;
const tools = [
    {
        type: "function",
        function: {
            name: "run_javascript",
            description:
                "Run a javascript snippet. Wrap code in an IIFE and return the result. Don't use console.log, just return the value.",
            parameters: {
                type: "object",
                properties: {
                    str: { type: "string", description: "the code snippet" },
                },
                required: ["str"],
            },
        },
    },
];

function run_javascript({ str }: { str: string }): string {
    try {
        return eval(str);
    } catch (error) {
        return `Error executing JavaScript: ${error}`;
    }
}

async function solve(depth: number, messages: Message[]): Promise<string> {
    console.log(`Fetching response from model, depth: ${depth}`);
    const response = await ollama.chat({
        model: "qwen3:4b",
        messages,
        tools: tools,
        stream: false,
    });
    console.log(response.done_reason, response.message, response.message.tool_calls);

    if (response.message.tool_calls?.length) {
        console.log("Processing tool calls...");
        const toolResponse: Array<[string, string]> = response.message.tool_calls.map((tool) => {
            if (tool.function.name === "run_javascript") {
                const result = run_javascript(tool.function.arguments as { str: string });
                return [tool.function.name, result] as const;
            } else {
                console.log(`Unknown tool: ${tool.function.name}`);
                return ["", ""];
            }
        });

        const msg = toolResponse.map(([name, result]) => `TOOL RESPONSE (${name}): ${result}`).join("\n");
        const newMessages: Message[] = [...messages, { role: "tool", content: msg }];
        console.log(`Response from tools:\n${msg}`);

        return solve(depth + 1, newMessages);
    } else {
        return response.message.content;
    }
}

const response = await solve(1, [
    { role: "system", content: system },
    { role: "user", content: user },
]);

console.log(response);

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions