File size: 9,175 Bytes
018b8c8
 
 
 
fb7858e
018b8c8
fb7858e
018b8c8
fb7858e
018b8c8
 
 
 
 
 
fb7858e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
018b8c8
fb7858e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
018b8c8
 
fb7858e
 
018b8c8
 
fb7858e
 
 
 
 
018b8c8
 
 
 
 
fb7858e
018b8c8
 
fb7858e
 
 
 
 
 
 
 
 
 
 
 
 
 
018b8c8
 
fb7858e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
018b8c8
 
 
fb7858e
018b8c8
 
 
 
8478148
018b8c8
966c2ab
 
 
 
8478148
93ce3aa
cb08de6
502e5d5
cb08de6
966c2ab
 
 
8478148
 
 
018b8c8
fb7858e
018b8c8
 
fb7858e
018b8c8
 
fb7858e
018b8c8
8478148
 
92bd21a
018b8c8
fb7858e
018b8c8
966c2ab
8478148
966c2ab
018b8c8
 
 
 
 
fb7858e
 
018b8c8
fb7858e
018b8c8
 
fb7858e
 
 
 
018b8c8
 
 
 
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
import os
from collections.abc import Iterator

import gradio as gr
from gradio import ChatMessage
from cohere import ClientV2
from cohere.core import RequestOptions

model_id = "command-a-reasoning-08-2025"

# Initialize Cohere client
api_key = os.getenv("COHERE_API_KEY")
if not api_key:
    raise ValueError("COHERE_API_KEY environment variable is required")

client = ClientV2(api_key=api_key, client_name="hf-command-a-reasoning-08-2025")

def format_chat_history(messages: list) -> list:
    """
    Formats the chat history into a structure Cohere can understand
    """
    formatted_history = []
    for message in messages:
        # Handle both ChatMessage objects and regular dictionaries
        if hasattr(message, "metadata") and message.metadata:
            # Skip thinking messages (messages with metadata)
            continue

        # Extract role and content safely
        if hasattr(message, "role"):
            role = message.role
            content = message.content
        elif isinstance(message, dict):
            role = message.get("role")
            content = message.get("content")
        else:
            continue

        if role and content:
            # Ensure content is a string to prevent validation issues
            if content is None:
                content = ""
            elif not isinstance(content, str):
                content = str(content)

            formatted_history.append({
                "role": role,
                "content": content
            })
    return formatted_history

