Spaces:
Paused
Paused
track tokens openrouter with apikey
Browse files- internal/handler/chat.go +1 -1
- internal/handler/openrouter.go +16 -10
internal/handler/chat.go
CHANGED
|
@@ -52,7 +52,7 @@ func HandleChatCompletions(w http.ResponseWriter, r *http.Request) {
|
|
| 52 |
if m, ok := tempReq["model"].(string); ok && IsOpenRouterModel(m) {
|
| 53 |
ok2, reason := CheckAndTrack(apiKey, 0)
|
| 54 |
if !ok2 { http.Error(w, reason, 429); return }
|
| 55 |
-
HandleOpenRouter(w, r, body)
|
| 56 |
return
|
| 57 |
}
|
| 58 |
var req model.ChatRequest
|
|
|
|
| 52 |
if m, ok := tempReq["model"].(string); ok && IsOpenRouterModel(m) {
|
| 53 |
ok2, reason := CheckAndTrack(apiKey, 0)
|
| 54 |
if !ok2 { http.Error(w, reason, 429); return }
|
| 55 |
+
HandleOpenRouter(w, r, body, apiKey)
|
| 56 |
return
|
| 57 |
}
|
| 58 |
var req model.ChatRequest
|
internal/handler/openrouter.go
CHANGED
|
@@ -11,10 +11,10 @@ import (
|
|
| 11 |
var openRouterKey = os.Getenv("OP_Rowproxy_key")
|
| 12 |
|
| 13 |
var openRouterModels = map[string]string{
|
| 14 |
-
"deepseek-r1":
|
| 15 |
-
"deepseek-v3":
|
| 16 |
-
"deepseek-r1-0528":
|
| 17 |
-
"deepseek-v3-turbo":
|
| 18 |
}
|
| 19 |
|
| 20 |
func IsOpenRouterModel(model string) bool {
|
|
@@ -22,7 +22,7 @@ func IsOpenRouterModel(model string) bool {
|
|
| 22 |
return ok
|
| 23 |
}
|
| 24 |
|
| 25 |
-
func HandleOpenRouter(w http.ResponseWriter, r *http.Request, body []byte) {
|
| 26 |
var req map[string]interface{}
|
| 27 |
json.Unmarshal(body, &req)
|
| 28 |
if m, ok := req["model"].(string); ok {
|
|
@@ -36,12 +36,18 @@ func HandleOpenRouter(w http.ResponseWriter, r *http.Request, body []byte) {
|
|
| 36 |
proxyReq.Header.Set("Content-Type", "application/json")
|
| 37 |
client := &http.Client{}
|
| 38 |
resp, err := client.Do(proxyReq)
|
| 39 |
-
if err != nil {
|
| 40 |
-
http.Error(w, "OpenRouter error", 500)
|
| 41 |
-
return
|
| 42 |
-
}
|
| 43 |
defer resp.Body.Close()
|
|
|
|
| 44 |
w.Header().Set("Content-Type", "application/json")
|
| 45 |
w.WriteHeader(resp.StatusCode)
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
}
|
|
|
|
| 11 |
var openRouterKey = os.Getenv("OP_Rowproxy_key")
|
| 12 |
|
| 13 |
var openRouterModels = map[string]string{
|
| 14 |
+
"deepseek-r1": "deepseek/deepseek-r1",
|
| 15 |
+
"deepseek-v3": "deepseek/deepseek-v3.2",
|
| 16 |
+
"deepseek-r1-0528": "deepseek/deepseek-r1-0528",
|
| 17 |
+
"deepseek-v3-turbo": "deepseek/deepseek-v3.1-terminus",
|
| 18 |
}
|
| 19 |
|
| 20 |
func IsOpenRouterModel(model string) bool {
|
|
|
|
| 22 |
return ok
|
| 23 |
}
|
| 24 |
|
| 25 |
+
func HandleOpenRouter(w http.ResponseWriter, r *http.Request, body []byte, apiKey string) {
|
| 26 |
var req map[string]interface{}
|
| 27 |
json.Unmarshal(body, &req)
|
| 28 |
if m, ok := req["model"].(string); ok {
|
|
|
|
| 36 |
proxyReq.Header.Set("Content-Type", "application/json")
|
| 37 |
client := &http.Client{}
|
| 38 |
resp, err := client.Do(proxyReq)
|
| 39 |
+
if err != nil { http.Error(w, "OpenRouter error", 500); return }
|
|
|
|
|
|
|
|
|
|
| 40 |
defer resp.Body.Close()
|
| 41 |
+
respBody, _ := io.ReadAll(resp.Body)
|
| 42 |
w.Header().Set("Content-Type", "application/json")
|
| 43 |
w.WriteHeader(resp.StatusCode)
|
| 44 |
+
w.Write(respBody)
|
| 45 |
+
var result map[string]interface{}
|
| 46 |
+
if json.Unmarshal(respBody, &result) == nil {
|
| 47 |
+
if usage, ok := result["usage"].(map[string]interface{}); ok {
|
| 48 |
+
if total, ok := usage["total_tokens"].(float64); ok {
|
| 49 |
+
TrackUsage(apiKey, int(total))
|
| 50 |
+
}
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
}
|