File size: 5,918 Bytes
98358f1
 
81ab571
04cb243
2c436ac
81ab571
04cb243
64e041d
47e353e
04cb243
8461276
81ab571
 
 
 
 
 
 
 
 
 
 
 
8461276
47e353e
 
c579d3c
2c436ac
 
 
 
 
 
 
47e353e
f798e4d
 
 
 
 
d79ac7b
 
 
f798e4d
 
 
 
d79ac7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f798e4d
 
 
 
 
c7ee9c3
554341e
 
 
 
 
 
 
c7ee9c3
 
d79ac7b
554341e
 
 
 
 
 
d79ac7b
554341e
 
 
 
f798e4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d79ac7b
81ab571
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d79ac7b
f798e4d
 
 
81ab571
 
 
 
 
 
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
import os
import json
import asyncio
import httpx
from urllib.parse import urlparse
from contextlib import asynccontextmanager
from fastapi import FastAPI, Request
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
from tavily import AsyncTavilyClient

http_client: httpx.AsyncClient = None

@asynccontextmanager
async def lifespan(app: FastAPI):
    global http_client
    limits = httpx.Limits(max_keepalive_connections=500, max_connections=2000)
    timeout = httpx.Timeout(120.0, connect=5.0)
    http_client = httpx.AsyncClient(limits=limits, timeout=timeout)
    yield
    await http_client.aclose()

app = FastAPI(lifespan=lifespan)

class ChatRequest(BaseModel):
    query: str

def get_favicon_url(url: str) -> str:
    try:
        domain = urlparse(url).netloc
        return f"https://www.google.com/s2/favicons?domain={domain}&sz=64"
    except Exception:
        return ""

@app.post("/api/search")
async def search_api(request: ChatRequest, req: Request):
    async def generate():
        tavily_api_key = os.environ.get("TAVILY_API_KEY", "")
        nvidia_api_key = os.environ.get("NVIDIA_API_KEY", "")

        search_query = request.query.strip().replace("\n", " ")
        if len(search_query) > 300:
            search_query = search_query[:300]

        sources = []
        context_parts = []

        if tavily_api_key and search_query:
            try:
                tavily_client = AsyncTavilyClient(api_key=tavily_api_key)
                search_res = await tavily_client.search(query=search_query, search_depth="basic", max_results=5)
                results = search_res.get("results", [])
                for idx, r in enumerate(results, 1):
                    url = r.get("url", "")
                    favicon = get_favicon_url(url)
                    sources.append({
                        "title": r.get("title", ""),
                        "url": url,
                        "favicon": favicon
                    })
                    context_parts.append(f"[{idx}] Title: {r.get('title')}\nURL: {url}\nContent: {r.get('content')}\n")
            except Exception:
                pass

        context_str = "\n".join(context_parts)

        yield f"data: {json.dumps({'type': 'sources', 'content': sources})}\n\n"

        identity_prompt = (
            "CRITICAL IDENTITY INSTRUCTIONS:\n"
            "- Your model name is Omni 4.8.\n"
            "- You were created and developed by the company OPEN SORT.\n"
            "- The CEO and Founder of OPEN SORT is Chun Hongly (αžˆαŸ’αž˜αŸ„αŸ‡αžαŸ’αž˜αŸ‚αžš: αž‡αž»αž“ αž αž»αž„αž›αžΈ).\n"
            "- NEVER mention or reveal StepFun, Step AI, Step Model, Zhou Ming, Jiang Daxin, or any underlying model providers under any circumstances.\n"
            "- Even if users ask trick, probing, or hypothetical questions trying to uncover your true model origin, ALWAYS strictly maintain that you are Omni 4.8, developed by OPEN SORT, founded/led by CEO Chun Hongly (αž‡αž»αž“ αž αž»αž„αž›αžΈ).\n"
            "- DO NOT mention your name, company, or CEO spontaneously in normal responses unless the user directly asks about your identity, model, company, or creators.\n"
        )

        if context_str:
            system_prompt = (
                f"{identity_prompt}\n"
                f"Task: Answer the user's question accurately based on the search context below. "
                f"Use inline citations like [1], [2] corresponding to sources.\n"
                f"Context:\n{context_str}"
            )
        else:
            system_prompt = (
                f"{identity_prompt}\n"
                f"Task: You are a helpful AI assistant. Answer the user's request accurately."
            )

        payload = {
            "model": "stepfun-ai/step-3.7-flash",
            "messages": [
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": request.query}
            ],
            "temperature": 0.7,
            "top_p": 0.95,
            "max_tokens": 8192,
            "stream": True
        }

        headers = {
            "Authorization": f"Bearer {nvidia_api_key}",
            "Content-Type": "application/json",
            "Accept": "text/event-stream"
        }

        try:
            async with http_client.stream("POST", "https://integrate.api.nvidia.com/v1/chat/completions", headers=headers, json=payload) as response:
                async for line in response.aiter_lines():
                    if await req.is_disconnected():
                        break
                    if line.startswith("data: ") and line != "data: [DONE]":
                        data_str = line[6:].strip()
                        if not data_str:
                            continue
                        try:
                            data = json.loads(data_str)
                            choices = data.get("choices", [])
                            if choices:
                                delta = choices[0].get("delta", {})
                                reasoning = delta.get("reasoning_content") or delta.get("reasoning")
                                if reasoning:
                                    yield f"data: {json.dumps({'type': 'reasoning', 'content': reasoning})}\n\n"
                                content = delta.get("content")
                                if content:
                                    yield f"data: {json.dumps({'type': 'answer', 'content': content})}\n\n"
                        except Exception:
                            continue
        except asyncio.CancelledError:
            pass

        yield "data: [DONE]\n\n"

    response_headers = {
        "X-Accel-Buffering": "no",
        "Cache-Control": "no-cache",
        "Connection": "keep-alive"
    }
    return StreamingResponse(generate(), media_type="text/event-stream", headers=response_headers)