-
Notifications
You must be signed in to change notification settings - Fork 0
Set up Web3AI foundation with Node.js, Python, and OpenAI integration #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lippytm
wants to merge
2
commits into
copilot/fix-7fbbae28-e9c5-4300-83fc-9a438ae0c679
Choose a base branch
from
copilot/setup-web3ai-project-structure
base: copilot/fix-7fbbae28-e9c5-4300-83fc-9a438ae0c679
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # OpenAI API Configuration | ||
| OPENAI_API_KEY=your_openai_api_key_here | ||
|
|
||
| # Application Configuration | ||
| PORT=3000 | ||
| NODE_ENV=development |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,24 @@ | ||
| *.agdai | ||
| MAlonzo/** | ||
|
|
||
| # Node.js | ||
| node_modules/ | ||
| npm-debug.log | ||
| yarn-error.log | ||
| package-lock.json | ||
|
|
||
| # Environment variables | ||
| .env | ||
|
|
||
| # Python | ||
| __pycache__/ | ||
| *.py[cod] | ||
| *$py.class | ||
| *.so | ||
| .Python | ||
| venv/ | ||
| env/ | ||
| ENV/ | ||
| *.egg-info/ | ||
| dist/ | ||
| build/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,180 @@ | ||
| # Web3AI | ||
| # Web3AI | ||
|
|
||
| Web3AI is an AI-powered blockchain and Web3 development toolkit that integrates OpenAI's capabilities with blockchain technologies. | ||
|
|
||
| ## 🌟 Features | ||
|
|
||
| - **Node.js Backend**: Server-side JavaScript for Web3 applications | ||
| - **OpenAI Integration**: AI-powered smart contract analysis, code generation, and blockchain insights | ||
| - **Python AI Scripts**: Advanced AI capabilities for blockchain development | ||
| - **Modular Architecture**: Easy to extend with additional AI or blockchain features | ||
|
|
||
| ## 📋 Prerequisites | ||
|
|
||
| Before you begin, ensure you have the following installed: | ||
|
|
||
| - **Node.js** (v14 or higher) - [Download here](https://nodejs.org/) | ||
| - **npm** (comes with Node.js) | ||
| - **Python** (v3.8 or higher) - [Download here](https://www.python.org/downloads/) | ||
| - **pip** (comes with Python) | ||
|
|
||
| ## 🚀 Getting Started | ||
|
|
||
| ### 1. Clone the Repository | ||
|
|
||
| ```bash | ||
| git clone https://github.com/lippytm/Web3AI.git | ||
| cd Web3AI | ||
| ``` | ||
|
|
||
| ### 2. Set Up Node.js Environment | ||
|
|
||
| Install Node.js dependencies: | ||
|
|
||
| ```bash | ||
| npm install | ||
| ``` | ||
|
|
||
| ### 3. Set Up Python Environment (Optional) | ||
|
|
||
| For Python AI scripts, install required packages: | ||
|
|
||
| ```bash | ||
| pip install -r requirements.txt | ||
| ``` | ||
|
|
||
| Or create a virtual environment (recommended): | ||
|
|
||
| ```bash | ||
| # Create virtual environment | ||
| python -m venv venv | ||
|
|
||
| # Activate virtual environment | ||
| # On Windows: | ||
| venv\Scripts\activate | ||
| # On macOS/Linux: | ||
| source venv/bin/activate | ||
|
|
||
| # Install dependencies | ||
| pip install -r requirements.txt | ||
| ``` | ||
|
|
||
| ### 4. Configure OpenAI API Key | ||
|
|
||
| 1. **Get your OpenAI API key**: | ||
| - Visit [OpenAI Platform](https://platform.openai.com/api-keys) | ||
| - Sign in or create an account | ||
| - Navigate to API Keys section | ||
| - Click "Create new secret key" | ||
| - Copy your API key (you won't be able to see it again!) | ||
|
|
||
| 2. **Set up environment variables**: | ||
|
|
||
| ```bash | ||
| # Copy the example environment file | ||
| cp .env.example .env | ||
|
|
||
| # Edit .env and add your API key | ||
| # Replace 'your_openai_api_key_here' with your actual API key | ||
| ``` | ||
|
|
||
| Your `.env` file should look like this: | ||
|
|
||
| ``` | ||
| OPENAI_API_KEY=sk-your-actual-api-key-here | ||
| PORT=3000 | ||
| NODE_ENV=development | ||
| ``` | ||
|
|
||
| ⚠️ **Important**: Never commit your `.env` file to version control. It's already included in `.gitignore`. | ||
|
|
||
| ## 🎯 Usage | ||
|
|
||
| ### Running the Node.js Application | ||
|
|
||
| Start the main application: | ||
|
|
||
| ```bash | ||
| npm start | ||
| ``` | ||
|
|
||
| This will run the OpenAI integration demo and show you how to interact with AI models. | ||
|
|
||
| ### Running Python AI Scripts | ||
|
|
||
| Execute the sample AI script: | ||
|
|
||
| ```bash | ||
| python ai_scripts/sample_ai.py | ||
| ``` | ||
|
|
||
| This demonstrates: | ||
| - Explaining Web3 concepts using AI | ||
| - Analyzing smart contract code | ||
| - Integration with OpenAI's Python library | ||
|
|
||
| ## 📁 Project Structure | ||
|
|
||
| ``` | ||
| Web3AI/ | ||
| ├── backend/ # Node.js backend code | ||
| │ └── openai.js # OpenAI API integration module | ||
| ├── ai_scripts/ # Python AI scripts | ||
| │ └── sample_ai.py # Sample AI functionality demo | ||
| ├── .env.example # Environment variable template | ||
| ├── .gitignore # Git ignore rules | ||
| ├── index.js # Main entry point | ||
| ├── package.json # Node.js dependencies and scripts | ||
| ├── requirements.txt # Python dependencies | ||
| └── README.md # This file | ||
| ``` | ||
|
|
||
| ## 🔧 Available Scripts | ||
|
|
||
| - `npm start` - Run the main application | ||
| - `npm test` - Run tests (to be implemented) | ||
|
|
||
| ## 🤝 Contributing | ||
|
|
||
| Contributions are welcome! Please feel free to submit a Pull Request. | ||
|
|
||
| ## 📝 License | ||
|
|
||
| This project is licensed under the ISC License. | ||
|
|
||
| ## 🆘 Troubleshooting | ||
|
|
||
| ### "OpenAI API key not configured" Error | ||
|
|
||
| Make sure you have: | ||
| 1. Created a `.env` file in the project root | ||
| 2. Added your OpenAI API key to the `.env` file | ||
| 3. The key starts with `sk-` | ||
|
|
||
| ### "Module not found" Error | ||
|
|
||
| Run `npm install` to install Node.js dependencies or `pip install openai python-dotenv` for Python. | ||
|
|
||
| ### Python Import Errors | ||
|
|
||
| Make sure you have activated your virtual environment (if using one) and installed all required packages. | ||
|
|
||
| ## 🔮 Future Enhancements | ||
|
|
||
| - Integration with blockchain networks (Ethereum, Polygon, etc.) | ||
| - Smart contract deployment automation | ||
| - AI-powered security auditing | ||
| - Web3 wallet integration | ||
| - NFT generation and analysis | ||
| - DeFi protocol interaction | ||
|
|
||
| ## 📚 Resources | ||
|
|
||
| - [OpenAI API Documentation](https://platform.openai.com/docs) | ||
| - [Node.js Documentation](https://nodejs.org/docs) | ||
| - [Python OpenAI Library](https://github.com/openai/openai-python) | ||
| - [Web3.js Documentation](https://web3js.readthedocs.io/) | ||
|
|
||
| --- | ||
|
|
||
| Made with ❤️ for the Web3 and AI community | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| #!/usr/bin/env python3 | ||
| """ | ||
| Web3AI - Python AI Script Example | ||
|
|
||
| This script demonstrates how to use OpenAI's Python library to interact | ||
| with AI models for Web3 and blockchain-related tasks. | ||
|
|
||
| Requirements: | ||
| pip install openai python-dotenv | ||
| """ | ||
|
|
||
| import os | ||
| from dotenv import load_dotenv | ||
|
|
||
| # Load environment variables from .env file | ||
| load_dotenv() | ||
|
|
||
|
|
||
| def setup_openai(): | ||
| """ | ||
| Initialize OpenAI client with API key from environment | ||
| """ | ||
| try: | ||
| from openai import OpenAI | ||
|
|
||
| api_key = os.getenv('OPENAI_API_KEY') | ||
|
|
||
| if not api_key or api_key == 'your_openai_api_key_here': | ||
| print('⚠️ Warning: OpenAI API key not configured!') | ||
| print('📝 Please set OPENAI_API_KEY in your .env file') | ||
| return None | ||
|
|
||
| client = OpenAI(api_key=api_key) | ||
| print('✅ OpenAI client initialized successfully\n') | ||
| return client | ||
|
|
||
| except ImportError: | ||
| print('❌ Error: openai package not installed') | ||
| print(' Install it with: pip install openai') | ||
| return None | ||
|
|
||
|
|
||
| def analyze_smart_contract(client, contract_code): | ||
| """ | ||
| Example function to analyze smart contract code using AI | ||
|
|
||
| Args: | ||
| client: OpenAI client instance | ||
| contract_code: Smart contract code to analyze | ||
|
|
||
| Returns: | ||
| AI analysis of the contract | ||
| """ | ||
| if not client: | ||
| return "Client not initialized" | ||
|
|
||
| try: | ||
| response = client.chat.completions.create( | ||
| model="gpt-3.5-turbo", | ||
| messages=[ | ||
| { | ||
| "role": "system", | ||
| "content": "You are an expert in blockchain and smart contract security. Analyze smart contracts for potential vulnerabilities and best practices." | ||
| }, | ||
| { | ||
| "role": "user", | ||
| "content": f"Analyze this smart contract code:\n\n{contract_code}" | ||
| } | ||
| ], | ||
| max_tokens=300, | ||
| temperature=0.5 | ||
| ) | ||
|
|
||
| return response.choices[0].message.content | ||
|
|
||
| except Exception as e: | ||
| return f"Error analyzing contract: {str(e)}" | ||
lippytm marked this conversation as resolved.
Show resolved
Hide resolved
lippytm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def explain_web3_concept(client, concept): | ||
| """ | ||
| Get AI explanation of Web3 concepts | ||
|
|
||
| Args: | ||
| client: OpenAI client instance | ||
| concept: Web3 concept to explain | ||
|
|
||
| Returns: | ||
| AI explanation | ||
| """ | ||
| if not client: | ||
| return "Client not initialized" | ||
|
|
||
| try: | ||
| response = client.chat.completions.create( | ||
| model="gpt-3.5-turbo", | ||
| messages=[ | ||
| { | ||
| "role": "system", | ||
| "content": "You are a Web3 educator. Explain blockchain and Web3 concepts in simple, clear terms." | ||
| }, | ||
| { | ||
| "role": "user", | ||
| "content": f"Explain: {concept}" | ||
| } | ||
| ], | ||
| max_tokens=200, | ||
| temperature=0.7 | ||
| ) | ||
|
|
||
| return response.choices[0].message.content | ||
|
|
||
| except Exception as e: | ||
| return f"Error getting explanation: {str(e)}" | ||
lippytm marked this conversation as resolved.
Show resolved
Hide resolved
lippytm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def run_demo(): | ||
| """ | ||
| Run demonstration of AI capabilities | ||
| """ | ||
| print('🚀 Web3AI Python Demo') | ||
| print('=' * 50) | ||
| print() | ||
|
|
||
| # Initialize OpenAI client | ||
| client = setup_openai() | ||
|
|
||
| if not client: | ||
| print('\n💡 To run this demo:') | ||
| print(' 1. Install dependencies: pip install openai python-dotenv') | ||
| print(' 2. Set OPENAI_API_KEY in .env file') | ||
| print(' 3. Get API key from: https://platform.openai.com/api-keys') | ||
| return | ||
|
|
||
| # Demo 1: Explain a Web3 concept | ||
| print('📚 Demo 1: Explaining Web3 Concepts') | ||
| print('-' * 50) | ||
| concept = "What is a blockchain consensus mechanism?" | ||
| print(f'Question: {concept}\n') | ||
| print('Thinking... 🤔\n') | ||
|
|
||
| explanation = explain_web3_concept(client, concept) | ||
| print('AI Response:') | ||
| print(explanation) | ||
| print() | ||
|
|
||
| # Demo 2: Smart contract analysis | ||
| print('\n🔍 Demo 2: Smart Contract Analysis') | ||
| print('-' * 50) | ||
|
|
||
| sample_contract = """ | ||
| pragma solidity ^0.8.0; | ||
|
|
||
| contract SimpleStorage { | ||
| uint256 private data; | ||
|
|
||
| function set(uint256 _data) public { | ||
| data = _data; | ||
| } | ||
|
|
||
| function get() public view returns (uint256) { | ||
| return data; | ||
| } | ||
| } | ||
| """ | ||
|
|
||
| print('Analyzing sample Solidity contract...\n') | ||
| print('Thinking... 🤔\n') | ||
|
|
||
| analysis = analyze_smart_contract(client, sample_contract) | ||
| print('AI Analysis:') | ||
| print(analysis) | ||
| print() | ||
|
|
||
| print('\n✅ Demo completed successfully!') | ||
| print('=' * 50) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| run_demo() | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.