Skip to content

Commit 9a0d42a

Browse files
Merge pull request #384 from DefangLabs/jordan/refactor-managed-llm-samples
Refactor managed llm samples
2 parents e0049f3 + 56e90a1 commit 9a0d42a

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ The `x-defang-llm` property on the `llm` service must be set to `true` in order
3636
To run the application locally, you can use the following command:
3737

3838
```bash
39-
docker compose -f compose.dev.yaml up --build
39+
docker compose -f compose.local.yaml up --build
4040
```
4141

4242
## Deployment

app/app.py

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import os
44

55
import requests
6-
from fastapi import FastAPI, Form, Request
6+
from fastapi import FastAPI, Form
77
from fastapi.responses import HTMLResponse
88
from fastapi.staticfiles import StaticFiles
99
from fastapi.responses import JSONResponse
10+
from fastapi.responses import FileResponse
1011

1112
app = FastAPI()
1213
app.mount("/static", StaticFiles(directory="static"), name="static")
@@ -22,33 +23,14 @@
2223
MODEL_ID = os.getenv("LLM_MODEL", "gpt-4-turbo")
2324

2425
# Get the API key for the LLM
25-
# For development, you can use your local API key. In production, the LLM gateway service will override the need for it.
26+
# For development, you have the option to use your local API key. In production, the LLM gateway service will override the need for it.
2627
def get_api_key():
2728
return os.getenv("OPENAI_API_KEY", "")
2829

2930
# Home page form
3031
@app.get("/", response_class=HTMLResponse)
3132
async def home():
32-
return """
33-
<html>
34-
<head>
35-
<title>Ask the AI Model</title>
36-
<script type="text/javascript" src="./static/app.js"></script>
37-
</head>
38-
<body>
39-
<h1>Ask the AI Model</h1>
40-
<form method="post" id="askForm" onsubmit="event.preventDefault(); submitForm(event);">
41-
<textarea id="prompt" name="prompt" autofocus="autofocus" rows="5" cols="60" placeholder="Enter your question here..."
42-
onkeydown="if(event.key==='Enter'&&!event.shiftKey){event.preventDefault();this.form.dispatchEvent(new Event('submit', {cancelable:true}));}"></textarea>
43-
<br><br>
44-
<input type="submit" value="Ask">
45-
</form>
46-
<hr>
47-
<h2>Model's Reply:</h2>
48-
<p id="reply"></p>
49-
</body>
50-
</html>
51-
"""
33+
return FileResponse("static/index.html", media_type="text/html")
5234

5335
# Handle form submission
5436
@app.post("/ask", response_class=JSONResponse)

app/static/index.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<html>
2+
<head>
3+
<title>Ask the AI Model</title>
4+
<script type="text/javascript" src="./static/app.js"></script>
5+
</head>
6+
<body>
7+
<h1>Ask the AI Model</h1>
8+
<form method="post" id="askForm" onsubmit="event.preventDefault(); submitForm(event);">
9+
<textarea id="prompt" name="prompt" autofocus="autofocus" rows="5" cols="60" placeholder="Enter your question here..."
10+
onkeydown="if(event.key==='Enter'&&!event.shiftKey){event.preventDefault();this.form.dispatchEvent(new Event('submit', {cancelable:true}));}"></textarea>
11+
<br><br>
12+
<input type="submit" value="Ask">
13+
</form>
14+
<hr>
15+
<h2>Model's Reply:</h2>
16+
<p id="reply"></p>
17+
</body>
18+
</html>

compose.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ services:
77
- "8000:8000"
88
restart: always
99
environment:
10-
- LLM_MODEL # LLM model ID used
10+
- LLM_MODEL=default
1111
# For other models, see https://docs.defang.io/docs/concepts/managed-llms/openai-access-gateway#model-mapping
1212
healthcheck:
1313
test: ["CMD", "python3", "-c", "import sys, urllib.request; urllib.request.urlopen(sys.argv[1]).read()", "http://localhost:8000/"]

0 commit comments

Comments
 (0)