How to Implement Generative Agents with Guidance Using Langchain and Local LLMs

Jul 21, 2023 | Data Science

Generative Agents have gained attention for their ability to simulate human behavior in interactive environments. In this article, we will walk through the implementation based on the paper Generative Agents: Interactive Simulacra of Human Behavior, utilizing Langchain and local LLMs. We will simplify the process for you, making it as user-friendly as possible, with tips on troubleshooting along the way.

Supported Features

  • Work with local LLM
  • Memory and Retrieval
  • Reflection
  • Planning (with room for improvement)
  • Reacting and Re-planning
  • Dialogue Generation (in need of some refinement)
  • Agent Summary
  • Interview functionality
  • Web UI (Gradio) – to be implemented

How to Get Started

To begin, you need to install various Python packages that will serve as the backbone of our implementation.

Step 1: Install Python Packages

Run the following commands to install these necessary packages:

pip install guidance==0.0.63

Then, install the following packages:

git clone https://github.com/oobabooga/GPTQ-for-LLaMa.git
pip install langchain==0.0.190
git clone https://github.com/facebookresearch/faiss.git

For the GPTQ-for-LLaMa model, follow the instructions available here.

Step 2: Running the Code

You can check the provided notebook file for all implementation details. The recommended model to try is the wizard-mega-13B-GPTQ, but feel free to experiment with others.

Code Implementation

Let’s create a generative agent named Sam. This is where the analogy comes in: think of Sam as a digital character in a video game, complete with unique traits and memories that influence his behavior.

Just like every character in a video game has a set of attributes defining them – like health, skills, and backstory – Sam has similar attributes defined in the code:


description = "Sam is a Ph.D student, his major is CS; Sam likes computers; Sam lives with his friend, Bob; Sam's father is a doctor; Sam has a dog named Max."
sam = GenerativeAgent(
    guidance=guidance,
    name="Sam",
    age=23,
    des=description,
    trails=("funny", "like football", "play CSGO"),
    embeddings_model=embeddings_model
)

Step 3: Adding Memories

Next, we introduce memories into Sam’s character. Imagine Sam having experiences that shape his reactions just like you do in real life.


sam_observations = [
    "Sam woke up in the morning.",
    "Sam feels tired because of playing games.",
    "Sam has an assignment for his AI course.",
    "Sam sees Max is sick.",
    "Bob says hello to Sam.",
    "Bob leaves the room.",
    "Sam says goodbye to Bob."
]
sam.add_memories(sam_observations)

Step 4: Summarizing Sam

To get a quick overview of Sam’s current situation, we can generate a summary. This is like having a character status screen in a game.


summary = sam.get_summary(force_refresh=True)
print(summary)

Troubleshooting

While implementing, you might run into a few hurdles. Here are some tips to help you out:

  • Ensure all dependencies are installed correctly. Missing libraries can lead to unexpected errors.
  • Double-check the versions of your installed packages to ensure they match the recommendations. For stability, using guidance==0.0.63 is key.
  • If you encounter issues with memory management, consider reviewing how you add observations, or check the logic behind your dialogue generation.
  • For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

At 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.

Conclusion

With this guide, you should be well on your way to implementing generative agents that mimic human behavior. Whether for gaming, simulations, or research, this technology has immense potential. Happy coding!

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

Tech News and Blog Highlights, Straight to Your Inbox