|
| 1 | +"""Conceptual Spans.""" |
| 2 | + |
| 3 | +from mellea.stdlib.base import ( |
| 4 | + CBlock, |
| 5 | + Component, |
| 6 | + ModelOutputThunk, |
| 7 | + TemplateRepresentation, |
| 8 | +) |
| 9 | + |
| 10 | +Span = CBlock | Component | ModelOutputThunk |
| 11 | + |
| 12 | + |
| 13 | +class SimpleComponent(Component): |
| 14 | + """A Component that is make up of named spans.""" |
| 15 | + |
| 16 | + def __init__(self, **kwargs): |
| 17 | + """Initialized a simple component of the constructor's kwargs.""" |
| 18 | + for key in kwargs.keys(): |
| 19 | + if type(kwargs[key]) is str: |
| 20 | + kwargs[key] = CBlock(value=kwargs[key]) |
| 21 | + self._kwargs_type_check(kwargs) |
| 22 | + self._kwargs = kwargs |
| 23 | + |
| 24 | + def parts(self): |
| 25 | + """Returns the values of the kwargs.""" |
| 26 | + return self._kwargs.values() |
| 27 | + |
| 28 | + def _kwargs_type_check(self, kwargs): |
| 29 | + for key in kwargs.keys(): |
| 30 | + value = kwargs[key] |
| 31 | + assert issubclass(type(value), Component) or issubclass( |
| 32 | + type(value), CBlock |
| 33 | + ), f"Expected span but found {type(value)}" |
| 34 | + assert type(key) is str |
| 35 | + return True |
| 36 | + |
| 37 | + @staticmethod |
| 38 | + def make_simple_string(kwargs): |
| 39 | + """Uses <|key|>value</|key|> to represent a simple component.""" |
| 40 | + return "\n".join( |
| 41 | + [f"<|{key}|>{value}</|{key}|>" for (key, value) in kwargs.items()] |
| 42 | + ) |
| 43 | + |
| 44 | + def format_for_llm(self): |
| 45 | + """Uses a string rep.""" |
| 46 | + return SimpleComponent.make_simple_string(self._kwargs) |
| 47 | + # """ Uses a simple tagging structure that needs to be changed in the future. """ |
| 48 | + # return TemplateRepresentation( |
| 49 | + # obj=self, |
| 50 | + # args=self._kwargs, |
| 51 | + # template=SimpleComponent.make_simple_template(self._kwargs), |
| 52 | + # ) |
0 commit comments