File size: 10,298 Bytes
8d83cb6
 
 
483a113
8d83cb6
 
 
 
 
 
 
483a113
8d83cb6
 
483a113
8d83cb6
 
 
 
 
483a113
8d83cb6
 
 
 
 
483a113
8d83cb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
483a113
 
 
 
 
 
8d83cb6
483a113
 
 
 
 
 
 
 
 
 
 
 
 
 
7eba146
483a113
 
8d83cb6
483a113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4fc749
 
 
 
 
483a113
f4fc749
483a113
 
 
 
 
 
 
 
 
 
f4fc749
483a113
f4fc749
483a113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d83cb6
483a113
8d83cb6
483a113
 
8d83cb6
483a113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7eba146
483a113
 
 
 
 
8d83cb6
483a113
 
 
 
8d83cb6
483a113
7eba146
8d83cb6
 
 
483a113
 
 
8d83cb6
483a113
8d83cb6
483a113
 
 
7eba146
483a113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d83cb6
 
483a113
8d83cb6
483a113
7eba146
483a113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d83cb6
483a113
 
 
 
 
8d83cb6
 
483a113
8d83cb6
 
483a113
8d83cb6
 
483a113
 
80c9037
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
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import JSONResponse, StreamingResponse
from fastapi.middleware.cors import CORSMiddleware
import requests
import json
import os
import time
import asyncio

app = FastAPI()

