Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
d5c837a
non-breaking:
HendrikStrobelt Sep 24, 2025
1917b40
non-breaking:
HendrikStrobelt Sep 24, 2025
484ccf4
Context, SimpleContext, ChatContext (nee:LinearContext)
HendrikStrobelt Sep 24, 2025
a14b422
expand -> add
HendrikStrobelt Sep 25, 2025
2df9fbe
remove RootContext in favor of _is_root attribute.
HendrikStrobelt Sep 25, 2025
2fe2e79
.
HendrikStrobelt Sep 25, 2025
89428c9
actions_for_available_tools is back
HendrikStrobelt Sep 25, 2025
6359b41
all backends adjusted to new context structure.
HendrikStrobelt Sep 25, 2025
e64b51b
feat: add top level funcs and start sample changes
jakelorocco Sep 25, 2025
f3e9137
context adjustments and tests
HendrikStrobelt Sep 25, 2025
c9ec7df
maybe breaking:
HendrikStrobelt Sep 25, 2025
3646edf
untested session and sampling changes
HendrikStrobelt Sep 25, 2025
c2b3c47
backends should add ACTION and MOT.
HendrikStrobelt Sep 25, 2025
a47dae8
as_chat_history adapted
HendrikStrobelt Sep 25, 2025
849a7ed
fix: ollama client instantiate to prevent stale event loops in httpx …
jakelorocco Sep 26, 2025
c3854f4
fix: mypy issues
jakelorocco Sep 26, 2025
0754a5d
Renaming:
HendrikStrobelt Sep 26, 2025
590a285
name cleanup
HendrikStrobelt Sep 26, 2025
e494305
adding contexts to sampling
HendrikStrobelt Sep 26, 2025
d0f6633
1) handling sampling results and context
HendrikStrobelt Sep 26, 2025
5e005bd
adding warning
HendrikStrobelt Sep 26, 2025
b94809b
name clarity
HendrikStrobelt Sep 26, 2025
9a2efbf
remove unnecessary function
HendrikStrobelt Sep 26, 2025
507d10c
updated tests:
HendrikStrobelt Sep 26, 2025
ede690e
fix: watsonx errors
jakelorocco Sep 26, 2025
4afa7cf
fix: sampling and vision tests
jakelorocco Sep 26, 2025
09a34b6
examples react - error to be looked into by @JAL
HendrikStrobelt Sep 26, 2025
3ca56a3
fixed react - thanks to @JAL
HendrikStrobelt Sep 26, 2025
597332c
examples for alora (untested) and gen slots (tested)
HendrikStrobelt Sep 26, 2025
6a137f4
more examples updated
HendrikStrobelt Sep 26, 2025
5b11c5c
more examples updated
HendrikStrobelt Sep 26, 2025
81446e9
remove old context
HendrikStrobelt Sep 26, 2025
f483afa
fix: best of n and misc changes
jakelorocco Sep 27, 2025
09a50b2
fix: improve runtime of best of n sampling
jakelorocco Sep 27, 2025
5fbf63e
fix: add tests
jakelorocco Sep 27, 2025
d1c7fb5
fix: remove references to LinearContext
jakelorocco Sep 29, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/examples/aLora/101_example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import time

from mellea import LinearContext, MelleaSession
from mellea import LegacyLinearContext, MelleaSession
from mellea.backends.aloras.huggingface.granite_aloras import HFConstraintAlora
from mellea.backends.cache import SimpleLRUCache
from mellea.backends.huggingface import LocalHFBackend
Expand All @@ -22,7 +22,7 @@
backend.add_alora(custom_stembolt_failure_constraint)

# Create M session
m = MelleaSession(backend, ctx=LinearContext())
m = MelleaSession(backend, ctx=LegacyLinearContext())

