Repository navigation
Description
What are you trying to do?
Environments are a way to define a specific set of functions an LLM will be able to see as tools. This allows to reduce the number of tools the LLM will see or to provide specific tools, both in order to improve the ability to complete a task.
Note
This is extracted from #8030 but focusing only the LLM/Env/type awareness and not on the self calling aspects.
It's currently possible to use a primitive type as an environment input (like Container or Directory for instance). But to use a specific type, a workspace for instance, that embeds primitives and expose only certain functions, this needs to be declared in a different module, and used as a dependency.
This makes the usage of environments more complex where it could be interesting to have the code of the type we want to expose to the LLM at the same place than the rest of the code calling the LLM.
The current limitation is the generated code on the Env type (like withXXXInput, for instance withContainerInput) is not aware of the actual types defined by the module. The generated code is only aware of the types that are exposed to the engine, including dependencies of the current module, but not the module itself.
Use Case
Context:
- a type
Workspacecontaining several functions to be seen as tools for the LLM
Currently:
dag.LLM().WithEnv(dag.Env().WithWorkspaceInput("input", dag.Workspace(src), "..."))....dag.Workspacerefers to aworkspacemodule, as a dependency of the current module
Goal:
dag.LLM().WithEnv(dag.Env().WithWorkspaceInput("input", Workspace{src}, "..."))....Workspacehere is a local type
Technical feasibility
This means the generated code on Env is aware of the local types. If not possible then it means we have to generate types as we do usually for the dependencies and have something more like dag.Env().WithWorkspaceInput("input", dag.Self().Workspace(src), "...")but that would be more confusing for the users.
Activity
eunomie commentedon May 6, 2025
Let's say I have a module
my-agentthat contains a typeworkspace(astructin Go, aclassin Java, etc).What I want is:
Type Exposure
By default the
Workspacetype is not exposed. It needs to be exposed by a function of the module to be exposed. So it needs somethings like that:Then we can imagine to keep the above call or to replace it by
Note
In Java all types are exposed if they have the
@Objectannotation. So we don't need this kind of function. Not sure that's the way to go or not.Circular Dependency
The
WithWorkspaceInputfunction is defined at the level of theEnvtype. In Go this is indagger/my-agent/internal/dagger. (I'm using Go as a reference, dependency management vary between languages but Go doesn't handle circular dependencies)This type needs to have access to the package where
Workspaceis defined,dagger/my-agent.But
dagger/my-agentimportsdagger/my-agent/internal/daggerto get theEnvtype.So we just created a circular dependency here.
Generated Types
One way to solve this circular dependency issue is to never have
internal/daggerimporting code defined at the module level.In this case
WithWorkspaceInputshould only refer to a generated type. So we need to create a type at the Dagger client level, likedag.Workspace.But then, there's the question of how to create an instance. If we reference the module from an other one, we would have something like:
If the the module is registered and exposed under a
Selffunction/keyword, then we can imagine to have:In this case we can't directly use the type we defined neither the
Workspacefunction above.Should also the
WithWorkspaceInputbe something likeWithSelfWorkspaceInput?shykes commentedon May 6, 2025
@eunomie I don't have the complete answer, but wanted to make a more general note on self-calls: if possible, I think we should avoid
dag.Self()and instead usedag.Foo()wherefoois the name of the module. In other words, the naming of the binding should be the same as for regular dependencies.I think there are several reasons for this. But one specific reason for me, is that I want to leave the door open to having more than one runtime per module in the future ("runtime stacking") - which means there would no longer be a single "self" to reference.
vito commentedon May 6, 2025
@eunomie The
dag.Self().Workspace()approach (ordag.MyModule().Workspace()per #10336 (comment)) makes the most sense to me, at a technical level.The problem with directly passing a
&Workspace{}is that all object arguments work by passing the object's ID, which is generated through the process of making API calls. If you directly create a&Workspace{}and pass it in, we don't have an easy way to translate that to an ID at the API layer.Building on that, I would also expect a single
WithWorkspaceInputthat takes a*dagger.Workspaceinstead of a*Workspace. I don't think we need aWithSelfvariant. 🤔Context dumping:
In many cases the motivation for self calls is actually to avoid natively using the local code bindings. In Go, for example, you might find yourself with this type of signature:
If I were to use the native code, I would have to repeat those defaults at each call site (
myMod.MyFunc(42, "hey", "world")). With self-calls I can rely on the API defaults (dag.MyMod().MyFunc(42)).A separate motivator for using the API instead of native code is caching. Since it goes out and back in through the API layer,
dag.MyMod().MyFunc(42)will be cached independently, whereasmyMod.MyFunc(42, "hey", "world")will not. In a lot of cases this doesn't actually matter much, sinceMyFuncmight just return a lazy*Containeror something, but still.shykes commentedon May 6, 2025
@vito also tracing 😍
eunomie commentedon May 15, 2025
All right, I have a first PoC ~working. I'll open a draft PR with it. It's just a PoC, there's multiple issues and improvements required:
javaSDK and it requires changes on the SDK sideBut I'm able to run my previous demo with the workspace within the main module, so without the need of a second/external one.
AlpineWorkspaceis a java class defined in the same package.A few notes based on this example:
dag().selfAgent().alpineWorkspace()instead of passing a simplenew AlpineWorkspace()kpenfound commentedon May 15, 2025
Excited to get this in!
My main feedback is agreeing with what @vito said earlier
I think the
withSelfAgentAlpineWorkspaceInputinstead ofwithAlpineWorkspaceInputis too confusing. Users don't want to care about our internal technical constraints of passing objects into the API, they just want to create a class and put it in an environment.I also still really want a way to pass in the object directly. I understand the reasons why that's hard, but it would be great if we had some SDK side translation that can construct the object the dagger way and hide that from the user.
Finally, unlocking this capability really advances the DX of passing objects in and out of the Dagger API, and I think we must solve the constructor issue. Requiring the user to make a constructor for the secondary object inside of the main object is going to be too confusing to everyone
jpadams commentedon May 15, 2025
+1 todag.MyModule().AlpineWorkspace()whereMyModule==selfThinking on this more...
kpenfound commentedon May 15, 2025
As I understand it, the current proposal would look like this in Go:
I'm advocating for this instead for the reasons stated in my previous comment:
eunomie commentedon May 16, 2025
I agree, that would be a better solution from a DX perspective for sure 👍
An intermediate step would be to remove the need of the function (to replace the constructor) then to remove the module prefix.
Then the question of the local type, I have some ideas, but I don't know yet how much they are good ideas or not...
In the above example I shared, one confusion could be my module is named
SelfAgent. I should have avoidself😅