Skip to content

Commit

Permalink
feat: single miner requests and cors
Browse files Browse the repository at this point in the history
  • Loading branch information
GentikSolm committed Nov 20, 2024
1 parent 9da8c5b commit 1e5afdb
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 6 deletions.
36 changes: 33 additions & 3 deletions api/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"net"
"net/http"
"os"
"strconv"
"strings"
"time"

Expand All @@ -29,6 +30,7 @@ import (
func preprocessOpenaiRequest(c *Context, db *sql.DB) (*RequestInfo, error) {
c.Request().Header.Add("Content-Type", "application/json")
bearer := c.Request().Header.Get("Authorization")
miner := c.Request().Header.Get("X-Targon-Miner-Uid")
c.Response().Header().Set("Content-Type", "text/event-stream; charset=utf-8")
c.Response().Header().Set("Cache-Control", "no-cache")
c.Response().Header().Set("Connection", "keep-alive")
Expand Down Expand Up @@ -78,13 +80,18 @@ func preprocessOpenaiRequest(c *Context, db *sql.DB) (*RequestInfo, error) {
}

body, err = json.Marshal(payload)
fmt.Println(string(body))
if err != nil {
c.Err.Println(err)
sendErrorToEndon(err, "/v1/chat/completions")
return nil, &RequestError{500, errors.New("Internal Server Error")}
}
return &RequestInfo{Body: body, UserId: userid, StartingCredits: credits}, nil

res := &RequestInfo{Body: body, UserId: userid, StartingCredits: credits}
miner_uid, err := strconv.Atoi(miner)
if err == nil {
res.Miner = &miner_uid
}
return res, nil
}

func safeEnv(env string) string {
Expand Down Expand Up @@ -157,7 +164,7 @@ func getMinersForModel(c *Context, model string) []Miner {
return miners
}

func queryMiners(c *Context, req []byte, method string) (ResponseInfo, error) {
func queryMiners(c *Context, req []byte, method string, miner_uid *int) (ResponseInfo, error) {
// Query miners with llm request
var requestBody RequestBody
err := json.Unmarshal(req, &requestBody)
Expand All @@ -171,7 +178,25 @@ func queryMiners(c *Context, req []byte, method string) (ResponseInfo, error) {
if miners == nil || len(miners) == 0 {
return ResponseInfo{}, errors.New("No Miners")
}

// Call specific miner if passed
miner := miners[0]
if miner_uid != nil {
c.Info.Printf("Attempting to find miner %d", *miner_uid)
found := false
for i := range miners {
m := miners[i]
if m.Uid == *miner_uid {
miner = m
found = true
break
}
}
if !found {
return ResponseInfo{}, errors.New("No Miners")
}
c.Info.Printf("Requesting Specific miner uid %d", miner.Uid)
}

// Build the rest of the body hash
tr := &http.Transport{
Expand Down Expand Up @@ -215,6 +240,11 @@ func queryMiners(c *Context, req []byte, method string) (ResponseInfo, error) {
timer := time.AfterFunc(4*time.Second, func() {
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
2 changes: 2 additions & 0 deletions api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/aidarkhanov/nanoid v1.0.8
github.com/go-sql-driver/mysql v1.8.1
github.com/google/uuid v1.6.0
github.com/labstack/echo v3.3.10+incompatible
github.com/labstack/echo/v4 v4.11.4
github.com/nitishm/go-rejson/v4 v4.2.0
github.com/redis/go-redis/v9 v9.5.1
Expand All @@ -16,6 +17,7 @@ require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/gtank/merlin v0.1.1-0.20191105220539-8318aed1a79f // indirect
github.com/gtank/ristretto255 v0.1.2 // indirect
Expand Down
4 changes: 4 additions & 0 deletions api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d/go.mod h1:tSxLoYXy
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
Expand All @@ -29,6 +31,8 @@ github.com/gtank/merlin v0.1.1-0.20191105220539-8318aed1a79f h1:8N8XWLZelZNibkhM
github.com/gtank/merlin v0.1.1-0.20191105220539-8318aed1a79f/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s=
github.com/gtank/ristretto255 v0.1.2 h1:JEqUCPA1NvLq5DwYtuzigd7ss8fwbYay9fi4/5uMzcc=
github.com/gtank/ristretto255 v0.1.2/go.mod h1:Ph5OpO6c7xKUGROZfWVLiJf9icMDwUeIvY4OmlYW69o=
github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg=
github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s=
github.com/labstack/echo/v4 v4.11.4 h1:vDZmA+qNeh1pd/cCkEicDMrjtrnMGQ1QFI9gWN1zGq8=
github.com/labstack/echo/v4 v4.11.4/go.mod h1:noh7EvLwqDsmh/X/HWKPUl1AjzJrhyptRyEbQJfxen8=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
Expand Down
6 changes: 4 additions & 2 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/aidarkhanov/nanoid"
_ "github.com/go-sql-driver/mysql"
"github.com/google/uuid"
"github.com/labstack/echo/middleware"
"github.com/labstack/echo/v4"
"github.com/redis/go-redis/v9"
)
Expand Down Expand Up @@ -59,6 +60,7 @@ func main() {
}

e := echo.New()
e.Use(middleware.CORS())
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
reqId, _ := nanoid.Generate("0123456789abcdefghijklmnopqrstuvwxyz", 12)
Expand Down Expand Up @@ -95,7 +97,7 @@ func main() {
}
request.Endpoint = "CHAT"
cc.Info.Println(string(request.Body))
res, err := queryMiners(cc, request.Body, "/v1/chat/completions")
res, err := queryMiners(cc, request.Body, "/v1/chat/completions", request.Miner)
go saveRequest(db, res, *request, cc.Err)

if err != nil {
Expand All @@ -121,7 +123,7 @@ func main() {
return cc.String(error.StatusCode, error.Err.Error())
}
request.Endpoint = "COMPLETION"
res, err := queryMiners(cc, request.Body, "/v1/completions")
res, err := queryMiners(cc, request.Body, "/v1/completions", request.Miner)

go saveRequest(db, res, *request, cc.Err)

Expand Down
1 change: 1 addition & 0 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type RequestInfo struct {
UserId int
Body []byte
Endpoint string
Miner *int
}

type ResponseInfo struct {
Expand Down
2 changes: 1 addition & 1 deletion miner-cache/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

async def sync_miners():
metagraph = subtensor.metagraph(netuid=4)
indices = numpy.argsort(metagraph.incentive)[-90:] # Top 50 miners
indices = numpy.argsort(metagraph.incentive)[-200:]

# Get the corresponding uids
uids_with_highest_incentives: List[int] = metagraph.uids[indices].tolist()
Expand Down

0 comments on commit 1e5afdb

Please sign in to comment.