Skip to content

Commit e862f4e

Browse files
authored
download llms.txt from docs when running cog init (#2504)
download llms.txt from docs when running cog init
1 parent 17968a8 commit e862f4e

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

pkg/cli/init-templates/pipeline/main.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
flux_schnell = replicate.use("black-forest-labs/flux-schnell")
88
claude = replicate.use("anthropic/claude-3.5-haiku")
99

10+
1011
def run(
1112
prompt: str = Input(description="Describe the image to generate"),
12-
seed: int = Input(description="A seed", default=0)
13+
seed: int = Input(description="A seed", default=0),
1314
) -> Path:
14-
detailed_prompt = claude(prompt=f"""
15+
detailed_prompt = claude(
16+
prompt=f"""
1517
Generate a detailed prompt for a generative image model that will
1618
generate a high quality dynamic image based on the following
1719
theme: {prompt}
18-
""")
20+
"""
21+
)
1922
output_paths = flux_schnell(prompt=detailed_prompt, seed=seed)
2023

2124
return Path(output_paths[0])

pkg/cli/init.go

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package cli
33
import (
44
"embed"
55
"fmt"
6+
"io"
7+
"net/http"
68
"os"
79
"path"
10+
"time"
811

912
"github.com/spf13/cobra"
1013

@@ -114,9 +117,28 @@ func processTemplateFile(fs embed.FS, templateDir, filename, cwd string) error {
114117
return fmt.Errorf("Error creating directory %s: %w", dirPath, err)
115118
}
116119

117-
content, err := fs.ReadFile(path.Join(templateDir, filename))
118-
if err != nil {
119-
return fmt.Errorf("Error reading %s: %w", filename, err)
120+
var content []byte
121+
122+
// Special handling for AGENTS.md - try to download from Replicate docs
123+
if filename == "AGENTS.md" {
124+
downloadedContent, err := downloadAgentsFile()
125+
if err != nil {
126+
console.Infof("Failed to download AGENTS.md: %v", err)
127+
console.Infof("Using template version instead...")
128+
// Fall back to template version
129+
content, err = fs.ReadFile(path.Join(templateDir, filename))
130+
if err != nil {
131+
return fmt.Errorf("Error reading template %s: %w", filename, err)
132+
}
133+
} else {
134+
content = downloadedContent
135+
}
136+
} else {
137+
// Regular template file processing
138+
content, err = fs.ReadFile(path.Join(templateDir, filename))
139+
if err != nil {
140+
return fmt.Errorf("Error reading %s: %w", filename, err)
141+
}
120142
}
121143

122144
if err := os.WriteFile(filePath, content, 0o644); err != nil {
@@ -127,6 +149,31 @@ func processTemplateFile(fs embed.FS, templateDir, filename, cwd string) error {
127149
return nil
128150
}
129151

152+
func downloadAgentsFile() ([]byte, error) {
153+
const agentsURL = "https://replicate.com/docs/reference/pipelines/llms.txt"
154+
155+
client := &http.Client{
156+
Timeout: 10 * time.Second,
157+
}
158+
159+
resp, err := client.Get(agentsURL)
160+
if err != nil {
161+
return nil, fmt.Errorf("%w", err)
162+
}
163+
defer resp.Body.Close()
164+
165+
if resp.StatusCode != http.StatusOK {
166+
return nil, fmt.Errorf("HTTP %d", resp.StatusCode)
167+
}
168+
169+
content, err := io.ReadAll(resp.Body)
170+
if err != nil {
171+
return nil, fmt.Errorf("failed to read response body: %w", err)
172+
}
173+
174+
return content, nil
175+
}
176+
130177
func addPipelineInit(cmd *cobra.Command) {
131178
const pipeline = "x-pipeline"
132179
cmd.Flags().BoolVar(&pipelineTemplate, pipeline, false, "Initialize a pipeline template")

0 commit comments

Comments
 (0)