This directory contains examples of creating and customizing Mellea sessions.
Demonstrates how to create custom session types with specialized behavior.
Key Features:
- Extending the base session class
- Custom context types
- Specialized session methods
- Session configuration patterns
- Session Customization: Creating domain-specific session types
- Context Management: Using different context types
- Backend Configuration: Setting up backends for sessions
- Session Lifecycle: Managing session state and resources
from mellea import start_session
# Default session (Ollama backend, SimpleContext)
m = start_session()
# With specific model
m = start_session(model_id="ibm-granite/granite-3.2-8b-instruct")
# With chat context
from mellea.stdlib.context import ChatContext
m = start_session(ctx=ChatContext())
# With model options
from mellea.backends import ModelOption
m = start_session(model_options={
ModelOption.MAX_NEW_TOKENS: 200,
ModelOption.TEMPERATURE: 0.7
})from mellea import MelleaSession
from mellea.backends.ollama import OllamaModelBackend
from mellea.stdlib.context import ChatContext
class MyCustomSession(MelleaSession):
def __init__(self, **kwargs):
backend = OllamaModelBackend(
model_id="my-model",
**kwargs
)
super().__init__(backend=backend, ctx=ChatContext())
def custom_method(self, prompt: str):
"""Add custom functionality."""
# Your custom logic here
return self.instruct(prompt)
# Use custom session
m = MyCustomSession()
result = m.custom_method("Hello!")# Automatic cleanup with context manager
with start_session() as m:
result = m.instruct("Generate text")
# Session automatically cleaned up# Ollama (default)
from mellea.backends.ollama import OllamaModelBackend
backend = OllamaModelBackend(model_id="llama2")
# OpenAI
from mellea.backends.openai import OpenAIBackend
backend = OpenAIBackend(model_id="gpt-4")
# HuggingFace
from mellea.backends.huggingface import LocalHFBackend
backend = LocalHFBackend(model_id="ibm-granite/granite-3.2-8b-instruct")
# LiteLLM
from mellea.backends.litellm import LiteLLMBackend
backend = LiteLLMBackend(model_id="claude-3-opus")
# Create session with custom backend
m = MelleaSession(backend=backend, ctx=ChatContext())- SimpleContext: Basic linear context
- ChatContext: Multi-turn conversation context
- Custom: Create your own context types
- See
mellea/stdlib/session.pyfor session implementation - See
mellea/stdlib/context.pyfor context types - See
mellea/backends/for backend options