From 1e5afdb866fcf7637111f70e220ceb60ce480e25 Mon Sep 17 00:00:00 2001 From: josh Date: Tue, 19 Nov 2024 22:08:21 -0800 Subject: [PATCH] feat: single miner requests and cors --- api/functions.go | 36 +++++++++++++++++++++++++++++++++--- api/go.mod | 2 ++ api/go.sum | 4 ++++ api/server.go | 6 ++++-- api/types.go | 1 + miner-cache/cache.py | 2 +- 6 files changed, 45 insertions(+), 6 deletions(-) diff --git a/api/functions.go b/api/functions.go index 46213c9..ca8c5e5 100644 --- a/api/functions.go +++ b/api/functions.go @@ -17,6 +17,7 @@ import ( "net" "net/http" "os" + "strconv" "strings" "time" @@ -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") @@ -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 { @@ -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) @@ -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{ @@ -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()) diff --git a/api/go.mod b/api/go.mod index 19aadae..385d57f 100644 --- a/api/go.mod +++ b/api/go.mod @@ -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 @@ -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 diff --git a/api/go.sum b/api/go.sum index 5259ace..573e5cb 100644 --- a/api/go.sum +++ b/api/go.sum @@ -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= @@ -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= diff --git a/api/server.go b/api/server.go index 2d82fa3..1fd7244 100644 --- a/api/server.go +++ b/api/server.go @@ -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" ) @@ -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) @@ -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 { @@ -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) diff --git a/api/types.go b/api/types.go index 90686d4..8c114f1 100644 --- a/api/types.go +++ b/api/types.go @@ -68,6 +68,7 @@ type RequestInfo struct { UserId int Body []byte Endpoint string + Miner *int } type ResponseInfo struct { diff --git a/miner-cache/cache.py b/miner-cache/cache.py index 2f19442..d4d55f2 100644 --- a/miner-cache/cache.py +++ b/miner-cache/cache.py @@ -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()