A Python package for generating functions using LLMs
Project description
genfunc
genfunc is a powerful Python package that leverages Large Language Models (LLMs) to automatically generate, manage, and execute Python functions. Simply describe what you want your function to do, and genfunc will create it for you.
Ideal Usecase
The ideal use case for the this package is to leverage LLMs to automatically generate, manage, and execute Python functions based on user-provided descriptions. This can be particularly useful for:
- Rapid prototyping and development
- Automating repetitive coding tasks
- Generating boilerplate code
- Creating utility functions on-the-fly
- Enhancing productivity by reducing manual coding effort
🚀 Features
- 🤖 Automatic function generation using state-of-the-art LLMs
- 🔄 Support for multiple LLM providers (OpenAI, Anthropic)
- 🔒 Secure API key handling
- 📁 Automatic function storage and management
- ⚡ Immediate function execution capability
- 🛡️ Comprehensive error handling and logging
- 🧪 Extensive test coverage
📋 Requirements
- Python 3.8 or higher
- Valid API key for at least one supported LLM provider (OpenAI or Anthropic)
💻 Installation
pip install genfunc
Additional Dependencies (of your choice, atleast one) -
pip install openai
or
pip install anthropic
🔧 Configuration
Before using func, you need to configure your LLM provider API keys. You have two options:
Option 1: Environment Variables
export OPENAI_API_KEY=your_openai_api_key
export ANTHROPIC_API_KEY=your_anthropic_api_key
Option 2: .env File
Create a .env file in your project root:
OPENAI_API_KEY=your_openai_api_key
ANTHROPIC_API_KEY=your_anthropic_api_key
🎯 Quick Start
Here's a simple example to get you started:
from genfunc import func
# Initialize with OpenAI
func.initialize(provider="openai")
# Generate a function without calling it
func.generate("Create a function that calculates the factorial of a number")
# import the function
from helpers.file_name import function_name
Alternatively,
# Generate and immediately call a function
result = func.generate(
"Create a function that adds two numbers",
call=True, # calls the function
a=5, # arg1
b=3 # arg2
)
print(result) # Output: 8
📖 Detailed Usage
Initialization
You can initialize func with different providers:
# Initialize with OpenAI
func.initialize(provider="openai", api_key="your_api_key")
# Initialize with Anthropic
func.initialize(provider="anthropic", api_key="your_api_key")
Function Generation
func offers two modes of function generation:
- Generate Only:
# This will create the function and save it to ./helpers/
func.generate("Create a function that converts temperature from Celsius to Fahrenheit")
- Generate and Execute:
# This will create the function, save it, and immediately execute it
result = func.generate(
"Create a function that calculates the area of a circle",
call=True,
radius=5
)
print(result) # Output: 78.53981633974483
Function Storage
Generated functions are automatically stored in the ./helpers/ directory. Each function is saved in its own Python file, making it easy to:
- Review generated code
- Modify functions if needed
- Import functions into other projects
- Version control your generated functions
Advanced Usage
Custom Provider Configuration
func.initialize(
provider="openai",
api_key="your_api_key",
model="gpt-4o" # Specify model version
)
🔍 Examples
Data Processing Function
# Generate a data processing function
func.generate("""
Create a function that takes a pandas DataFrame with columns 'date' and 'value',
and returns the moving average with a window of 7 days
""")
Algorithm Implementation
# Generate a sorting algorithm
result = func.generate(
"Create a function that implements the quicksort algorithm",
call=True,
arr=[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
)
print(result) # Output: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
API Integration
# Generate an API wrapper function
func.generate("""
Create a function that takes a city name and returns the current weather
using the OpenWeatherMap API
""")
View more examples here
🧪 Testing
Run the test suite:
# Install dev dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Run tests with coverage
pytest --cov=func tests/
🤝 Contributing
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Please make sure to update tests as appropriate and adhere to the existing coding style.
View more details here
📝 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- OpenAI for their GPT models
- Anthropic for Claude
- All contributors who have helped shape this project
📚 Documentation
For more detailed documentation, visit our documentation site.
🔗 Links
🤔 FAQ
Q: Is there a limit to how complex the generated functions can be?
A: The complexity limit depends on the LLM provider's token limits and capabilities. Generally, func works best with focused, single-purpose functions.
Q: Can I modify the generated functions?
A: Yes! Generated functions are stored as regular Python files in the ./helpers/ directory and can be modified like any other Python code.
Q: How secure is my API key?
A: API keys are handled securely through environment variables or .env files. They are never stored in the generated code or exposed in any way.
⚠️ Known Issues
- Very long function descriptions might be truncated by LLM token limits
- Some complex mathematical operations might require manual verification
- Generated functions might need optimization for production use
🗺️ Roadmap (to-do)
- Add support for more LLM providers
- Implement function optimization suggestions
- Add type hint generation
- Create a web interface for function generation
- Add support for async functions
- Implement function chaining
📊 Stats
- Release Date: 04-12-2024
- Latest Version: 0.1.0
- Python Versions: 3.8+
Made with ❤️ by Argish
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file genfunc-0.1.0.tar.gz.
File metadata
- Download URL: genfunc-0.1.0.tar.gz
- Upload date:
- Size: 18.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b056138e804ed6c1c357b6d89291d729484b3eb2deb59f75ee701131bbb55832
|
|
| MD5 |
d185af6e4bb45f30e44ffe1bfc0eeb56
|
|
| BLAKE2b-256 |
40aa69126a2b9f42086ef4bb81eda4bbf4122715d7c6e23de8d196139de7ff7a
|
File details
Details for the file genfunc-0.1.0-py3-none-any.whl.
File metadata
- Download URL: genfunc-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a3cc88010683e1c2db373868f39e0aaca90907eddf78b265c618a06f3c03f95
|
|
| MD5 |
9fdeec1c520512f8b3c6b8d8cda14545
|
|
| BLAKE2b-256 |
8b69a3640d8135207f7e8e075e93f6a19ff101f42ad0f2b6ddc64567b9e25bb9
|