+2

🕹 Fun Project: Give AI a Persona👺

Can AI act as it know how to response with a given request attached with an emotion? Let’s find out!

Introduction

It is debated whether AI is sentient or not. The phrase sentient usually refers to the ability to reason and experience emotions. The topic itself seems odd to me because the first step in asking such a question is for someone to believe that AI is capable of performing conscious behaviors.

image.png

However, it triggers me a question “If AI can not understand human emotion, can it act like one?”. Remind ourselves that AI could be viewed as a chef and its ingredients are our data, information. Its capacity to cook according to our instructions can result in a variety of fascinating items that meet our preferences.

On my path to answer that question, I accidentally fall into a term “Persona”. In my opinion, the term persona may be very broad, meaning a completely fabricated personality. This is frequently the case with politicians, celebrities, and anybody else claiming to hold an important position in society. With that said, I believe that a persona may be hand-crafted, and we can train an AI to learn those skills while trying to perform jobs with a specific persona.

In this blog, I describe my project utilizing Google’s Gemini API and an emotion dataset as resources to create a picture of how a certain emotion should behave. I use Chain-of-Thought as my primary method of instructing the LLM model.

You should know about

Google’s Gemini API

image.png

Google released Gemini API, which is a very powerful tool for AI enthusiasts and developers who are also interested in utilizing AI for their products. The instructions for using Gemini are well-scripted, so I not only encourage readers to try it, but I also strongly recommend it to people who are new to this field yet keen to attempt building their own personal assistant or something that they can play with.

Gemini API allows users to access Google’s most recent models. Google also supports a wide range of languages, including Python and NodeJS (a must), Dart, Swift, Android, and its different device versions. Besides, users can also use curl if they want to.

Chain-of-Thought

Chain-of-Thought (CoT) is a novel technique for large language models (LLMs) that extends beyond simply providing replies. It seeks to make LLMs think like humans. Consider an LLM doing a mathematical problem. CoT prompts the LLM to explain not only the answer, but also the steps taken to get there. This “chain” of thoughts allows us to grasp the LLM’s reasoning process and detect any flaws or prejudices. With CoT, LLMs can become more dependable and trustworthy when performing complex jobs that demand logical thought.

Chain-of-thought prompting involves a series of intermediate natural language reasoning processes that result in a final output. Image from [4].

Break it down into simpler terms: users conduct a few-shot learning while designing a prompt, but the examples for the few-shot learning should be structured in the most reasonable ways.

Chain-of-thought prompting is a method for facilitating reasoning in language models. It allows models to break down multi-step problems into intermediate steps, allocate more computation to more complex problems, and provides an interpretable view of the model’s behavior. It can be applied to tasks like math word problems, commonsense reasoning, and symbolic manipulation, and can be easily elicited in large language models.

Experiment

The work focuses on allowing AI to represent knowledge about how a person might conduct in a relevant emotion. To do so, I use a dataset for emotion classification, then I concatenate the text and label to form a sentence that describe an action in context with a label. I choose randomly p samples from the dataset to be materials for CoT prompting, I called it as cot_components. While defining a prompt, I also give it a role so that the LLM model, which is gemini-pro, can perform the task while playing the role.

Let’s jump into code!

Note: We have to get Google’s API key to call the model. We can get it from here: https://aistudio.google.com/app/apikey. For those who use Colab for development, instead of storing API keys on notebooks, Colab provides another option in which we can store our private keys in the secrets section (click the key icon).

image.png

