File size: 12,555 Bytes
a23421d
 
 
7be6f21
 
 
 
 
 
a23421d
 
 
7be6f21
a23421d
 
 
7be6f21
 
 
 
 
a23421d
 
 
7be6f21
 
 
a23421d
 
 
7be6f21
a23421d
 
 
7be6f21
 
 
a23421d
 
 
7be6f21
 
a23421d
 
 
7be6f21
 
 
 
 
a23421d
 
 
7be6f21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a23421d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7be6f21
a23421d
 
 
 
 
 
 
 
7be6f21
 
 
 
 
a23421d
 
 
7be6f21
 
 
 
a23421d
 
 
7be6f21
 
 
 
 
a23421d
 
 
 
 
7be6f21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a23421d
 
 
7be6f21
 
 
 
 
 
 
 
 
a23421d
 
 
7be6f21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a23421d
 
 
7be6f21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a23421d
 
 
7be6f21
 
 
 
 
a23421d
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package main

import (
    "bufio"
    "encoding/json"
    "fmt"
    "net/http"
    "strings"
    "time"
)

type YouChatResponse struct {
    YouChatToken string `json:"youChatToken"`
}

type OpenAIStreamResponse struct {
    ID      string   `json:"id"`
    Object  string   `json:"object"`
    Created int64    `json:"created"`
    Model   string   `json:"model"`
    Choices []Choice `json:"choices"`
}

type Choice struct {
    Delta        Delta  `json:"delta"`
    Index        int    `json:"index"`
    FinishReason string `json:"finish_reason"`
}

type Delta struct {
    Content string `json:"content"`
}

type OpenAIRequest struct {
    Messages []Message `json:"messages"`
    Stream   bool      `json:"stream"`
    Model    string    `json:"model"`
}

type Message struct {
    Role    string `json:"role"`
    Content string `json:"content"`
}

type OpenAIResponse struct {
    ID      string         `json:"id"`
    Object  string         `json:"object"`
    Created int64          `json:"created"`
    Model   string         `json:"model"`
    Choices []OpenAIChoice `json:"choices"`
}

type OpenAIChoice struct {
    Message      Message `json:"message"`
    Index        int     `json:"index"`
    FinishReason string  `json:"finish_reason"`
}

type ModelResponse struct {
    Object string        `json:"object"`
    Data   []ModelDetail `json:"data"`
}

type ModelDetail struct {
    ID      string `json:"id"`
    Object  string `json:"object"`
    Created int64  `json:"created"`
    OwnedBy string `json:"owned_by"`
}

var modelMap = map[string]string{
    "deepseek-reasoner":       "deepseek_r1",
    "deepseek-chat":           "deepseek_v3",
    "o3-mini-high":            "openai_o3_mini_high",
    "o3-mini-medium":          "openai_o3_mini_medium",
    "o1":                      "openai_o1",
    "o1-mini":                 "openai_o1_mini",
    "o1-preview":              "openai_o1_preview",
    "gpt-4o":                  "gpt_4o",
    "gpt-4o-mini":             "gpt_4o_mini",
    "gpt-4-turbo":             "gpt_4_turbo",
    "gpt-4":                   "gpt_4",
    "claude-3-7-sonnet-think": "claude_3_7_sonnet_thinking",
    "claude-3-7-sonnet":       "claude_3_7_sonnet",
    "claude-3.5-sonnet":       "claude_3_5_sonnet",
    "claude-3-opus":           "claude_3_opus",
    "claude-3-sonnet":         "claude_3_sonnet",
    "claude-3.5-haiku":        "claude_3_5_haiku",
    "grok-2":                  "grok_2",
    "llama-3.70b":             "llama3_3_70b",
    "llama-3.2-90b":           "llama3_2_90b",
    "llama-3.1-405b":          "llama3_1_405b",
    "mistral-large-2":         "mistral_large_2",
    "gemini-2-flash":          "gemini_2_flash",
    "gemini-1.5-flash":        "gemini_1_5_flash",
    "gemini-1.5-pro":          "gemini_1_5_pro",
    "databricks-dbrx-instruct": "databricks_dbrx_instruct",
    "qwen-2.5-72b":            "qwen2p5_72b",
    "qwen-2.5-coder-32b":      "qwen2p5_coder_32b",
    "command-r-plus":          "command_r_plus",
    "solar-1-mini":            "solar_1_mini",
    "dolphin-2.5":             "dolphin_2_5",
}

func getReverseModelMap() map[string]string {
    reverse := make(map[string]string, len(modelMap))
    for k, v := range modelMap {
        reverse[v] = k
    }
    return reverse
}

