Skip to content

Commit

Permalink
fix: a bunch of misc issues
Browse files Browse the repository at this point in the history
  • Loading branch information
GentikSolm committed Nov 27, 2024
1 parent c6adf01 commit 5603291
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
22 changes: 10 additions & 12 deletions api/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func preprocessOpenaiRequest(c *Context, db *sql.DB) (*RequestInfo, error) {
c.Response().Header().Set("X-Accel-Buffering", "no")

var (
credits int
credits int64
userid int
)
err := db.QueryRow("SELECT user.credits, user.id FROM user INNER JOIN api_key ON user.id = api_key.user_id WHERE api_key.id = ?", strings.Split(bearer, " ")[1]).Scan(&credits, &userid)
Expand All @@ -46,13 +46,10 @@ func preprocessOpenaiRequest(c *Context, db *sql.DB) (*RequestInfo, error) {
return nil, &RequestError{401, errors.New("Unauthorized")}
}
if err != nil {
c.Err.Println(err)
c.Err.Println("Error fetching user data from api key: %v", err)
sendErrorToEndon(err, "/v1/chat/completions")
return nil, &RequestError{500, errors.New("Interal Server Error")}
}
if credits < 0 {
return nil, &RequestError{403, errors.New("Out of credits")}
}
var payload map[string]interface{}
body, err := io.ReadAll(c.Request().Body)
err = json.Unmarshal(body, &payload)
Expand All @@ -65,6 +62,7 @@ func preprocessOpenaiRequest(c *Context, db *sql.DB) (*RequestInfo, error) {
if stream, ok := payload["stream"]; !ok || !stream.(bool) {
return nil, &RequestError{400, errors.New("Targon currently only supports streaming requests")}
}

// Defaults
if _, ok := payload["seed"]; !ok {
payload["seed"] = rand.Intn(100000)
Expand All @@ -79,6 +77,10 @@ func preprocessOpenaiRequest(c *Context, db *sql.DB) (*RequestInfo, error) {
payload["logprobs"] = true
}

if credits < int64(payload["max_tokens"].(float64)) {
return nil, &RequestError{403, errors.New("Out of credits")}
}

body, err = json.Marshal(payload)
if err != nil {
c.Err.Println(err)
Expand Down Expand Up @@ -241,10 +243,6 @@ func queryMiners(c *Context, req []byte, method string, miner_uid *int) (Respons
cancel()
})

c.Info.Printf("%+v\n", headers)
c.Info.Printf("%+v\n", string(req))
c.Info.Printf("%+v\n", message)

r, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(req))
if err != nil {
c.Warn.Printf("Failed miner request: %s\n", err.Error())
Expand Down Expand Up @@ -332,12 +330,12 @@ func saveRequest(db *sql.DB, res ResponseInfo, req RequestInfo, logger *log.Logg
logger.Println(err)
return
}

_, err = db.Exec("UPDATE user SET credits=? WHERE id=?",
req.StartingCredits-(res.Tokens*cpt),
max(req.StartingCredits-int64(res.Tokens*cpt), 0),
req.UserId)
if err != nil {
logger.Println("Failed to update")
logger.Println(err)
logger.Printf("Failed to update credits: %d - %d\n%s\n", req.StartingCredits, res.Tokens*cpt, err)
}

responseJson, _ := json.Marshal(res.Responses)
Expand Down
12 changes: 6 additions & 6 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func main() {

e.POST("/v1/chat/completions", func(c echo.Context) error {
cc := c.(*Context)
cc.Info.Printf("/api/chat/completions\n")
request, err := preprocessOpenaiRequest(cc, db)
cc.Info.Printf("/api/chat/completions - %d\n", request.UserId)
if err != nil {
error := err.(*RequestError)
cc.Err.Println(err)
Expand All @@ -107,16 +107,16 @@ func main() {
}

if !res.Success {
cc.Warn.Printf("Miner: %s %s\nTimed out\n", res.Miner.Hotkey, res.Miner.Coldkey)
return c.String(500, fmt.Sprintf("Miner UID %d Timed out. Try Again.", res.Miner.Uid))
cc.Warn.Printf("Miner: %s %s\n Failed request\n", res.Miner.Hotkey, res.Miner.Coldkey, res.Miner.Uid)
return c.String(500, fmt.Sprintf("Miner UID %d Failed Request. Try Again.", res.Miner.Uid))
}

return c.String(200, "")
})
e.POST("/v1/completions", func(c echo.Context) error {
cc := c.(*Context)
cc.Info.Printf("/api/completions\n")
request, err := preprocessOpenaiRequest(cc, db)
cc.Info.Printf("/api/completions - %d\n", request.UserId)
if err != nil {
error := err.(*RequestError)
cc.Err.Println(err)
Expand All @@ -134,8 +134,8 @@ func main() {
}

if !res.Success {
cc.Warn.Printf("Miner: %s %s\nTimed out\n", res.Miner.Hotkey, res.Miner.Coldkey)
return c.String(500, fmt.Sprintf("Miner UID %d Timed out. Try Again.", res.Miner.Uid))
cc.Warn.Printf("Miner: %s %s\n Failed request\n", res.Miner.Hotkey, res.Miner.Coldkey, res.Miner.Uid)
return c.String(500, fmt.Sprintf("Miner UID %d Failed Request. Try Again.", res.Miner.Uid))
}

return c.String(200, "")
Expand Down
2 changes: 1 addition & 1 deletion api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type Event struct {
}

type RequestInfo struct {
StartingCredits int
StartingCredits int64
UserId int
Body []byte
Endpoint string
Expand Down

0 comments on commit 5603291

Please sign in to comment.