Skip to main content

LLM

The LLM type initializes a Large Language Model (LLM).

Env Type

You use an LLM in conjunction with Env. The Env type is used to represent the environment in which an LLM operates. It allows the LLM to interact with inputs and outputs, such as directories, containers, and custom modules.

Common operations

Some of the common operations available on the LLM type include:

FieldDescription
modelReturns the model used by the LLM
lastReplyReturns the last reply from the LLM
historyReturns the LLM message history
withEnvAllows the LLM to interact with an environment
withPromptAppends a prompt to the LLM context
withPromptFileAppends a prompt file to the LLM context
withPromptVarAdds a string variable to the LLM context
withModelSets the model used by the LLM

Examples

Build a Golang coding agent

The following Dagger Function creates a Golang coding agent. The input is a programming assignment and the output is a Go executable satisfying the assignment. The environment consists of a base golang container, which is given to an LLM with a prompt describing how it should perform the work.

package main

import (
"dagger/coding-agent/internal/dagger"
)

type CodingAgent struct{}

// Write a Go program
func (m *CodingAgent) GoProgram(
// The programming assignment, e.g. "write me a curl clone"
assignment string,
) *dagger.Container {
environment := dag.Env().
WithStringInput("assignment", assignment, "the assignment to complete").
WithContainerInput("builder",
dag.Container().From("golang").WithWorkdir("/app"),
"a container to use for building Go code").
WithContainerOutput("completed", "the completed assignment in the Golang container")

work := dag.LLM().
WithEnv(environment).
WithPrompt(`
You are an expert Go programmer with an assignment to create a Go program
Create files in the default directory in $builder
Always build the code to make sure it is valid
Do not stop until your assignment is completed and the code builds
Your assignment is: $assignment
`)

return work.
Env().
Output("completed").
AsContainer()
}

Example

Have the agent update the container and open an interactive terminal to inspect the container filesystem:

dagger -c 'agent | terminal'