Spaces:
Paused
Paused
Update api/main.go
Browse files- api/main.go +330 -330
api/main.go
CHANGED
|
@@ -1,330 +1,330 @@
|
|
| 1 |
-
package
|
| 2 |
-
|
| 3 |
-
import (
|
| 4 |
-
"bufio"
|
| 5 |
-
"encoding/json"
|
| 6 |
-
"fmt"
|
| 7 |
-
"net/http"
|
| 8 |
-
"strings"
|
| 9 |
-
"time"
|
| 10 |
-
)
|
| 11 |
-
|
| 12 |
-
type YouChatResponse struct {
|
| 13 |
-
YouChatToken string `json:"youChatToken"`
|
| 14 |
-
}
|
| 15 |
-
|
| 16 |
-
type OpenAIStreamResponse struct {
|
| 17 |
-
ID string `json:"id"`
|
| 18 |
-
Object string `json:"object"`
|
| 19 |
-
Created int64 `json:"created"`
|
| 20 |
-
Model string `json:"model"`
|
| 21 |
-
Choices []Choice `json:"choices"`
|
| 22 |
-
}
|
| 23 |
-
|
| 24 |
-
type Choice struct {
|
| 25 |
-
Delta Delta `json:"delta"`
|
| 26 |
-
Index int `json:"index"`
|
| 27 |
-
FinishReason string `json:"finish_reason"`
|
| 28 |
-
}
|
| 29 |
-
|
| 30 |
-
type Delta struct {
|
| 31 |
-
Content string `json:"content"`
|
| 32 |
-
}
|
| 33 |
-
|
| 34 |
-
type OpenAIRequest struct {
|
| 35 |
-
Messages []Message `json:"messages"`
|
| 36 |
-
Stream bool `json:"stream"`
|
| 37 |
-
Model string `json:"model"`
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
-
type Message struct {
|
| 41 |
-
Role string `json:"role"`
|
| 42 |
-
Content string `json:"content"`
|
| 43 |
-
}
|
| 44 |
-
|
| 45 |
-
type OpenAIResponse struct {
|
| 46 |
-
ID string `json:"id"`
|
| 47 |
-
Object string `json:"object"`
|
| 48 |
-
Created int64 `json:"created"`
|
| 49 |
-
Model string `json:"model"`
|
| 50 |
-
Choices []OpenAIChoice `json:"choices"`
|
| 51 |
-
}
|
| 52 |
-
|
| 53 |
-
type OpenAIChoice struct {
|
| 54 |
-
Message Message `json:"message"`
|
| 55 |
-
Index int `json:"index"`
|
| 56 |
-
FinishReason string `json:"finish_reason"`
|
| 57 |
-
}
|
| 58 |
-
|
| 59 |
-
var modelMap = map[string]string{
|
| 60 |
-
"deepseek-reasoner": "deepseek_r1",
|
| 61 |
-
"deepseek-chat": "deepseek_v3",
|
| 62 |
-
"o3-mini-high": "openai_o3_mini_high",
|
| 63 |
-
"o3-mini-medium": "openai_o3_mini_medium",
|
| 64 |
-
"o1": "openai_o1",
|
| 65 |
-
"o1-mini": "openai_o1_mini",
|
| 66 |
-
"o1-preview": "openai_o1_preview",
|
| 67 |
-
"gpt-4o": "gpt_4o",
|
| 68 |
-
"gpt-4o-mini": "gpt_4o_mini",
|
| 69 |
-
"gpt-4-turbo": "gpt_4_turbo",
|
| 70 |
-
"gpt-3.5-turbo": "gpt_3.5",
|
| 71 |
-
"claude-3-opus": "claude_3_opus",
|
| 72 |
-
"claude-3-sonnet": "claude_3_sonnet",
|
| 73 |
-
"claude-3.5-sonnet": "claude_3_5_sonnet",
|
| 74 |
-
"claude-3.5-haiku": "claude_3_5_haiku",
|
| 75 |
-
"gemini-1.5-pro": "gemini_1_5_pro",
|
| 76 |
-
"gemini-1.5-flash": "gemini_1_5_flash",
|
| 77 |
-
"llama-3.2-90b": "llama3_2_90b",
|
| 78 |
-
"llama-3.1-405b": "llama3_1_405b",
|
| 79 |
-
"mistral-large-2": "mistral_large_2",
|
| 80 |
-
"qwen-2.5-72b": "qwen2p5_72b",
|
| 81 |
-
"qwen-2.5-coder-32b": "qwen2p5_coder_32b",
|
| 82 |
-
"command-r-plus": "command_r_plus",
|
| 83 |
-
}
|
| 84 |
-
|
| 85 |
-
func getReverseModelMap() map[string]string {
|
| 86 |
-
reverse := make(map[string]string, len(modelMap))
|
| 87 |
-
for k, v := range modelMap {
|
| 88 |
-
reverse[v] = k
|
| 89 |
-
}
|
| 90 |
-
return reverse
|
| 91 |
-
}
|
| 92 |
-
|
| 93 |
-
func mapModelName(openAIModel string) string {
|
| 94 |
-
if mappedModel, exists := modelMap[openAIModel]; exists {
|
| 95 |
-
return mappedModel
|
| 96 |
-
}
|
| 97 |
-
return "deepseek_v3"
|
| 98 |
-
}
|
| 99 |
-
|
| 100 |
-
func reverseMapModelName(youModel string) string {
|
| 101 |
-
reverseMap := getReverseModelMap()
|
| 102 |
-
if mappedModel, exists := reverseMap[youModel]; exists {
|
| 103 |
-
return mappedModel
|
| 104 |
-
}
|
| 105 |
-
return "deepseek-chat"
|
| 106 |
-
}
|
| 107 |
-
|
| 108 |
-
var originalModel string
|
| 109 |
-
|
| 110 |
-
func Handler(w http.ResponseWriter, r *http.Request) {
|
| 111 |
-
if r.URL.Path != "/v1/chat/completions" {
|
| 112 |
-
w.Header().Set("Content-Type", "application/json")
|
| 113 |
-
json.NewEncoder(w).Encode(map[string]string{
|
| 114 |
-
"status": "You2Api Service Running...",
|
| 115 |
-
"message": "MoLoveSze...",
|
| 116 |
-
})
|
| 117 |
-
return
|
| 118 |
-
}
|
| 119 |
-
|
| 120 |
-
w.Header().Set("Access-Control-Allow-Origin", "*")
|
| 121 |
-
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
|
| 122 |
-
w.Header().Set("Access-Control-Allow-Headers", "*")
|
| 123 |
-
|
| 124 |
-
if r.Method == "OPTIONS" {
|
| 125 |
-
w.WriteHeader(http.StatusOK)
|
| 126 |
-
return
|
| 127 |
-
}
|
| 128 |
-
|
| 129 |
-
authHeader := r.Header.Get("Authorization")
|
| 130 |
-
if !strings.HasPrefix(authHeader, "Bearer ") {
|
| 131 |
-
http.Error(w, "Missing or invalid authorization header", http.StatusUnauthorized)
|
| 132 |
-
return
|
| 133 |
-
}
|
| 134 |
-
dsToken := strings.TrimPrefix(authHeader, "Bearer ")
|
| 135 |
-
|
| 136 |
-
var openAIReq OpenAIRequest
|
| 137 |
-
if err := json.NewDecoder(r.Body).Decode(&openAIReq); err != nil {
|
| 138 |
-
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
| 139 |
-
return
|
| 140 |
-
}
|
| 141 |
-
|
| 142 |
-
originalModel = openAIReq.Model
|
| 143 |
-
lastMessage := openAIReq.Messages[len(openAIReq.Messages)-1].Content
|
| 144 |
-
var chatHistory []map[string]interface{}
|
| 145 |
-
for _, msg := range openAIReq.Messages {
|
| 146 |
-
chatMsg := map[string]interface{}{
|
| 147 |
-
"question": msg.Content,
|
| 148 |
-
"answer": "",
|
| 149 |
-
}
|
| 150 |
-
if msg.Role == "assistant" {
|
| 151 |
-
chatMsg["question"] = ""
|
| 152 |
-
chatMsg["answer"] = msg.Content
|
| 153 |
-
}
|
| 154 |
-
chatHistory = append(chatHistory, chatMsg)
|
| 155 |
-
}
|
| 156 |
-
|
| 157 |
-
chatHistoryJSON, _ := json.Marshal(chatHistory)
|
| 158 |
-
|
| 159 |
-
youReq, _ := http.NewRequest("GET", "https://you.com/api/streamingSearch", nil)
|
| 160 |
-
|
| 161 |
-
q := youReq.URL.Query()
|
| 162 |
-
q.Add("q", lastMessage)
|
| 163 |
-
q.Add("page", "1")
|
| 164 |
-
q.Add("count", "10")
|
| 165 |
-
q.Add("safeSearch", "Moderate")
|
| 166 |
-
q.Add("mkt", "zh-HK")
|
| 167 |
-
q.Add("enable_worklow_generation_ux", "true")
|
| 168 |
-
q.Add("domain", "youchat")
|
| 169 |
-
q.Add("use_personalization_extraction", "true")
|
| 170 |
-
q.Add("pastChatLength", fmt.Sprintf("%d", len(chatHistory)-1))
|
| 171 |
-
q.Add("selectedChatMode", "custom")
|
| 172 |
-
q.Add("selectedAiModel", mapModelName(openAIReq.Model))
|
| 173 |
-
q.Add("enable_agent_clarification_questions", "true")
|
| 174 |
-
q.Add("use_nested_youchat_updates", "true")
|
| 175 |
-
q.Add("chat", string(chatHistoryJSON))
|
| 176 |
-
youReq.URL.RawQuery = q.Encode()
|
| 177 |
-
|
| 178 |
-
youReq.Header = http.Header{
|
| 179 |
-
"sec-ch-ua-platform": {"Windows"},
|
| 180 |
-
"Cache-Control": {"no-cache"},
|
| 181 |
-
"sec-ch-ua": {`"Not(A:Brand";v="99", "Microsoft Edge";v="133", "Chromium";v="133"`},
|
| 182 |
-
"sec-ch-ua-bitness": {"64"},
|
| 183 |
-
"sec-ch-ua-model": {""},
|
| 184 |
-
"sec-ch-ua-mobile": {"?0"},
|
| 185 |
-
"sec-ch-ua-arch": {"x86"},
|
| 186 |
-
"sec-ch-ua-full-version": {"133.0.3065.39"},
|
| 187 |
-
"Accept": {"text/event-stream"},
|
| 188 |
-
"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"},
|
| 189 |
-
"sec-ch-ua-platform-version": {"19.0.0"},
|
| 190 |
-
"Sec-Fetch-Site": {"same-origin"},
|
| 191 |
-
"Sec-Fetch-Mode": {"cors"},
|
| 192 |
-
"Sec-Fetch-Dest": {"empty"},
|
| 193 |
-
"Host": {"you.com"},
|
| 194 |
-
}
|
| 195 |
-
|
| 196 |
-
cookies := getCookies(dsToken)
|
| 197 |
-
var cookieStrings []string
|
| 198 |
-
for name, value := range cookies {
|
| 199 |
-
cookieStrings = append(cookieStrings, fmt.Sprintf("%s=%s", name, value))
|
| 200 |
-
}
|
| 201 |
-
youReq.Header.Add("Cookie", strings.Join(cookieStrings, ";"))
|
| 202 |
-
|
| 203 |
-
if !openAIReq.Stream {
|
| 204 |
-
handleNonStreamingResponse(w, youReq)
|
| 205 |
-
return
|
| 206 |
-
}
|
| 207 |
-
|
| 208 |
-
handleStreamingResponse(w, youReq)
|
| 209 |
-
}
|
| 210 |
-
|
| 211 |
-
func getCookies(dsToken string) map[string]string {
|
| 212 |
-
return map[string]string{
|
| 213 |
-
"guest_has_seen_legal_disclaimer": "true",
|
| 214 |
-
"youchat_personalization": "true",
|
| 215 |
-
"DS": dsToken,
|
| 216 |
-
"you_subscription": "youpro_standard_year",
|
| 217 |
-
"youpro_subscription": "true",
|
| 218 |
-
"ai_model": "deepseek_r1",
|
| 219 |
-
"youchat_smart_learn": "true",
|
| 220 |
-
}
|
| 221 |
-
}
|
| 222 |
-
|
| 223 |
-
func handleNonStreamingResponse(w http.ResponseWriter, youReq *http.Request) {
|
| 224 |
-
client := &http.Client{
|
| 225 |
-
Timeout: 60 * time.Second,
|
| 226 |
-
}
|
| 227 |
-
resp, err := client.Do(youReq)
|
| 228 |
-
if err != nil {
|
| 229 |
-
http.Error(w, err.Error(), http.StatusInternalServerError)
|
| 230 |
-
return
|
| 231 |
-
}
|
| 232 |
-
defer resp.Body.Close()
|
| 233 |
-
|
| 234 |
-
var fullResponse strings.Builder
|
| 235 |
-
scanner := bufio.NewScanner(resp.Body)
|
| 236 |
-
|
| 237 |
-
buf := make([]byte, 0, 64*1024)
|
| 238 |
-
scanner.Buffer(buf, 1024*1024)
|
| 239 |
-
|
| 240 |
-
for scanner.Scan() {
|
| 241 |
-
line := scanner.Text()
|
| 242 |
-
if strings.HasPrefix(line, "event: youChatToken") {
|
| 243 |
-
scanner.Scan()
|
| 244 |
-
data := scanner.Text()
|
| 245 |
-
if !strings.HasPrefix(data, "data: ") {
|
| 246 |
-
continue
|
| 247 |
-
}
|
| 248 |
-
var token YouChatResponse
|
| 249 |
-
if err := json.Unmarshal([]byte(strings.TrimPrefix(data, "data: ")), &token); err != nil {
|
| 250 |
-
continue
|
| 251 |
-
}
|
| 252 |
-
fullResponse.WriteString(token.YouChatToken)
|
| 253 |
-
}
|
| 254 |
-
}
|
| 255 |
-
|
| 256 |
-
if scanner.Err() != nil {
|
| 257 |
-
http.Error(w, "Error reading response", http.StatusInternalServerError)
|
| 258 |
-
return
|
| 259 |
-
}
|
| 260 |
-
|
| 261 |
-
openAIResp := OpenAIResponse{
|
| 262 |
-
ID: "chatcmpl-" + fmt.Sprintf("%d", time.Now().Unix()),
|
| 263 |
-
Object: "chat.completion",
|
| 264 |
-
Created: time.Now().Unix(),
|
| 265 |
-
Model: reverseMapModelName(mapModelName(originalModel)),
|
| 266 |
-
Choices: []OpenAIChoice{
|
| 267 |
-
{
|
| 268 |
-
Message: Message{
|
| 269 |
-
Role: "assistant",
|
| 270 |
-
Content: fullResponse.String(),
|
| 271 |
-
},
|
| 272 |
-
Index: 0,
|
| 273 |
-
FinishReason: "stop",
|
| 274 |
-
},
|
| 275 |
-
},
|
| 276 |
-
}
|
| 277 |
-
|
| 278 |
-
w.Header().Set("Content-Type", "application/json")
|
| 279 |
-
if err := json.NewEncoder(w).Encode(openAIResp); err != nil {
|
| 280 |
-
http.Error(w, "Error encoding response", http.StatusInternalServerError)
|
| 281 |
-
return
|
| 282 |
-
}
|
| 283 |
-
}
|
| 284 |
-
|
| 285 |
-
func handleStreamingResponse(w http.ResponseWriter, youReq *http.Request) {
|
| 286 |
-
client := &http.Client{}
|
| 287 |
-
resp, err := client.Do(youReq)
|
| 288 |
-
if err != nil {
|
| 289 |
-
http.Error(w, err.Error(), http.StatusInternalServerError)
|
| 290 |
-
return
|
| 291 |
-
}
|
| 292 |
-
defer resp.Body.Close()
|
| 293 |
-
|
| 294 |
-
w.Header().Set("Content-Type", "text/event-stream")
|
| 295 |
-
w.Header().Set("Cache-Control", "no-cache")
|
| 296 |
-
w.Header().Set("Connection", "keep-alive")
|
| 297 |
-
|
| 298 |
-
scanner := bufio.NewScanner(resp.Body)
|
| 299 |
-
for scanner.Scan() {
|
| 300 |
-
line := scanner.Text()
|
| 301 |
-
|
| 302 |
-
if strings.HasPrefix(line, "event: youChatToken") {
|
| 303 |
-
scanner.Scan()
|
| 304 |
-
data := scanner.Text()
|
| 305 |
-
|
| 306 |
-
var token YouChatResponse
|
| 307 |
-
json.Unmarshal([]byte(strings.TrimPrefix(data, "data: ")), &token)
|
| 308 |
-
|
| 309 |
-
openAIResp := OpenAIStreamResponse{
|
| 310 |
-
ID: "chatcmpl-" + fmt.Sprintf("%d", time.Now().Unix()),
|
| 311 |
-
Object: "chat.completion.chunk",
|
| 312 |
-
Created: time.Now().Unix(),
|
| 313 |
-
Model: reverseMapModelName(mapModelName(originalModel)),
|
| 314 |
-
Choices: []Choice{
|
| 315 |
-
{
|
| 316 |
-
Delta: Delta{
|
| 317 |
-
Content: token.YouChatToken,
|
| 318 |
-
},
|
| 319 |
-
Index: 0,
|
| 320 |
-
FinishReason: "",
|
| 321 |
-
},
|
| 322 |
-
},
|
| 323 |
-
}
|
| 324 |
-
|
| 325 |
-
respBytes, _ := json.Marshal(openAIResp)
|
| 326 |
-
fmt.Fprintf(w, "data: %s\n\n", string(respBytes))
|
| 327 |
-
w.(http.Flusher).Flush()
|
| 328 |
-
}
|
| 329 |
-
}
|
| 330 |
-
}
|
|
|
|
| 1 |
+
package main
|
| 2 |
+
|
| 3 |
+
import (
|
| 4 |
+
"bufio"
|
| 5 |
+
"encoding/json"
|
| 6 |
+
"fmt"
|
| 7 |
+
"net/http"
|
| 8 |
+
"strings"
|
| 9 |
+
"time"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
type YouChatResponse struct {
|
| 13 |
+
YouChatToken string `json:"youChatToken"`
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
type OpenAIStreamResponse struct {
|
| 17 |
+
ID string `json:"id"`
|
| 18 |
+
Object string `json:"object"`
|
| 19 |
+
Created int64 `json:"created"`
|
| 20 |
+
Model string `json:"model"`
|
| 21 |
+
Choices []Choice `json:"choices"`
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
type Choice struct {
|
| 25 |
+
Delta Delta `json:"delta"`
|
| 26 |
+
Index int `json:"index"`
|
| 27 |
+
FinishReason string `json:"finish_reason"`
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
type Delta struct {
|
| 31 |
+
Content string `json:"content"`
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
type OpenAIRequest struct {
|
| 35 |
+
Messages []Message `json:"messages"`
|
| 36 |
+
Stream bool `json:"stream"`
|
| 37 |
+
Model string `json:"model"`
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
type Message struct {
|
| 41 |
+
Role string `json:"role"`
|
| 42 |
+
Content string `json:"content"`
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
type OpenAIResponse struct {
|
| 46 |
+
ID string `json:"id"`
|
| 47 |
+
Object string `json:"object"`
|
| 48 |
+
Created int64 `json:"created"`
|
| 49 |
+
Model string `json:"model"`
|
| 50 |
+
Choices []OpenAIChoice `json:"choices"`
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
type OpenAIChoice struct {
|
| 54 |
+
Message Message `json:"message"`
|
| 55 |
+
Index int `json:"index"`
|
| 56 |
+
FinishReason string `json:"finish_reason"`
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
var modelMap = map[string]string{
|
| 60 |
+
"deepseek-reasoner": "deepseek_r1",
|
| 61 |
+
"deepseek-chat": "deepseek_v3",
|
| 62 |
+
"o3-mini-high": "openai_o3_mini_high",
|
| 63 |
+
"o3-mini-medium": "openai_o3_mini_medium",
|
| 64 |
+
"o1": "openai_o1",
|
| 65 |
+
"o1-mini": "openai_o1_mini",
|
| 66 |
+
"o1-preview": "openai_o1_preview",
|
| 67 |
+
"gpt-4o": "gpt_4o",
|
| 68 |
+
"gpt-4o-mini": "gpt_4o_mini",
|
| 69 |
+
"gpt-4-turbo": "gpt_4_turbo",
|
| 70 |
+
"gpt-3.5-turbo": "gpt_3.5",
|
| 71 |
+
"claude-3-opus": "claude_3_opus",
|
| 72 |
+
"claude-3-sonnet": "claude_3_sonnet",
|
| 73 |
+
"claude-3.5-sonnet": "claude_3_5_sonnet",
|
| 74 |
+
"claude-3.5-haiku": "claude_3_5_haiku",
|
| 75 |
+
"gemini-1.5-pro": "gemini_1_5_pro",
|
| 76 |
+
"gemini-1.5-flash": "gemini_1_5_flash",
|
| 77 |
+
"llama-3.2-90b": "llama3_2_90b",
|
| 78 |
+
"llama-3.1-405b": "llama3_1_405b",
|
| 79 |
+
"mistral-large-2": "mistral_large_2",
|
| 80 |
+
"qwen-2.5-72b": "qwen2p5_72b",
|
| 81 |
+
"qwen-2.5-coder-32b": "qwen2p5_coder_32b",
|
| 82 |
+
"command-r-plus": "command_r_plus",
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
func getReverseModelMap() map[string]string {
|
| 86 |
+
reverse := make(map[string]string, len(modelMap))
|
| 87 |
+
for k, v := range modelMap {
|
| 88 |
+
reverse[v] = k
|
| 89 |
+
}
|
| 90 |
+
return reverse
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
func mapModelName(openAIModel string) string {
|
| 94 |
+
if mappedModel, exists := modelMap[openAIModel]; exists {
|
| 95 |
+
return mappedModel
|
| 96 |
+
}
|
| 97 |
+
return "deepseek_v3"
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
func reverseMapModelName(youModel string) string {
|
| 101 |
+
reverseMap := getReverseModelMap()
|
| 102 |
+
if mappedModel, exists := reverseMap[youModel]; exists {
|
| 103 |
+
return mappedModel
|
| 104 |
+
}
|
| 105 |
+
return "deepseek-chat"
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
var originalModel string
|
| 109 |
+
|
| 110 |
+
func Handler(w http.ResponseWriter, r *http.Request) {
|
| 111 |
+
if r.URL.Path != "/v1/chat/completions" {
|
| 112 |
+
w.Header().Set("Content-Type", "application/json")
|
| 113 |
+
json.NewEncoder(w).Encode(map[string]string{
|
| 114 |
+
"status": "You2Api Service Running...",
|
| 115 |
+
"message": "MoLoveSze...",
|
| 116 |
+
})
|
| 117 |
+
return
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
w.Header().Set("Access-Control-Allow-Origin", "*")
|
| 121 |
+
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
|
| 122 |
+
w.Header().Set("Access-Control-Allow-Headers", "*")
|
| 123 |
+
|
| 124 |
+
if r.Method == "OPTIONS" {
|
| 125 |
+
w.WriteHeader(http.StatusOK)
|
| 126 |
+
return
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
authHeader := r.Header.Get("Authorization")
|
| 130 |
+
if !strings.HasPrefix(authHeader, "Bearer ") {
|
| 131 |
+
http.Error(w, "Missing or invalid authorization header", http.StatusUnauthorized)
|
| 132 |
+
return
|
| 133 |
+
}
|
| 134 |
+
dsToken := strings.TrimPrefix(authHeader, "Bearer ")
|
| 135 |
+
|
| 136 |
+
var openAIReq OpenAIRequest
|
| 137 |
+
if err := json.NewDecoder(r.Body).Decode(&openAIReq); err != nil {
|
| 138 |
+
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
| 139 |
+
return
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
originalModel = openAIReq.Model
|
| 143 |
+
lastMessage := openAIReq.Messages[len(openAIReq.Messages)-1].Content
|
| 144 |
+
var chatHistory []map[string]interface{}
|
| 145 |
+
for _, msg := range openAIReq.Messages {
|
| 146 |
+
chatMsg := map[string]interface{}{
|
| 147 |
+
"question": msg.Content,
|
| 148 |
+
"answer": "",
|
| 149 |
+
}
|
| 150 |
+
if msg.Role == "assistant" {
|
| 151 |
+
chatMsg["question"] = ""
|
| 152 |
+
chatMsg["answer"] = msg.Content
|
| 153 |
+
}
|
| 154 |
+
chatHistory = append(chatHistory, chatMsg)
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
chatHistoryJSON, _ := json.Marshal(chatHistory)
|
| 158 |
+
|
| 159 |
+
youReq, _ := http.NewRequest("GET", "https://you.com/api/streamingSearch", nil)
|
| 160 |
+
|
| 161 |
+
q := youReq.URL.Query()
|
| 162 |
+
q.Add("q", lastMessage)
|
| 163 |
+
q.Add("page", "1")
|
| 164 |
+
q.Add("count", "10")
|
| 165 |
+
q.Add("safeSearch", "Moderate")
|
| 166 |
+
q.Add("mkt", "zh-HK")
|
| 167 |
+
q.Add("enable_worklow_generation_ux", "true")
|
| 168 |
+
q.Add("domain", "youchat")
|
| 169 |
+
q.Add("use_personalization_extraction", "true")
|
| 170 |
+
q.Add("pastChatLength", fmt.Sprintf("%d", len(chatHistory)-1))
|
| 171 |
+
q.Add("selectedChatMode", "custom")
|
| 172 |
+
q.Add("selectedAiModel", mapModelName(openAIReq.Model))
|
| 173 |
+
q.Add("enable_agent_clarification_questions", "true")
|
| 174 |
+
q.Add("use_nested_youchat_updates", "true")
|
| 175 |
+
q.Add("chat", string(chatHistoryJSON))
|
| 176 |
+
youReq.URL.RawQuery = q.Encode()
|
| 177 |
+
|
| 178 |
+
youReq.Header = http.Header{
|
| 179 |
+
"sec-ch-ua-platform": {"Windows"},
|
| 180 |
+
"Cache-Control": {"no-cache"},
|
| 181 |
+
"sec-ch-ua": {`"Not(A:Brand";v="99", "Microsoft Edge";v="133", "Chromium";v="133"`},
|
| 182 |
+
"sec-ch-ua-bitness": {"64"},
|
| 183 |
+
"sec-ch-ua-model": {""},
|
| 184 |
+
"sec-ch-ua-mobile": {"?0"},
|
| 185 |
+
"sec-ch-ua-arch": {"x86"},
|
| 186 |
+
"sec-ch-ua-full-version": {"133.0.3065.39"},
|
| 187 |
+
"Accept": {"text/event-stream"},
|
| 188 |
+
"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"},
|
| 189 |
+
"sec-ch-ua-platform-version": {"19.0.0"},
|
| 190 |
+
"Sec-Fetch-Site": {"same-origin"},
|
| 191 |
+
"Sec-Fetch-Mode": {"cors"},
|
| 192 |
+
"Sec-Fetch-Dest": {"empty"},
|
| 193 |
+
"Host": {"you.com"},
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
cookies := getCookies(dsToken)
|
| 197 |
+
var cookieStrings []string
|
| 198 |
+
for name, value := range cookies {
|
| 199 |
+
cookieStrings = append(cookieStrings, fmt.Sprintf("%s=%s", name, value))
|
| 200 |
+
}
|
| 201 |
+
youReq.Header.Add("Cookie", strings.Join(cookieStrings, ";"))
|
| 202 |
+
|
| 203 |
+
if !openAIReq.Stream {
|
| 204 |
+
handleNonStreamingResponse(w, youReq)
|
| 205 |
+
return
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
handleStreamingResponse(w, youReq)
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
func getCookies(dsToken string) map[string]string {
|
| 212 |
+
return map[string]string{
|
| 213 |
+
"guest_has_seen_legal_disclaimer": "true",
|
| 214 |
+
"youchat_personalization": "true",
|
| 215 |
+
"DS": dsToken,
|
| 216 |
+
"you_subscription": "youpro_standard_year",
|
| 217 |
+
"youpro_subscription": "true",
|
| 218 |
+
"ai_model": "deepseek_r1",
|
| 219 |
+
"youchat_smart_learn": "true",
|
| 220 |
+
}
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
func handleNonStreamingResponse(w http.ResponseWriter, youReq *http.Request) {
|
| 224 |
+
client := &http.Client{
|
| 225 |
+
Timeout: 60 * time.Second,
|
| 226 |
+
}
|
| 227 |
+
resp, err := client.Do(youReq)
|
| 228 |
+
if err != nil {
|
| 229 |
+
http.Error(w, err.Error(), http.StatusInternalServerError)
|
| 230 |
+
return
|
| 231 |
+
}
|
| 232 |
+
defer resp.Body.Close()
|
| 233 |
+
|
| 234 |
+
var fullResponse strings.Builder
|
| 235 |
+
scanner := bufio.NewScanner(resp.Body)
|
| 236 |
+
|
| 237 |
+
buf := make([]byte, 0, 64*1024)
|
| 238 |
+
scanner.Buffer(buf, 1024*1024)
|
| 239 |
+
|
| 240 |
+
for scanner.Scan() {
|
| 241 |
+
line := scanner.Text()
|
| 242 |
+
if strings.HasPrefix(line, "event: youChatToken") {
|
| 243 |
+
scanner.Scan()
|
| 244 |
+
data := scanner.Text()
|
| 245 |
+
if !strings.HasPrefix(data, "data: ") {
|
| 246 |
+
continue
|
| 247 |
+
}
|
| 248 |
+
var token YouChatResponse
|
| 249 |
+
if err := json.Unmarshal([]byte(strings.TrimPrefix(data, "data: ")), &token); err != nil {
|
| 250 |
+
continue
|
| 251 |
+
}
|
| 252 |
+
fullResponse.WriteString(token.YouChatToken)
|
| 253 |
+
}
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
if scanner.Err() != nil {
|
| 257 |
+
http.Error(w, "Error reading response", http.StatusInternalServerError)
|
| 258 |
+
return
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
openAIResp := OpenAIResponse{
|
| 262 |
+
ID: "chatcmpl-" + fmt.Sprintf("%d", time.Now().Unix()),
|
| 263 |
+
Object: "chat.completion",
|
| 264 |
+
Created: time.Now().Unix(),
|
| 265 |
+
Model: reverseMapModelName(mapModelName(originalModel)),
|
| 266 |
+
Choices: []OpenAIChoice{
|
| 267 |
+
{
|
| 268 |
+
Message: Message{
|
| 269 |
+
Role: "assistant",
|
| 270 |
+
Content: fullResponse.String(),
|
| 271 |
+
},
|
| 272 |
+
Index: 0,
|
| 273 |
+
FinishReason: "stop",
|
| 274 |
+
},
|
| 275 |
+
},
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
w.Header().Set("Content-Type", "application/json")
|
| 279 |
+
if err := json.NewEncoder(w).Encode(openAIResp); err != nil {
|
| 280 |
+
http.Error(w, "Error encoding response", http.StatusInternalServerError)
|
| 281 |
+
return
|
| 282 |
+
}
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
func handleStreamingResponse(w http.ResponseWriter, youReq *http.Request) {
|
| 286 |
+
client := &http.Client{}
|
| 287 |
+
resp, err := client.Do(youReq)
|
| 288 |
+
if err != nil {
|
| 289 |
+
http.Error(w, err.Error(), http.StatusInternalServerError)
|
| 290 |
+
return
|
| 291 |
+
}
|
| 292 |
+
defer resp.Body.Close()
|
| 293 |
+
|
| 294 |
+
w.Header().Set("Content-Type", "text/event-stream")
|
| 295 |
+
w.Header().Set("Cache-Control", "no-cache")
|
| 296 |
+
w.Header().Set("Connection", "keep-alive")
|
| 297 |
+
|
| 298 |
+
scanner := bufio.NewScanner(resp.Body)
|
| 299 |
+
for scanner.Scan() {
|
| 300 |
+
line := scanner.Text()
|
| 301 |
+
|
| 302 |
+
if strings.HasPrefix(line, "event: youChatToken") {
|
| 303 |
+
scanner.Scan()
|
| 304 |
+
data := scanner.Text()
|
| 305 |
+
|
| 306 |
+
var token YouChatResponse
|
| 307 |
+
json.Unmarshal([]byte(strings.TrimPrefix(data, "data: ")), &token)
|
| 308 |
+
|
| 309 |
+
openAIResp := OpenAIStreamResponse{
|
| 310 |
+
ID: "chatcmpl-" + fmt.Sprintf("%d", time.Now().Unix()),
|
| 311 |
+
Object: "chat.completion.chunk",
|
| 312 |
+
Created: time.Now().Unix(),
|
| 313 |
+
Model: reverseMapModelName(mapModelName(originalModel)),
|
| 314 |
+
Choices: []Choice{
|
| 315 |
+
{
|
| 316 |
+
Delta: Delta{
|
| 317 |
+
Content: token.YouChatToken,
|
| 318 |
+
},
|
| 319 |
+
Index: 0,
|
| 320 |
+
FinishReason: "",
|
| 321 |
+
},
|
| 322 |
+
},
|
| 323 |
+
}
|
| 324 |
+
|
| 325 |
+
respBytes, _ := json.Marshal(openAIResp)
|
| 326 |
+
fmt.Fprintf(w, "data: %s\n\n", string(respBytes))
|
| 327 |
+
w.(http.Flusher).Flush()
|
| 328 |
+
}
|
| 329 |
+
}
|
| 330 |
+
}
|