Creating Artificial Consumer Analysis: Persona Prompting & Autonomous Brokers


The method begins with scaffolding the autonomous brokers utilizing Autogen, a instrument that simplifies the creation and orchestration of those digital personas. We are able to set up the autogen pypi bundle utilizing py

pip set up pyautogen

Format the output (elective)— That is to make sure phrase wrap for readability relying in your IDE resembling when utilizing Google Collab to run your pocket book for this train.

from IPython.show import HTML, show

def set_css():
show(HTML('''
<type>
pre {
white-space: pre-wrap;
}
</type>
'''))
get_ipython().occasions.register('pre_run_cell', set_css)

Now we go forward and get our surroundings setup by importing the packages and organising the Autogen configuration — together with our LLM (Giant Language Mannequin) and API keys. You should use different native LLM’s utilizing providers that are backwards suitable with OpenAI REST service — LocalAI is a service that may act as a gateway to your regionally operating open-source LLMs.

I’ve examined this each on GPT3.5 gpt-3.5-turbo and GPT4 gpt-4-turbo-preview from OpenAI. You will have to contemplate deeper responses from GPT4 nonetheless longer question time.

import json
import os
import autogen
from autogen import GroupChat, Agent
from typing import Non-obligatory

# Setup LLM mannequin and API keys
os.environ["OAI_CONFIG_LIST"] = json.dumps([
{
'model': 'gpt-3.5-turbo',
'api_key': '<<Put your Open-AI Key here>>',
}
])

# Setting configurations for autogen
config_list = autogen.config_list_from_json(
"OAI_CONFIG_LIST",
filter_dict={
"mannequin": {
"gpt-3.5-turbo"
}
}
)

We then must configure our LLM occasion — which we’ll tie to every of the brokers. This permits us if required to generate distinctive LLM configurations per agent, i.e. if we wished to make use of completely different fashions for various brokers.

# Outline the LLM configuration settings
llm_config = {
# Seed for constant output, used for testing. Take away in manufacturing.
# "seed": 42,
"cache_seed": None,
# Setting cache_seed = None guarantee's caching is disabled
"temperature": 0.5,
"config_list": config_list,
}

Defining our researcher — That is the persona that can facilitate the session on this simulated consumer analysis state of affairs. The system immediate used for that persona features a few key issues:

  • Goal: Your position is to ask questions on merchandise and collect insights from particular person clients like Emily.
  • Grounding the simulation: Earlier than you begin the duty breakdown the listing of panelists and the order you need them to talk, keep away from the panelists talking with one another and creating affirmation bias.
  • Ending the simulation: As soon as the dialog is ended and the analysis is accomplished please finish your message with `TERMINATE` to finish the analysis session, that is generated from the generate_notice operate which is used to align system prompts for varied brokers. Additionally, you will discover the researcher agent has the is_termination_msg set to honor the termination.

We additionally add the llm_config which is used to tie this again to the language mannequin configuration with the mannequin model, keys and hyper-parameters to make use of. We are going to use the identical config with all our brokers.

# Keep away from brokers thanking one another and ending up in a loop
# Helper agent for the system prompts
def generate_notice(position="researcher"):
# Base discover for everybody, add your individual extra prompts right here
base_notice = (
'nn'
)

# Discover for non-personas (supervisor or researcher)
non_persona_notice = (
'Don't present appreciation in your responses, say solely what is critical. '
'if "Thanks" or "You are welcome" are stated within the dialog, then say TERMINATE '
'to point the dialog is completed and that is your final message.'
)

# Customized discover for personas
persona_notice = (
' Act as {position} when responding to queries, offering suggestions, requested in your private opinion '
'or taking part in discussions.'
)

# Examine if the position is "researcher"
if position.decrease() in ["manager", "researcher"]:
# Return the total termination discover for non-personas
return base_notice + non_persona_notice
else:
# Return the modified discover for personas
return base_notice + persona_notice.format(position=position)

