diff --git a/api.py b/api.py index 006dadd..3b4221c 100644 --- a/api.py +++ b/api.py @@ -23,8 +23,8 @@ MODEL_CACHE = {} # --- Pydantic Models --- class PredictRequest(BaseModel): prompt: str - temperature: float = 0.7 - n: int = 3 + temperature: float = 1.6 + n: int = 4 length: int = 5 @@ -84,6 +84,14 @@ async def predict(request: PredictRequest): return PredictResponse(prediction=prediction) +@app.get("/api") +async def api_docs(): + """ + API documentation page. + """ + return FileResponse(os.path.join(UI_DIR, "api.html")) + + # --- Static Files and Root --- app.mount("/ui", StaticFiles(directory=UI_DIR), name="ui") diff --git a/ui/api.html b/ui/api.html new file mode 100644 index 0000000..42d9fa8 --- /dev/null +++ b/ui/api.html @@ -0,0 +1,395 @@ + + +
+ + +Text generation API powered by N-gram language models
++ The Kreatyw API provides a simple REST endpoint for + generating text continuations using N-gram language models. + The API uses Markov chains trained on source texts to + predict and generate coherent text sequences. +
+http://localhost:8000+
Generate text continuation based on a given prompt.
+ +| Parameter | +Type | +Required | +Description | +
|---|---|---|---|
| prompt | +string | ++ Required + | +The starting text to continue from | +
| n | +integer | ++ Optional + | ++ N-gram size (2-5). Default: 4. Higher values + produce more coherent but less creative text. + | +
| + temperature + | +float | ++ Optional + | ++ Sampling temperature (0.1-2.0). Default: 1.6. + Higher values increase randomness. + | +
| length | +integer | ++ Optional + | ++ Number of words to generate (1-500). Default: 5. + | +
| Field | +Type | +Description | +
|---|---|---|
| prediction | +string | +The generated text continuation | +
+curl -X POST http://localhost:8000/api/predict \
+ -H "Content-Type: application/json" \
+ -d '{
+ "prompt": "Once upon a time",
+ "n": 4,
+ "temperature": 1.2,
+ "length": 20
+ }'
+
+{
+ "prediction": "in a kingdom far away there lived a brave knight who sought adventure..."
+}
+
+const response = await fetch('/api/predict', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json'
+ },
+ body: JSON.stringify({
+ prompt: 'The old wizard',
+ n: 3,
+ temperature: 0.8,
+ length: 15
+ })
+});
+
+const data = await response.json();
+console.log(data.prediction);
+
+import requests
+
+response = requests.post('http://localhost:8000/api/predict',
+ json={
+ 'prompt': 'In the beginning',
+ 'n': 4,
+ 'temperature': 1.0,
+ 'length': 25
+ }
+)
+
+result = response.json()
+print(result['prediction'])
+ + Controls the context window size. Higher values use more + context words to predict the next word: +
+Controls the randomness of predictions:
+The API returns standard HTTP status codes:
++ Currently, there are no rate limits imposed on the API. For + production use, consider implementing appropriate rate + limiting. +
+