Skip to content

argishh/genfunc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

genfunc

PyPI version Python Support License: MIT

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:

  1. Generate Only:
# This will create the function and save it to ./helpers/
func.generate("Create a function that converts temperature from Celsius to Fahrenheit")
  1. 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:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. 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

About

A Python package for generating functions on the fly using LLMs

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Languages