Skip to content

Commit bc00bf4

Browse files
author
Jawher Kallel
committed
add and implement open-router service
1 parent 89e1aba commit bc00bf4

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

gateway/internal/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type Config struct {
1313
HuggingFaceKey string
1414
GroqKey string
1515
RedisAddr string
16+
OpenRouterKey string
1617
}
1718

1819
func Load() *Config {
@@ -26,6 +27,7 @@ func Load() *Config {
2627
OpenAIKey: getEnv("OPENAI_API_KEY", ""),
2728
HuggingFaceKey: getEnv("HF_API_KEY", ""),
2829
GroqKey: getEnv("GROQ_API_KEY", ""),
30+
OpenRouterKey: getEnv("OPENROUTER_API_KEY", ""),
2931
RedisAddr: getEnv("REDIS_ADDR", "localhost:6379"),
3032
}
3133
if cfg.OpenAIKey == "" {

gateway/internal/handlers/query.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ func QueryHandler(cfg *config.Config, cache *cache.Cache) gin.HandlerFunc {
4646
response, err = services.QueryHuggingFace(cfg.HuggingFaceKey, req.Prompt)
4747
case "groq":
4848
response, err = services.QueryGroq(cfg.GroqKey, req.Prompt)
49+
case "openrouter":
50+
response, err = services.QueryOpenRouter(cfg.OpenRouterKey, req.Prompt)
4951
default:
5052
c.JSON(http.StatusBadRequest, gin.H{"error": "Unsupported provider"})
5153
return
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package services
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"io"
8+
"net/http"
9+
)
10+
11+
type OpenRouterRequest struct {
12+
Model string `json:"model"`
13+
Messages []map[string]string `json:"messages"`
14+
}
15+
16+
type OpenRouterResponse struct {
17+
Choices []struct {
18+
Message struct {
19+
Content string `json:"content"`
20+
} `json:"message"`
21+
} `json:"choices"`
22+
Error struct {
23+
Message string `json:"message"`
24+
} `json:"error"`
25+
}
26+
27+
func QueryOpenRouter(apiKey, prompt string) (string, error) {
28+
reqBody := OpenRouterRequest{
29+
Model: "openai/gpt-4o-mini",
30+
Messages: []map[string]string{
31+
{"role": "system", "content": "You are a helpful assistant."},
32+
{"role": "user", "content": prompt},
33+
},
34+
}
35+
jsonData, _ := json.Marshal(reqBody)
36+
37+
req, _ := http.NewRequest("POST", "https://openrouter.ai/api/v1/chat/completions", bytes.NewBuffer(jsonData))
38+
req.Header.Set("Authorization", "Bearer "+apiKey)
39+
req.Header.Set("Content-Type", "application/json")
40+
41+
client := &http.Client{}
42+
resp, err := client.Do(req)
43+
if err != nil {
44+
return "", err
45+
}
46+
defer resp.Body.Close()
47+
48+
bodyBytes, _ := io.ReadAll(resp.Body)
49+
if resp.StatusCode != http.StatusOK {
50+
var errResp OpenRouterResponse
51+
json.Unmarshal(bodyBytes, &errResp)
52+
if errResp.Error.Message != "" {
53+
return "", fmt.Errorf("OpenRouter error: %s", errResp.Error.Message)
54+
}
55+
return "", fmt.Errorf("OpenRouter API error: %s", string(bodyBytes))
56+
}
57+
58+
var result OpenRouterResponse
59+
if err := json.Unmarshal(bodyBytes, &result); err != nil {
60+
return "", fmt.Errorf("failed to parse OpenRouter response: %w", err)
61+
}
62+
63+
if len(result.Choices) > 0 {
64+
return result.Choices[0].Message.Content, nil
65+
}
66+
return "", fmt.Errorf("no choices returned from OpenRouter: %s", string(bodyBytes))
67+
}

0 commit comments

Comments
 (0)