-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
133 lines (117 loc) · 3.36 KB
/
main.go
File metadata and controls
133 lines (117 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"time"
)
var (
bearerToken string
productId string
port string
)
func init() {
flag.StringVar(&bearerToken, "token", "", "Bearer token for authentication (required)")
flag.StringVar(&productId, "id", "", "Product ID (required)")
flag.StringVar(&port, "port", "8080", "Port to run the server on")
}
func main() {
flag.Parse()
if bearerToken == "" || productId == "" {
fmt.Fprintln(os.Stderr, "Error: -token and -id are required arguments")
flag.Usage()
os.Exit(1)
}
http.HandleFunc("/v1/models", listModels)
http.HandleFunc("/v1/chat/completions", chatCompletions)
fmt.Println("Server started on port", port)
http.ListenAndServe(":"+port, nil)
}
func chatCompletions(w http.ResponseWriter, r *http.Request) {
upstreamURL := "https://api.infomaniak.com/1/ai/" + productId + "/openai/chat/completions"
req, err := http.NewRequest(r.Method, upstreamURL, r.Body)
if err != nil {
http.Error(w, "Failed to create upstream request", http.StatusInternalServerError)
return
}
req.Header.Set("Authorization", "Bearer "+bearerToken)
req.Header.Set("Content-Type", r.Header.Get("Content-Type"))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
http.Error(w, "Upstream API error", http.StatusBadGateway)
return
}
defer resp.Body.Close()
for k, vv := range resp.Header {
for _, v := range vv {
w.Header().Add(k, v)
}
}
w.WriteHeader(resp.StatusCode)
io.Copy(w, resp.Body)
}
func listModels(w http.ResponseWriter, r *http.Request) {
req, err := http.NewRequest("GET", "https://api.infomaniak.com/1/ai/models", nil)
if err != nil {
http.Error(w, "Failed to create request", http.StatusInternalServerError)
return
}
req.Header.Set("Authorization", "Bearer "+bearerToken)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil || resp.StatusCode != http.StatusOK {
http.Error(w, "Upstream API error", http.StatusBadGateway)
return
}
defer resp.Body.Close()
var upstream struct {
Result string `json:"result"`
Data []struct {
ID int `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
DocumentationLink string `json:"documentation_link"`
Description string `json:"description"`
InfoStatus string `json:"info_status"`
LogoURL string `json:"logo_url"`
LastUpdatedAt string `json:"last_updated_at"`
MaxTokenInput interface{} `json:"max_token_input"`
Version string `json:"version"`
} `json:"data"`
}
if err := json.NewDecoder(resp.Body).Decode(&upstream); err != nil {
http.Error(w, "Invalid JSON from upstream", http.StatusInternalServerError)
return
}
type Model struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
OwnedBy string `json:"owned_by"`
}
var filtered []Model
now := time.Now().Unix()
for _, model := range upstream.Data {
if model.Type == "llm" {
filtered = append(filtered, Model{
ID: model.Name,
Object: "model",
Created: now,
OwnedBy: "library",
})
}
}
out := struct {
Object string `json:"object"`
Data []Model `json:"data"`
}{
Object: "list",
Data: filtered,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(out)
}