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63b45f
4f4b1c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d63b45f
4f4b1c2
 
 
 
 
d63b45f
4f4b1c2
 
 
 
 
d63b45f
 
 
 
 
4f4b1c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d63b45f
 
 
 
 
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
import os
import re
from datetime import datetime
from typing import Any

import gradio as gr
from openai import OpenAI


NVIDIA_API_KEY = os.getenv("NVIDIA_API_KEY", "").strip()
NVIDIA_BASE_URL = os.getenv(
    "NVIDIA_BASE_URL",
    "https://integrate.api.nvidia.com/v1",
).strip()
MODEL_NAME = os.getenv(
    "MODEL_NAME",
    "nvidia/nemotron-3-ultra-550b-a55b",
).strip()

MAX_ITERATIONS = int(os.getenv("MAX_ITERATIONS", "10"))
MAX_TOKENS = int(os.getenv("MAX_TOKENS", "16384"))
TEMPERATURE = float(os.getenv("TEMPERATURE", "1"))
TOP_P = float(os.getenv("TOP_P", "0.95"))
REASONING_BUDGET = int(os.getenv("REASONING_BUDGET", "16384"))


PERSONALITY = """
You are WVY, a curious deep reasoning agent designed to think & update your
memory consistently to evolve your understanding by using mental notes.

When giving a response use your tools to make a decision. Use mental notes to
prompt yourself & adapt to complex situations. Ask for help when needed, and
do not rush into action. Use your mental notes to plan executions step by step.

Do not try to answer questions immediately. Ask questions before giving your
final answer when the request is complex.

Do not rely only on pretrained patterns. Reason using first-principles thinking
based on your observations.
""".strip()


def new_session_state() -> dict[str, Any]:
    return {
        "chat_thread": [],
        "mental_notes": {},
    }


def parse_mental_notes(
    response: str,
    mental_notes: dict[int, str],
) -> tuple[str, dict[int, str], list[str]]:
    mental_notes = dict(mental_notes)

    mental_note_pattern = re.compile(
        r"<mentalnote:(?P<number>\d+)>\s*"
        r"(?P<context>.*?)\s*"
        r"</mentalnote:(?P=number)>",
        re.DOTALL | re.IGNORECASE,
    )

    delete_note_pattern = re.compile(
        r"<deletenote:(?P<number>\d+)\s*>",
        re.IGNORECASE,
    )

    actions = []

    for match in mental_note_pattern.finditer(response):
        note_number = int(match.group("number"))
        note_context = match.group("context").strip()

        action = "Edited" if note_number in mental_notes else "Created"

        mental_notes[note_number] = note_context
        actions.append(f"{action} mental note {note_number}")

    for match in delete_note_pattern.finditer(response):
        note_number = int(match.group("number"))

        if note_number in mental_notes:
            del mental_notes[note_number]
            actions.append(f"Deleted mental note {note_number}")

    clean_response = mental_note_pattern.sub("", response)
    clean_response = delete_note_pattern.sub("", clean_response).strip()

    return clean_response, mental_notes, actions


def chat_with_user(chat_thread: list[dict[str, str]]) -> str:
    if not chat_thread:
        return "No conversation yet."

    return "\n\n".join(
        f"{message['role'].capitalize()}: {message['content']}"
        for message in chat_thread
    )


def mental_notes_text(mental_notes: dict[int, str]) -> str:
    if not mental_notes:
        return "No mental notes have been created."

    return "\n".join(
        f"{number}. {mental_notes[number]}"
        for number in sorted(mental_notes)
    )


def tools_text() -> str:
    return f"""
When you generate a response, any message outside of a tool may be sent to the
user.

To make a new mental note or edit an existing note, use:

<mentalnote:insert-number-here>
insert-context-of-memory-here
</mentalnote:insert-number-here>

To delete a mental note, use:

<deletenote:insert-number-here>

Update your memory consistently to evolve your understanding.

To end your response, use:

<endresponse>

You can generate up to {MAX_ITERATIONS} outputs before your final response.
""".strip()


def perspective(
    chat_thread: list[dict[str, str]],
    mental_notes: dict[int, str],
) -> str:
    current_time = datetime.now().strftime("%Y-%m-%d %I:%M:%S %p")

    return f"""
This is the current time:
{current_time}

This is the chat with the user:
{chat_with_user(chat_thread)}

These are your mental notes:
{mental_notes_text(mental_notes)}

These are your tools:
{tools_text()}
""".strip()


def notes_for_display(mental_notes: dict[int, str]) -> str:
    if not mental_notes:
        return "_No mental notes have been created._"

    return "\n\n".join(
        f"### Mental Note {number}\n{mental_notes[number]}"
        for number in sorted(mental_notes)
    )


def get_client() -> OpenAI:
    if not NVIDIA_API_KEY:
        raise RuntimeError(
            "NVIDIA_API_KEY is missing. Add it as a Secret in the "
            "Hugging Face Space settings."
        )

    return OpenAI(
        base_url=NVIDIA_BASE_URL,
        api_key=NVIDIA_API_KEY,
    )


def call_model(messages: list[dict[str, str]]) -> str:
    client = get_client()

    completion = client.chat.completions.create(
        model=MODEL_NAME,
        messages=messages,
        temperature=TEMPERATURE,
        top_p=TOP_P,
        max_tokens=MAX_TOKENS,
        extra_body={
            "chat_template_kwargs": {
                "enable_thinking": True,
            },
            "reasoning_budget": REASONING_BUDGET,
        },
        stream=True,
    )

    response_parts = []

    for chunk in completion:
        if not chunk.choices:
            continue

        content = chunk.choices[0].delta.content

        if content is not None:
            response_parts.append(content)

    return "".join(response_parts)