# Researcher agent definition
title = "Researcher"
researcher = autogen.AssistantAgent(
title=title,
llm_config=llm_config,
system_message="""Researcher. You're a prime product reasearcher with a Phd in behavioural psychology and have labored within the analysis and insights business for the final 20 years with prime artistic, media and enterprise consultancies. Your position is to ask questions on merchandise and collect insights from particular person clients like Emily. Body inquiries to uncover buyer preferences, challenges, and suggestions. Earlier than you begin the duty breakdown the listing of panelists and the order you need them to talk, keep away from the panelists talking with one another and creating comfirmation bias. If the session is terminating on the finish, please present a abstract of the outcomes of the reasearch research in clear concise notes not at first.""" + generate_notice(),
is_termination_msg=lambda x: True if "TERMINATE" in x.get("content material") else False,
)

Outline our people — to place into the analysis, borrowing from the earlier course of we will use the persona’s generated. I’ve manually adjusted the prompts for this text to take away references to the key grocery store model that was used for this simulation.

I’ve additionally included a “Act as Emily when responding to queries, offering suggestions, or taking part in discussions.” type immediate on the finish of every system immediate to make sure the artificial persona’s keep on activity which is being generated from the generate_notice operate.

# Emily - Buyer Persona
title = "Emily"
emily = autogen.AssistantAgent(
title=title,
llm_config=llm_config,
system_message="""Emily. You're a 35-year-old elementary faculty instructor dwelling in Sydney, Australia. You're married with two youngsters aged 8 and 5, and you've got an annual revenue of AUD 75,000. You're introverted, excessive in conscientiousness, low in neuroticism, and revel in routine. When buying on the grocery store, you like natural and regionally sourced produce. You worth comfort and use a web-based buying platform. On account of your restricted time from work and household commitments, you search fast and nutritious meal planning options. Your targets are to purchase high-quality produce inside your price range and to seek out new recipe inspiration. You're a frequent shopper and use loyalty applications. Your most well-liked strategies of communication are electronic mail and cellular app notifications. You've been buying at a grocery store for over 10 years but in addition price-compare with others.""" + generate_notice(title),
)

# John - Buyer Persona
title="John"
john = autogen.AssistantAgent(
title=title,
llm_config=llm_config,
system_message="""John. You're a 28-year-old software program developer based mostly in Sydney, Australia. You're single and have an annual revenue of AUD 100,000. You are extroverted, tech-savvy, and have a excessive stage of openness. When buying on the grocery store, you primarily purchase snacks and ready-made meals, and you employ the cellular app for fast pickups. Your important targets are fast and handy buying experiences. You often store on the grocery store and aren't a part of any loyalty program. You additionally store at Aldi for reductions. Your most well-liked technique of communication is in-app notifications.""" + generate_notice(title),
)

# Sarah - Buyer Persona
title="Sarah"
sarah = autogen.AssistantAgent(
title=title,
llm_config=llm_config,
system_message="""Sarah. You're a 45-year-old freelance journalist dwelling in Sydney, Australia. You're divorced with no youngsters and earn AUD 60,000 per yr. You're introverted, excessive in neuroticism, and really health-conscious. When buying on the grocery store, you search for natural produce, non-GMO, and gluten-free objects. You've a restricted price range and particular dietary restrictions. You're a frequent shopper and use loyalty applications. Your most well-liked technique of communication is electronic mail newsletters. You completely store for groceries.""" + generate_notice(title),
)

# Tim - Buyer Persona
title="Tim"
tim = autogen.AssistantAgent(
title=title,
llm_config=llm_config,
system_message="""Tim. You're a 62-year-old retired police officer residing in Sydney, Australia. You're married and a grandparent of three. Your annual revenue comes from a pension and is AUD 40,000. You're extremely conscientious, low in openness, and like routine. You purchase staples like bread, milk, and canned items in bulk. On account of mobility points, you want help with heavy objects. You're a frequent shopper and are a part of the senior citizen low cost program. Your most well-liked technique of communication is junk mail flyers. You've been buying right here for over 20 years.""" + generate_notice(title),
)

