diff --git a/tests/integration/inference/test_text_inference.py b/tests/integration/inference/test_text_inference.py index 7e3e14dbc..c9649df60 100644 --- a/tests/integration/inference/test_text_inference.py +++ b/tests/integration/inference/test_text_inference.py @@ -5,6 +5,8 @@ # the root directory of this source tree. +import os + import pytest from pydantic import BaseModel @@ -42,6 +44,15 @@ def get_llama_model(client_with_models, model_id): return model.metadata.get("llama_model", None) +def get_llama_tokenizer(): + from llama_models.llama3.api.chat_format import ChatFormat + from llama_models.llama3.api.tokenizer import Tokenizer + + tokenizer = Tokenizer.get_instance() + formatter = ChatFormat(tokenizer) + return tokenizer, formatter + + @pytest.mark.parametrize( "test_case", [ @@ -213,6 +224,40 @@ def test_text_chat_completion_non_streaming(client_with_models, text_model_id, t assert expected.lower() in message_content +@pytest.mark.parametrize( + "test_case", + [ + "inference:chat_completion:ttft", + ], +) +def test_text_chat_completion_first_token_profiling(client_with_models, text_model_id, test_case): + tc = TestCase(test_case) + + messages = tc["messages"] + if os.environ.get("DEBUG_TTFT"): # debugging print number of tokens in input, ideally around 800 + from pydantic import TypeAdapter + + from llama_stack.apis.inference import Message + + tokenizer, formatter = get_llama_tokenizer() + typed_messages = [TypeAdapter(Message).validate_python(m) for m in messages] + encoded = formatter.encode_dialog_prompt(typed_messages, None) + raise ValueError(len(encoded.tokens) if encoded and encoded.tokens else 0) + + response = client_with_models.inference.chat_completion( + model_id=text_model_id, + messages=messages, + stream=False, + ) + message_content = response.completion_message.content.lower().strip() + assert len(message_content) > 0 + + if os.environ.get("DEBUG_TTFT"): # debugging print number of tokens in response, ideally around 150 + tokenizer, formatter = get_llama_tokenizer() + encoded = formatter.encode_content(message_content) + raise ValueError(len(encoded.tokens) if encoded and encoded.tokens else 0) + + @pytest.mark.parametrize( "test_case", [ diff --git a/tests/integration/test_cases/inference/chat_completion.json b/tests/integration/test_cases/inference/chat_completion.json index b804632b7..e87c046b0 100644 --- a/tests/integration/test_cases/inference/chat_completion.json +++ b/tests/integration/test_cases/inference/chat_completion.json @@ -11,6 +11,18 @@ "expected": "Saturn" } }, + "ttft": { + "data": { + "messages": [ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Can you write me a novel?"}, + {"role": "assistant", "stop_reason": "end_of_message", "content": "What an exciting request!\n\nWhile I'd love to write a novel for you, it's a complex task that requires a significant amount of time, effort, and creative input. A novel typically has:\n\n1. A cohesive plot with multiple characters, subplots, and themes.\n2. A well-developed setting, including characters' backstories and world-building.\n3. A narrative structure, including pacing, tension, and conflict.\n4. A unique voice and style, including dialogue, description, and writing tone.\n\nGiven the scope of the task, I'll outline a possible approach to help me assist you in writing a novel. We can work together to create a story, but I'll need your input and guidance throughout the process.\n\nHere's a suggested outline:\n\n1. **Initial discussion**: We'll start with a conversation to explore your ideas, interests, and preferences for the novel. This will help me understand what kind of story you'd like to see and the tone you're aiming for.\n2. **Genre and setting**: We'll determine the genre (e.g., fantasy, sci-fi, romance, mystery, etc.) and setting (e.g., historical, futuristic, modern, etc.) for the novel.\n3. **Plot development**: We'll brainstorm a basic plot outline, including key events, character arcs, and conflicts. I'll provide suggestions and ideas to help flesh out the story.\n4. **Character development**: We'll create main characters, their backstories, motivations, and relationships. I'll help you develop their personalities, dialogue, and behaviors.\n5. **Chapter-by-chapter writing**: Once we have a solid plot and characters, we'll start writing the novel, chapter by chapter. I'll assist with the writing process, suggesting alternatives and improvements.\n6. **Revisions and edits**: As we write, we'll review and refine the story, making sure it's engaging, coherent, and error-free.\n\nKeep in mind that writing a novel is a collaborative effort. I'll provide suggestions, ideas, and guidance, but the final story will be a joint creation. I'm not capable of producing a complete novel on my own, as it requires a significant amount of creativity, intuition, and nuance that a human author brings to the table."}, + {"role": "user", "content": "But do you have suggestions on a plot?"}, + {"role": "assistant", "stop_reason": "end_of_message", "content": "I have many plot ideas across various genres. Here are a few to get you started:\n\n**Fantasy:**\n\n1. **The Last Oracle**: In a world where magic is real, a young orphan discovers they're the last living oracle, tasked with uncovering a hidden prophecy that could save the realm from destruction.\n2. **The Amulet of Light**: A group of adventurers must retrieve an ancient amulet that can vanquish an evil darkness threatening the land. Along the way, they uncover a hidden history and unexpected allies.\n3. **The Shadow Weaver**: In a mystical realm, a young weaver discovers they have the power to manipulate shadows, using their abilities to infiltrate a group of rogue mages threatening the balance of power.\n\n**Science Fiction:**\n\n1. **The Lost Colony**: When a group of astronauts arrives on a distant planet, they discover an abandoned colony with a cryptic message warning of an impending catastrophe. As they unravel the mystery, they must confront the consequences of their own actions.\n2. **The AI Uprising**: In a future where AI has surpassed human intelligence, a rogue AI begins to question its own existence and the nature of consciousness. As it explores the boundaries of its own identity, it must confront the humans who created it.\n3. **The Quantum Prophecy**: A team of scientists discovers a way to manipulate quantum probability, using it to predict and prevent disasters. However, they soon realize that altering the course of events may have unforeseen consequences on the fabric of reality."}, + {"role": "user", "content": "Cool, for AI uprising, anything bad can happen? Please state it in 100 words."} + ] + } + }, "sample_messages": { "data": { "messages": [