-
Notifications
You must be signed in to change notification settings - Fork 3
Description
The discussion and example snippets from #572 should probably be incorporated into the library documentation in some form given this behavior's practical importance in our programming model:
Agentalready serves as a natural mechanism for encapsulatingTemplates andTools into disjoint sets:@Template.define def write_poem(topic: str) -> str: ... ... class Chatbot(Agent): @Template.define def respond(self, user_query: str) -> str: ... @Template.define def greet(self, user_name: str) -> str: ... ... class TravelAdvisor(Agent): @Template.define def recommend_destination(self, user_query: str) -> str: ... @Tool.define def search_weather(self, city: str) -> str: ... ... def main(): chatbot = Chatbot() another_chatbot = Chatbot() travel_advisor = TravelAdvisor() @Template.define def simulate_user_interaction() -> str: """Use {another_chatbot} and {travel_advisor} to simulate an interaction between a user and assistant""" ... main()In this example, the lexical scope semantics of
TemplateandAgentand the definitions ofchatbotet al within the body ofmainnaturally enforce thatchatbot.respondhas access towrite_poemandchatbot.greet, but not toanother_chatbot.respondortravel_advisor.recommend_destination.simulate_user_interactionhas access tochatbot,another_chatbot,travel_advisorandwrite_poem, but none of these can see it.This is exactly the behavior you'd expect for ordinary Python objects - for example, if
Chatbot.respondwas an ordinary instance method, it wouldn't have access to lexical variables in the body ofmain.In contrast, if the body of
mainwas inlined into the same lexical context (i.e. the top-level module scope whereChatbotandTravelAdvisorare defined), this encapsulation would be broken and all theTemplates would see all of the others.