How to Use Phi 2 Persona-Chat for Conversational AI

Apr 3, 2024 | Educational

In the vast world of conversational AI, creating engaging and believable characters is key. The Phi 2 Persona-Chat model is a fine-tuned version of the base Phi 2 model designed to facilitate just that. With its ability to generate text-based on specific persona attributes, it’s an excellent choice for developers looking to enhance user interaction scenarios. This guide will walk you through running the Phi 2 Persona-Chat model step-by-step, while also providing insights for troubleshooting.

Understanding Phi 2 Persona-Chat

The Phi 2 Persona-Chat model utilizes a specialized dataset known as nazlicantopersona-based-chat, which includes over 64,000 conversations between different personas. Think of this dataset as a well-curated library of dialogues where every participant adds depth to their role, allowing the model to learn various conversational styles and persona traits.

Prerequisites to Run the Model

Before you dive in, ensure you have the following ready:

  • Python environment set up with the necessary packages installed: torch, transformers, and datasets.
  • Access to the Hugging Face model hub for fetching the Phi 2 model.

Step-by-Step Instructions

1. Import Libraries

Start by importing the necessary libraries.

from random import randrange
import torch
from datasets import load_dataset
from transformers import AutoTokenizer, AutoModelForCausalLM

2. Create Your Prompt

A well-crafted prompt sets the stage for the conversation. Here’s an example of how to structure yours:

prompt = f"""Person B has the following Persona information.
Persona of Person B: My name is David and I'm a 35 year old math teacher.
Persona of Person B: I like to hike and spend time in nature.
Persona of Person B: I'm married with two kids.
Instruct: Person A and Person B are now having a conversation. 
Following the conversation below, write a response that Person B would say based on the above Persona information.
Persona A: Morning! I think I saw you at the parent meeting, what's your name?"""

3. Load the Model and Tokenizer

Next, you need to load the fine-tuned model and its tokenizer. Remember, you’ll need to set trust_remote_code=True for this model to work properly.

model = AutoModelForCausalLM.from_pretrained("nazlicantophi-2-persona-chat", trust_remote_code=True)
model.to("cuda")
tokenizer = AutoTokenizer.from_pretrained("nazlicantophi-2-persona-chat", trust_remote_code=True)

4. Tokenize the Input Prompt

Convert your prompt into token IDs the model can understand.

input_ids = tokenizer(prompt, return_tensors="pt", truncation=True).input_ids.cuda()

5. Generate the Output

Now it’s time for the model to produce a response!

with torch.inference_mode():
    outputs = model.generate(
        input_ids=input_ids,
        max_new_tokens=50,
        do_sample=True,
        top_p=0.1,
        temperature=0.7
    )
# decode output tokens
outputs = outputs.detach().cpu().numpy()
outputs = tokenizer.batch_decode(outputs, skip_special_tokens=True)
output = outputs[0][len(prompt):]
print(output)

Analogy to Enhance Understanding

Imagine you’re a director in a movie, and you have actors (representing the AI model) who need to embody their characters (the personas). The script (model prompt) you give them sets the conversation scene. Each actor must rely on their character’s background (persona information) to respond authentically to the dialogue (the input prompt). The more detailed the character profile, the more engaging the conversation will be, allowing users to feel they’re truly interacting with someone real.

Troubleshooting Tips

If you run into issues when executing the above code, consider these troubleshooting strategies:

  • Ensure your Python environment has compatible versions of the torch and transformers libraries.
  • Double-check that your GPU is correctly set up, and that you’re calling model.to("cuda") if using a GPU.
  • If you encounter model loading errors, verify your internet connection and that Hugging Face’s model API is operational.
  • For further help or to connect with a community of developers and innovators, reach out to **[fxis.ai](https://fxis.ai)**.

Conclusion

With Phi 2 Persona-Chat, you can bring characters to life in your applications, leading to much more engaging user interactions. By following the guidelines above, you’ll be well-equipped to harness this powerful model in your projects.

At **[fxis.ai](https://fxis.ai)**, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox