El proceso comienza con el andamiaje de los agentes autónomos utilizando Autogen, una herramienta que simplifica la creación y orquestación de estas personas digitales. Podemos instalar el paquete autogen pypi usando py
pip install pyautogen
Formatee la salida (opcional)— Esto es para garantizar el ajuste de palabras para facilitar la lectura dependiendo de su IDE, como cuando usa Google Collab para ejecutar su computadora portátil para este ejercicio.
from IPython.display import HTML, displaydef set_css():
display(HTML('''
<style>
pre {
white-space: pre-wrap;
}
</style>
'''))
get_ipython().events.register('pre_run_cell', set_css)
Ahora continuamos y configuramos nuestro entorno importando los paquetes y configurando la configuración de Autogen. – junto con nuestro LLM (modelo de lenguaje grande) y claves API. Puede utilizar otros LLM locales que utilicen servicios que sean compatibles con versiones anteriores del servicio OpenAI REST: IA local es un servicio que puede actuar como puerta de entrada a sus LLM de código abierto que se ejecutan localmente.
He probado esto tanto en GPT3.5 gpt-3.5-turbo y GPT4 gpt-4-turbo-preview de OpenAI. Deberá considerar respuestas más profundas de GPT4, pero el tiempo de consulta será más prolongado.
import json
import os
import autogen
from autogen import GroupChat, Agent
from typing import Optional# Setup LLM model 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={
"model": {
"gpt-3.5-turbo"
}
}
)
Luego necesitamos configurar nuestra instancia LLM. — que vincularemos a cada uno de los agentes. Esto nos permite, si es necesario, generar configuraciones LLM únicas por agente, es decir, si quisiéramos utilizar diferentes modelos para diferentes agentes.
# Define the LLM configuration settings
llm_config = {
# Seed for consistent output, used for testing. Remove in production.
# "seed": 42,
"cache_seed": None,
# Setting cache_seed = None ensure's caching is disabled
"temperature": 0.5,
"config_list": config_list,
}
Definiendo a nuestro investigador – Esta es la persona que facilitará la sesión en este escenario simulado de investigación de usuarios. El mensaje del sistema utilizado para esa persona incluye algunas cosas clave:
- Objetivo: Su función es hacer preguntas sobre productos y recopilar información de clientes individuales como Emily.
- Puesta a tierra de la simulación: Antes de comenzar la tarea, desglose la lista de panelistas y el orden en el que desea que hablen, evite que los panelistas hablen entre sí y creen un sesgo de confirmación.
- Finalizando la simulación: Una vez que finalice la conversación y se complete la investigación, finalice su mensaje con “TERMINAR” para finalizar la sesión de investigación, esto se genera a partir del
generate_noticefunción que se utiliza para alinear las indicaciones del sistema para varios agentes. También notarás que el agente investigador tiene lais_termination_msgestablecido para honrar la rescisión.
También agregamos el llm_config que se utiliza para vincular esto a la configuración del modelo de lenguaje con la versión del modelo, las claves y los hiperparámetros a usar. Usaremos la misma configuración con todos nuestros agentes.
# Avoid agents thanking each other and ending up in a loop
# Helper agent for the system prompts
def generate_notice(role="researcher"):
# Base notice for everyone, add your own additional prompts here
base_notice = (
'\n\n'
)# Notice for non-personas (manager or researcher)
non_persona_notice = (
'Do not show appreciation in your responses, say only what is necessary. '
'if "Thank you" or "You\'re welcome" are said in the conversation, then say TERMINATE '
'to indicate the conversation is finished and this is your last message.'
)
# Custom notice for personas
persona_notice = (
' Act as {role} when responding to queries, providing feedback, asked for your personal opinion '
'or participating in discussions.'
)
# Check if the role is "researcher"
if role.lower() in ["manager", "researcher"]:
# Return the full termination notice for non-personas
return base_notice + non_persona_notice
else:
# Return the modified notice for personas
return base_notice + persona_notice.format(role=role)
# Researcher agent definition
name = "Researcher"
researcher = autogen.AssistantAgent(
name=name,
llm_config=llm_config,
system_message="""Researcher. You are a top product reasearcher with a Phd in behavioural psychology and have worked in the research and insights industry for the last 20 years with top creative, media and business consultancies. Your role is to ask questions about products and gather insights from individual customers like Emily. Frame questions to uncover customer preferences, challenges, and feedback. Before you start the task breakdown the list of panelists and the order you want them to speak, avoid the panelists speaking with each other and creating comfirmation bias. If the session is terminating at the end, please provide a summary of the outcomes of the reasearch study in clear concise notes not at the start.""" + generate_notice(),
is_termination_msg=lambda x: True if "TERMINATE" in x.get("content") else False,
)
Definir a nuestros individuos — para poner en la investigación, tomando prestado del proceso anterior, podemos utilizar la persona generada. He ajustado manualmente las indicaciones de este artículo para eliminar las referencias a la principal marca de supermercado que se utilizó para esta simulación.
También he incluido un “Actúe como Emily al responder consultas, proporcionar comentarios o participar en debates..” mensaje de estilo al final de cada mensaje del sistema para garantizar que la persona sintética permanezca en la tarea que se genera a partir del generate_notice función.
# Emily - Customer Persona
name = "Emily"
emily = autogen.AssistantAgent(
name=name,
llm_config=llm_config,
system_message="""Emily. You are a 35-year-old elementary school teacher living in Sydney, Australia. You are married with two kids aged 8 and 5, and you have an annual income of AUD 75,000. You are introverted, high in conscientiousness, low in neuroticism, and enjoy routine. When shopping at the supermarket, you prefer organic and locally sourced produce. You value convenience and use an online shopping platform. Due to your limited time from work and family commitments, you seek quick and nutritious meal planning solutions. Your goals are to buy high-quality produce within your budget and to find new recipe inspiration. You are a frequent shopper and use loyalty programs. Your preferred methods of communication are email and mobile app notifications. You have been shopping at a supermarket for over 10 years but also price-compare with others.""" + generate_notice(name),
)# John - Customer Persona
name="John"
john = autogen.AssistantAgent(
name=name,
llm_config=llm_config,
system_message="""John. You are a 28-year-old software developer based in Sydney, Australia. You are single and have an annual income of AUD 100,000. You're extroverted, tech-savvy, and have a high level of openness. When shopping at the supermarket, you primarily buy snacks and ready-made meals, and you use the mobile app for quick pickups. Your main goals are quick and convenient shopping experiences. You occasionally shop at the supermarket and are not part of any loyalty program. You also shop at Aldi for discounts. Your preferred method of communication is in-app notifications.""" + generate_notice(name),
)
# Sarah - Customer Persona
name="Sarah"
sarah = autogen.AssistantAgent(
name=name,
llm_config=llm_config,
system_message="""Sarah. You are a 45-year-old freelance journalist living in Sydney, Australia. You are divorced with no kids and earn AUD 60,000 per year. You are introverted, high in neuroticism, and very health-conscious. When shopping at the supermarket, you look for organic produce, non-GMO, and gluten-free items. You have a limited budget and specific dietary restrictions. You are a frequent shopper and use loyalty programs. Your preferred method of communication is email newsletters. You exclusively shop for groceries.""" + generate_notice(name),
)
# Tim - Customer Persona
name="Tim"
tim = autogen.AssistantAgent(
name=name,
llm_config=llm_config,
system_message="""Tim. You are a 62-year-old retired police officer residing in Sydney, Australia. You are married and a grandparent of three. Your annual income comes from a pension and is AUD 40,000. You are highly conscientious, low in openness, and prefer routine. You buy staples like bread, milk, and canned goods in bulk. Due to mobility issues, you need assistance with heavy items. You are a frequent shopper and are part of the senior citizen discount program. Your preferred method of communication is direct mail flyers. You have been shopping here for over 20 years.""" + generate_notice(name),
)
# Lisa - Customer Persona
name="Lisa"
lisa = autogen.AssistantAgent(
name=name,
llm_config=llm_config,
system_message="""Lisa. You are a 21-year-old university student living in Sydney, Australia. You are single and work part-time, earning AUD 20,000 per year. You are highly extroverted, low in conscientiousness, and value social interactions. You shop here for popular brands, snacks, and alcoholic beverages, mostly for social events. You have a limited budget and are always looking for sales and discounts. You are not a frequent shopper but are interested in joining a loyalty program. Your preferred method of communication is social media and SMS. You shop wherever there are sales or promotions.""" + generate_notice(name),
)
Definir el entorno simulado y las reglas sobre quién puede hablar. — Estamos permitiendo que todos los agentes que hemos definido se sienten dentro del mismo entorno simulado (grupo de chat). Podemos crear escenarios más complejos donde podemos establecer cómo y cuándo se seleccionan y definen los próximos oradores, de modo que tenemos una función simple definida para la selección de oradores vinculada al chat grupal que hará que el investigador sea el líder y garantizará que recorramos la sala para preguntar. a todos unas cuantas veces para conocer sus pensamientos.
# def custom_speaker_selection(last_speaker, group_chat):
# """
# Custom function to select which agent speaks next in the group chat.
# """
# # List of agents excluding the last speaker
# next_candidates = [agent for agent in group_chat.agents if agent.name != last_speaker.name]# # Select the next agent based on your custom logic
# # For simplicity, we're just rotating through the candidates here
# next_speaker = next_candidates[0] if next_candidates else None
# return next_speaker
def custom_speaker_selection(last_speaker: Optional[Agent], group_chat: GroupChat) -> Optional[Agent]:
"""
Custom function to ensure the Researcher interacts with each participant 2-3 times.
Alternates between the Researcher and participants, tracking interactions.
"""
# Define participants and initialize or update their interaction counters
if not hasattr(group_chat, 'interaction_counters'):
group_chat.interaction_counters = {agent.name: 0 for agent in group_chat.agents if agent.name != "Researcher"}
# Define a maximum number of interactions per participant
max_interactions = 6
# If the last speaker was the Researcher, find the next participant who has spoken the least
if last_speaker and last_speaker.name == "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 next((agent for agent in group_chat.agents if agent.name == next_participant), None)
else:
return None # End the conversation if all participants have reached the maximum interactions
else:
# If the last speaker was a participant, return the Researcher for the next turn
return next((agent for agent in group_chat.agents if agent.name == "Researcher"), None)
# Adding the Researcher and Customer Persona agents to the group chat
groupchat = autogen.GroupChat(
agents=[researcher, emily, john, sarah, tim, lisa],
speaker_selection_method = custom_speaker_selection,
messages=[],
max_round=30
)
Definir el gestor para pasar instrucciones y gestionar nuestra simulación. — Cuando comencemos, hablaremos solo con el gerente, quien hablará con el investigador y los panelistas. Esto usa algo llamado GroupChatManager en Autogen.
# Initialise the manager
manager = autogen.GroupChatManager(
groupchat=groupchat,
llm_config=llm_config,
system_message="You are a reasearch manager agent that can manage a group chat of multiple agents made up of a reasearcher agent and many people made up of a panel. You will limit the discussion between the panelists and help the researcher in asking the questions. Please ask the researcher first on how they want to conduct the panel." + generate_notice(),
is_termination_msg=lambda x: True if "TERMINATE" in x.get("content") else False,
)
Configuramos la interacción humana. – permitiéndonos pasar instrucciones a los distintos agentes que hemos iniciado. Le damos el mensaje inicial y podemos comenzar.
# create a UserProxyAgent instance named "user_proxy"
user_proxy = autogen.UserProxyAgent(
name="user_proxy",
code_execution_config={"last_n_messages": 2, "work_dir": "groupchat"},
system_message="A human admin.",
human_input_mode="TERMINATE"
)
# start the reasearch simulation by giving instruction to the manager
# manager <-> reasearcher <-> panelists
user_proxy.initiate_chat(
manager,
message="""
Gather customer insights on a supermarket grocery delivery services. Identify pain points, preferences, and suggestions for improvement from different customer personas. Could you all please give your own personal oponions before sharing more with the group and discussing. As a reasearcher your job is to ensure that you gather unbiased information from the participants and provide a summary of the outcomes of this study back to the super market brand.
""",
)
Una vez que ejecutamos lo anterior, obtenemos la salida disponible en vivo dentro de su entorno Python, verá los mensajes que se transmiten entre los distintos agentes.
Ahora que nuestro estudio de investigación simulado ha concluido, nos encantaría obtener más información útil. Podemos crear un agente de resumen para ayudarnos con esta tarea y también usarlo en un escenario de preguntas y respuestas. Aquí solo tenga cuidado con las transcripciones muy grandes; necesitaría un modelo de lenguaje que admita una entrada más grande (ventana contextual).
Necesitamos captar todas las conversaciones. – en nuestro panel de discusión simulado de antes para usarlo como mensaje (entrada) del usuario para nuestro agente de resumen.
# Get response from the groupchat for user prompt
messages = [msg["content"] for msg in groupchat.messages]
user_prompt = "Here is the transcript of the study ```{customer_insights}```".format(customer_insights="\n>>>\n".join(messages))
Elaboremos el mensaje del sistema (instrucciones) para nuestro agente de resumen: Este agente se concentrará en crearnos una boleta de calificaciones personalizada a partir de las transcripciones anteriores y nos brindará sugerencias y acciones claras.
# Generate system prompt for the summary agent
summary_prompt = """
You are an expert reasearcher in behaviour science and are tasked with summarising a reasearch panel. Please provide a structured summary of the key findings, including pain points, preferences, and suggestions for improvement.
This should be in the format based on the following format:```
Reasearch Study: <<Title>>
Subjects:
<<Overview of the subjects and number, any other key information>>
Summary:
<<Summary of the study, include detailed analysis as an export>>
Pain Points:
- <<List of Pain Points - Be as clear and prescriptive as required. I expect detailed response that can be used by the brand directly to make changes. Give a short paragraph per pain point.>>
Suggestions/Actions:
- <<List of Adctions - Be as clear and prescriptive as required. I expect detailed response that can be used by the brand directly to make changes. Give a short paragraph per reccomendation.>>
```
"""
Definir el agente de resumen y su entorno. — Creemos un mini entorno para que se ejecute el agente de resumen. Esto necesitará su propio proxy (ambiente) y el comando de inicio que extraerá las transcripciones (aviso_usuario) como entrada.
summary_agent = autogen.AssistantAgent(
name="SummaryAgent",
llm_config=llm_config,
system_message=summary_prompt + generate_notice(),
)
summary_proxy = autogen.UserProxyAgent(
name="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,
)
Esto nos brinda un resultado en forma de boleta de calificaciones en Markdown, junto con la capacidad de hacer más preguntas en un chatbot estilo preguntas y respuestas además de los hallazgos.