|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# |
| 4 | +# This source code is licensed under the MIT license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +# pyre-strict |
| 8 | + |
| 9 | +from typing import Any, Literal |
| 10 | + |
| 11 | +from ax.core.llm_provider import LLMMessage, LLMProvider |
| 12 | +from ax.utils.common.testutils import TestCase |
| 13 | + |
| 14 | + |
| 15 | +class LLMMessageTest(TestCase): |
| 16 | + def test_llm_message(self) -> None: |
| 17 | + """Test LLMMessage creation and validation.""" |
| 18 | + test_cases: list[tuple[Literal["user", "system", "assistant"], str]] = [ |
| 19 | + ("user", "Hello"), |
| 20 | + ("system", "You are helpful"), |
| 21 | + ("assistant", "Hi there"), |
| 22 | + ] |
| 23 | + for role, content in test_cases: |
| 24 | + with self.subTest(role=role): |
| 25 | + msg = LLMMessage(role=role, content=content) |
| 26 | + self.assertEqual(msg.role, role) |
| 27 | + self.assertEqual(msg.content, content) |
| 28 | + self.assertEqual(msg.metadata, {}) |
| 29 | + |
| 30 | + def test_llm_message_with_metadata(self) -> None: |
| 31 | + """Test LLMMessage with assistant metadata (response case).""" |
| 32 | + msg = LLMMessage( |
| 33 | + role="assistant", |
| 34 | + content="Hello world", |
| 35 | + metadata={ |
| 36 | + "finish_reason": "stop", |
| 37 | + "usage": { |
| 38 | + "prompt_tokens": 10, |
| 39 | + "completion_tokens": 20, |
| 40 | + "total_tokens": 30, |
| 41 | + }, |
| 42 | + }, |
| 43 | + ) |
| 44 | + self.assertEqual(msg.role, "assistant") |
| 45 | + self.assertEqual(msg.content, "Hello world") |
| 46 | + self.assertEqual(msg.metadata["finish_reason"], "stop") |
| 47 | + self.assertEqual(msg.metadata["usage"]["total_tokens"], 30) |
| 48 | + |
| 49 | + # With minimal fields |
| 50 | + msg_minimal = LLMMessage(role="assistant", content="Hello") |
| 51 | + self.assertEqual(msg_minimal.metadata, {}) |
| 52 | + |
| 53 | + |
| 54 | +class LLMProviderProtocolTest(TestCase): |
| 55 | + def test_protocol_compliance(self) -> None: |
| 56 | + """Test that custom classes can implement the LLMProvider protocol.""" |
| 57 | + |
| 58 | + class MockProvider: |
| 59 | + """A mock provider that implements the LLMProvider protocol.""" |
| 60 | + |
| 61 | + def generate( |
| 62 | + self, |
| 63 | + messages: list[LLMMessage], |
| 64 | + **kwargs: Any, |
| 65 | + ) -> LLMMessage: |
| 66 | + return LLMMessage( |
| 67 | + role="assistant", |
| 68 | + content=f"Mock response to: {messages[-1].content}", |
| 69 | + ) |
| 70 | + |
| 71 | + provider = MockProvider() |
| 72 | + |
| 73 | + # Test that it's recognized as implementing the protocol |
| 74 | + self.assertIsInstance(provider, LLMProvider) |
| 75 | + |
| 76 | + # Test that it works |
| 77 | + response = provider.generate( |
| 78 | + messages=[LLMMessage(role="user", content="Hello")] |
| 79 | + ) |
| 80 | + self.assertEqual(response.role, "assistant") |
| 81 | + self.assertIn("Hello", response.content) |
0 commit comments