func mapModelName(openAIModel string) string {
    if mappedModel, exists := modelMap[openAIModel]; exists {
        return mappedModel
    }
    return "deepseek_v3"
}

func reverseMapModelName(youModel string) string {
    reverseMap := getReverseModelMap()
    if mappedModel, exists := reverseMap[youModel]; exists {
        return mappedModel
    }
    return "deepseek-chat"
}

var originalModel string

func Handler(w http.ResponseWriter, r *http.Request) {
    // 新增模型列表接口
    if r.URL.Path == "/hf/v1/models" {
        w.Header().Set("Content-Type", "application/json")
        w.Header().Set("Access-Control-Allow-Origin", "*")
        w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS")
        w.Header().Set("Access-Control-Allow-Headers", "*")

        if r.Method == "OPTIONS" {
            w.WriteHeader(http.StatusOK)
            return
        }

        models := make([]ModelDetail, 0, len(modelMap))
        created := time.Now().Unix()
        for modelID := range modelMap {
            models = append(models, ModelDetail{
                ID:      modelID,
                Object:  "model",
                Created: created,
                OwnedBy: "organization-owner",
            })
        }

        response := ModelResponse{
            Object: "list",
            Data:   models,
        }

        json.NewEncoder(w).Encode(response)
        return
    }

    // 原有逻辑保持不变
    if r.URL.Path != "/hf/v1/chat/completions" {
        w.Header().Set("Content-Type", "application/json")
        json.NewEncoder(w).Encode(map[string]string{
            "status":  "You2Api Service Running...",
            "message": "MoLoveSze...",
        })
        return
    }

    w.Header().Set("Access-Control-Allow-Origin", "*")
    w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
    w.Header().Set("Access-Control-Allow-Headers", "*")

    if r.Method == "OPTIONS" {
        w.WriteHeader(http.StatusOK)
        return
    }

    authHeader := r.Header.Get("Authorization")
    if !strings.HasPrefix(authHeader, "Bearer ") {
        http.Error(w, "Missing or invalid authorization header", http.StatusUnauthorized)
        return
    }
    dsToken := strings.TrimPrefix(authHeader, "Bearer ")

    var openAIReq OpenAIRequest
    if err := json.NewDecoder(r.Body).Decode(&openAIReq); err != nil {
        http.Error(w, "Invalid request body", http.StatusBadRequest)
        return
    }

    originalModel = openAIReq.Model
    lastMessage := openAIReq.Messages[len(openAIReq.Messages)-1].Content
    var chatHistory []map[string]interface{}
    for _, msg := range openAIReq.Messages {
        chatMsg := map[string]interface{}{
            "question": msg.Content,
            "answer":   "",
        }
        if msg.Role == "assistant" {
            chatMsg["question"] = ""
            chatMsg["answer"] = msg.Content
        }
        chatHistory = append(chatHistory, chatMsg)
    }

    chatHistoryJSON, _ := json.Marshal(chatHistory)

    youReq, _ := http.NewRequest("GET", "https://you.com/api/streamingSearch", nil)

    q := youReq.URL.Query()
    q.Add("q", lastMessage)
    q.Add("page", "1")
    q.Add("count", "10")
    q.Add("safeSearch", "Moderate")
    q.Add("mkt", "zh-HK")
    q.Add("enable_worklow_generation_ux", "true")
    q.Add("domain", "youchat")
    q.Add("use_personalization_extraction", "true")
    q.Add("pastChatLength", fmt.Sprintf("%d", len(chatHistory)-1))
    q.Add("selectedChatMode", "custom")
    q.Add("selectedAiModel", mapModelName(openAIReq.Model))
    q.Add("enable_agent_clarification_questions", "true")
    q.Add("use_nested_youchat_updates", "true")
    q.Add("chat", string(chatHistoryJSON))
    youReq.URL.RawQuery = q.Encode()

    youReq.Header = http.Header{
        "sec-ch-ua-platform":         {"Windows"},
        "Cache-Control":              {"no-cache"},
        "sec-ch-ua":                  {`"Not(A:Brand";v="99", "Microsoft Edge";v="133", "Chromium";v="133"`},
        "sec-ch-ua-bitness":          {"64"},
        "sec-ch-ua-model":            {""},
        "sec-ch-ua-mobile":           {"?0"},
        "sec-ch-ua-arch":             {"x86"},
        "sec-ch-ua-full-version":     {"133.0.3065.39"},
        "Accept":                     {"text/event-stream"},
        "User-Agent":                 {"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0"},
        "sec-ch-ua-platform-version": {"19.0.0"},
        "Sec-Fetch-Site":             {"same-origin"},
        "Sec-Fetch-Mode":             {"cors"},
        "Sec-Fetch-Dest":             {"empty"},
        "Host":                       {"you.com"},
    }

    cookies := getCookies(dsToken)
    var cookieStrings []string
    for name, value := range cookies {
        cookieStrings = append(cookieStrings, fmt.Sprintf("%s=%s", name, value))
    }
    youReq.Header.Add("Cookie", strings.Join(cookieStrings, ";"))

    if !openAIReq.Stream {
        handleNonStreamingResponse(w, youReq)
        return
    }

    handleStreamingResponse(w, youReq)
}