def generate(message: str, history: list, thinking_budget: int) -> Iterator[list]:
    # Create a clean working copy of the history (excluding thinking messages)
    working_history = []
    for msg in history:
        # Skip thinking messages (messages with metadata)
        if hasattr(msg, "metadata") and msg.metadata:
            continue
        working_history.append(msg)

    # Format chat history for Cohere API (exclude thinking messages)
    messages = format_chat_history(working_history)

    # Add current message
    if message:
        messages.append({"role": "user", "content": message})

    try:
        # Set thinking type based on thinking_budget
        if thinking_budget == 0:
            thinking_param = {"type": "disabled"}
        else:
            thinking_param = {"type": "enabled", "token_budget": thinking_budget}
        # Call Cohere API using the correct event type and delta access
        response = client.chat_stream(
            model=model_id,
            messages=messages,
            temperature=0.3,
            request_options=RequestOptions(additional_body_parameters={"thinking": thinking_param})
        )

        # Initialize buffers
        thought_buffer = ""
        response_buffer = ""
        thinking_complete = False

        # Start with just the new assistant messages for this interaction
        current_interaction = [
            ChatMessage(
                role="assistant",
                content="",
                metadata={"title": "🧠 Thinking..."}
            )
        ]

        for event in response:
            if getattr(event, "type", None) == "content-delta":
                delta = event.delta

                if hasattr(delta, 'message'):
                    message = delta.message

                    if hasattr(message, 'content'):
                        content = message.content

                        # Check for thinking tokens first
                        thinking_text = getattr(content, 'thinking', None)
                        if thinking_text:
                            thought_buffer += thinking_text
                            # Update thinking message with metadata
                            current_interaction[0] = ChatMessage(
                                role="assistant",
                                content=thought_buffer,
                                metadata={"title": "🧠 Thinking..."}
                            )
                            # Yield only the current interaction, but ensure proper formatting
                            yield [
                                {
                                    "role": msg.role,
                                    "content": msg.content,
                                    "metadata": getattr(msg, "metadata", None)
                                } for msg in current_interaction
                            ]
                            continue

                        # Check for regular text tokens
                        text = getattr(content, 'text', None)
                        if text:
                            # Ensure text is a string
                            if text is None:
                                text = ""
                            elif not isinstance(text, str):
                                text = str(text)

                            # If we haven't completed thinking yet, this might be the start of the response
                            if not thinking_complete and thought_buffer:
                                thinking_complete = True
                                # Add response message below thinking
                                current_interaction.append(
                                    ChatMessage(
                                        role="assistant",
                                        content=""
                                    )
                                )

                            if thinking_complete:
                                # if thinking is complete, we collapse the thinking message
                                current_interaction[0] = ChatMessage(
                                    role="assistant",
                                    content=thought_buffer,
                                    metadata={"title": "🧠 Thoughts", "status": "done"}
                                )

                            response_buffer += text
                            # Update response message
                            current_interaction[-1] = ChatMessage(
                                role="assistant",
                                content=response_buffer
                            )
                            # Yield only the current interaction, but ensure proper formatting
                            yield [
                                {
                                    "role": msg.role,
                                    "content": msg.content,
                                    "metadata": getattr(msg, "metadata", None)
                                } for msg in current_interaction
                            ]

        # Final cleanup: ensure the final response is clean
        if thought_buffer and response_buffer:
            # Keep both thinking and response messages in the final history
            # The thinking message will be preserved with its metadata
            pass

    except Exception as e:
        gr.Warning(f"Error calling Cohere API: {str(e)}")
        yield []


examples = [
    [
        "A man walks into a bar and asks the bartender for a glass of water. The bartender pulls out a gun instead. The man says 'thank you' and leaves. Why?"
    ],
    [
        "Twenty-four red socks and 24 blue socks are lying in a drawer in a dark room. What is the minimum number of socks I must take out of the drawer which will guarantee that I have at least two socks of the same color?"
    ],
    [
        "A farmer needs to transport a fox, a chicken, and a sack of grain across a river in a boat that can only carry the farmer and one other thing at a time. If left alone together, the fox will eat the chicken, and the chicken will eat the grain. How does the farmer get everything across safely?"
    ],
    [
        "'''\nX   +\n   *\n''' \n\nReason about the above scene depicted in the markdown code block. If I interchange the locations of * and X, and then I interchange the locations of * and +, and then I  flip the image like a left-right mirror, which symbol is on the leftmost part of the image?"  
    ],
    [
        "You are running a race and overtake the person at position 76487423. What place are you in now?"  
    ],
    [
        "A man dies of old age on his 25 birthday. How is this possible?"
    ],
    [
        "Como sair de um helicóptero que caiu na água?"
    ],
    [
        "What is the best way to learn machine learning?"
    ],
    [
        "Explain quantum computing in simple terms"
    ],
    [
        "How many months have 28 days?"
    ],
    [
        "Explique la théorie de la relativité en français"
    ],
    [
        "Write a COBOL function to reverse a string"
    ]
]

demo = gr.ChatInterface(
    fn=generate,
    type="messages",
    autofocus=True,
    title="Command A Reasoning",
    examples=examples,
    run_examples_on_click=True,
    css_paths="style.css",
    delete_cache=(1800, 1800),
    cache_examples=False,
    additional_inputs=[
        gr.Slider(label="Thinking Budget", minimum=0, maximum=2000, step=10, value=500),
    ],
)

if __name__ == "__main__":
    demo.launch()