OptimAI is a powerful Python module designed to optimize your code by analyzing its performance and providing actionable suggestions. It leverages a large language model (LLM) to give you detailed insights and recommendations based on the profiling data collected during the execution of your code. This module supports various kinds of profilers from the perfwatch package.
You can install OptimAI using pip:
pip install optimizeai
To use OptimAI, you need to configure it with your preferred LLM provider and API key. Supported LLM providers include Google (Gemini models), OpenAI, Ollama, HuggingFace and Anthropic. For Ollama you need to have Ollama installed and the model artifacts also need to be downloaded previously.
Select the LLM Provider:
llm = "google"
llm = "openai"
llm = "huggingface"
llm = "anthropic"
llm = "ollama"
Choose the Model:
model = "gpt-4"
, model = "gemini-1.5-flash"
, model = "codegemma"
, or any other model specific to the chosen LLM provider.Set the API Key:
Here's a basic example demonstrating how to use OptimAI to optimize a function:
from optimizeai.decorators.optimize import optimize
from optimizeai.config import Config
from dotenv import load_dotenv
import time
import os
# Load environment variables
load_dotenv()
llm = os.getenv("LLM")
key = os.getenv("API_KEY")
model = os.getenv("MODEL")
# Configure LLM
llm_config = Config(llm=llm, model=model, key=key)
perfwatch_params = ["line", "cpu", "time"]
# Define a test function to be optimized
@optimize(config=llm_config, profiler_types=perfwatch_params)
def test():
for _ in range(10):
time.sleep(0.1)
print("Hello World!")
pass
if __name__ == "__main__":
test()
You can set the environment variables (LLM
, API_KEY
, MODEL
) in a .env
file for ease of use:
LLM=google
API_KEY=your_google_api_key
MODEL=gemini-1.5-flash
We welcome contributions to OptimAI! If you have an idea for a new feature or have found a bug, please open an issue on GitHub. If you'd like to contribute code, please fork the repository and submit a pull request.
git checkout -b feature-branch
).git commit -m 'Add new feature'
).git push origin feature-branch
).OptimAI is licensed under the MIT License. See the LICENSE file for more details.