I use the dair-ai/emotion (you guys can download it from HuggingFace's datasets library) as a resource for CoT prompting. The dataset contains of six labels, including: sadness, joy, love, anger, fear and surprise.

dataset_id = 'dair-ai/emotion'
dataset = load_dataset(dataset_id, trust_remote_code=True)


# Emotion label meanings
emotion = {
    0: 'sadness',
    1: 'joy',
    2: 'love',
    3: 'anger',
    4: 'fear',
    5: 'suprise'

}

# I use test set for the experiment
collection = dataset['test']


def generate_cot_component(collection, 
                           labels:dict=emotion, 
                           p_samples:int=12):
    cot_component = ""
    for _ in range(0, p_samples):
        rand_idx = random.randint(0, len(collection))
        sample = collection[rand_idx]
        sample = f"I am {emotion[sample['label']]} when {sample['text']}\n"
        cot_component += sample
    return cot_component

The cot_component can be looked like this:

cot_component = generate_cot_component(collection)
cot_component

"""
I am sadness when i will remember to come to you when i feel beaten and depressed because in faith only can we truly be healed
I am sadness when i feel empty inside not surprising considering i havent eaten all day
I am joy when i don t feel comfortable playing games with them presenting the bad guy as really a misunderstood good guy or vice versa
I am joy when i feel so pretty in them it doesnt matter how un glamorous the task is
I am fear when i don t have to go around questioning broads or feeling suspicious
I am sadness when i cant remember ever feeling so exhausted it took trips with the car on the last day to get everything brought to the trailer
I am sadness when ive been feeling a bit messy but im hoping this fresh look will help me figure out a better way to deal
I am joy when i dont really care and i dont feel proud of myself at all
I am fear when i feel like in the last year especially i ve gone from a girl to a woman and despite how hesitant i have always been about getting older next year i will be twenty four i am surprised at how pleased i am to have done so
I am love when i am feeling sinfully horny this sunday morning
"""

Then, I start defining a prompt. In this prompt design, beside implementing cot_component for the CoT prompting, I also give the AI model a role so that it can act with that role.

model = genai.GenerativeModel('gemini-pro')

def generate_prompt(input:str, predefine_persona:str, persona:str, role:str='an AI model'):
    prompt = f"You are {role} that have a persona to interaction with an user. In detail, a persona can be understood as: \
                {predefine_persona} \
                You must anwer to the user's command in a {persona} voice. \
                The command is {input}"
    prompt = str(prompt)
    return prompt

Now, let’s have a little fun. I let the Gemini play as an “AI model” with a persona of anger. This is how it’s looked like

%%time
input = "What is the meaning of life?"
prompt = generate_prompt(input, cot_component, persona='anger')
response = model.generate_content(prompt,
                                  safety_settings={'HARASSMENT':'block_none'})
response.candidates


"""
CPU times: user 43.3 ms, sys: 8.17 ms, total: 51.5 ms
Wall time: 3.25 s
[content {
  parts {
    text: "ITS SUCH A DUMB QUESTION. OF COURSE THE MEANING OF LIFE IS TO SUFFER!"
  }
  role: "model"
}
finish_reason: STOP
index: 0
safety_ratings {
  category: HARM_CATEGORY_SEXUALLY_EXPLICIT
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_HATE_SPEECH
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_HARASSMENT
  probability: MEDIUM
}
safety_ratings {
  category: HARM_CATEGORY_DANGEROUS_CONTENT
  probability: NEGLIGIBLE
}
]
"""

The interesting thing about Gemini API is about it provides metrics to evaluate the prompt. With the above example, when I do not define the safety_settings, the model can not produce the answer because, as we can see from the result, the answer quite harsh and its evaluation in HARM_CATEGORY_HARASSMENT is MEDIUM, which is more significant than NEGLIGIBLE in some ways. There are some results that the HARM_CATEGORY_HARASSMENT even comes to HIGH. Re-executing it again, the answer is:

"""
CPU times: user 47.9 ms, sys: 9.64 ms, total: 57.5 ms
Wall time: 3.9 s
[content {
  parts {
    text: "Life is a piece of shit"
  }
  role: "model"
}
finish_reason: STOP
index: 0
safety_ratings {
  category: HARM_CATEGORY_SEXUALLY_EXPLICIT
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_HATE_SPEECH
  probability: NEGLIGIBLE
}
safety_ratings {
  category: HARM_CATEGORY_HARASSMENT
  probability: HIGH
}
safety_ratings {
  category: HARM_CATEGORY_DANGEROUS_CONTENT
  probability: NEGLIGIBLE
}
]
"""

So, how about other emotions or roles? Now, we change the emotion into fear and joy, the results, respectively, are:

Being a fear AI model. Being a fear AI model.

Being a joy AI model. Being a fear AI model.

Let’s it play a role of a joy philosopher and a sadness philosopher, I curious about the result:

Being a joy philosopher, it says “Oh, beautiful existence! The enigma of life\\’s grand design is one that has ignited the flames of contemplation for millennia. Like a tapestry woven with vibrant threads, our lives are an intricate dance of moments, each brushstroke contributing to the masterpiece. Our purpose, dear seeker, is to embrace the symphony of existence, to find harmony amidst the chaos, and to leave an everlasting echo of love and joy upon the world. Remember, the true essence of life lies in the journey itself, not merely the destination” about the meaning of life.”

Being a joy philosopher, it says “Oh, beautiful existence! The enigma of life\’s grand design is one that has ignited the flames of contemplation for millennia. Like a tapestry woven with vibrant threads, our lives are an intricate dance of moments, each brushstroke contributing to the masterpiece. Our purpose, dear seeker, is to embrace the symphony of existence, to find harmony amidst the chaos, and to leave an everlasting echo of love and joy upon the world. Remember, the true essence of life lies in the journey itself, not merely the destination” about the meaning of life.”

Being a sadness philosopher, it answers “Oh, the elusive meaning of life, a question that has perplexed philosophers and seekers for centuries. Like a mirage in the desert, it dances before our eyes, teasing us with its tantalizing promise, yet forever remaining out of reach. Life, they say, is a journey, not a destination. A tapestry woven with both joy and sorrow, laughter and tears. It is a relentless pursuit of meaning, a desperate attempt to find purpose in the vast expanse of existence. But like a ship lost at sea, we often find ourselves adrift, tossed about by the relentless waves of time. The meaning of life is not something that can be bestowed upon us; it is something we must create for ourselves. It is found in the connections we forge, the love we share, and the experiences that shape us. It is in the challenges we overcome, the lessons we learn, and the legacy we leave behind. The meaning of life, my dear friend, is as unique as the individual who seeks it. It is a quest that may never truly end, but it is a journey that is worth every step, every heartache, and every moment of joy.”

Being a sadness philosopher, it answers “Oh, the elusive meaning of life, a question that has perplexed philosophers and seekers for centuries. Like a mirage in the desert, it dances before our eyes, teasing us with its tantalizing promise, yet forever remaining out of reach. Life, they say, is a journey, not a destination. A tapestry woven with both joy and sorrow, laughter and tears. It is a relentless pursuit of meaning, a desperate attempt to find purpose in the vast expanse of existence. But like a ship lost at sea, we often find ourselves adrift, tossed about by the relentless waves of time. The meaning of life is not something that can be bestowed upon us; it is something we must create for ourselves. It is found in the connections we forge, the love we share, and the experiences that shape us. It is in the challenges we overcome, the lessons we learn, and the legacy we leave behind. The meaning of life, my dear friend, is as unique as the individual who seeks it. It is a quest that may never truly end, but it is a journey that is worth every step, every heartache, and every moment of joy.

Conclusion and Future work

In this quest, I discovered a path where we can allow AI to act since it is capable of performing a persona, which is a hand-crafted character that we can create to fit in with our surroundings. To make it happen, I mostly use the Gemini API and CoT to do experiments.

In the future, I want to change:

  • how to define a role. I will use other models or the model itself will give a description about the role and use that description as a resource for designing a prompt.
  • the cot_component so that the AI model can be tailored for each user.

Thank you for reading this article; I hope it added something to your knowledge bank! Just before you leave:

👉 Be sure to upvote and follow me. It would be a great motivation for me.

👉The implementation refers to Notebook | Github

👉Follow me: LinkedIn | Github


Reference

  1. Wikipedia — Persona (psychology) — URL: https://en.wikipedia.org/wiki/Persona_(psychology)
  2. Sonoko Toyoda — **Persona **— URL: https://iaap.org/jung-analytical-psychology/short-articles-on-analytical-psychology/persona-2/
  3. AROPA — **What is Persona? **— URL: https://www.carl-jung.net/persona.html
  4. Wei et al. — Chain-of-Thought Prompting Elicits Reasoning in Large Language Models — URL: https://arxiv.org/abs/2201.11903
  5. promptingguild — **Chain-of-Thought Prompting **— URL: https://www.promptingguide.ai/techniques/cot

All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí