Fine-tune LLM agents with online reinforcement learning
🔗 Agents for Web Data Extraction • 🐦 Twitter
LlamaGym
"Agents" originated in reinforcement learning, where they learn by interacting with an environment and receiving a reward signal. However, LLM-based agents today do not learn online (i.e. continuously in real time) via reinforcement.
OpenAI created Gym to standardize and simplify RL environments, but if you try dropping an LLM-based agent into a Gym environment for training, you'd find it's still quite a bit of code to handle LLM conversation context, episode batches, reward assignment, PPO setup, and more.
LlamaGym seeks to simplify fine-tuning LLM agents with RL. Right now, it's a single Agent abstract class that handles all the issues mentioned above, letting you quickly iterate and experiment with agent prompting & hyperparameters across any Gym environment.
Usage
Fine-tuning an LLM-based agent to play in a Gym-style environment with RL has never been easier! Once you install LlamaGym...
pip install llamagym
First, implement 3 abstract methods on the Agent class:
from llamagym import Agent
class BlackjackAgent(Agent):
def get_system_prompt(self) -> str:
return "You are an expert blackjack player."
def format_observation(self, observation) -> str:
return f"Your current total is {observation[0]}"
def extract_action(self, response: str):
return 0 if "stick" in response else 1
Then, define your base LLM (as you would for any fine-tuning job) and instantiate your agent:
model = AutoModelForCausalLMWithValueHead.from_pretrained("Llama-2-7b").to(device)
tokenizer = AutoTokenizer.from_pretrained("Llama-2-7b")
agent = BlackjackAgent(model, tokenizer, device)
Finally, write your RL loop as usual and simply call your agent to act, reward, and terminate:
env = gym.make("Blackjack-v1")
for episode in trange(5000):
observation, info = env.reset()
done = False
while not done:
action = agent.act(observation) # act based on observation
observation, reward, terminated, truncated, info = env.step(action)
agent.assign_reward(reward) # provide reward to agent
done = terminated or truncated
train_stats = agent.terminate_episode() # trains if batch is full
Some reminders:
- above code snippets are mildly simplified above but a fully working example is available in
examples/blackjack.py - getting online RL to converge is notoriously difficult so you'll have to mess with hyperparameters to see improvement
- your model may also benefit from a supervised fine-tuning stage on sampled trajectories before running RL (we may add this feature in the future)
- our implementation values simplicity so is not as compute efficient as e.g. Lamorel, but easier to start playing around with
- LlamaGym is a weekend project and still a WIP, but we love contributions!
Relevant Work
- Grounding Large Language Models with Online Reinforcement Learning
- True Knowledge Comes from Practice: Aligning LLMs with Embodied Environments via Reinforcement Learning
Citation
bibtex
@misc{pandey2024llamagym,
title = {LlamaGym: Fine-tune LLM agents with Online Reinforcement Learning},
author = {Rohan Pandey},
year = {2024},
howpublished = {GitHub},
url = {https://github.com/KhoomeiK/LlamaGym}
}
Metadata
Release files for llamagym 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| llamagym-0.1.1.tar.gz | 5.2 kB | Without provenance | Details for llamagym-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| llamagym-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for llamagym-0.1.1-py3-none-any.whl |
Total release size: 10.9 kB
Release files / llamagym-0.1.1.tar.gz
| Download URL | llamagym-0.1.1.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
360a0b6315261018bf06aa6779bdb1c7205d07b20bda2ab4622076795109fc31
|
|
BLAKE2b-256 checksum How to use checksums |
b3f3c634acf96c26fffde64cfcaddb92b8e5102be67cd2f14a024cfe835cec5b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.9.18
|
Release files / llamagym-0.1.1-py3-none-any.whl
| Download URL | llamagym-0.1.1-py3-none-any.whl |
|---|---|
| Size | 5.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
24cf73effc11cc9d1fd5bef8d5d59816982c39ef8147a95b203f04094f04f046
|
|
BLAKE2b-256 checksum How to use checksums |
f72fe680605d96e1159ead0b06a82868477ad139acb5f8b95f72f5cab0b3d111
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.9.18
|
Security for llamagym 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.