Spaces:
Configuration error
Configuration error
BOHE
commited on
Commit
·
74975e2
1
Parent(s):
d851ae1
huggingface 支持
Browse files- api/main.go +44 -1
- huggingDockerfile +30 -0
- proxy/proxy.go +5 -5
api/main.go
CHANGED
|
@@ -56,6 +56,18 @@ type OpenAIChoice struct {
|
|
| 56 |
FinishReason string `json:"finish_reason"`
|
| 57 |
}
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
var modelMap = map[string]string{
|
| 60 |
"deepseek-reasoner": "deepseek_r1",
|
| 61 |
"deepseek-chat": "deepseek_v3",
|
|
@@ -108,7 +120,38 @@ func reverseMapModelName(youModel string) string {
|
|
| 108 |
var originalModel string
|
| 109 |
|
| 110 |
func Handler(w http.ResponseWriter, r *http.Request) {
|
| 111 |
-
if r.URL.Path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
w.Header().Set("Content-Type", "application/json")
|
| 113 |
json.NewEncoder(w).Encode(map[string]string{
|
| 114 |
"status": "You2Api Service Running...",
|
|
|
|
| 56 |
FinishReason string `json:"finish_reason"`
|
| 57 |
}
|
| 58 |
|
| 59 |
+
type ModelResponse struct {
|
| 60 |
+
Object string `json:"object"`
|
| 61 |
+
Data []ModelDetail `json:"data"`
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
type ModelDetail struct {
|
| 65 |
+
ID string `json:"id"`
|
| 66 |
+
Object string `json:"object"`
|
| 67 |
+
Created int64 `json:"created"`
|
| 68 |
+
OwnedBy string `json:"owned_by"`
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
var modelMap = map[string]string{
|
| 72 |
"deepseek-reasoner": "deepseek_r1",
|
| 73 |
"deepseek-chat": "deepseek_v3",
|
|
|
|
| 120 |
var originalModel string
|
| 121 |
|
| 122 |
func Handler(w http.ResponseWriter, r *http.Request) {
|
| 123 |
+
if r.URL.Path == "/v1/models" || r.URL.Path == "/api/v1/models" {
|
| 124 |
+
w.Header().Set("Content-Type", "application/json")
|
| 125 |
+
w.Header().Set("Access-Control-Allow-Origin", "*")
|
| 126 |
+
w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
|
| 127 |
+
w.Header().Set("Access-Control-Allow-Headers", "*")
|
| 128 |
+
|
| 129 |
+
if r.Method == "OPTIONS" {
|
| 130 |
+
w.WriteHeader(http.StatusOK)
|
| 131 |
+
return
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
models := make([]ModelDetail, 0, len(modelMap))
|
| 135 |
+
created := time.Now().Unix()
|
| 136 |
+
for modelID := range modelMap {
|
| 137 |
+
models = append(models, ModelDetail{
|
| 138 |
+
ID: modelID,
|
| 139 |
+
Object: "model",
|
| 140 |
+
Created: created,
|
| 141 |
+
OwnedBy: "organization-owner",
|
| 142 |
+
})
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
response := ModelResponse{
|
| 146 |
+
Object: "list",
|
| 147 |
+
Data: models,
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
json.NewEncoder(w).Encode(response)
|
| 151 |
+
return
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
if r.URL.Path != "/v1/chat/completions" && r.URL.Path != "/api/v1/chat/completions" {
|
| 155 |
w.Header().Set("Content-Type", "application/json")
|
| 156 |
json.NewEncoder(w).Encode(map[string]string{
|
| 157 |
"status": "You2Api Service Running...",
|
huggingDockerfile
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 使用最新的 Go 1.22 版本作为基础镜像
|
| 2 |
+
FROM golang:1.22-alpine
|
| 3 |
+
|
| 4 |
+
# 安装必要的系统依赖
|
| 5 |
+
RUN apk --no-cache add ca-certificates git
|
| 6 |
+
|
| 7 |
+
# 设置工作目录
|
| 8 |
+
WORKDIR /app
|
| 9 |
+
|
| 10 |
+
# 克隆项目代码
|
| 11 |
+
RUN git clone https://github.com/bohesocool/you2api.git .
|
| 12 |
+
|
| 13 |
+
# 下载依赖
|
| 14 |
+
RUN go mod download
|
| 15 |
+
|
| 16 |
+
# 构建应用
|
| 17 |
+
RUN CGO_ENABLED=0 GOOS=linux go build -o main .
|
| 18 |
+
|
| 19 |
+
# 设置环境变量
|
| 20 |
+
ENV PORT=8080
|
| 21 |
+
ENV ENABLE_PROXY=false
|
| 22 |
+
ENV PROXY_URL=""
|
| 23 |
+
ENV PROXY_TIMEOUT_MS=5000
|
| 24 |
+
ENV LOG_LEVEL=info
|
| 25 |
+
|
| 26 |
+
# 暴露端口
|
| 27 |
+
EXPOSE 8080
|
| 28 |
+
|
| 29 |
+
# 运行应用
|
| 30 |
+
CMD ["./main"]
|
proxy/proxy.go
CHANGED
|
@@ -8,9 +8,9 @@ import (
|
|
| 8 |
)
|
| 9 |
|
| 10 |
type Proxy struct {
|
| 11 |
-
target
|
| 12 |
-
proxy
|
| 13 |
-
timeoutMS
|
| 14 |
}
|
| 15 |
|
| 16 |
func NewProxy(targetURL string, timeoutMS int) (*Proxy, error) {
|
|
@@ -27,7 +27,7 @@ func NewProxy(targetURL string, timeoutMS int) (*Proxy, error) {
|
|
| 27 |
proxy.proxy = &httputil.ReverseProxy{
|
| 28 |
Director: proxy.director,
|
| 29 |
Transport: &http.Transport{
|
| 30 |
-
Proxy:
|
| 31 |
ResponseHeaderTimeout: time.Duration(timeoutMS) * time.Millisecond,
|
| 32 |
},
|
| 33 |
}
|
|
@@ -43,4 +43,4 @@ func (p *Proxy) director(req *http.Request) {
|
|
| 43 |
|
| 44 |
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
| 45 |
p.proxy.ServeHTTP(w, r)
|
| 46 |
-
}
|
|
|
|
| 8 |
)
|
| 9 |
|
| 10 |
type Proxy struct {
|
| 11 |
+
target *url.URL
|
| 12 |
+
proxy *httputil.ReverseProxy
|
| 13 |
+
timeoutMS int
|
| 14 |
}
|
| 15 |
|
| 16 |
func NewProxy(targetURL string, timeoutMS int) (*Proxy, error) {
|
|
|
|
| 27 |
proxy.proxy = &httputil.ReverseProxy{
|
| 28 |
Director: proxy.director,
|
| 29 |
Transport: &http.Transport{
|
| 30 |
+
Proxy: http.ProxyURL(target),
|
| 31 |
ResponseHeaderTimeout: time.Duration(timeoutMS) * time.Millisecond,
|
| 32 |
},
|
| 33 |
}
|
|
|
|
| 43 |
|
| 44 |
func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
| 45 |
p.proxy.ServeHTTP(w, r)
|
| 46 |
+
}
|