# define a requirement
failure_check = ALoraRequirement(
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/agents/react.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def react(


if __name__ == "__main__":
m = mellea.start_session(ctx=mellea.stdlib.base.LinearContext())
m = mellea.start_session(ctx=mellea.stdlib.base.LegacyLinearContext())

def zip_lookup_tool_fn(city: str):
"""Returns the ZIP code for the `city`."""
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/agents/react_instruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def react(


if __name__ == "__main__":
m = mellea.start_session(ctx=mellea.stdlib.base.LinearContext())
m = mellea.start_session(ctx=mellea.stdlib.base.LegacyLinearContext())

def zip_lookup_tool_fn(city: str):
"""Returns the ZIP code for the `city`."""
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/generative_slots/generate_with_context.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from mellea import LinearContext, generative, start_session
from mellea import LegacyLinearContext, generative, start_session
from mellea.backends.types import ModelOption
from mellea.stdlib.base import CBlock

Expand Down Expand Up @@ -34,7 +34,7 @@ def give_feedback(essay: str) -> list[str]:

if __name__ == "__main__":
m = start_session(
ctx=LinearContext(), model_options={ModelOption.MAX_NEW_TOKENS: 100}
ctx=LegacyLinearContext(), model_options={ModelOption.MAX_NEW_TOKENS: 100}
)

text = """
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/image_text_models/vision_ollama_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

from PIL import Image

from mellea import LinearContext, start_session
from mellea import LegacyLinearContext, start_session
from mellea.stdlib.base import ImageBlock

m = start_session(model_id="granite3.2-vision", ctx=LinearContext())
m = start_session(model_id="granite3.2-vision", ctx=LegacyLinearContext())
# m = start_session(model_id="llava", ctx=LinearContext())

# load image
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/mify/rich_document_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from mellea.stdlib.base import ModelOutputThunk, TemplateRepresentation

# Use a `SimpleContext` so that each LLM call is independent.
m = mellea.start_session(backend_name="hf", ctx=mellea.SimpleContext())
m = mellea.start_session(backend_name="hf", ctx=mellea.LegacySimpleContext())

# 2. Let's import docling so that we can process pdf documents.

Expand Down
6 changes: 3 additions & 3 deletions docs/examples/notebooks/context_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"source": [
"## Import Mellea and Start a Session with LinearContext\n",
"\n",
"Up to this point we have used SimpleContext, a context manager that resets the chat message history on each model call. That is, the model's context is entirely determined by the current Component. \n",
"Up to this point we have used SimpleContext, a context manager that resets the chat message history on each model call. That is, the model's context is entirely determined by the current Component.\n",
"\n",
"Mellea also provides a LinearContext, which behaves like a chat history. We will use the LinearContext to interact with cat hmodels:"
]
Expand All @@ -84,9 +84,9 @@
},
"outputs": [],
"source": [
"from mellea import LinearContext, start_session\n",
"from mellea import LegacyLinearContext, start_session\n",
"\n",
"m = start_session(ctx=LinearContext())\n",
"m = start_session(ctx=LegacyLinearContext())\n",
"m.chat(\"Make up a math problem.\")\n",
"m.chat(\"Solve your math problem.\")\n",
"print(m.ctx.last_output())\n",
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/notebooks/m_serve_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@
"\n",
"import mellea\n",
"from cli.serve.models import ChatMessage\n",
"from mellea.stdlib.base import LinearContext, ModelOutputThunk\n",
"from mellea.stdlib.base import LegacyLinearContext, ModelOutputThunk\n",
"from mellea.stdlib.requirement import Requirement, simple_validate\n",
"from mellea.stdlib.sampling import RejectionSamplingStrategy, SamplingResult\n",
"\n",
"session = mellea.start_session(ctx=LinearContext())\n",
"session = mellea.start_session(ctx=LegacyLinearContext())\n",
"\n",
"\n",
"def validate_hi_bob(email: str) -> bool:\n",
Expand Down
7 changes: 6 additions & 1 deletion docs/examples/safety.py/guardian.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
from mellea.backends import model_ids
from mellea.backends.dummy import DummyBackend
from mellea.backends.ollama import OllamaModelBackend
from mellea.stdlib.base import Context, ContextTurn, ModelOutputThunk, SimpleContext
from mellea.stdlib.base import (
ContextTurn,
LegacyContext,
LegacySimpleContext,
ModelOutputThunk,
)
from mellea.stdlib.chat import Message
from mellea.stdlib.safety.guardian import GuardianCheck, GuardianRisk

Expand Down
11 changes: 8 additions & 3 deletions docs/examples/sessions/creating_a_new_type_of_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
from mellea import MelleaSession
from mellea.backends import Backend, BaseModelSubclass
from mellea.backends.ollama import OllamaModelBackend
from mellea.stdlib.base import CBlock, Context, LinearContext, ModelOutputThunk
from mellea.stdlib.base import (
CBlock,
LegacyContext,
LegacyLinearContext,
ModelOutputThunk,
)
from mellea.stdlib.chat import Message
from mellea.stdlib.requirement import Requirement, reqify
from mellea.stdlib.safety.guardian import GuardianCheck, GuardianRisk
Expand All @@ -18,7 +23,7 @@ def __init__(
self,
requirements: list[str | Requirement],
backend: Backend,
ctx: Context | None = None,
ctx: LegacyContext | None = None,
check_immediately: bool = True,
):
super().__init__(backend, ctx)
Expand Down Expand Up @@ -66,7 +71,7 @@ def chat(
m = ChatCheckingSession(
requirements=[GuardianCheck("jailbreak"), GuardianCheck("profanity")],
backend=OllamaModelBackend(),
ctx=LinearContext(),
ctx=LegacyLinearContext(),
)

# You can run this code to see the immediate checks working.
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/tutorial/context_example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from mellea import LinearContext, start_session
from mellea import LegacyLinearContext, start_session

m = start_session(ctx=LinearContext())
m = start_session(ctx=LegacyLinearContext())
m.chat("Make up a math problem.")
m.chat("Solve your math problem.")

Expand Down
4 changes: 2 additions & 2 deletions docs/examples/tutorial/m_serve_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

import mellea
from cli.serve.models import ChatMessage
from mellea.stdlib.base import LinearContext, ModelOutputThunk
from mellea.stdlib.base import LegacyLinearContext, ModelOutputThunk
from mellea.stdlib.requirement import Requirement, simple_validate
from mellea.stdlib.sampling import RejectionSamplingStrategy, SamplingResult

session = mellea.start_session(ctx=LinearContext())
session = mellea.start_session(ctx=LegacyLinearContext())


def validate_hi_bob(email: str) -> bool:
Expand Down
6 changes: 3 additions & 3 deletions mellea/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Mellea is a library for building robust LLM applications."""

import mellea.backends.model_ids as model_ids
from mellea.stdlib.base import LinearContext, SimpleContext
from mellea.stdlib.base import LegacyLinearContext, LegacySimpleContext
from mellea.stdlib.genslot import generative
from mellea.stdlib.session import (
MelleaSession,
Expand All @@ -14,9 +14,9 @@
)

__all__ = [
"LinearContext",
"LegacyLinearContext",
"LegacySimpleContext",
"MelleaSession",
"SimpleContext",
"chat",
"generative",
"instruct",
Expand Down
10 changes: 8 additions & 2 deletions mellea/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@

from mellea.backends.model_ids import ModelIdentifier
from mellea.backends.types import ModelOption
from mellea.stdlib.base import CBlock, Component, Context, GenerateLog, ModelOutputThunk
from mellea.stdlib.base import (
CBlock,
Component,
GenerateLog,
LegacyContext,
ModelOutputThunk,
)

BaseModelSubclass = TypeVar(
"BaseModelSubclass", bound=pydantic.BaseModel
Expand Down Expand Up @@ -37,7 +43,7 @@ def __init__(
def generate_from_context(
self,
action: Component | CBlock,
ctx: Context,
ctx: LegacyContext,
*,
format: type[BaseModelSubclass] | None = None,
model_options: dict | None = None,
Expand Down
10 changes: 8 additions & 2 deletions mellea/backends/dummy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
"""This module holds shim backends used for smoke tests."""

from mellea.backends import Backend, BaseModelSubclass
from mellea.stdlib.base import CBlock, Component, Context, GenerateLog, ModelOutputThunk
from mellea.stdlib.base import (
CBlock,
Component,
GenerateLog,
LegacyContext,
ModelOutputThunk,
)


class DummyBackend(Backend):
Expand All @@ -19,7 +25,7 @@ def __init__(self, responses: list[str] | None):
def generate_from_context(
self,
action: Component | CBlock,
ctx: Context,
ctx: LegacyContext,
*,
format: type[BaseModelSubclass] | None = None,
model_options: dict | None = None,
Expand Down
14 changes: 7 additions & 7 deletions mellea/backends/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
from mellea.stdlib.base import (
CBlock,
Component,
Context,
LinearContext,
LegacyContext,
LegacyLinearContext,
LegacySimpleContext,
ModelOutputThunk,
SimpleContext,
TemplateRepresentation,
)
from mellea.stdlib.chat import Message, ToolMessage
Expand All @@ -36,7 +36,7 @@ def print(self, c: Component | CBlock) -> str:
...

@abc.abstractmethod
def print_context(self, ctx: Context) -> str:
def print_context(self, ctx: LegacyContext) -> str:
"""Renders a Context for input to a model."""
...

Expand Down Expand Up @@ -170,17 +170,17 @@ def _parse(
else:
return result

def print_context(self, ctx: Context) -> str:
def print_context(self, ctx: LegacyContext) -> str:
"""Renders a Context for input to a model."""
assert not ctx.is_chat_context, (
"Chat contexts should be handled in a backend by first using `Formatter.to_chat_messages` and then passing the dict to an API endpoint or using hf.apply_chat_template."
)
match ctx:
case LinearContext():
case LegacyLinearContext():
linearized_ctx = ctx.render_for_generation()
assert linearized_ctx is not None
return "".join([self.print(x) for x in linearized_ctx])
case SimpleContext():
case LegacySimpleContext():
raise Exception("Do not know how to handle a SimpleContext yet.")
case _:
raise Exception(
Expand Down
8 changes: 4 additions & 4 deletions mellea/backends/huggingface.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
from mellea.stdlib.base import (
CBlock,
Component,
Context,
GenerateLog,
GenerateType,
LegacyContext,
ModelOutputThunk,
ModelToolCall,
)
Expand Down Expand Up @@ -186,7 +186,7 @@ def alora_model(self, model: "aLoRAPeftModelForCausalLM | None"): # noqa: UP037
def generate_from_context(
self,
action: Component | CBlock,
ctx: Context,
ctx: LegacyContext,
*,
format: type[BaseModelSubclass] | None = None,
model_options: dict | None = None,
Expand Down Expand Up @@ -218,7 +218,7 @@ def generate_from_context(
def _generate_from_context_alora(
self,
action: Component | CBlock,
ctx: Context,
ctx: LegacyContext,
*,
format: type[BaseModelSubclass] | None = None,
model_options: dict[str, Any],
Expand Down Expand Up @@ -265,7 +265,7 @@ def _generate_from_context_alora(
def _generate_from_context_standard(
self,
action: Component | CBlock,
ctx: Context,
ctx: LegacyContext,
*,
format: type[BaseModelSubclass] | None = None,
model_options: dict[str, Any],
Expand Down
6 changes: 3 additions & 3 deletions mellea/backends/litellm.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
from mellea.stdlib.base import (
CBlock,
Component,
Context,
GenerateLog,
GenerateType,
LegacyContext,
ModelOutputThunk,
ModelToolCall,
)
Expand Down Expand Up @@ -106,7 +106,7 @@ def __init__(
def generate_from_context(
self,
action: Component | CBlock,
ctx: Context,
ctx: LegacyContext,
*,
format: type[BaseModelSubclass] | None = None,
model_options: dict | None = None,
Expand Down Expand Up @@ -208,7 +208,7 @@ def _make_backend_specific_and_remove(
def _generate_from_chat_context_standard(
self,
action: Component | CBlock,
ctx: Context,
ctx: LegacyContext,
*,
format: type[BaseModelSubclass]
| None = None, # Type[BaseModelSubclass] is a class object of a subclass of BaseModel
Expand Down
6 changes: 3 additions & 3 deletions mellea/backends/ollama.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
from mellea.stdlib.base import (
CBlock,
Component,
Context,
GenerateLog,
GenerateType,
LegacyContext,
ModelOutputThunk,
ModelToolCall,
TemplateRepresentation,
Expand Down Expand Up @@ -232,7 +232,7 @@ def _make_backend_specific_and_remove(
def generate_from_context(
self,
action: Component | CBlock,
ctx: Context,
ctx: LegacyContext,
*,
format: type[BaseModelSubclass] | None = None,
model_options: dict | None = None,
Expand All @@ -253,7 +253,7 @@ def generate_from_context(
def generate_from_chat_context(
self,
action: Component | CBlock,
ctx: Context,
ctx: LegacyContext,
*,
format: type[BaseModelSubclass] | None = None,
model_options: dict | None = None,
Expand Down
Loading