Skip to content

Commit 1c927bf

Browse files
authored
LLM generated content updates (kargig#150)
Download LLM generated content from R2 on backend startup Increase cache time for LLM generated content on production
1 parent b9980dc commit 1c927bf

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

backend/generate_llm_content.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,26 @@ def should_generate_local():
8383
return False
8484
return True
8585

86+
def download_from_r2(client):
87+
"""Download all required files from R2 to local directory."""
88+
print("⬇️ Downloading LLM content from R2...")
89+
os.makedirs(OUTPUT_DIR, exist_ok=True)
90+
success = True
91+
for filename in REQUIRED_FILES:
92+
try:
93+
client.download_file(
94+
Bucket=R2_BUCKET_NAME,
95+
Key=f"llm_content/{filename}",
96+
Filename=os.path.join(OUTPUT_DIR, filename)
97+
)
98+
except Exception as e:
99+
print(f"⚠️ Failed to download {filename}: {e}")
100+
success = False
101+
102+
if success:
103+
print(f"✅ All content downloaded to {OUTPUT_DIR}.")
104+
return success
105+
86106
def generate_content(db: Session, r2_client=None):
87107
# Data gathering
88108
sites = db.query(DiveSite).all()
@@ -232,6 +252,7 @@ def generate_content(db: Session, r2_client=None):
232252
if not args.force:
233253
if r2:
234254
if check_r2_freshness(r2):
255+
download_from_r2(r2)
235256
sys.exit(0)
236257
else:
237258
# Local check

nginx/dev.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,35 @@ http {
5050
proxy_pass http://backend/llm_content/llms.txt;
5151
proxy_set_header Host $host;
5252
add_header Content-Type text/plain;
53+
add_header Cache-Control "public, max-age=300";
5354
}
5455

5556
location = /dive-sites.md {
5657
proxy_pass http://backend/llm_content/dive-sites.md;
5758
proxy_set_header Host $host;
5859
add_header Content-Type text/markdown;
60+
add_header Cache-Control "public, max-age=300";
5961
}
6062

6163
location = /dive-routes.md {
6264
proxy_pass http://backend/llm_content/dive-routes.md;
6365
proxy_set_header Host $host;
6466
add_header Content-Type text/markdown;
67+
add_header Cache-Control "public, max-age=300";
6568
}
6669

6770
location = /diving-centers.md {
6871
proxy_pass http://backend/llm_content/diving-centers.md;
6972
proxy_set_header Host $host;
7073
add_header Content-Type text/markdown;
74+
add_header Cache-Control "public, max-age=300";
7175
}
7276

7377
location = /dives.md {
7478
proxy_pass http://backend/llm_content/dives.md;
7579
proxy_set_header Host $host;
7680
add_header Content-Type text/markdown;
81+
add_header Cache-Control "public, max-age=300";
7782
}
7883

7984
# Frontend routes

nginx/prod.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,30 +106,35 @@ http {
106106
proxy_pass http://backend/llm_content/llms.txt;
107107
proxy_set_header Host $host;
108108
add_header Content-Type text/plain;
109+
add_header Cache-Control "public, max-age=86400";
109110
}
110111

111112
location = /dive-sites.md {
112113
proxy_pass http://backend/llm_content/dive-sites.md;
113114
proxy_set_header Host $host;
114115
add_header Content-Type text/markdown;
116+
add_header Cache-Control "public, max-age=86400";
115117
}
116118

117119
location = /dive-routes.md {
118120
proxy_pass http://backend/llm_content/dive-routes.md;
119121
proxy_set_header Host $host;
120122
add_header Content-Type text/markdown;
123+
add_header Cache-Control "public, max-age=86400";
121124
}
122125

123126
location = /diving-centers.md {
124127
proxy_pass http://backend/llm_content/diving-centers.md;
125128
proxy_set_header Host $host;
126129
add_header Content-Type text/markdown;
130+
add_header Cache-Control "public, max-age=86400";
127131
}
128132

129133
location = /dives.md {
130134
proxy_pass http://backend/llm_content/dives.md;
131135
proxy_set_header Host $host;
132136
add_header Content-Type text/markdown;
137+
add_header Cache-Control "public, max-age=86400";
133138
}
134139

135140
# SPA routing - serve index.html for all other routes

0 commit comments

Comments
 (0)