-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
42 lines (35 loc) · 1.06 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"fmt"
"io"
"os/exec"
"strconv"
"github.com/valyala/fasthttp"
)
func main() {
LLAMA_MAIN := "/path/to/llama.cpp/main"
LLAMA_MODEL_PATH := "/path/to/7B/ggml-model-f16.bin"
LLAMA_NUM_THREADS := "16"
MAX_OUTPUT_LENGTH := int(256)
handler := func(ctx *fasthttp.RequestCtx) {
modelInput := ctx.QueryArgs().Peek("text")
cmd := exec.Command(LLAMA_MAIN, "-m", LLAMA_MODEL_PATH, "-t", LLAMA_NUM_THREADS, "-n", strconv.Itoa(MAX_OUTPUT_LENGTH), "-p", fmt.Sprintf("%s", modelInput))
stdout, err := cmd.StdoutPipe()
if err != nil {
ctx.Error("Internal Server Error", fasthttp.StatusInternalServerError)
return
}
if err := cmd.Start(); err != nil {
ctx.Error("Internal Server Error", fasthttp.StatusInternalServerError)
return
}
ctx.Response.Header.Set("Content-Type", "text/plain; charset=utf-8")
ctx.Response.Header.Set("Cache-Control", "no-cache")
ctx.Response.SetBodyStream(io.Reader(stdout), -1)
}
port := 8080
addr := ":" + strconv.Itoa(port)
if err := fasthttp.ListenAndServe(addr, handler); err != nil {
panic(err)
}
}