Craft an Iron Sword: Dynamically Generating Interactive Game Characters by Prompting Large Language Models Tuned on Code

Ryan Volum, Sudha Rao, Michael Xu, Gabriel A DesGarennes, Chris Brockett, Benjamin Van Durme, Olivia Deng, Akanksha Malhotra, Bill DolanWordplay @ NAACL 2022

TL;DR

This paper introduces a novel approach to creating dynamic, interactive Non-Player Characters (NPCs) in video games by leveraging large language models (LLMs) fine-tuned on code. By providing a few conversational examples, the system can generate both natural language responses and executable code, allowing NPCs to perform in-game actions and engage in free-form, multi-turn conversations. This significantly enhances player experience by moving beyond scripted interactions.

What Problem Does This Paper Address?

Traditional NPCs in video games are often limited to pre-scripted dialogue options and cannot dynamically alter the game state. This leads to repetitive interactions and a less immersive experience. The authors aim to bridge this gap by enabling NPCs to understand and respond to player commands in a more natural, flexible way, including the ability to execute game actions by generating code. This is particularly challenging in complex game environments like Minecraft, which have rich APIs for in-game actions.

Methods

The core of the proposed solution is the use of OpenAI's Codex, a GPT-3 model fine-tuned on GitHub code. The authors employ a few-shot prompting strategy. This involves providing Codex with a carefully crafted prompt that includes:

  • Seed Prompt: A set of example conversational prompts and corresponding code snippets that demonstrate how to achieve basic NPC functionalities (e.g., moving, crafting, chatting).
  • Player Input: The player's command is appended to this seed prompt.
  • Code Generation: Codex then generates a completion that can be either natural language or executable code.
  • Function Wrapping: To simplify the interaction and map natural language to game actions, the authors created wrapper functions for the Mineflayer API (a JavaScript library for Minecraft bots). These wrappers abstract away lower-level API calls.
  • Stop Sequences and Syntactic Sugaring: Techniques like stop sequences are used to ensure the model generates only the desired NPC response, and syntactic sugaring helps map complex commands to simpler code.
  • Function Chaining: The system can chain multiple function calls together to execute more complex sequences of actions, as demonstrated in crafting or escape room scenarios.

Datasets

The primary "dataset" used for training and evaluation is implicitly the vast amount of code and natural language data that OpenAI Codex was pre-trained on. For the specific application, the authors used Minecraft as their testbed. They conducted a user study with eight experienced Minecraft players who interacted with the Codex-powered NPC. The study involved specific tasks like crafting recipes, mining resources, crafting items, and escaping from two in-game rooms.

Key Insights and Ideas

  • Unified Code and Language Generation: The most significant insight is that a single LLM (Codex) can effectively generate both natural language responses and functional code for game actions, seamlessly integrating conversational AI with game mechanics.
  • Prompt Engineering is Key: The success of the system heavily relies on the design of the prompt, which needs to include illustrative examples of both conversational turns and code generation to guide the model.
  • Minecraft as a Rich Testbed: Minecraft's open-world nature and extensive API make it an ideal environment for testing and demonstrating the capabilities of such dynamic NPCs, showcasing the potential for complex interactions.
  • Player Expectations vs. Current Capabilities: The user study revealed that players quickly develop expectations for NPCs to have visual capabilities and a consistent persona, highlighting areas where current LLM-based NPCs still fall short.
  • Few-Shot Learning for Game Actions: The ability to perform novel actions and generate complex function chains with just a few examples in the prompt is a powerful demonstration of few-shot learning in a dynamic, interactive environment.

Key Results

  • Successful Task Completion: The NPC was able to assist players in various tasks, including obtaining crafting recipes, mining resources, crafting items, and successfully escaping from two escape rooms.
  • High Success Rate in Crafting: For getting crafting recipes, the success rate was 85% for specified resources and 75% for the "any resource" category.
  • Multi-Turn Conversation: The NPC could maintain context across multiple turns, responding to follow-up questions and instructions.
  • Code Generation for Actions: The NPC successfully generated code to perform actions like mining, crafting, and dropping items.
  • User Satisfaction: A majority of participants found the interaction fun and would interact with such an NPC if it existed in a game, indicating positive user reception.

Limitations and Future Work

  • Visual Capabilities: The NPC lacks visual understanding, leading to misunderstandings when players refer to objects or locations visually (e.g., "place the crafting table here"). Future work should incorporate visual input.
  • Context Window Limitations: Long conversations can exceed the prompt's token limit, causing the NPC to lose context. Solutions like multi-stage prompting or context pruning are suggested.
  • Factual Inaccuracies and Inconsistencies: The NPC sometimes provides incorrect answers to general knowledge questions or exhibits inconsistent personas. Integrating external knowledge sources and persona management strategies are proposed.
  • Repetition: NPCs can become repetitive, especially in long, non-action-oriented conversations. Metaprompt programming or multi-stage prompting could help.
  • Calling Non-Existent Functions: The NPC occasionally attempts to call functions not present in the API. Fine-tuning on the game API or providing a more comprehensive list of functions in the prompt could mitigate this.
  • Persona and History: Users desire NPCs with consistent personas and a history of past interactions, suggesting the need for more sophisticated memory and personality modeling.
  • Ethical Considerations: The authors acknowledge the risk of exposing users to offensive content from the training data and the potential for misuse, emphasizing the need for guardrails and safety mechanisms.

Summary updated

BibTeX
@inproceedings{volum2022craft,
  author = {Ryan Volum and Sudha Rao and Michael Xu and Gabriel A DesGarennes and Chris Brockett and Benjamin Van Durme and Olivia Deng and Akanksha Malhotra and Bill Dolan},
  title = {{Craft an Iron Sword: Dynamically Generating Interactive Game Characters by Prompting Large Language Models Tuned on Code}},
  booktitle = {Wordplay @ NAACL},
  year = {2022},
  url = {https://openreview.net/forum?id=I9glM3N6iAa},
}
Figure