Main R6 class for managing and running a focus group simulation. It orchestrates the simulation through distinct phases (Opening, Icebreaker, Engagement, Exploration, Closing) guided by a question script, manages agent interactions via a turn-taking flow, and provides methods for analysis and visualization of the conversation.
Provides utilities for simulating and analyzing focus group discussions using Large Language Models. Enables researchers to create virtual focus groups with diverse AI agents with detailed personas, conduct structured discussions across multiple phases, and perform analysis of the resulting conversations.
Author
Maintainer: Ali Sanaei sanaei@uchicago.edu
Public fields
topicCharacter. The main topic of the focus group discussion.
purposeCharacter. The primary purpose or research objective of conducting the focus group.
agentsNamed list. A list of `FGAgent` objects participating in the simulation, indexed by their IDs.
moderator_idCharacter. The ID of the agent designated as the moderator.
conversation_logList. A chronological log of all messages. Each message is a list that includes at least `turn`, `speaker_id`, `is_moderator`, `text`, `timestamp`, and `phase`, along with call metadata: `response_id`, `finish_reason`, `sent_tokens`, `rec_tokens`, `total_tokens`, `duration_s`, `provider`, and `model`.
turn_taking_flowA `ConversationFlow` object dictating participant turn-taking.
prompt_templatesList. Holds prompt templates for agent/moderator actions.
question_scriptList. A structured list defining phases and specific questions/actions for the moderator. Each element is a list with `phase` (e.g., "opening", "icebreaker_question") and optionally `text` (for specific questions).
current_phase_indexInteger. Tracks the current position in the `question_script`.
current_question_textCharacter. Text of the current question being discussed.
current_conversation_summaryCharacter. An LLM-generated summary of earlier parts of the conversation, used for managing context length in prompts.
final_summaryCharacter. Final LLM-generated summary from the most recent simulation run.
llm_config_adminAn `llm_config` object for group-level LLM tasks (summarization, analysis).
max_tokens_utteranceInteger. Default max tokens for participant utterances.
max_tokens_moderatorInteger. Default max tokens for moderator utterances.
max_tokens_desireInteger. Default max tokens for desire-to-talk queries.
max_participant_responsesInteger. Maximum number of participant exchanges per round before the moderator can intervene. Can also be set globally via `options(focusgroup.max_participant_responses = N)`.
total_tokens_sentNumeric. Total tokens sent across all LLM calls in the group.
total_tokens_receivedNumeric. Total tokens received across all LLM calls.
Methods
Method new()
Initialize a new FocusGroup simulation.
Usage
FocusGroup$new(
topic,
purpose,
agents,
moderator_id,
turn_taking_flow,
question_script = list(),
prompt_templates = list(),
llm_config_admin = NULL,
max_tokens_config = list(),
max_participant_responses = NULL
)Arguments
topicCharacter. The main discussion topic.
purposeCharacter. The primary purpose of the focus group.
agentsNamed list of initialized `FGAgent` objects.
moderator_idCharacter. The ID of the agent acting as moderator.
turn_taking_flowAn initialized `ConversationFlow` object.
question_scriptList. Moderator's script defining phases and questions. If empty, a minimal default script (opening, generic discussion, closing) is used.
prompt_templatesList. Custom prompt templates. Defaults are used if not provided.
llm_config_adminAn `llm_config` object for administrative LLM tasks (e.g., summarization, LLM-based analysis). If NULL, the moderator's `model_config` will be used for these tasks.
max_tokens_configList. Optional. Named list with `utterance`, `moderator`, `desire` to override default max token limits for these LLM call types.
max_participant_responsesInteger. Optional. Maximum participant exchanges per round before moderator intervention. Defaults to `getOption("focusgroup.max_participant_responses", 3)`.
Method run_simulation()
Run the full focus group simulation. Iterates through the `question_script` phases or a specified number of turns.
Arguments
num_turnsInteger. Optional. Maximum number of turns to run. If `NULL` (default), the simulation runs until the `question_script` is exhausted or the moderator decides to end. If both `num_turns` and `question_script` are provided, the simulation stops at whichever condition is met first.
verboseLogical. If `TRUE`, prints progress and utterances to the console.
Method advance_turn()
Advance the simulation by one logical step (moderator action + participant response if applicable).
Method summarize()
Generate a summary of the conversation using an LLM.
Usage
FocusGroup$summarize(
llm_config = NULL,
summary_level = 1,
max_tokens = NULL,
internal_call = FALSE,
transcript_override = NULL
)Arguments
llm_configAn `llm_config` object for the summarization LLM. If `NULL`, uses `self$llm_config_admin`.
summary_levelInteger (1-3). 1: Prose overview, 2: Detailed bulleted, 3: Short bulleted takeaways.
max_tokensInteger. Optional. Max tokens for the summary.
internal_callLogical. If TRUE, this is an internal call (e.g. for context window management) and token counts are not added to the agent who "owns" llm_config_admin.
transcript_overrideCharacter. Optional. If provided, this transcript is summarized instead of `self$conversation_log`.
Method analyze()
Basic analysis of the conversation log.
Method analyze_topics()
Perform LDA topic modeling on the conversation.
Usage
FocusGroup$analyze_topics(
num_topics = 5,
min_doc_length = 20,
top_n_terms = 10,
turns = NULL,
speaker_ids = NULL,
...
)Arguments
num_topicsInteger. Number of topics to identify.
min_doc_lengthInteger. Min words for a speaker's aggregated text to be a document.
top_n_termsInteger. Number of top terms per topic to return.
turnsInteger vector. Optional. Specific turns to analyze.
speaker_idsCharacter vector. Optional. Specific speakers to analyze.
...Additional arguments to `topicmodels::LDA()`.
Method analyze_tfidf()
Calculate TF-IDF scores for terms per participant.
Method analyze_readability()
Calculate readability scores for each participant's aggregated text.
Method analyze_themes()
Perform LLM-assisted thematic analysis on the transcript.
Method analyze_statistics()
Perform statistical analysis on conversation patterns.
Method analyze_participation_balance()
Analyze participation balance and dominance patterns.
Method analyze_response_patterns()
Analyze response patterns and interaction behaviors.
Method analyze_question_patterns()
Analyze question asking patterns during the conversation.
Method analyze_key_phrases()
Extract and analyze key phrases using n-grams.
Method plot_participation_timeline()
Create participation timeline plot showing cumulative turns by participant across phases.
Method plot_word_count_distribution()
Create word count distribution plot showing message length patterns.
Method plot_participation_by_agent()
Create participation by agent plot showing total turns per participant.
Method plot_turn_length_timeline()
Create turn length timeline plot showing message length evolution over time.