# CORS settings
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Allow all origins. In production, change this to specific domains.
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Environment variable configuration
STATUS_URL = os.environ.get("STATUS_URL", "https://duckduckgo.com/duckchat/v1/status")
CHAT_URL = os.environ.get("CHAT_URL", "https://duckduckgo.com/duckchat/v1/chat")
REFERER = os.environ.get("REFERER", "https://duckduckgo.com/")
ORIGIN = os.environ.get("ORIGIN", "https://duckduckgo.com")
USER_AGENT = os.environ.get("USER_AGENT", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36")
COOKIE = os.environ.get("COOKIE", "dcm=3; s=l; bf=1")  # Get cookie from environment variable

DEFAULT_HEADERS = {
    "User-Agent": USER_AGENT,
    "Accept": "text/event-stream",
    "Accept-Language": "en-US,en;q=0.5",
    "Referer": REFERER,
    "Content-Type": "application/json",
    "Origin": ORIGIN,
    "Connection": "keep-alive",
    "Cookie": COOKIE,
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-origin",
    "Pragma": "no-cache",
    "TE": "trailers",
}

SUPPORTED_MODELS = [
    "o3-mini",
    "gpt-4o-mini",
    "claude-3-haiku-20240307",
    "meta-llama/Llama-3.3-70B-Instruct-Turbo",
]

async def get_vqd():
    """Get the VQD value for DuckDuckGo Chat."""
    headers = {**DEFAULT_HEADERS, "x-vqd-accept": "1"}
    try:
        response = requests.get(STATUS_URL, headers=headers)
        response.raise_for_status()  # Raise HTTPError if status code is not 200
        vqd = response.headers.get("x-vqd-4")
        if not vqd:
            raise ValueError("x-vqd-4 header not found in the response.")
        return vqd
    except requests.exceptions.RequestException as e:
        raise HTTPException(status_code=500, detail=f"HTTP request failed: {e}")
    except ValueError as e:
        raise HTTPException(status_code=500, detail=str(e))

async def duckduckgo_chat_stream(model, messages):
    """Interact with DuckDuckGo Chat with streaming output."""
    try:
        x_vqd_4 = await get_vqd()

        chat_headers = {
            **DEFAULT_HEADERS,
            "x-vqd-4": x_vqd_4,
            "Accept": "text/event-stream",  # Ensure we accept SSE
        }

        body = json.dumps({
            "model": model,
            "messages": messages,
        })

        response = requests.post(CHAT_URL, headers=chat_headers, data=body, stream=True)
        response.raise_for_status()

        async def event_stream():
            try:
                for line in response.iter_lines():
                    if line:
                        decoded_line = line.decode('utf-8')
                        if decoded_line.startswith("data: "):
                            content = decoded_line[5:].strip()
                            # Check if this is the final marker
                            if content == "[DONE]":
                                yield f"data: [DONE]\n\n"
                                break
                            try:
                                json_data = json.loads(content)
                                message_content = json_data.get("message", "")
                                if message_content:
                                    # Build OpenAI style streaming response
                                    openai_stream_response = {
                                        "id": f"chatcmpl-{int(time.time() * 1000)}",
                                        "object": "chat.completion.chunk",
                                        "created": int(time.time()),
                                        "model": model,
                                        "choices": [
                                            {
                                                "delta": {"content": message_content},
                                                "index": 0,
                                                "finish_reason": None,
                                            }
                                        ],
                                    }
                                    yield f"data: {json.dumps(openai_stream_response)}\n\n"
                                await asyncio.sleep(0.01)  # Prevent high CPU usage
                            except json.JSONDecodeError as e:
                                print(f"JSON decode error: {e}, line: {decoded_line}")
                                yield f"data: {json.dumps({'error': 'JSON decode error'})}\n\n"
                                break  # Stop the stream
            except requests.exceptions.RequestException as e:
                print(f"Request error: {e}")
                yield f"data: {json.dumps({'error': 'Request error'})}\n\n"
            except Exception as e:
                print(f"An error occurred: {e}")
                yield f"data: {json.dumps({'error': 'An error occurred'})}\n\n"
            finally:
                yield "data: [DONE]\n\n"  # End SSE stream

        return StreamingResponse(event_stream(), media_type="text/event-stream")

    except requests.exceptions.RequestException as e:
        raise HTTPException(status_code=500, detail=f"HTTP request failed: {e}")
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error during chat: {e}")

async def duckduckgo_chat_non_stream(model, messages):
    """Interact with DuckDuckGo Chat without streaming output."""
    try:
        x_vqd_4 = await get_vqd()

        chat_headers = {
            **DEFAULT_HEADERS,
            "x-vqd-4": x_vqd_4,
        }

        body = json.dumps({
            "model": model,
            "messages": messages,
        })

        response = requests.post(CHAT_URL, headers=chat_headers, data=body)
        response.raise_for_status()

        full_message = ""
        for line in response.iter_lines():
            if line:
                decoded_line = line.decode('utf-8')
                if decoded_line.startswith("data: "):
                    try:
                        json_data = json.loads(decoded_line[5:])
                        full_message += json_data.get("message", "")
                    except json.JSONDecodeError as e:
                        print(f"JSON decode error: {e}, line: {decoded_line}")
                        pass  # Ignore decoding errors

        return full_message

    except requests.exceptions.RequestException as e:
        raise HTTPException(status_code=500, detail=f"HTTP request failed: {e}")
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error during chat: {e}")

@app.post("/v1/chat/completions")
async def chat_completions(request: Request):
    try:
        body = await request.json()
        if not body:
            raise HTTPException(status_code=400, detail="Invalid request body")

        model = body.get("model", "o3-mini")
        if model not in SUPPORTED_MODELS:
            raise HTTPException(
                status_code=400,
                detail=f"Model \"{model}\" is not supported. Supported models are: {', '.join(SUPPORTED_MODELS)}."
            )

        messages = body.get("messages")
        if not messages:
            raise HTTPException(status_code=400, detail="No message content provided")

        stream = body.get("stream", False)  # Get the stream parameter, default is False

        # Process system message
        system_message = next((msg for msg in messages if msg.get("role") == "system"), None)
        system_prompt = f"You will play the role of a {system_message['content']}.\n" if system_message else ""

        # Extract and format the conversation history
        history_messages = "\n".join(
            f"{msg['role']}: {msg['content']}"
            for msg in messages
            if msg.get("role") != "system" and msg != messages[-1]
        )

        # Get the last user message
        last_user_message = messages[-1]
        current_question = last_user_message["content"] if last_user_message.get("role") == "user" else ""

        # Build the combined message
        combined_message_content = (
            f"{system_prompt}Below is the conversation history:\n{history_messages}\n"
            f"User's current question: {current_question}"
        )
        combined_message = {"role": "user", "content": combined_message_content}

        if stream:
            return await duckduckgo_chat_stream(model, [combined_message])
        else:
            response_text = await duckduckgo_chat_non_stream(model, [combined_message])

            # Build OpenAI style response
            openai_response = {
                "id": f"chatcmpl-{int(time.time() * 1000)}",  # Unique ID
                "object": "chat.completion",
                "created": int(time.time()),
                "model": model,
                "choices": [
                    {
                        "message": {
                            "role": "assistant",
                            "content": response_text,
                        },
                        "finish_reason": "stop",
                        "index": 0,
                    },
                ],
                "usage": {
                    "prompt_tokens": 0,
                    "completion_tokens": 0,
                    "total_tokens": 0
                },
            }

            return JSONResponse(content=openai_response)

    except HTTPException as e:
        raise e  # Re-raise HTTPException so FastAPI can handle it
    except Exception as e:
        print(f"API error: {e}")
        raise HTTPException(status_code=500, detail=f"Internal server error: {e}")

@app.exception_handler(HTTPException)
async def http_exception_handler(request: Request, exc: HTTPException):
    return JSONResponse(
        status_code=exc.status_code,
        content={"detail": exc.detail},
    )

@app.get("/")
async def greet_json():
    return {"Hello": "World!"}