Theory of Mind modeling for Software Engineering assistants

1.0.3 · active · verified Sat Apr 11

ToM-SWE (Theory of Mind - Software Engineering) is a Python library designed to imbue LLM-based software engineering assistants with a 'Theory of Mind' (ToM). This allows assistants to better understand and anticipate user intentions, beliefs, and goals, leading to more contextually relevant and helpful interactions. The current version is 1.0.3, with minor updates released periodically for optimizations and typing.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `ToMController`, add system and user messages, and crucially, use `Reflection` to inject Theory of Mind insights. The controller then processes these messages and reflections to generate more informed responses from the underlying LLM.

import os
from tom_swe import ToMController, System, User, Assistant, Reflection

# Ensure OPENAI_API_KEY is set in your environment
# Alternatively, pass it directly: ToMController(openai_api_key="sk-...")
controller = ToMController(
    openai_api_key=os.environ.get('OPENAI_API_KEY', '')
)

# Initialize with a system message
controller.add_message(System("You are a helpful software engineering assistant."))

# User query
controller.add_message(User("Help me write a Python function to calculate factorial."))
response_1 = controller.run()
print(f"Assistant (initial): {response_1}")

# Add a reflection to guide the assistant's 'mind'
controller.add_message(Reflection("The user might be looking for both iterative and recursive solutions, or error handling."))

# Follow-up user query based on reflection
controller.add_message(User("Can you also add some error handling for non-integer inputs?"))
response_2 = controller.run()
print(f"Assistant (with reflection): {response_2}")

view raw JSON →