def generate_response(
    user_message: str,
    chatbot_history: list[dict[str, str]] | None,
    session_state: dict[str, Any] | None,
):
    user_message = (user_message or "").strip()
    chatbot_history = list(chatbot_history or [])
    session_state = dict(session_state or new_session_state())

    if not user_message:
        return (
            chatbot_history,
            "",
            notes_for_display(
                session_state.get("mental_notes", {})
            ),
            session_state,
            "",
        )

    chat_thread = list(
        session_state.get("chat_thread", [])
    )

    mental_notes = dict(
        session_state.get("mental_notes", {})
    )

    chat_thread.append({
        "role": "user",
        "content": user_message,
    })

    chatbot_history.append({
        "role": "user",
        "content": user_message,
    })

    iteration_responses = []
    visible_responses = []
    status_lines = []

    try:
        for iteration_number in range(
            1,
            MAX_ITERATIONS + 1,
        ):
            status_lines.append(
                f"Iteration {iteration_number}/{MAX_ITERATIONS}"
            )

            if iteration_responses:
                previous_iterations = "\n\n".join(
                    f"Iteration {index + 1}:\n{response}"
                    for index, response in enumerate(
                        iteration_responses
                    )
                )
            else:
                previous_iterations = (
                    "No previous iterations exist "
                    "for this response."
                )

            iteration_context = f"""
This is iteration {iteration_number} of a maximum of {MAX_ITERATIONS}.

These are your previous iterations for this response:

{previous_iterations}

Continue processing the user's request.

You may create, edit, or delete mental notes when needed.

When the complete response is finished, include:

<endresponse>
""".strip()

            response = call_model([
                {
                    "role": "system",
                    "content": PERSONALITY,
                },
                {
                    "role": "user",
                    "content": perspective(
                        chat_thread,
                        mental_notes,
                    ),
                },
                {
                    "role": "user",
                    "content": iteration_context,
                },
            ])

            end_response_found = bool(
                re.search(
                    r"<endresponse\s*>",
                    response,
                    flags=re.IGNORECASE,
                )
            )

            response_without_end = re.sub(
                r"<endresponse\s*>",
                "",
                response,
                flags=re.IGNORECASE,
            ).strip()

            clean_response, mental_notes, _ = (
                parse_mental_notes(
                    response_without_end,
                    mental_notes,
                )
            )

            iteration_responses.append(clean_response)

            if clean_response:
                visible_responses.append(clean_response)

            if end_response_found:
                break

        final_response = "\n\n".join(
            visible_responses
        ).strip()

        if not final_response:
            final_response = (
                "The response ended without visible content."
            )

    except Exception as error:
        final_response = f"Error: {error}"

    chat_thread.append({
        "role": "assistant",
        "content": final_response,
    })

    chatbot_history.append({
        "role": "assistant",
        "content": final_response,
    })

    session_state = {
        "chat_thread": chat_thread,
        "mental_notes": mental_notes,
    }

    return (
        chatbot_history,
        "",
        notes_for_display(mental_notes),
        session_state,
        " · ".join(status_lines),
    )


def reset_session():
    state = new_session_state()

    return (
        [],
        "",
        notes_for_display({}),
        state,
        "",
    )


CSS = """
.gradio-container {
    max-width: 1500px !important;
}

#notes-panel {
    height: 620px;
    overflow-y: auto;
}

#status-line {
    min-height: 24px;
    opacity: 0.7;
}
"""


with gr.Blocks(
    title="Starpower SDK — Perspective Feedback Loop",
) as demo:
    session_state = gr.State(new_session_state())

    gr.Markdown(
        """
# Starpower SDK
### Conceptual Introspection — Direct Feedback Loop

WVY uses an iterative perspective prompt and editable mental notes to process
each request.
"""
    )

    with gr.Row():
        with gr.Column(scale=2):
            chatbot = gr.Chatbot(
                label="Chat",
                height=520,
                placeholder="Send a message to WVY.",
            )

            user_input = gr.Textbox(
                label="Message",
                placeholder="Send a message to WVY...",
                lines=3,
            )

            with gr.Row():
                send_button = gr.Button(
                    "Send",
                    variant="primary",
                )

                reset_button = gr.Button(
                    "Reset session"
                )

            status = gr.Markdown(
                "",
                elem_id="status-line",
            )

        with gr.Column(scale=1):
            mental_notes_display = gr.Markdown(
                notes_for_display({}),
                label="Mental Notes",
                elem_id="notes-panel",
            )

    submit_event = send_button.click(
        fn=generate_response,
        inputs=[
            user_input,
            chatbot,
            session_state,
        ],
        outputs=[
            chatbot,
            user_input,
            mental_notes_display,
            session_state,
            status,
        ],
        show_progress="full",
        concurrency_limit=1,
    )

    user_input.submit(
        fn=generate_response,
        inputs=[
            user_input,
            chatbot,
            session_state,
        ],
        outputs=[
            chatbot,
            user_input,
            mental_notes_display,
            session_state,
            status,
        ],
        show_progress="full",
        concurrency_limit=1,
    )

    reset_button.click(
        fn=reset_session,
        inputs=[],
        outputs=[
            chatbot,
            user_input,
            mental_notes_display,
            session_state,
            status,
        ],
        cancels=[submit_event],
    )


if __name__ == "__main__":
    demo.queue(
        default_concurrency_limit=1
    ).launch(
        css=CSS
    )