Enha: server update
This commit is contained in:
3
Makefile
3
Makefile
@@ -3,6 +3,9 @@
|
|||||||
run: setconfig
|
run: setconfig
|
||||||
go build -o elefant && ./elefant
|
go build -o elefant && ./elefant
|
||||||
|
|
||||||
|
server: setconfig
|
||||||
|
go build -o elefant && ./elefant -port 3333
|
||||||
|
|
||||||
setconfig:
|
setconfig:
|
||||||
find config.toml &>/dev/null || cp config.example.toml config.toml
|
find config.toml &>/dev/null || cp config.example.toml config.toml
|
||||||
|
|
||||||
|
|||||||
12
bot.go
12
bot.go
@@ -46,25 +46,25 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func fetchModelName() {
|
func fetchModelName() *models.LLMModels {
|
||||||
api := "http://localhost:8080/v1/models"
|
api := "http://localhost:8080/v1/models"
|
||||||
resp, err := httpClient.Get(api)
|
resp, err := httpClient.Get(api)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warn("failed to get model", "link", api, "error", err)
|
logger.Warn("failed to get model", "link", api, "error", err)
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
llmModel := models.LLMModels{}
|
llmModel := models.LLMModels{}
|
||||||
if err := json.NewDecoder(resp.Body).Decode(&llmModel); err != nil {
|
if err := json.NewDecoder(resp.Body).Decode(&llmModel); err != nil {
|
||||||
logger.Warn("failed to decode resp", "link", api, "error", err)
|
logger.Warn("failed to decode resp", "link", api, "error", err)
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
if resp.StatusCode != 200 {
|
if resp.StatusCode != 200 {
|
||||||
currentModel = "none"
|
currentModel = "none"
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
currentModel = path.Base(llmModel.Data[0].ID)
|
currentModel = path.Base(llmModel.Data[0].ID)
|
||||||
updateStatusLine()
|
return &llmModel
|
||||||
}
|
}
|
||||||
|
|
||||||
// func fetchProps() {
|
// func fetchProps() {
|
||||||
@@ -88,7 +88,7 @@ func fetchModelName() {
|
|||||||
// updateStatusLine()
|
// updateStatusLine()
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// func sendMsgToLLM(body io.Reader) (*models.LLMRespChunk, error) {
|
// TODO: should be a part of server?
|
||||||
func sendMsgToLLM(body io.Reader) {
|
func sendMsgToLLM(body io.Reader) {
|
||||||
// nolint
|
// nolint
|
||||||
resp, err := httpClient.Post(cfg.CurrentAPI, "application/json", body)
|
resp, err := httpClient.Post(cfg.CurrentAPI, "application/json", body)
|
||||||
|
|||||||
7
main.go
7
main.go
@@ -3,7 +3,6 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
@@ -30,10 +29,8 @@ func main() {
|
|||||||
apiPort := flag.Int("port", 0, "port to host api")
|
apiPort := flag.Int("port", 0, "port to host api")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if apiPort != nil && *apiPort > 3000 {
|
if apiPort != nil && *apiPort > 3000 {
|
||||||
// start api server
|
srv := Server{}
|
||||||
http.HandleFunc("POST /completion", completion)
|
srv.ListenToRequests(fmt.Sprintf("%d", *apiPort))
|
||||||
http.ListenAndServe(fmt.Sprintf(":%d", *apiPort), nil)
|
|
||||||
// no tui
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
pages.AddPage("main", flex, true, true)
|
pages.AddPage("main", flex, true, true)
|
||||||
|
|||||||
38
server.go
38
server.go
@@ -1,14 +1,40 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"elefant/config"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Server struct {
|
||||||
|
config config.Config
|
||||||
|
}
|
||||||
|
|
||||||
|
func (srv *Server) ListenToRequests(port string) {
|
||||||
|
// h := srv.actions
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
server := &http.Server{
|
||||||
|
Addr: "localhost:" + port,
|
||||||
|
Handler: mux,
|
||||||
|
ReadTimeout: time.Second * 5,
|
||||||
|
WriteTimeout: time.Second * 5,
|
||||||
|
}
|
||||||
|
mux.HandleFunc("GET /ping", pingHandler)
|
||||||
|
mux.HandleFunc("GET /model", modelHandler)
|
||||||
|
mux.HandleFunc("POST /completion", completionHandler)
|
||||||
|
fmt.Println("Listening", "addr", server.Addr)
|
||||||
|
server.ListenAndServe()
|
||||||
|
}
|
||||||
|
|
||||||
// create server
|
// create server
|
||||||
// listen to the completion endpoint handler
|
// listen to the completion endpoint handler
|
||||||
|
func pingHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
|
w.Write([]byte("pong"))
|
||||||
|
}
|
||||||
|
|
||||||
func completion(w http.ResponseWriter, req *http.Request) {
|
func completionHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
// post request
|
// post request
|
||||||
body := req.Body
|
body := req.Body
|
||||||
// get body as io.reader
|
// get body as io.reader
|
||||||
@@ -19,9 +45,19 @@ out:
|
|||||||
select {
|
select {
|
||||||
case chunk := <-chunkChan:
|
case chunk := <-chunkChan:
|
||||||
fmt.Println(chunk)
|
fmt.Println(chunk)
|
||||||
|
w.Write([]byte(chunk))
|
||||||
case <-streamDone:
|
case <-streamDone:
|
||||||
break out
|
break out
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func modelHandler(w http.ResponseWriter, req *http.Request) {
|
||||||
|
llmModel := fetchModelName()
|
||||||
|
payload, err := json.Marshal(llmModel)
|
||||||
|
if err != nil {
|
||||||
|
// return err
|
||||||
|
}
|
||||||
|
w.Write(payload)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user