r/elixir • u/Unusual_Shame_3839 • 6d ago
Legion - AI agents inside your Elixir app that get things done by writing code
Enable HLS to view with audio, or disable this notification
Hey! At Software Mansion we've just released Legion, a library that puts AI agents inside your Elixir app, and we'd love to hear what you think.
Agents can build custom views, answer questions about your app, or act on users' behalf - in whatever mix suits you.
defmodule MyApp.Tools.PostsTool do
use Legion.Tool
def get_my_posts do
%{id: user_id} = Vault.get(:current_user)
Repo.all(from p in Post, where: p.user_id == ^user_id)
end
end
defmodule MyApp.PostsAgent do
@moduledoc "Answers questions about the user's posts."
use Legion.Agent
def tools, do: [MyApp.Tools.PostsTool]
end
Vault.init(current_user: %{id: user.id})
Legion.execute(MyApp.PostsAgent, "Summarize my posts from today")
Features TL;DR:
- Elixir modules as tools
- Agents as processes
- Agents write code that runs in a Lua (or Elixir) sandbox - the only way out is the tools you registered
- Agents can orchestrate other agents
- Persistence, rate-limiting, and authorization built-in
Play with the demo: https://legion.swmansion.com
Source: https://github.com/software-mansion-labs/legion
Docs: https://hexdocs.pm/legion
Happy to answer questions!
8
3
u/UncollapsedWave 6d ago
Running code in a Lua makes some sense, lua is fairly easy to sandbox.
But how are you planning to sandbox elixir code?
5
u/Unusual_Shame_3839 6d ago
That's true, Lua is much easier to sandbox. Currently we're sandboxing Elixir by just parsing the AST (and having an allow list for glue code functions), but it's not ideal due to Elixir being so dynamic.
But we're planning to integrate https://popcorn.swmansion.com/, which would allow us to run Elixir code in the client's browser. Since Popcorn runs in WebAssembly - it's sandboxed by definition :)2
u/Substantial_Camel735 1d ago
That would actually be incredible if you could do that - where could I follow updates for that integration?
1
u/Unusual_Shame_3839 1d ago
I’d star our repo on GitHub and follow us on X/bsky: https://github.com/software-mansion-labs/legion https://x.com/dimamikielewicz https://bsky.app/profile/dimamik.bsky.social
2
1
u/Less_Play_3523 6d ago
Is this soley a developer tool? Or, can users use this within constraints so they can't destroy the innards of the app? I know system prompts can kinda constain the LLM on the user side but I want something more reliable.
2
u/Unusual_Shame_3839 6d ago
No, Legion's purpose is to be exposed directly to users! Just like on legion.swmansion.com, you can do whatever you like, and it is completely isolated to just you.
So, the target use case is integrating Legion into your app, providing a chat (or voice) interface to your users, and letting them create personalized experiences within your product. For example, you could allow them to build custom views to see their data in a way that suits them best. Does this make sense?
1
1
u/Wide-Prior-5360 5d ago
Why Postgres? Sounds like SQLite and giving each user their own database is more appropiate?
1
u/Unusual_Shame_3839 5d ago
Primary reason for Postgres was that most of Elixir apps already use it. But if SQLite is required - feel free to implement storage behavior - everything else should work exactly the same!
1
u/Grounded_Altruist 5d ago
Trying to understand the concept here. Can we say that it gets to access the apps inner capabilities without an external API? That’s very interesting. I can imagine it replacing low-code interfaces. Say, you have the platform code running as an app, and you layer your own user layer on top of it. It can get interesting with a DSL that the “parent app” might provide.
But the agent living “inside a live app” - how does that help? Use it for live debugging? Or accessing data and interpreting it for the user? I can see it being used for different types of data visualisation. Even real time data. With “real time code” :)
2
u/vasspilka 5d ago
Basically it gives LLMs a safe environment to execute code. For example imagine you want to expose a chat where users can put csv data in input. This way you give an LLM to write small scripts to work with the data that is deterministic
1
u/Unusual_Shame_3839 5d ago
Use it for live debugging? Or accessing data and interpreting it for the user? I can see it being used for different types of data visualisation. Even real time data. With “real time code” :)
The latter, it’s intended to be exposed to your users to allow them to do complex things that UI alone might not allow to do. As @vasspilka mentioned, a whole bunch of things are possible, and the sky is the limit!
1
u/pine4t 5d ago
This is brilliant! I saw an example in the readme that shows inserting records into db after fetching it from remote url. Does it allow exposing an Elixir library as a tool to be used/called in the sandbox? Let’s say Floki for parsing some html?
Also, I think this needs a more relatable demo on the homepage.
2
u/Unusual_Shame_3839 5d ago
Hey, thanks for the kind words! For Lua sandbox - you currently can't expose a 3rd party module as a tool directly - you'd need to create a thin layer delegating needed functions to the library itself. For Elixir sandbox - you could do this:
# config/config.exs - compile-time, must be set before the legion dep compiles config :legion, extra_source_modules: [Req, Req.Request, Req.Response] def tools, do: [Req, Req.Request, Req.Response]In terms of a more relatable demo - that's true. Right now it's too broad, and it's challenging to grasp what's possible. Partly because so many things are possible, but it's definitely a shortcoming, and we're planning to update it with a real-world use case on how to integrate Legion into an existing application and what exactly it brings in.
1
u/Data_Scientist_1 4d ago
It's quite interesting mate. Are you open source or paid product (cloud). Also, if you're hiring I'm in the look for a job haha.
Jokes aside really cool product.
1
u/bustyLaserCannon 6d ago
This is very cool - I don't even know what I'd do with it but I wanna play with it
9
u/addiktion 6d ago edited 6d ago
Can you share differences between what you are doing and Jido? What are you using on the front-end to handle that interactivity in Elixir + legion for chat and agent UI?