|
13 | 13 | # See the License for the specific language governing permissions and |
14 | 14 | # limitations under the License. |
15 | 15 |
|
| 16 | +import copy |
16 | 17 | import textwrap |
17 | 18 |
|
18 | 19 | import pytest |
@@ -295,3 +296,164 @@ def test_stop_configuration_parameter(): |
295 | 296 | # Check if the stop tokens are correctly set in the rendered prompt |
296 | 297 | for stop_token in expected_stop_tokens: |
297 | 298 | assert stop_token in task_prompt.stop |
| 299 | + |
| 300 | + |
| 301 | +def test_preprocess_events_removes_reasoning_traces(): |
| 302 | + """Test that reasoning traces are removed from bot messages in rendered prompts.""" |
| 303 | + config = RailsConfig.from_content( |
| 304 | + yaml_content=textwrap.dedent( |
| 305 | + """ |
| 306 | + models: |
| 307 | + - type: main |
| 308 | + engine: openai |
| 309 | + model: gpt-3.5-turbo-instruct |
| 310 | + reasoning_config: |
| 311 | + start_token: "<think>" |
| 312 | + end_token: "</think>" |
| 313 | + rails: |
| 314 | + output: |
| 315 | + apply_to_reasoning_traces: true |
| 316 | + prompts: |
| 317 | + - task: generate_user_intent |
| 318 | + content: |- |
| 319 | + {% if examples %}{{ examples }}{% endif %} |
| 320 | + {{ history | colang }} |
| 321 | + user "{{ user_input }}" |
| 322 | + user intent: |
| 323 | + """ |
| 324 | + ) |
| 325 | + ) |
| 326 | + |
| 327 | + llm_task_manager = LLMTaskManager(config) |
| 328 | + |
| 329 | + events = [ |
| 330 | + {"type": "UtteranceUserActionFinished", "final_transcript": "Hello"}, |
| 331 | + { |
| 332 | + "type": "StartUtteranceBotAction", |
| 333 | + "script": "<think>Let me think how to respond some crazy COT</think>Hi there!", |
| 334 | + }, |
| 335 | + {"type": "UtteranceUserActionFinished", "final_transcript": "How are you?"}, |
| 336 | + ] |
| 337 | + |
| 338 | + rendered_prompt = llm_task_manager.render_task_prompt( |
| 339 | + task=Task.GENERATE_USER_INTENT, |
| 340 | + context={"user_input": "How are you?", "examples": ""}, |
| 341 | + events=events, |
| 342 | + ) |
| 343 | + |
| 344 | + assert isinstance(rendered_prompt, str) |
| 345 | + |
| 346 | + assert "<think>" not in rendered_prompt |
| 347 | + assert "</think>" not in rendered_prompt |
| 348 | + assert "Let me think how to respond..." not in rendered_prompt |
| 349 | + |
| 350 | + assert "Hi there!" in rendered_prompt |
| 351 | + |
| 352 | + |
| 353 | +def test_preprocess_events_preserves_original_events(): |
| 354 | + """Test that _preprocess_events_for_prompt doesn't modify the original events.""" |
| 355 | + config = RailsConfig.from_content( |
| 356 | + yaml_content=textwrap.dedent( |
| 357 | + """ |
| 358 | + models: |
| 359 | + - type: main |
| 360 | + engine: openai |
| 361 | + model: gpt-3.5-turbo-instruct |
| 362 | + reasoning_config: |
| 363 | + start_token: "<think>" |
| 364 | + end_token: "</think>" |
| 365 | + rails: |
| 366 | + output: |
| 367 | + apply_to_reasoning_traces: true |
| 368 | + """ |
| 369 | + ) |
| 370 | + ) |
| 371 | + |
| 372 | + llm_task_manager = LLMTaskManager(config) |
| 373 | + |
| 374 | + original_events = [ |
| 375 | + {"type": "UtteranceUserActionFinished", "final_transcript": "Hello"}, |
| 376 | + { |
| 377 | + "type": "StartUtteranceBotAction", |
| 378 | + "script": "<think>Let me think how to respond some crazy COT</think>Hi there!", |
| 379 | + }, |
| 380 | + {"type": "UtteranceUserActionFinished", "final_transcript": "How are you?"}, |
| 381 | + ] |
| 382 | + |
| 383 | + events_copy = copy.deepcopy(original_events) |
| 384 | + |
| 385 | + processed_events = llm_task_manager._preprocess_events_for_prompt(events_copy) |
| 386 | + |
| 387 | + assert events_copy == original_events |
| 388 | + |
| 389 | + assert "<think>" not in processed_events[1]["script"] |
| 390 | + assert "</think>" not in processed_events[1]["script"] |
| 391 | + assert processed_events[1]["script"] == "Hi there!" |
| 392 | + |
| 393 | + |
| 394 | +def test_reasoning_traces_not_included_in_prompt_history(): |
| 395 | + """Test that reasoning traces don't get included in prompt history for subsequent LLM calls.""" |
| 396 | + config = RailsConfig.from_content( |
| 397 | + yaml_content=textwrap.dedent( |
| 398 | + """ |
| 399 | + models: |
| 400 | + - type: main |
| 401 | + engine: openai |
| 402 | + model: gpt-3.5-turbo-instruct |
| 403 | + reasoning_config: |
| 404 | + start_token: "<think>" |
| 405 | + end_token: "</think>" |
| 406 | + rails: |
| 407 | + output: |
| 408 | + apply_to_reasoning_traces: true |
| 409 | + prompts: |
| 410 | + - task: generate_user_intent |
| 411 | + content: |- |
| 412 | + {% if examples %}{{ examples }}{% endif %} |
| 413 | + Previous conversation: |
| 414 | + {{ history | colang }} |
| 415 | +
|
| 416 | + Current user message: |
| 417 | + user "{{ user_input }}" |
| 418 | + user intent: |
| 419 | + """ |
| 420 | + ) |
| 421 | + ) |
| 422 | + |
| 423 | + llm_task_manager = LLMTaskManager(config) |
| 424 | + |
| 425 | + events = [ |
| 426 | + {"type": "UtteranceUserActionFinished", "final_transcript": "Hello"}, |
| 427 | + { |
| 428 | + "type": "StartUtteranceBotAction", |
| 429 | + "script": "<think>I should greet the user back.</think>Hi there!", |
| 430 | + }, |
| 431 | + { |
| 432 | + "type": "UtteranceUserActionFinished", |
| 433 | + "final_transcript": "What's the weather like?", |
| 434 | + }, |
| 435 | + { |
| 436 | + "type": "StartUtteranceBotAction", |
| 437 | + "script": "<think>I should explain I don't have real-time weather data.</think>I don't have access to real-time weather information.", |
| 438 | + }, |
| 439 | + {"type": "UtteranceUserActionFinished", "final_transcript": "Tell me about AI"}, |
| 440 | + ] |
| 441 | + |
| 442 | + rendered_prompt = llm_task_manager.render_task_prompt( |
| 443 | + task=Task.GENERATE_USER_INTENT, |
| 444 | + context={"user_input": "Tell me about AI", "examples": ""}, |
| 445 | + events=events, |
| 446 | + ) |
| 447 | + |
| 448 | + assert isinstance(rendered_prompt, str) |
| 449 | + |
| 450 | + assert "<think>I should greet the user back.</think>" not in rendered_prompt |
| 451 | + assert ( |
| 452 | + "<think>I should explain I don't have real-time weather data.</think>" |
| 453 | + not in rendered_prompt |
| 454 | + ) |
| 455 | + |
| 456 | + assert ( |
| 457 | + "Hi there!" in rendered_prompt |
| 458 | + or "I don't have access to real-time weather information." in rendered_prompt |
| 459 | + ) |
0 commit comments