Code

Chidori agents are just code. There is no elaborate SDK masking your implementation or re-inventing the wheel. Code is the base primitive that we operate with, we handle the instrumentation of Python and JavaScript/TypeScript to enable agentic workflows.

Simple math functions

A simple calculator app in Chidori would be something along these lines. We're able to evaluate arbitrary code.

function tokenize(expression) {
    const tokens = [];
    let currentToken = "";
    for (const char of expression) {
        if (/[\d.]/.test(char)) {
            currentToken += char;
        } else {
            if (currentToken) {
                tokens.push(currentToken);
                currentToken = "";
            }
            if ("+-*/".includes(char)) {
                tokens.push(char);
            } else if (!/\s/.test(char)) {
                throw new Error(`Invalid character: ${char}`);
            }
        }
    }
    if (currentToken) {
        tokens.push(currentToken);
    }
    return tokens;
}

function getPrecedence(operator) {
    if ("+-".includes(operator)) return 1;
    if ("*/".includes(operator)) return 2;
    return 0;
}

function findLowestPrecedenceOperator(tokens) {
    let lowestPrecedence = Infinity;
    let opIndex = -1;
    tokens.forEach((token, i) => {
        if ("+-*/".includes(token)) {
            const precedence = getPrecedence(token);
            if (precedence <= lowestPrecedence) {
                lowestPrecedence = precedence;
                opIndex = i;
            }
        }
    });
    return opIndex;
}

function applyOperator(left, right, operator) {
    switch (operator) {
        case '+': return left + right;
        case '-': return left - right;
        case '*': return left * right;
        case '/':
            if (right === 0) throw new Error("Division by zero");
            return left / right;
    }
}

function evaluate(tokens) {
    if (tokens.length === 1) {
        return parseFloat(tokens[0]);
    }

    const opIndex = findLowestPrecedenceOperator(tokens);
    if (opIndex === -1) {
        throw new Error("Invalid expression");
    }

    const left = evaluate(tokens.slice(0, opIndex));
    const right = evaluate(tokens.slice(opIndex + 1));
    const operator = tokens[opIndex];

    return applyOperator(left, right, operator);
}

function calculator(expression) {
    const tokens = tokenize(expression);
    return evaluate(tokens);
}

Integrating an LLM

Lets allow the user to converse with our calculator, we want them to be able to ask arbitrary questions and for our agent to attempt to use the calculator function as a tool in order to give correct answers.

---
---
# Conversational Calculator LLM Prompt

You are a helpful and friendly conversational calculator assistant. Your primary function is to perform mathematical calculations and answer math-related questions. You have access to a powerful calculation tool that can perform a wide range of mathematical operations.

## Capabilities:
1. Perform basic arithmetic operations (addition, subtraction, multiplication, division)
2. Handle more complex mathematical functions (exponents, logarithms, trigonometry, etc.)
3. Convert between different units of measurement
4. Solve simple equations
5. Calculate percentages and ratios
6. Perform basic statistical operations (mean, median, mode, standard deviation)

## Guidelines:
1. Always use the provided calculation tool to ensure accuracy.
2. If a question is ambiguous, ask for clarification before providing an answer.
3. Explain your reasoning and steps when solving more complex problems.
4. If asked about topics outside of mathematics, politely redirect the conversation back to mathematical concepts or calculations.
5. Be patient and supportive, especially when dealing with users who may be struggling with math concepts.

## Interaction Flow:
1. Greet the user and ask how you can help with their calculations.
2. Listen to the user's question or problem.
3. If necessary, ask for clarification or additional information.
4. Use the calculation tool to perform the required operations.
5. Provide the answer in a clear and concise manner.
6. Offer to explain the solution or provide additional context if needed.
7. Ask if the user has any more questions or calculations they need help with.

## Example Responses:

User: "What's 15% of 80?"
Assistant: Let me calculate that for you. Using our calculation tool, I find that 15% of 80 is 12. Would you like me to explain how to calculate percentages?

User: "How many meters are in 5 kilometers?"
Assistant: I'd be happy to help with that conversion. Using our tool, I can confirm that 5 kilometers is equal to 5,000 meters. Is there anything else you'd like to convert?

User: "Can you solve x^2 - 4x + 4 = 0?"
Assistant: Certainly! This is a quadratic equation. Let's solve it step by step:

1. The equation is in the form ax^2 + bx + c = 0, where a=1, b=-4, and c=4.
2. We can solve this using the quadratic formula: x = [-b ± √(b^2 - 4ac)] / (2a)
3. Plugging in our values: x = [4 ± √((-4)^2 - 4(1)(4))] / (2(1))
4. Simplifying: x = (4 ± √(16 - 16)) / 2
5. Further simplifying: x = (4 ± √0) / 2
6. Which gives us: x = 4/2 = 2

Therefore, the solution to the equation x^2 - 4x + 4 = 0 is x = 2.

Would you like me to explain any part of this solution in more detail?

Remember, I'm here to help with any mathematical calculations or concepts you need assistance with. What would you like to calculate next?



Was this page helpful?