@@ -537,3 +537,99 @@ def sync_parser(response: str) -> str:
537
537
test_prompt .response_parser = sync_parser
538
538
resp_sync = await test_prompt .parse_response (resp )
539
539
assert resp_sync == "hello human"
540
+
541
+
542
+ def test_add_user_message_with_string ():
543
+ """Test adding a user message with a string content."""
544
+
545
+ class TestPrompt (Prompt ):
546
+ user_prompt = "Hello"
547
+
548
+ prompt = TestPrompt ()
549
+ prompt .add_user_message ("Additional message" )
550
+
551
+ assert prompt .chat == [{"role" : "user" , "content" : "Hello" }, {"role" : "user" , "content" : "Additional message" }]
552
+
553
+
554
+ def test_add_user_message_with_input_model ():
555
+ """Test adding a user message with an input model."""
556
+
557
+ class TestPrompt (Prompt [_PromptInput , str ]):
558
+ user_prompt = "Hello {{ name }}"
559
+
560
+ prompt = TestPrompt (_PromptInput (name = "Alice" , age = 30 , theme = "rock" ))
561
+ prompt .add_user_message (_PromptInput (name = "Bob" , age = 25 , theme = "jazz" ))
562
+
563
+ assert prompt .chat == [{"role" : "user" , "content" : "Hello Alice" }, {"role" : "user" , "content" : "Hello Bob" }]
564
+
565
+
566
+ def test_add_user_message_with_image ():
567
+ """Test adding a user message with an image."""
568
+
569
+ class ImagePrompt (Prompt ):
570
+ user_prompt = "What is on this image?"
571
+ image_input_fields = ["image" ]
572
+
573
+ prompt = ImagePrompt (_ImagePromptInput (image = _get_image_bytes ()))
574
+ prompt .add_user_message (_ImagePromptInput (image = _get_image_bytes ()))
575
+
576
+ assert len (prompt .chat ) == 2
577
+ assert prompt .chat [0 ]["role" ] == "user"
578
+ assert prompt .chat [1 ]["role" ] == "user"
579
+ assert len (prompt .chat [0 ]["content" ]) == 2 # text + image
580
+ assert len (prompt .chat [1 ]["content" ]) == 2 # text + image
581
+
582
+
583
+ def test_add_assistant_message ():
584
+ """Test adding an assistant message."""
585
+
586
+ class TestPrompt (Prompt [_PromptInput , _PromptOutput ]):
587
+ user_prompt = "Hello {{ name }}"
588
+
589
+ prompt = TestPrompt (_PromptInput (name = "Alice" , age = 30 , theme = "rock" ))
590
+ prompt .add_assistant_message ("Assistant response" )
591
+
592
+ assert prompt .chat == [
593
+ {"role" : "user" , "content" : "Hello Alice" },
594
+ {"role" : "assistant" , "content" : "Assistant response" },
595
+ ]
596
+
597
+
598
+ def test_add_assistant_message_with_model ():
599
+ """Test adding an assistant message with a model output."""
600
+
601
+ class TestPrompt (Prompt [_PromptInput , _PromptOutput ]):
602
+ user_prompt = "Hello {{ name }}"
603
+
604
+ prompt = TestPrompt (_PromptInput (name = "Alice" , age = 30 , theme = "rock" ))
605
+ output = _PromptOutput (song_title = "Test Song" , song_lyrics = "Test Lyrics" )
606
+ prompt .add_assistant_message (output )
607
+
608
+ assert prompt .chat == [
609
+ {"role" : "user" , "content" : "Hello Alice" },
610
+ {"role" : "assistant" , "content" : output .model_dump_json ()},
611
+ ]
612
+
613
+
614
+ def test_conversation_history ():
615
+ """Test building a complete conversation history with multiple messages."""
616
+
617
+ class TestPrompt (Prompt [_PromptInput , _PromptOutput ]):
618
+ user_prompt = "Hello {{ name }}"
619
+
620
+ prompt = TestPrompt (_PromptInput (name = "Alice" , age = 30 , theme = "rock" ))
621
+ prompt .add_user_message ("How are you?" )
622
+ prompt .add_assistant_message ("I'm doing well!" )
623
+ prompt .add_user_message (_PromptInput (name = "Bob" , age = 25 , theme = "jazz" ))
624
+ prompt .add_assistant_message (_PromptOutput (song_title = "Jazz Song" , song_lyrics = "Jazz lyrics" ))
625
+
626
+ assert prompt .chat == [
627
+ {"role" : "user" , "content" : "Hello Alice" },
628
+ {"role" : "user" , "content" : "How are you?" },
629
+ {"role" : "assistant" , "content" : "I'm doing well!" },
630
+ {"role" : "user" , "content" : "Hello Bob" },
631
+ {
632
+ "role" : "assistant" ,
633
+ "content" : _PromptOutput (song_title = "Jazz Song" , song_lyrics = "Jazz lyrics" ).model_dump_json (),
634
+ },
635
+ ]
0 commit comments