AutoGenLib

Import wisdom, export code.

License

MIT license 32 stars 3 forks Branches Tags Activity

cofob/autogenlib

main BranchesTags

Folders and files

Name| Name| Last commit message| Last commit date
---|---|---|---
autogenlib| autogenlib
examples| examples
.env| .env
.gitignore| .gitignore
.python-version| .python-version
LICENSE| LICENSE
README.md| README.md
pyproject.toml| pyproject.toml
uv.lock| uv.lock

Repository files navigation

AutoGenLib

你永远需要的唯一库。 Import wisdom, export code.

AutoGenLib 是一个 Python 库,它使用 OpenAI 的 API 自动实时生成代码。当您尝试导入一个不存在的模块或函数时,AutoGenLib 会根据对您所需内容的高级描述为您创建它。

特性

安装

pip install autogenlib

或者从源码安装:

git clone https://github.com/cofob/autogenlib.git
cd autogenlib
pip install -e .

要求

快速开始

OPENAI_API_KEY 环境变量中设置 OpenAI API key。

# 导入一个尚未存在的函数 - 它将被自动生成
from autogenlib.tokens import generate_token
# 使用生成的函数
token = generate_token(length=32)
print(token)

工作原理

  1. 您使用对所需内容的描述初始化 AutoGenLib

  2. 当您导入 autogenlib 命名空间下的模块或函数时,该库:

    • 检查模块/函数是否已存在
    • 如果不存在,它会分析执行导入的代码以了解上下文
    • 它向 OpenAI 的 API 发送包含您的描述和上下文的请求
    • API 生成适当的代码
    • 代码经过验证和执行
    • 请求的模块/函数可供使用

示例

生成 TOTP 生成器

from autogenlib.totp import totp_generator
print(totp_generator("SECRETKEY123"))

稍后添加验证函数

# 稍后在您的应用程序中,您需要验证:
from autogenlib.totp import verify_totp
result = verify_totp("SECRETKEY123", "123456")
print(f"Verification result: {result}")

使用上下文感知

# 导入一个函数 - AutoGenLib 将看到您的数据是如何构建的
from autogenlib.processor import get_highest_score
# 定义您的数据结构
data = [{"user": "Alice", "score": 95}, {"user": "Bob", "score": 82}]
# 该函数将与您的数据结构一起使用,而无需您指定详细信息
print(get_highest_score(data)) # 将正确提取最高分

创建多个模块

# 您可以使用 init 函数来额外提示您的库的用途
from autogenlib import init
init("Cryptographic utility library")
# 生成加密模块
from autogenlib.encryption import encrypt_text, decrypt_text
encrypted = encrypt_text("Secret message", "password123")
decrypted = decrypt_text(encrypted, "password123")
print(decrypted)
# 生成哈希模块
from autogenlib.hashing import hash_password, verify_password
hashed = hash_password("my_secure_password")
is_valid = verify_password("my_secure_password", hashed)
print(f"Password valid: {is_valid}")

配置

设置 OpenAI API Key

将您的 OpenAI API key 设置为环境变量:

export OPENAI_API_KEY="your-api-key-here"
# Optional
export OPENAI_API_BASE_URL="https://openrouter.ai/api/v1" # Use OpenRouter API
export OPENAI_MODEL="openai/gpt-4.1"

或者在您的 Python 代码中(不推荐用于生产环境):

import os
os.environ["OPENAI_API_KEY"] = "your-api-key-here"

缓存行为

默认情况下,AutoGenLib 不缓存生成的代码。 这意味着:

如果您想启用缓存(为了保持一致性或减少 API 调用):

from autogenlib import init
init("Library for data processing", enable_caching=True)

或者在运行时切换缓存:

from autogenlib import init, set_caching
init("Library for data processing")
# Later in your code:
set_caching(True) # Enable caching
set_caching(False) # Disable caching

启用缓存后,生成的代码将存储在 ~/.autogenlib_cache 中。

限制

高级用法

检查生成的代码

您可以检查为模块生成的代码:

from autogenlib.totp import totp_generator
import inspect
print(inspect.getsource(totp_generator))

AutoGenLib 如何使用 OpenAI API

AutoGenLib 为 OpenAI API 创建提示,其中包括:

  1. 您提供的描述
  2. 正在增强的模块中的任何现有代码
  3. 所有先前生成的模块的完整上下文
  4. 导入模块/函数的代码(新功能!)
  5. 所需的特定功能或特性

这种全面的上下文有助于 LLM 生成与您现有代码库一致并且与您打算使用它的方式完美契合的代码。

贡献

不欢迎捐款!这只是一个有趣的 PoC 项目。

许可证

MIT 许可证

注意:此库旨在用于原型设计和实验。在生产环境中使用自动生成的代码之前,请务必对其进行审查。

注意:当然,此库的 100% 代码都是通过 LLM 生成的

关于

Import wisdom, export code.

Resources

Readme

License

MIT license Activity

Stars

32 stars

Watchers

1 watching

Forks

3 forks