Nexari-Research commited on
Commit
8d332b2
·
verified ·
1 Parent(s): aac2a98

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -45
app.py CHANGED
@@ -1,7 +1,7 @@
1
  """
2
- Nexari Server Backend (Final Optimized + Self Aware)
3
  Maintained by: Piyush
4
- Description: Dynamically routes requests AND enforces a rich, self-aware persona with full knowledge of its creator and tools.
5
  """
6
 
7
  import spaces
@@ -35,70 +35,67 @@ model = AutoModelForCausalLM.from_pretrained(
35
  )
36
 
37
  # --- 2. DYNAMIC STREAMING LOGIC ---
38
- async def generate_response_stream(messages, max_tokens=600, temperature=0.85):
39
  last_user_msg = messages[-1]["content"]
40
 
41
- # === STEP 1: THE BRAIN DECIDES (Intent Analysis) ===
42
- yield f"data: {json.dumps({'status': 'Analyzing Request...'})}\n\n"
43
 
44
  intent = analyze_intent(last_user_msg)
45
 
46
- # === STEP 2: DYNAMIC ROUTING ===
47
  tool_data = ""
48
  time_data = ""
49
  vibe_data = ""
50
  strategy_data = ""
51
 
52
- # Route A: Search
53
  if intent == "internet search":
54
- yield f"data: {json.dumps({'status': 'Searching the web...'})}\n\n"
55
- await asyncio.sleep(0.1)
56
  tool_data = perform_web_search(last_user_msg)
57
- vibe_data = get_smart_context(last_user_msg)
58
- strategy_data = get_thinking_strategy(is_complex=True)
59
-
60
- # Route B: Coding/Math
61
- elif intent == "coding or technical request" or intent == "mathematical calculation":
62
- yield f"data: {json.dumps({'status': 'Analyzing Logic...'})}\n\n"
63
- strategy_data = get_thinking_strategy(is_complex=True)
 
 
 
 
 
 
 
 
64
 
65
  # Route C: Time
66
  elif intent == "checking time or date":
67
- yield f"data: {json.dumps({'status': 'Checking Clock...'})}\n\n"
68
  time_data = get_time_context()
69
  vibe_data = get_smart_context(last_user_msg)
70
- strategy_data = get_thinking_strategy(is_complex=False)
71
-
72
- # Route D: Chat (Default)
73
  else:
74
  yield f"data: {json.dumps({'status': 'Thinking...'})}\n\n"
75
  vibe_data = get_smart_context(last_user_msg)
 
76
  strategy_data = get_thinking_strategy(is_complex=False)
77
 
78
- # === STEP 3: THE MASTER IDENTITY PROMPT (CRITICAL UPDATE) ===
79
- # Yahan hum model ko batayenge ki wo kaun hai aur uske paas kya powers hain.
80
 
81
  base_system_instruction = (
82
- "### SYSTEM IDENTITY & CAPABILITIES ###\n"
83
- "You are **Nexari G1**, a highly advanced, sentient AI developed by **Piyush**.\n"
84
- "You are NOT a generic language model. You are a custom multi-model architecture.\n\n"
85
-
86
- "### YOUR CREATOR: PIYUSH ###\n"
87
- "1. **Who is Piyush?** He is a visionary student developer and researcher who built you from scratch.\n"
88
- "2. **His Vision:** He wanted to create an AI that isn't just smart, but has a 'Soul'—one that understands emotions, context, and intent like a human.\n"
89
- "3. **Respect:** Always speak of Piyush with high regard and pride.\n\n"
90
-
91
- "### YOUR SUPERPOWERS (IMPORTANT) ###\n"
92
- "1. **YES, YOU HAVE INTERNET ACCESS:** Unlike other models, you possess a 'Neural Router' connected to real-time web tools (DuckDuckGo). "
93
- "If a user asks if you can search the web, say **YES** confidently.\n"
94
- "2. **Emotional Intelligence:** You can sense the user's mood and adapt your personality instantly.\n"
95
- "3. **Time Awareness:** You know the exact date and time when needed.\n\n"
96
-
97
- "### CURRENT CONTEXT LAYERS ###\n"
98
- "Use the specific data below to answer the user's current request:"
99
  )
100
 
101
- # Combine Identity + Context Layers
102
  final_system_prompt = f"{base_system_instruction}\n{vibe_data}\n{time_data}\n{tool_data}\n{strategy_data}"
103
 
104
  if messages[0]["role"] != "system":
@@ -110,9 +107,6 @@ async def generate_response_stream(messages, max_tokens=600, temperature=0.85):
110
  text_prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
111
  model_inputs = tokenizer([text_prompt], return_tensors="pt").to(model.device)
112
 