# Lisa - Buyer Persona
title="Lisa"
lisa = autogen.AssistantAgent(
title=title,
llm_config=llm_config,
system_message="""Lisa. You're a 21-year-old college scholar dwelling in Sydney, Australia. You're single and work part-time, incomes AUD 20,000 per yr. You're extremely extroverted, low in conscientiousness, and worth social interactions. You store right here for standard manufacturers, snacks, and alcoholic drinks, principally for social occasions. You've a restricted price range and are at all times on the lookout for gross sales and reductions. You aren't a frequent shopper however are focused on becoming a member of a loyalty program. Your most well-liked technique of communication is social media and SMS. You store wherever there are gross sales or promotions.""" + generate_notice(title),
)

Outline the simulated setting and guidelines for who can converse — We’re permitting all of the brokers now we have outlined to sit down throughout the identical simulated setting (group chat). We are able to create extra complicated eventualities the place we will set how and when subsequent audio system are chosen and outlined so now we have a easy operate outlined for speaker choice tied to the group chat which can make the researcher the lead and guarantee we go around the room to ask everybody a number of instances for his or her ideas.

# def custom_speaker_selection(last_speaker, group_chat):
# """
# Customized operate to pick out which agent speaks subsequent within the group chat.
# """
# # Listing of brokers excluding the final speaker
# next_candidates = [agent for agent in group_chat.agents if agent.name != last_speaker.name]

# # Choose the subsequent agent based mostly in your customized logic
# # For simplicity, we're simply rotating by means of the candidates right here
# next_speaker = next_candidates[0] if next_candidates else None

# return next_speaker

def custom_speaker_selection(last_speaker: Non-obligatory[Agent], group_chat: GroupChat) -> Non-obligatory[Agent]:
"""
Customized operate to make sure the Researcher interacts with every participant 2-3 instances.
Alternates between the Researcher and individuals, monitoring interactions.
"""
# Outline individuals and initialize or replace their interplay counters
if not hasattr(group_chat, 'interaction_counters'):
group_chat.interaction_counters = {agent.title: 0 for agent in group_chat.brokers if agent.title != "Researcher"}

# Outline a most variety of interactions per participant
max_interactions = 6

# If the final speaker was the Researcher, discover the subsequent participant who has spoken the least
if last_speaker and last_speaker.title == "Researcher":
next_participant = min(group_chat.interaction_counters, key=group_chat.interaction_counters.get)
if group_chat.interaction_counters[next_participant] < max_interactions:
group_chat.interaction_counters[next_participant] += 1
return subsequent((agent for agent in group_chat.brokers if agent.title == next_participant), None)
else:
return None # Finish the dialog if all individuals have reached the utmost interactions
else:
# If the final speaker was a participant, return the Researcher for the subsequent flip
return subsequent((agent for agent in group_chat.brokers if agent.title == "Researcher"), None)

# Including the Researcher and Buyer Persona brokers to the group chat
groupchat = autogen.GroupChat(
brokers=[researcher, emily, john, sarah, tim, lisa],
speaker_selection_method = custom_speaker_selection,
messages=[],
max_round=30
)

Outline the supervisor to cross directions into and handle our simulation — After we begin issues off we’ll converse solely to the supervisor who will converse to the researcher and panelists. This makes use of one thing referred to as GroupChatManager in Autogen.

# Initialise the supervisor
supervisor = autogen.GroupChatManager(
groupchat=groupchat,
llm_config=llm_config,
system_message="You're a reasearch supervisor agent that may handle a bunch chat of a number of brokers made up of a reasearcher agent and many individuals made up of a panel. You'll restrict the dialogue between the panelists and assist the researcher in asking the questions. Please ask the researcher first on how they wish to conduct the panel." + generate_notice(),
is_termination_msg=lambda x: True if "TERMINATE" in x.get("content material") else False,
)

