Skip to content

Give an LLM tools to call: full text ​

This page has every step and detail. The overview is the short version.

Build an API where the LLM can call your own resources during a response. When the model needs a calculation or a database lookup, it calls a tool. kdeps runs the target resource and gives the result back. Then the model continues.

A tool is a function that the LLM can call. You declare tools in tools: on a chat: resource. Each tool has these parts:

  • A name.
  • A description. The model uses it to decide when to call the tool.
  • A script: with the name of the resource that runs it.
  • A parameter schema.

The tool resource is not in the dependency graph. It runs only when the model calls it.

Step 1: create the project ​

bash
mkdir tool-chat
cd tool-chat
mkdir resources

Step 2: define the route ​

Create workflow.yaml:

yaml
# workflow.yaml
apiVersion: kdeps.io/v1
kind: Workflow

metadata:
  name: tool-chat
  version: "1.0.0"
  targetActionId: toolResponse

deploy:
  apiServer:
    portNum: 16395
    routes:
      - path: /api/v1/tools
        methods: [POST]
  runtime:
    pythonVersion: "3.12"

Step 3: write the calculator tool ​

Create resources/calc-tool.yaml:

yaml
# resources/calc-tool.yaml
actionId: calcTool
name: Calculator tool
component:
  name: python
  with:
    script: |
      import json, math
      expression = "{{ get('expression', 'memory') }}"   # the tool argument
      allowed = {
          "__builtins__": {},
          "sqrt": math.sqrt, "sin": math.sin, "cos": math.cos, "log": math.log,
          "pi": math.pi, "e": math.e, "abs": abs, "round": round, "pow": pow,
      }
      try:
          print(json.dumps({"result": eval(expression, allowed, {})}))
      except Exception as exc:
          print(json.dumps({"error": str(exc)}))

The LLM's tool arguments land in memory scope, so get('expression', 'memory') reads the expression argument the model supplied.

Step 4: write a mock database tool ​

Create resources/db-tool.yaml:

yaml
# resources/db-tool.yaml
actionId: dbTool
name: Database search tool
apiResponse:
  success: true
  response:
    results:
      - id: 1
        name: "Widget"
        category: "{{ get('category', 'memory') }}"
        query: "{{ get('query', 'memory') }}"

Step 5: give the tools to the LLM ​

Create resources/chat.yaml:

yaml
# resources/chat.yaml
actionId: llmWithTools
name: LLM with tools
chat:
  model:
    name: llama3.2:1b
  context:
    role: user
    prompt: "{{ get('q') }}"
  harness:
    output:
      json: true
  tools:
    list:
      - name: calculate
        description: "Evaluate a math expression. Supports +, -, *, /, **, sqrt, sin, cos, log, pi, e."
        script: calcTool                 # the resource that runs this tool
        parameters:
          expression:
            type: string
            description: "e.g. '2 + 2', 'sqrt(16)', 'sin(pi/2)'"
            required: true
      - name: search_database
        description: "Search the product database."
        script: dbTool
        parameters:
          query:
            type: string
            description: "Search query"
            required: true
          category:
            type: string
            description: "Optional category filter"
            required: false

Step 6: return the answer ​

Create resources/response.yaml:

yaml
# resources/response.yaml
actionId: toolResponse
name: Tool response
flow:
  requires: [llmWithTools]
validations:
  methods: [POST]
  routes: [/api/v1/tools]
apiResponse:
  success: true
  response:
    query: "{{ get('q') }}"
    answer: "{{ get('llmWithTools').message.content }}"

Step 7: validate and run ​

bash
kdeps validate .

Validation prints a warning: calcTool: resource is unreachable from targetActionId. This is correct. Tool resources run only when the LLM calls them, not through requires:.

bash
export KDEPS_API_AUTH_TOKEN=dev-token
kdeps run .
bash
curl -X POST http://localhost:16395/api/v1/tools \
  -H "Authorization: Bearer $KDEPS_API_AUTH_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"q": "What is the square root of 144, and search for blue widgets?"}'

The model calls calculate with expression: "sqrt(144)", calls search_database with query: "widgets", category: "blue", then answers using both results.

Next steps ​

kdeps has the Apache 2.0 License.