https://github.com/a-r-j/optimallm
Optimise python functions with LLMs
https://github.com/a-r-j/optimallm
Last synced: about 2 months ago
JSON representation
Optimise python functions with LLMs
- Host: GitHub
- URL: https://github.com/a-r-j/optimallm
- Owner: a-r-j
- License: mit
- Created: 2023-03-22T14:04:37.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-03-22T16:05:19.000Z (about 2 years ago)
- Last Synced: 2025-03-14T00:47:02.185Z (2 months ago)
- Language: Python
- Size: 5.86 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# optimallm
Optimise python functions with LLMs
## Installation
```bash
pip install optimallm
```### Example Usage
```python
import os
from optimallm import optos.environ["OPENAI_API_KEY"] = "YOUR_API_KEY"
def calculate_sum(a, b):
result = a
for i in range(b):
result += 1
return resultopt(calculate_sum)
```Output:
> Here's an optimized version of the calculate_sum function:
>
> ```python
> def calculate_sum(a, b):
> return a + b
> ```
>
>This optimized version simply adds a and b together and returns the result. This is much faster than the original version which uses a loop to add 1 to a b times. Additionally, this version is more readable and concise.### Custom prompts
Code is injected into the prompt by replacing `$code`. Similarly, the function name can be injected by replacing `$name`.
```python
prompt = "Make this function go brrr: \n $code"
opt(calculate_sum, prompt)prompt = "Make $name go brrr: \n $code"
opt(calculate_sum, prompt)```
#### Setting a new default
```python
import optimallmoptimallm.DEFAULT_PROMPT = "Make code go brrrr: \n $code"
optimallm.opt(my_bad_function)
```### API Key
API Keys can be set in two ways:
1. As an env var:
```python
import os
os.environ["OPENAI_API_KEY"] = "YOUR_API_KEY"
```2. Using OpenAI's library
```python
import openai
openai.api_key = "YOUR_API_KEY"
```