|
1 | | -<!-- TITLE --> |
2 | | -<p align="center"> |
3 | | - <img width="100px" src="https://github.com/celo-org/celo-composer/blob/main/images/readme/celo_isotype.svg" align="center" alt="Celo" /> |
4 | | - <h2 align="center">Celo Composer</h2> |
5 | | - <p align="center">Build, deploy, and iterate quickly on decentralized applications using Celo.</p> |
6 | | -</p> |
7 | | - <p align="center"> |
8 | | - <a href="https://github.com/celo-org/celo-composer/graphs/stars"> |
9 | | - <img alt="GitHub Contributors" src="https://img.shields.io/github/stars/celo-org/celo-composer?color=FCFF52" /> |
10 | | - </a> |
11 | | - <a href="https://github.com/celo-org/celo-composer/graphs/contributors"> |
12 | | - <img alt="GitHub Contributors" src="https://img.shields.io/github/contributors/celo-org/celo-composer?color=E7E3D4" /> |
13 | | - </a> |
14 | | - <a href="https://github.com/celo-org/celo-composer/issues"> |
15 | | - <img alt="Issues" src="https://img.shields.io/github/issues/celo-org/celo-composer?color=E7E3D4" /> |
16 | | - </a> |
17 | | - <a href="https://github.com/celo-org/celo-composer/pulls"> |
18 | | - <img alt="GitHub pull requests" src="https://img.shields.io/github/issues-pr/celo-org/celo-composer?color=E7E3D4" /> |
19 | | - </a> |
20 | | - <a href="https://opensource.org/license/mit/"> |
21 | | - <img alt="MIT License" src="https://img.shields.io/badge/License-MIT-yellow.svg" /> |
22 | | - </a> |
23 | | - </p> |
24 | | -</p> |
25 | | - |
26 | | -<!-- TABLE OF CONTENTS --> |
27 | | - |
28 | | -<details> |
29 | | - <summary>Table of Contents</summary> |
30 | | - <ol> |
31 | | - <li> |
32 | | - <a href="#about-the-project">About The Project</a> |
33 | | - <ul> |
34 | | - <li><a href="#built-with">Built With</a></li> |
35 | | - </ul> |
36 | | - </li> |
37 | | - <li> |
38 | | - <ul> |
39 | | - <li><a href="#prerequisites">Prerequisites</a></li> |
40 | | - <li><a href="#installation">Installation</a></li> |
41 | | - </ul> |
42 | | - </li> |
43 | | - <li><a href="#usage">Usage</a></li> |
44 | | - <li><a href="#roadmap">Roadmap</a></li> |
45 | | - <li><a href="#contributing">Contributing</a></li> |
46 | | - <li><a href="#license">License</a></li> |
47 | | - <li><a href="#contact">Contact</a></li> |
48 | | - <li><a href="#acknowledgments">Acknowledgments</a></li> |
49 | | - </ol> |
50 | | -</details> |
51 | | - |
52 | | -<!-- ABOUT THE PROJECT --> |
53 | | - |
54 | | -## About The Project |
55 | | - |
56 | | -Celo Composer allows you to quickly build, deploy, and iterate on decentralized applications using Celo. It provides a number of frameworks, examples, and Celo specific functionality to help you get started with your next dApp. |
57 | | - |
58 | | -<p align="right">(<a href="#top">back to top</a>)</p> |
59 | | - |
60 | | -## Built With |
61 | | - |
62 | | -Celo Composer is built on Celo to make it simple to build dApps using a variety of front-end frameworks, and libraries. |
63 | | - |
64 | | -- [Celo](https://celo.org/) |
65 | | -- [Solidity](https://docs.soliditylang.org/en/v0.8.19/) |
66 | | -- [Next.js](https://nextjs.org/) |
67 | | -- [React.js](https://reactjs.org/) |
68 | | -- [Material UI](https://mui.com/) |
69 | | -- [React Native](https://reactnative.dev/) |
70 | | -- [Flutter](https://docs.flutter.dev/) |
71 | | -- [React-celo](https://github.com/celo-org/react-celo/) |
72 | | -- [Rainbowkit-celo](https://github.com/celo-org/rainbowkit-celo) |
73 | | - |
74 | | -<p align="right">(<a href="#top">back to top</a>)</p> |
75 | | - |
76 | | -<!-- GETTING STARTED --> |
| 1 | +# Call an AI agent from your Celo dApp |
| 2 | + |
| 3 | +Learn how to call an AI agent and pay for it in CELO. |
| 4 | + |
| 5 | +This tutorial is based on the guide for the [Olas mech-client](https://github.com/valory-xyz/mech-client). |
| 6 | + |
| 7 | +This tutorial will guide you through integrating Olas into your dApp by setting up a backend, configuring the environment, and connecting to the frontend. By the end, you'll have a functional project that utilizes the Olas mech for generating prompts and interacting with blockchain tools. |
| 8 | + |
| 9 | + |
| 10 | +--- |
77 | 11 |
|
78 | 12 | ## Prerequisites |
| 13 | +Before you start, ensure you have the following installed: |
| 14 | +- Python 3.8+ |
| 15 | +- Node.js and Yarn |
| 16 | +- [Poetry](https://python-poetry.org/docs/#installation) (for Python dependency management) |
| 17 | +- A MetaMask wallet to export a private key |
| 18 | +- A Quicknode account (for RPC and WSS endpoints) |
79 | 19 |
|
80 | | -- Node |
81 | | -- Git (v2.38 or higher) |
| 20 | +--- |
82 | 21 |
|
83 | | -## How to use Celo Composer |
84 | 22 |
|
85 | | -The easiest way to start with Celo Composer is using `@celo/celo-composer`. This CLI tool lets you quickly start building dApps on Celo for multiple frameworks, including React (with either react-celo or rainbowkit-celo), React Native (w/o Expo), Flutter, and Angular. To get started, just run the following command, and follow the steps: |
| 23 | +This example project is built using the [Celo Composer](https://github.com/celo-org/celo-composer). You can do the quickstart by following [this guide](.backend/README.md). |
86 | 24 |
|
87 | | -```bash |
88 | | -npx @celo/celo-composer@latest create |
89 | | -``` |
| 25 | +Follow the guide below to call the mech from any dapp. |
90 | 26 |
|
91 | | -### Front-end framework |
| 27 | +## Add the Agent to an Existing Project |
92 | 28 |
|
93 | | - |
| 29 | +### 1. Set Up the Backend |
94 | 30 |
|
95 | | -### Web3 library (for react-app) |
| 31 | +#### Initialize a Python Project |
| 32 | +Inside your dApp's directory, create a new backend folder and set up a Python project: |
96 | 33 |
|
97 | | - |
| 34 | +```bash |
| 35 | +poetry new backend |
| 36 | +cd backend |
| 37 | +``` |
98 | 38 |
|
99 | | -### Smart contract framework |
| 39 | +#### Activate Poetry Environment |
| 40 | +Activate the Poetry shell: |
100 | 41 |
|
101 | | - |
| 42 | +```bash |
| 43 | +poetry shell |
| 44 | +``` |
102 | 45 |
|
103 | | -### Subgraph |
| 46 | +#### Install Dependencies |
| 47 | +Install the `mech-client` package: |
104 | 48 |
|
105 | | - |
| 49 | +```bash |
| 50 | +poetry add mech-client |
| 51 | +``` |
106 | 52 |
|
107 | | -### Name your dApp |
| 53 | +--- |
108 | 54 |
|
109 | | - |
| 55 | +### 2. Set Up the Environment |
110 | 56 |
|
111 | | -**_🔥Voila, you have a dApp ready to go. Voila, you have a dApp ready to go. Start building your dApp on Celo._** |
| 57 | +#### Configure Private Key |
112 | 58 |
|
113 | | -### Getting started |
| 59 | +1. Create a file to store your private key securely: |
| 60 | + ```bash |
| 61 | + touch ethereum_private_key.txt |
| 62 | + ``` |
114 | 63 |
|
115 | | -Once your custom dApp has been created, just install dependencies, either with `yarn` or `npm i`, and run the respective script from the `package.json` file. |
116 | | -## Supported Frameworks |
| 64 | +2. Export your private key from [MetaMask](https://support.metamask.io/hc/en-us/articles/360015289632-How-to-export-an-account-s-private-key) and save it in `ethereum_private_key.txt`. It should be prefunded with some CELO. This agent will only run on Celo Mainnet. |
117 | 65 |
|
118 | | -### React |
| 66 | +3. Add the file to your `.gitignore` to prevent accidental uploads: |
| 67 | + ```bash |
| 68 | + echo ethereum_private_key.txt >> .gitignore |
| 69 | + ``` |
119 | 70 |
|
120 | | -- Support for Website and Progressive Web Application. |
121 | | -- Works with all major crypto wallets. |
| 71 | +#### Add Environment Variables |
| 72 | +Create an `.env` file to store your RPC and WSS endpoints. We recommend using [Quicknode](https://www.quicknode.com/): |
122 | 73 |
|
123 | | -Check [package readme](https://github.com/celo-org/celo-composer/blob/main/packages/react-app/README.md) to learn more about. |
| 74 | +```bash |
| 75 | +MECHX_CHAIN_RPC=https://proud-proud-layer.celo-mainnet.quiknode.pro/<your-key> |
| 76 | +MECHX_WSS_ENDPOINT=wss://proud-proud-layer.celo-mainnet.quiknode.pro/<your-key> |
| 77 | +``` |
124 | 78 |
|
125 | | -### React Native |
| 79 | +--- |
126 | 80 |
|
127 | | -- Out of the box config, just focus on buidl. |
128 | | -- Support for Android and IOS. |
129 | | -- Works with and without [Expo](https://expo.dev/). |
130 | | -- Working example app included. |
| 81 | +### 3. Create the Script |
131 | 82 |
|
132 | | -Check [package readme](https://github.com/celo-org/celo-composer/blob/main/packages/react-native-app/README.md) to learn more about. |
| 83 | +#### Write the Python Script |
| 84 | +Create a script file: |
133 | 85 |
|
134 | | -### Flutter |
| 86 | +```bash |
| 87 | +touch my_script.py |
| 88 | +``` |
135 | 89 |
|
136 | | -- One command to get started - Type `flutter run` to start development in your mobile phone. |
137 | | -- Works with all major mobile crypto wallets. |
138 | | -- Support for Android, IOS (Web, Windows, and Linux coming soon). |
139 | | -- Working example app included. |
| 90 | +Edit `my_script.py`: |
| 91 | + |
| 92 | +```python |
| 93 | +from mech_client.interact import interact, ConfirmationType |
| 94 | +
|
| 95 | +def get_prompt(prompt_text): |
| 96 | + agent_id = 2 |
| 97 | + tool_name = "openai-gpt-3.5-turbo" # Replace with your tool |
| 98 | + chain_config = "celo" |
| 99 | + private_key_path = "ethereum_private_key.txt" |
| 100 | +
|
| 101 | + result = interact( |
| 102 | + prompt=prompt_text, |
| 103 | + agent_id=agent_id, |
| 104 | + tool=tool_name, |
| 105 | + chain_config=chain_config, |
| 106 | + confirmation_type=ConfirmationType.ON_CHAIN, |
| 107 | + private_key_path=private_key_path |
| 108 | + ) |
| 109 | + return result |
| 110 | +``` |
140 | 111 |
|
141 | | -Check [package readme](https://github.com/celo-org/celo-composer/blob/main/packages/flutter-app/README.md) to learn more about. |
| 112 | +#### Handle Errors |
| 113 | +If you encounter the error: |
| 114 | +```bash |
| 115 | +ModuleNotFoundError: No module named 'pkg_resources' |
| 116 | +``` |
| 117 | +Resolve it by upgrading `setuptools`: |
| 118 | +```bash |
| 119 | +pip install --upgrade setuptools |
| 120 | +``` |
142 | 121 |
|
143 | | -### Angular |
| 122 | +#### Run the Script |
| 123 | +Execute the script: |
| 124 | +```bash |
| 125 | +python my_script.py |
| 126 | +``` |
144 | 127 |
|
145 | | -- Support for Website and Progressive Web Application. |
146 | | -- Works with all major crypto wallets. |
| 128 | +--- |
147 | 129 |
|
148 | | -Check [package readme](https://github.com/celo-org/celo-composer/blob/main/packages/angular-app/README.md) to learn more about. |
| 130 | +### 4. Set Up the API |
149 | 131 |
|
150 | | -<!-- USAGE EXAMPLES --> |
| 132 | +#### Install Flask |
| 133 | +Install Flask and Flask-CORS: |
| 134 | +```bash |
| 135 | +pip install Flask flask-cors |
| 136 | +``` |
151 | 137 |
|
152 | | -## 🔭 Learning Solidity |
| 138 | +#### Create the API |
| 139 | +Create `app.py` and add the following: |
153 | 140 |
|
154 | | -📕 Read the docs: <https://docs.soliditylang.org> |
| 141 | +```python |
| 142 | +from flask import Flask, jsonify, request |
| 143 | +from flask_cors import CORS |
| 144 | +from my_script import get_prompt |
155 | 145 |
|
156 | | -- [Primitive Data Types](https://solidity-by-example.org/primitives/) |
157 | | -- [Mappings](https://solidity-by-example.org/mapping/) |
158 | | -- [Structs](https://solidity-by-example.org/structs/) |
159 | | -- [Modifiers](https://solidity-by-example.org/function-modifier/) |
160 | | -- [Events](https://solidity-by-example.org/events/) |
161 | | -- [Inheritance](https://solidity-by-example.org/inheritance/) |
162 | | -- [Payable](https://solidity-by-example.org/payable/) |
163 | | -- [Fallback](https://solidity-by-example.org/fallback/) |
| 146 | +app = Flask(__name__) |
| 147 | +CORS(app) |
164 | 148 |
|
165 | | -📧 Learn the [Solidity globals and units](https://solidity.readthedocs.io/en/v0.8.19/units-and-global-variables.html) |
| 149 | +@app.route('/get-prompt', methods=['GET']) |
| 150 | +def get_chat_gpt_request(): |
| 151 | + prompt = request.args.get('prompt', 'Write a Haiku about web3 hackathons?') |
| 152 | + try: |
| 153 | + response = get_prompt(prompt) |
| 154 | + return jsonify({"success": True, "response": response}), 200 |
| 155 | + except Exception as e: |
| 156 | + return jsonify({"success": False, "error": str(e)}), 500 |
166 | 157 |
|
167 | | -## Support |
| 158 | +if __name__ == '__main__': |
| 159 | + app.run(debug=True) |
| 160 | +``` |
168 | 161 |
|
169 | | -Join the Celo Discord server at <https://chat.celo.org>. Reach out on the dedicated repo channel [here](https://discord.com/channels/600834479145353243/941003424298856448). |
| 162 | +--- |
170 | 163 |
|
171 | | -<!-- ROADMAP --> |
| 164 | +## Integrate with the Frontend |
172 | 165 |
|
173 | | -## Roadmap |
| 166 | +### 1. Create an API Route |
| 167 | +Inside your Next.js project, create `pages/api/get-prompt.ts`: |
174 | 168 |
|
175 | | -See the [open issues](https://github.com/celo-org/celo-composer/issues) for a full list of proposed features (and known issues). |
| 169 | +```typescript |
| 170 | +import type { NextApiRequest, NextApiResponse } from 'next'; |
| 171 | +import axios from 'axios'; |
176 | 172 |
|
177 | | -<p align="right">(<a href="#top">back to top</a>)</p> |
| 173 | +export default async function handler(req: NextApiRequest, res: NextApiResponse) { |
| 174 | + const prompt = req.query.prompt; |
| 175 | + try { |
| 176 | + const { data } = await axios.get(`http://127.0.0.1:5000/get-prompt?prompt=${prompt}`); |
| 177 | + res.status(200).json(data); |
| 178 | + } catch (error) { |
| 179 | + res.status(500).json({ message: error.message }); |
| 180 | + } |
| 181 | +} |
| 182 | +``` |
178 | 183 |
|
179 | | -<!-- CONTRIBUTING --> |
| 184 | +### 2. Add Functionality |
| 185 | +In your React component, define state and handlers: |
180 | 186 |
|
181 | | -## Contributing |
| 187 | +```typescript |
| 188 | +const [yourPrompt, setYourPrompt] = useState(''); |
| 189 | +const [response, setResponse] = useState<string | null>(null); |
182 | 190 |
|
183 | | -We welcome contributions from the community. |
| 191 | +async function fetchPromptData(prompt: string) { |
| 192 | + const res = await fetch(`/api/get-prompt?prompt=${encodeURIComponent(prompt)}`); |
| 193 | + const data = await res.json(); |
| 194 | + setResponse(data.response); |
| 195 | +} |
184 | 196 |
|
185 | | -<p align="right">(<a href="#top">back to top</a>)</p> |
| 197 | +const handleFetchClick = () => { |
| 198 | + if (yourPrompt) fetchPromptData(yourPrompt); |
| 199 | +}; |
| 200 | +``` |
186 | 201 |
|
187 | | -## License |
| 202 | +Add these elements to your component: |
| 203 | +
|
| 204 | +```typescript |
| 205 | +<div> |
| 206 | + <input |
| 207 | + type="text" |
| 208 | + value={yourPrompt} |
| 209 | + onChange={(e) => setYourPrompt(e.target.value)} |
| 210 | + /> |
| 211 | + <button onClick={handleFetchClick}>Fetch Prompt</button> |
| 212 | + {response && <p>Response: {response}</p>} |
| 213 | +</div> |
| 214 | +``` |
188 | 215 |
|
189 | | -Distributed under the MIT License. See `LICENSE.txt` for more information. |
| 216 | +--- |
190 | 217 |
|
191 | | -<!-- CONTACT --> |
192 | | -## Contact |
| 218 | +## Next Steps |
| 219 | +- **Testing**: Use Postman or `curl` to test your API before frontend integration. |
| 220 | +- **Deployment**: Consider hosting your backend using services like Heroku or AWS. |
| 221 | +- **Extensions**: Explore more tools available in the [Olas Mech library](https://github.com/valory-xyz/mech/tree/main/packages/valory/customs). |
193 | 222 |
|
194 | | -- [@CeloDevs](https://twitter.com/CeloDevs) |
195 | | -- [Discord](https://discord.com/invite/celo) |
| 223 | +--- |
196 | 224 |
|
197 | | -<p align="right">(<a href="#top">back to top</a>)</p> |
| 225 | +Let me know if you'd like more adjustments! |
0 commit comments