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