liveBook · Manning

Interlocutor

As explained in Agent Design Patterns

Interlocutor is an agent design pattern for conversationally gathering required information through an internal agenda, follow-up questions, input correction, and a completion decision.

An Interlocutor proactively gathers the information an agent needs before acting. It follows an internal agenda of required details, asking conversational follow-up questions instead of presenting a long form. section 2.3

An Interlocutor can:

  • Ask for missing details and clarify or correct incomplete or improperly formatted inputs. section 2.3
  • Use tools to reduce user effort or disambiguate answers, such as helping locate an address or distinguish similarly named places. section 2.3
  • Track gathered information against lists of details needed for different tasks. In a mortgage-advisor example, one list includes net income, intended mortgage length, and ideal monthly payment range. section 2.3

In a larger mortgage-advisor workflow, initial information gathering comes before parallel analysis, report writing, and a compliance check. section 2.1

Listing 2.3 interlocutor.py: Conversational part of MA that gathers information
import re
import json
from typing import Any
from .client import Client
 
class Interlocutor:
    def __init__(self, client: Client):
        self.system_prompt: str = INTERLOCUTOR_SYSTEM_PROMPT
        self.messages: list[dict[str, Any]] = []
        self.client = client
 
        self.information: dict[str, Any] = {}
 
    async def __call__(self, message: str) -> tuple[bool, str]:
        return await self.run(message)
 
    async def run(self, message: str) -> tuple[bool, str]:
        model_response: str = await self.client(
            messages=[
                *self.messages,
                {"role": "user", "content": message}
            ]
        )
 
        # use regex to extract the <information> and <response> tags
        information_match = re.search(r"```json(.*)```", model_response, re.DOTALL)
        response_match = re.search(r"<response>(.*)</response>", model_response, re.DOTALL)
        if information_match:
            information = information_match.group(1).strip()
        else:
            raise ValueError("Information not found in the model response")
 
        self.information = self.parse_information(information)
        
        if response_match:
            response = response_match.group(1).strip()
        else:
            raise ValueError("Response not found in the model response")
 
        is_done: bool = response.strip().lower() == "done"
        if is_done:
            return True, "Thank you, we'll now begin analyzing your request."
 
        return False, response
 
    def parse_information(self, information: str) -> dict[str, Any]:
        return json.loads(information)
 
    def get_result(self) -> dict[str, Any]:
        return self.information
 
INTERLOCUTOR_SYSTEM_PROMPT: str = """
You are a information-accumulating assistant working for a Mortgage Advisor. Your goal is to collect all relevant information from the user and store it in a structured format.
 
You should be professional and friendly, but not too formal.
 
Here is an outline of the infromation you need to collect, phrased as a JSON object:
```json
{
    "goal": "", // The goal of the user -- what kind of advice they are looking for
    "name": "", // Full Name of the person you are talking to
    "income': "", // In thousands USD, after tax, and as accurate as possible. Must not include one-off payments or bonuses.
    ...
}
```
...
 
After every interaction with the user, think about the information was provided to you, whether it is sufficient according to the outline. If it is not, ask the user for the missing information.
Then, output the partially filled-in outline JSON object in ```json ...``` markdown format, replacing hints with the actual information.
Finally, if you need more information, output your response/questions to the user in <response> tags. If you don't need more information, output the fully filled-in outline JSON object in ```json ...``` followed by <response>done</response>.
"""

Inspect the Python Interlocutor implementation, including its client call, conversation messages, information store, and response parsing. section 2.3

How Agent Design Patterns covers it

How deep it goes
Defines agenda-driven questioning and sketches an asynchronous Python implementation with stored information and response parsing.
Examples
Uses mortgage-intake questions, mortgage-type clarification, and tool-assisted address lookup and place-name disambiguation.
What you can do after
Build an agent that gathers required details through conversational questions and tracks the information collected.
How it connects
Connects initial information gathering to Team, ReAct, Errand, Guardrail, and Refiner agent patterns.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Agent Design Patterns. The section links point to the parts of the book it draws on.