|
| 1 | +# pytest: unit |
| 2 | +"""Convert a heterogeneous context to a generic chat history. |
| 3 | +
|
| 4 | +The as_generic_chat_history() function converts any Context into a list of |
| 5 | +Messages, gracefully handling unknown component types by converting them to |
| 6 | +strings. This is useful for working with mixed-type contexts or when you need |
| 7 | +a more flexible interface than as_chat_history(). |
| 8 | +""" |
| 9 | + |
| 10 | +from mellea.core import CBlock, ModelOutputThunk |
| 11 | +from mellea.stdlib.components import Message, as_generic_chat_history |
| 12 | +from mellea.stdlib.context import ChatContext |
| 13 | + |
| 14 | + |
| 15 | +def basic_example() -> list[Message]: |
| 16 | + """Convert a standard Message-based context to chat history.""" |
| 17 | + ctx = ChatContext() |
| 18 | + ctx = ctx.add(Message("user", "What is 2+2?")) |
| 19 | + ctx = ctx.add(Message("assistant", "2+2 equals 4.")) |
| 20 | + |
| 21 | + history = as_generic_chat_history(ctx) |
| 22 | + assert len(history) == 2 |
| 23 | + assert history[0].content == "What is 2+2?" |
| 24 | + assert history[1].content == "2+2 equals 4." |
| 25 | + return history |
| 26 | + |
| 27 | + |
| 28 | +def with_heterogeneous_components() -> list[Message]: |
| 29 | + """Handle mixed component types gracefully. |
| 30 | +
|
| 31 | + Unlike as_chat_history(), as_generic_chat_history() can handle any |
| 32 | + component type by converting unknown types to strings. |
| 33 | + """ |
| 34 | + ctx = ChatContext() |
| 35 | + ctx = ctx.add(Message("user", "Summarize this")) |
| 36 | + ctx = ctx.add(CBlock("Some inline content to process")) |
| 37 | + mot = ModelOutputThunk(value="The summary is...") |
| 38 | + ctx = ctx.add(mot) |
| 39 | + |
| 40 | + history = as_generic_chat_history(ctx) |
| 41 | + assert len(history) == 3 |
| 42 | + assert history[0].role == "user" |
| 43 | + assert history[1].role == "user" # CBlock defaults to 'user' |
| 44 | + assert history[2].role == "assistant" # MOT defaults to 'assistant' |
| 45 | + return history |
| 46 | + |
| 47 | + |
| 48 | +def with_custom_formatter() -> list[Message]: |
| 49 | + """Use a custom formatter for ModelOutputThunk with unparsed content. |
| 50 | +
|
| 51 | + You can provide a formatter function to customize how unparsed outputs |
| 52 | + or other unknown types are converted to strings. |
| 53 | + """ |
| 54 | + |
| 55 | + def my_formatter(obj: object) -> str: |
| 56 | + return f"[Formatted: {type(obj).__name__}]" |
| 57 | + |
| 58 | + ctx = ChatContext() |
| 59 | + ctx = ctx.add(Message("user", "Process this")) |
| 60 | + # Add a ModelOutputThunk with a non-Message parsed_repr |
| 61 | + mot = ModelOutputThunk(value="raw data") |
| 62 | + mot.parsed_repr = {"type": "dict", "data": "structured"} |
| 63 | + ctx = ctx.add(mot) |
| 64 | + |
| 65 | + history = as_generic_chat_history(ctx, formatter=my_formatter) |
| 66 | + assert len(history) == 2 |
| 67 | + assert "[Formatted:" in history[1].content |
| 68 | + return history |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + basic = basic_example() |
| 73 | + print("Basic example:") |
| 74 | + for msg in basic: |
| 75 | + print(f" {msg.role}: {msg.content}") |
| 76 | + |
| 77 | + heterogeneous = with_heterogeneous_components() |
| 78 | + print("\nHeterogeneous example:") |
| 79 | + for msg in heterogeneous: |
| 80 | + print(f" {msg.role}: {msg.content}") |
| 81 | + |
| 82 | + custom = with_custom_formatter() |
| 83 | + print("\nCustom formatter example:") |
| 84 | + for msg in custom: |
| 85 | + print(f" {msg.role}: {msg.content}") |
0 commit comments