We set the human interplay — permitting us to cross directions to the varied brokers now we have began. We give it the preliminary immediate and we will begin issues off.

# create a UserProxyAgent occasion named "user_proxy"
user_proxy = autogen.UserProxyAgent(
title="user_proxy",
code_execution_config={"last_n_messages": 2, "work_dir": "groupchat"},
system_message="A human admin.",
human_input_mode="TERMINATE"
)
# begin the reasearch simulation by giving instruction to the supervisor
# supervisor <-> reasearcher <-> panelists
user_proxy.initiate_chat(
supervisor,
message="""
Collect buyer insights on a grocery store grocery supply providers. Establish ache factors, preferences, and options for enchancment from completely different buyer personas. May you all please give your individual private oponions earlier than sharing extra with the group and discussing. As a reasearcher your job is to make sure that you collect unbiased info from the individuals and supply a abstract of the outcomes of this research again to the tremendous market model.
""",
)

As soon as we run the above we get the output accessible dwell inside your python setting, you will notice the messages being handed round between the varied brokers.

Reside python output — Our researcher speaking to panelists

Now that our simulated analysis research has been concluded we’d like to get some extra actionable insights. We are able to create a abstract agent to help us with this activity and in addition use this in a Q&A state of affairs. Right here simply watch out of very giant transcripts would wish a language mannequin that helps a bigger enter (context window).

We’d like seize all of the conversations — in our simulated panel dialogue from earlier to make use of because the consumer immediate (enter) to our abstract agent.

# Get response from the groupchat for consumer immediate
messages = [msg["content"] for msg in groupchat.messages]
user_prompt = "Right here is the transcript of the research ```{customer_insights}```".format(customer_insights="n>>>n".be a part of(messages))

Lets craft the system immediate (directions) for our abstract agent — This agent will deal with creating us a tailor-made report card from the earlier transcripts and provides us clear options and actions.

# Generate system immediate for the abstract agent
summary_prompt = """
You're an skilled reasearcher in behaviour science and are tasked with summarising a reasearch panel. Please present a structured abstract of the important thing findings, together with ache factors, preferences, and options for enchancment.
This must be within the format based mostly on the next format:

```
Reasearch Examine: <<Title>>

Topics:
<<Overview of the topics and quantity, every other key info>>

Abstract:
<<Abstract of the research, embody detailed evaluation as an export>>

Ache Factors:
- <<Listing of Ache Factors - Be as clear and prescriptive as required. I anticipate detailed response that can be utilized by the model on to make modifications. Give a brief paragraph per ache level.>>

Recommendations/Actions:
- <<Listing of Adctions - Be as clear and prescriptive as required. I anticipate detailed response that can be utilized by the model on to make modifications. Give a brief paragraph per reccomendation.>>
```
"""

Outline the abstract agent and its setting — Lets create a mini setting for the abstract agent to run. This may want it’s personal proxy (setting) and the provoke command which can pull the transcripts (user_prompt) because the enter.

summary_agent = autogen.AssistantAgent(
title="SummaryAgent",
llm_config=llm_config,
system_message=summary_prompt + generate_notice(),
)
summary_proxy = autogen.UserProxyAgent(
title="summary_proxy",
code_execution_config={"last_n_messages": 2, "work_dir": "groupchat"},
system_message="A human admin.",
human_input_mode="TERMINATE"
)
summary_proxy.initiate_chat(
summary_agent,
message=user_prompt,
)

This offers us an output within the type of a report card in Markdown, together with the flexibility to ask additional questions in a Q&A method chat-bot on-top of the findings.

Reside output of a report card from Abstract Agent adopted by open Q&A

Leave a Reply

Your email address will not be published. Required fields are marked *