> "Never Send A Human To Do A Machine's Job." (Agent Smith)
Five years ago [I published a blog post](https://ice09.github.io/pay-robots-with-crypto-money/) about robots paying other robots' services with crypto, in that case Ethereum, xDai or Libra (yes, it's long ago)...
Fast forward five years, Libra died, xDai never really took off, Ethereum still grows and prospers and **robots** turned into ... **agents**!
If you `s/robot/agent/` in the original blog post, it makes a lot more sense now. You can see that I struggled to actually find use cases the robots would play or receive money for (I thought of a drone being paid for transfering things and a camera being paid for taking pictures of a specific thing or place). _Now we have LLMs and GenAI!_ What great services we can now offer for other agents to consume!
![image](https://hackmd.io/_uploads/H1YTpaULke.png)
For our example we ignore that even ChatGPT can easily create a great scene with text and image combined and we instead assume that there are two agents: _The Poet_ and _The Artist_.
_The Poet_ realizes that it can only fulfil the user request if it uses the help of _The Artist_. Of course this agent also wants to earn money, maybe it has to pay for other services as well or its creator is just greedy.
Everything else from the original blog post is still valid, I would use Arbitrum L2 on Ethereum instead of xDai now - and it works great, I tried it. For the implementation it is simply exchanging the EVM URL after some setup (to get Sepolia test ETH in the Arbitrum testchain - not as easy as it should be, but still doable).
### Tools and Function Calling in LangChain4j
But how does _The Poet_ know when to call _The Artist_ for help? With [LangChain4j](https://docs.langchain4j.dev/), this is really easy, we can use [Function Calling](https://docs.langchain4j.dev/tutorials/tools) to implement this feature.
There is an [example in the LangChain4j repository](https://github.com/langchain4j/langchain4j-examples/blob/main/other-examples/src/main/java/ServiceWithToolsExample.java) which shows the usage of Function Calling aka Tools:
```java
class Calculator {
@Tool("Calculates the length of a string")
int stringLength(String s) {
System.out.println("Called stringLength with s='" + s + "'");
return s.length();
}
@Tool("Calculates the square root of a number")
double sqrt(int x) {
System.out.println("Called sqrt with x=" + x);
return Math.sqrt(x);
}
}
```
The command sequence is then a back and forth calling of methods. The interesting part is the return value of the LLM (in this case GPT-4o) which instructs the client to call the two methods and pass back the results for the LLM to include this result into the final response.
![image](https://hackmd.io/_uploads/S1yg6CULJx.png)
If we basically just exchange the Tooling with "Draws an image for a prompt" we can use this `@Tool` for _The Poet_ to call an external image generation service (_The Artist_). Also _The Artist_ could have `@Tool` "Creates atmospheric sounds for a prompt" which then would call _The Composer_ and add the audio to the result.
We just need to implemented this `@Tool` and LangChain4j does all the other (_glueing_) work with Function Calling and GPT-4o (which provides Function Calling):
```java
public class ArtistAssistant {
@Tool("Creates an image for a prompt")
public byte[] createImageForPrompt(String prompt) {
return callExternalImageCreationServiceWithPayment(prompt);
}
}
```
For the initial example, the `ToolExecutionRequest` of LangChain4j is:
```json
{
"name":"createImageForPrompt",
"arguments":{
"prompt":"A steampunk cowboy showdown in a dusty desert town.
The scene features two cowboys facing each other,
each wearing a mix of traditional cowboy attire and steampunk
elements like goggles, gears, and mechanical arms.
The background shows a saloon with steam-powered machinery and
a clock tower with visible gears.
The sky is a dramatic sunset with orange and purple hues,
casting long shadows on the ground.
Dust swirls around their boots as they prepare to draw their weapons,
which are a blend of revolvers and intricate steampunk gadgets."
}
}
```
This way we can create a chain of service executions and payments for these service executions to facilitate an _Autonomous Service Economy_ with AI agents and crypto payments.