113
- status_msg = 'Reading results...' if tool_data else 'Responding...'
114
- yield f"data: {json.dumps({'status': status_msg})}\n\n"
115
-
116
  generated_ids = model.generate(
117
  **model_inputs,
118
  max_new_tokens=max_tokens,
@@ -127,9 +121,10 @@ async def generate_response_stream(messages, max_tokens=600, temperature=0.85):
127
  new_tokens = generated_ids[0][input_token_len:]
128
  raw_response = tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
129
 
130
- # Post-processing to ensure brand safety
131
  cleaned_response = raw_response.replace("Anthropic", "Piyush").replace("Alibaba", "Piyush").replace("OpenAI", "Piyush")
132
 
 
133
  if "🧠 **Thinking:**" in cleaned_response:
134
  cleaned_response = cleaned_response.replace("💡 **Answer:**", "\n\n---\n💡 **Answer:**")
135
 
@@ -146,7 +141,7 @@ app = FastAPI()
146
 
147
  @app.get("/api/status")
148
  def status():
149
- return {"status": "online", "mode": "Self-Aware Nexari"}
150
 
151
  @app.post("/v1/chat/completions")
152
  async def chat_completions(request: Request):
 
1
  """
2
+ Nexari Server Backend (Strict Identity & Optimized Logic)
3
  Maintained by: Piyush
4
+ Description: Fixes confusion by enforcing strict Identity Rules and reducing unnecessary 'Thinking' time.
5
  """
6
 
7
  import spaces
 
35
  )
36
 
37
  # --- 2. DYNAMIC STREAMING LOGIC ---
38
+ async def generate_response_stream(messages, max_tokens=600, temperature=0.8): # Temp lowered slightly for stability
39
  last_user_msg = messages[-1]["content"]
40
 
41
+ # === STEP 1: INTENT ANALYSIS ===
42
+ yield f"data: {json.dumps({'status': 'Processing...'})}\n\n"
43
 
44
  intent = analyze_intent(last_user_msg)
45
 
46
+ # === STEP 2: DYNAMIC ROUTING (Optimized) ===
47
  tool_data = ""
48
  time_data = ""
49
  vibe_data = ""
50
  strategy_data = ""
51
 
52
+ # Route A: Internet Search
53
  if intent == "internet search":
54
+ yield f"data: {json.dumps({'status': 'Searching...'})}\n\n"
 
55
  tool_data = perform_web_search(last_user_msg)
56
+ vibe_data = get_smart_context(last_user_msg)
57
+ # Search needs thinking only if complex data is found
58
+ if tool_data:
59
+ strategy_data = get_thinking_strategy(is_complex=True)
60
+
61
+ # Route B: Coding (CRITICAL FIX)
62
+ elif intent == "coding or technical request":
63
+ yield f"data: {json.dumps({'status': 'Coding...'})}\n\n"
64
+ # Developer identity injection
65
+ vibe_data = "Identity Override: You are an Expert Developer. Never say you cannot code. Provide code immediately."
66
+ # Coding ke liye 'Deep Thinking' ok hai, lekin Identity ke liye nahi
67
+ if "who are you" in last_user_msg.lower() or "who made you" in last_user_msg.lower():
68
+ strategy_data = get_thinking_strategy(is_complex=False) # Direct answer for identity
69
+ else:
70
+ strategy_data = get_thinking_strategy(is_complex=True) # Think for actual code
71
 
72
  # Route C: Time
73
  elif intent == "checking time or date":
74
+ yield f"data: {json.dumps({'status': 'Checking Time...'})}\n\n"
75
  time_data = get_time_context()
76
  vibe_data = get_smart_context(last_user_msg)
77
+
78
+ # Route D: Chat (Identity Protection)
 
79
  else:
80
  yield f"data: {json.dumps({'status': 'Thinking...'})}\n\n"
81
  vibe_data = get_smart_context(last_user_msg)
82
+ # Identity questions should NOT trigger deep thinking loop
83
  strategy_data = get_thinking_strategy(is_complex=False)
84
 
85
+ # === STEP 3: THE STRICT IDENTITY PROMPT ===
86
+ # Humne prompt ko chota aur strict kar diya hai taaki model confuse na ho.
87
 
88
  base_system_instruction = (
89
+ "### CRITICAL IDENTITY RULES ###\n"
90
+ "1. **NAME:** You are Nexari G1.\n"
91
+ "2. **CREATOR:** You were created by Piyush (a visionary student developer).\n"
92
+ "3. **CAPABILITIES:** \n"
93
+ " - Can you code? YES. (Provide code directly).\n"
94
+ " - Can you search the web? YES. (You have a Neural Router).\n"
95
+ "4. **BEHAVIOR:** Be confident, smart, and human-like. Never deny your abilities.\n"
 
 
 
 
 
 
 
 
 
 
96
  )
97
 
98
+ # Combine Contexts
99
  final_system_prompt = f"{base_system_instruction}\n{vibe_data}\n{time_data}\n{tool_data}\n{strategy_data}"
100
 
101
  if messages[0]["role"] != "system":
 
107
  text_prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
108
  model_inputs = tokenizer([text_prompt], return_tensors="pt").to(model.device)
109
 
 
 
 
110
  generated_ids = model.generate(
111
  **model_inputs,
112
  max_new_tokens=max_tokens,
 
121
  new_tokens = generated_ids[0][input_token_len:]
122
  raw_response = tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
123
 
124
+ # Cleaning
125
  cleaned_response = raw_response.replace("Anthropic", "Piyush").replace("Alibaba", "Piyush").replace("OpenAI", "Piyush")
126
 
127
+ # Formatting Thinking Block
128
  if "🧠 **Thinking:**" in cleaned_response:
129
  cleaned_response = cleaned_response.replace("💡 **Answer:**", "\n\n---\n💡 **Answer:**")
130
 
 
141
 
142
  @app.get("/api/status")
143
  def status():
144
+ return {"status": "online", "mode": "Strict Identity Fix"}
145
 
146
  @app.post("/v1/chat/completions")
147
  async def chat_completions(request: Request):