func getCookies(dsToken string) map[string]string {
    return map[string]string{
        "guest_has_seen_legal_disclaimer": "true",
        "youchat_personalization":         "true",
        "DS":                              dsToken,
        "you_subscription":                "youpro_standard_year",
        "youpro_subscription":             "true",
        "ai_model":                        "deepseek_r1",
        "youchat_smart_learn":             "true",
    }
}

func handleNonStreamingResponse(w http.ResponseWriter, youReq *http.Request) {
    client := &http.Client{
        Timeout: 60 * time.Second,
    }
    resp, err := client.Do(youReq)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    defer resp.Body.Close()

    var fullResponse strings.Builder
    scanner := bufio.NewScanner(resp.Body)

    buf := make([]byte, 0, 64*1024)
    scanner.Buffer(buf, 1024*1024)

    for scanner.Scan() {
        line := scanner.Text()
        if strings.HasPrefix(line, "event: youChatToken") {
            scanner.Scan()
            data := scanner.Text()
            if !strings.HasPrefix(data, "data: ") {
                continue
            }
            var token YouChatResponse
            if err := json.Unmarshal([]byte(strings.TrimPrefix(data, "data: ")), &token); err != nil {
                continue
            }
            fullResponse.WriteString(token.YouChatToken)
        }
    }

    if scanner.Err() != nil {
        http.Error(w, "Error reading response", http.StatusInternalServerError)
        return
    }

    openAIResp := OpenAIResponse{
        ID:      "chatcmpl-" + fmt.Sprintf("%d", time.Now().Unix()),
        Object:  "chat.completion",
        Created: time.Now().Unix(),
        Model:   reverseMapModelName(mapModelName(originalModel)),
        Choices: []OpenAIChoice{
            {
                Message: Message{
                    Role:    "assistant",
                    Content: fullResponse.String(),
                },
                Index:        0,
                FinishReason: "stop",
            },
        },
    }

    w.Header().Set("Content-Type", "application/json")
    if err := json.NewEncoder(w).Encode(openAIResp); err != nil {
        http.Error(w, "Error encoding response", http.StatusInternalServerError)
        return
    }
}

func handleStreamingResponse(w http.ResponseWriter, youReq *http.Request) {
    client := &http.Client{}
    resp, err := client.Do(youReq)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    defer resp.Body.Close()

    w.Header().Set("Content-Type", "text/event-stream")
    w.Header().Set("Cache-Control", "no-cache")
    w.Header().Set("Connection", "keep-alive")

    scanner := bufio.NewScanner(resp.Body)
    for scanner.Scan() {
        line := scanner.Text()

        if strings.HasPrefix(line, "event: youChatToken") {
            scanner.Scan()
            data := scanner.Text()

            var token YouChatResponse
            json.Unmarshal([]byte(strings.TrimPrefix(data, "data: ")), &token)

            openAIResp := OpenAIStreamResponse{
                ID:      "chatcmpl-" + fmt.Sprintf("%d", time.Now().Unix()),
                Object:  "chat.completion.chunk",
                Created: time.Now().Unix(),
                Model:   reverseMapModelName(mapModelName(originalModel)),
                Choices: []Choice{
                    {
                        Delta: Delta{
                            Content: token.YouChatToken,
                        },
                        Index:        0,
                        FinishReason: "",
                    },
                },
            }

            respBytes, _ := json.Marshal(openAIResp)
            fmt.Fprintf(w, "data: %s\n\n", string(respBytes))
            w.(http.Flusher).Flush()
        }
    }
}

func main() {
    http.HandleFunc("/", Handler)
    fmt.Println("Server starting on :7860...")
    if err := http.ListenAndServe(":7860", nil); err != nil {
        fmt.Printf("Error starting server: %v\n", err)
    }
}