Skip to content

Instantly share code, notes, and snippets.

@up1
Last active March 30, 2025 11:23
Show Gist options
  • Save up1/1688aebc24d3f43e342b41a40a11edcd to your computer and use it in GitHub Desktop.
Save up1/1688aebc24d3f43e342b41a40a11edcd to your computer and use it in GitHub Desktop.
Dagger LLM
// ขั้นตอนที่ 1
$export ANTHROPIC_API_KEY=<your key>
$dagger
─ ←↑↓→ move · home first · end last · +/- verbosity=1 · q quit ─────────────────────────────● connect 0.1s
● connect 0.2s
│ ● connecting to engine 0.0s │✔ connect 0.3s ✔ connect 0.3s
Dagger interactive shell. Type ".help" for more information. Press Ctrl+D to exit.\n
✔ connect 0.3s ⋈
─ esc nav mode · > run prompt ──────────────────────────────────────────────────────────────
// ขั้นตอนที่ 2
llm | with-container $(container | from alpine) | with-prompt "You have an alpine container. Install tools to develop with Python." | container | terminal
// ผลการทำงาน
● Attaching terminal:
container: Container!
.from(address: "docker.io/library/alpine:latest@sha256:a8560b36e8b8210634f77d9f7f9efd7ffa463e380b75e2e74aff4511df3ef88c"): Container!
.withExec(args: ["apk", "add", "python3", "py3-pip", "python3-dev"]): Container!
.withExec(args: ["apk", "add", "build-base", "gcc", "musl-dev"]): Container!
dagger / $
// 1. Create project
$dagger init --sdk go --name coding-agent
// 2. Install demo module
$dagger install github.com/shykes/toy-programmer/toy-workspace
// 3. Write Agent to generate Go code in file main.go
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 {
result := dag.LLM().
WithToyWorkspace(dag.ToyWorkspace()).
WithPromptVar("assignment", assignment).
WithPrompt(`
You are an expert go programmer. You have access to a workspace.
Use the default directory in the workspace.
Do not stop until the code builds.
Do not use the container.
Complete the assignment: $assignment
`).
ToyWorkspace().
Container()
return result
}
// 1. เปิด Dagger shell
$dagger
// 2. ทำการสั่งให้เขียน code เพื่อสร้าง program ทำงานเหมือนกับ curl
$go-program "write a curl clone" | terminal
// 3. ผลการทำงาน ได้ code ออกมา
root@1l8nccabffnim:/app# cat main.go
package main
import (
"flag"
"fmt"
"io"
"net/http"
"os"
)
func main() {
// Parse command line flags
flag.Parse()
// Get URL from command line arguments
args := flag.Args()
if len(args) < 1 {
fmt.Println("Usage: curl-clone URL")
os.Exit(1)
}
url := args[0]
// Create HTTP client
client := &http.Client{}
// Create request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating request: %v\n", err)
os.Exit(1)
}
// Set a default User-Agent
req.Header.Set("User-Agent", "curl-clone/1.0")
// Send request
resp, err := client.Do(req)
if err != nil {
fmt.Fprintf(os.Stderr, "Error making request: %v\n", err)
os.Exit(1)
}
defer resp.Body.Close()
// Copy response body to stdout
_, err = io.Copy(os.Stdout, resp.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading response: %v\n", err)
os.Exit(1)
}
}
// 4. ลอง run code ดู
root@1l8nccabffnim:/app# go run main.go https://www.somkiat.cc
package main
import (
"net/http"
"strconv"
"github.com/labstack/echo/v4"
)
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
}
// Mock database
var users = map[int]User{
1: {ID: 1, Name: "John Doe", Age: 30},
2: {ID: 2, Name: "Jane Smith", Age: 25},
}
func getUser(c echo.Context) error {
// Get id from URL parameter
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{
"error": "Invalid ID format",
})
}
// Find user
user, exists := users[id]
if !exists {
return c.JSON(http.StatusNotFound, map[string]string{
"error": "User not found",
})
}
return c.JSON(http.StatusOK, user)
}
func main() {
e := echo.New()
// Route for getting user by ID
e.GET("/users/:id", getUser)
// Start server
e.Start(":8080")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment