![](https://hackmd.io/_uploads/ByFrkMxY2.png) ## Introduction Imagine an AI agent that could learn new tricks whenever it needed to. That's what our new project is all about – it's an agent that's powered by OpenAI's function API and uses Polywrap to learn skills on-demand via wraps, which are portable and composable software modules. ## What are AI agents? AI agents are like computer helpers that can make decisions and do tasks on their own. They're first given a goal and then create a series of tasks to work towards that goal. As they work through tasks, they process feedback from their environment and internal monologue to find the best path towards their objectives. A big challenge for AI agents is that their abilities are limited to their training. So, they struggle with tasks that they were not originally trained to handle. We recently created a prototype of an AI agent that overcomes this challenge. Let's dive in! ## AI Agent powered by Polywrap Our agent is dynamic and can enhance its skill set by accessing wraps when needed. Wraps equip the agent with on-demand capabilities that the agent might not have had before. As an example, suppose an AI agent needed to know a blockchain's Chain ID but it didn't have the capability to query Ethereum via a library like Ethers.js. Typically, the agent would need to be bundled with the Ethers.js package and then rebuilt and redeployed. The prototype agent that we've created works differently. It understands the tasks at hand, and if it doesn't have the proper capabilities to handle the tasks, it'll fetch wraps on the fly. ## Inside Our Agent: Understanding its Core Functions In this section, we're going to take a close look at the inner workings of our prototype AI agent. We'll focus on two critical functions that drive its on-demand learning capability: `LoadWrap` and `InvokeWrap`. These functions are the core of our AI agent's dynamism. By using `LoadWrap`, our agent can fetch and equip itself with new wraps, extending its skill set to tackle tasks it couldn't handle before. To leverage the new skills, our agent uses`InvokeWrap`. This function allows the agent to execute a task using the newly equipped wrap. Let's delve into these two functions and see how our AI agent learns on the fly. ### `LoadWrap` `LoadWrap` lets the agent fetch a specified wrap and accesses its functions. **Let's look at a scenario where a user prompts the agent to read a file located on the user's computer.** > **User:** Load the filesystem wrap. > > **Agent:** Do you wish to execute the following function? > >``` > LoadWrap ({ > "name": "filesystem" >}) > > (Y/N) >``` > **User:** Y > **Agent:** Wrap loaded Great! Now the agent is equipped with the `filesystem` wrap. ### `invokeWrap` `invokeWrap` is the command to execute a function on the wrap. > **User:** Read the file `package.json` from `./` directory > > **Agent:** Do you wish to execute the following function? > >``` >InvokeWrap ({ > "uri": "plugin/file-system@1.0.0", > "method": "readFileAsString", > "args": { > "path": "./package.json", > "encoding": "UTF8" > } >}) > > (Y/N) >``` > **User:** Y > **Agent:** *Prints `./package.json` contents* In the above scenario, the agent didn't have the `filesystem` wrap pre-loaded so it can't access the user's filesystem. By calling `LoadWrap`, the agent is able to fetch filesystem capabilities on-demand, and then `invokeWrap` to call the `readFileAsString` function. ## Diagram Here's a diagram showcasing the above user-agent flow. ![](https://hackmd.io/_uploads/B1YyNMxYn.png) ## The Wrap Library One of the foundational elements of our AI agent's ability to learn on-the-fly is the wrap library. Think of it as a toolkit or a repository filled with versatile capabilities ready to be utilized by the agent. The library consists of wrap manifest files that instructs the agent on where to fetch, analyze and consume the wrap. Here's an example of the `ethers.json` wrap manifest file: ```json { "aliases": ["ethereum", "eth"], "description": "Wrap to interact with the ethereum blockchain", "uri": "wrap://ipfs/QmNkqWQuTAiZaLSR3VE53uazuAEqZevv1XQtvAYahuyJ6d", "abi": "https://raw.githubusercontent.com/polywrap/ethers/main/wraps/core/polywrap.graphql" } ``` Let's take a look at what each of these mean: * **`aliases`** – These are other names that the Ethers wrap can have. For example, the user can tell the agent to load the `ethereum` wrap, and the agent would still understand. * **`description`** – Brief description of what the wrap does * **`uri`** – The URI shows the agent where to look for the wrap. In this case, the Ethers wrap is stored on [IPFS](https://www.ipfs.tech), a decentralized storage solution. * **abi** – This is the interface exposing the wrap's available functions. We can ask the agent to show us what functions are on the wrap like so: > **User:** Show me the functions available on the `filesystem` wrap. > > **Agent:** The available functions on the `filesystem` wrap are: > > >1. `readFile(path: String!): Bytes!` > - Reads a file and returns the content as bytes. > >2. `readFileAsString(path: String!, encoding: Encoding): String!` > - Reads a file and returns the content as a string. > - Parameters: > - `path`: The path of the file to read. > - `encoding` (optional): The encoding to use for reading the file. >Default is UTF8. > >... ## Use-case: AI Agents in Decentralized Finance (DeFi) DeFi platforms often require interactions with multiple smart contracts, which may seem daunting to users not familiar with blockchain technology. Our AI agent, with its ability to load wraps such as Ethers, can streamline this process. For instance, a user could ask the agent to swap tokens on a DeFi exchange, and the agent could then load the necessary wraps to interact with the correct smart contracts, handle the transaction, and keep the user informed of each step. ## Conclusion: The Future of AI Agents The advent of dynamic AI agents marks an important step forward in artificial intelligence. Traditional AI agents are static, limited by their training data, and often struggle with unfamiliar tasks. In contrast, our AI agent prototype utilizes Polywrap and OpenAI's function API to fetch capabilities on-demand, allowing it to tackle a broader array of tasks. While we're still in the early stages of developing this agent, the prospects are promising. You can learn more about our progress by subscribing to this newsletter and joining our [Discord community](https://discord.gg/qK9S46gTbF). ## Demo * [**Agent Learning Demo**](https://github.com/polywrap/agent-learning-demo/tree/dev) We've created a demo repository for you to install the agent locally and try it out yourself. Let us know what you think!