Skip to main content

AIpy(love-python): Seamless AI Integration into Python

AIpy is a third-party library designed to effortlessly embed AI capabilities into Python, allowing users to solve problems through natural language descriptions and gradually transition to writing code. Inspired by the GitHub project vibesort, AIpy aims to go beyond simple sorting by enabling AI to handle a wide range of tasks directly.

Key Features

  • @ai_func: Directly uses AI to perform calculations. Ideal for complex problems (slower but low error rate).
  • @ai_act: Generates Python code from natural language descriptions. Best for simple tasks (faster but moderate error rate).
  • @ai_code: Automatically selects the most suitable method (AI calculation or code generation) for any problem (fast and low error rate).

How It Works

  1. Function Documentation: AIpy parses the function's docstring, parameters, and return types.
  2. AI Invocation: Based on the analysis, it calls the large language model to generate either:
    • A direct calculation result (@ai_func)
    • Executable Python code (@ai_act)
    • Optimized code generation (@ai_code)
  3. Result Handling: The result is automatically converted to the expected type and returned.

Installation

pip install love-python

Quick Start

Example: Solving a Problem with @ai_code

from lovepython import ai_code

@ai_code
def find_max(numbers: list[int]) -> int:
    """
    Find the maximum number in a list.
    """
    # AI will fill this in

print(find_max([3, 1, 4, 1, 5]))  # Output: 5

Example: Using @ai_func for Calculation

from lovepython import ai_func

@ai_func
def calculate_area(radius: float) -> float:
    """
    Calculate the area of a circle with the given radius.
    """
    # AI will compute the result directly

print(calculate_area(5.0))  # Output: 78.53981633974483

Model Configuration

AIpy supports using OpenAI format APIs for computation and code generation. You can configure the model information used in the current path at ./__pycache__/.aipy/meta.json, or modify the meta.json file in the directory where this library is located to set default configurations. If no changes are made, the local ollama will be used as the inference API by default, for example:

{
    "model": "qwen2.5:32b",
    "url": "http://localhost:11434/",
    "temperature": 0.7,
    "api_key": "ollama"
}

Performance Optimization

To minimize token usage and execution time, AIpy caches generated code in ./__pycache__/.aipy/. If the function name and prompt remain unchanged, the cached code is reused. You can force regeneration by setting force=True in the decorator.


AIpy(爱-Python):让AI无缝嵌入Python

AIpy 是一个第三方库,旨在将AI能力无缝嵌入Python,使用户能够通过自然语言描述解决问题,并逐步过渡到编写代码。灵感来源于GitHub项目vibesort,AIpy的目标是超越简单的排序,让AI直接处理各种任务。

主要功能

  • @ai_func:直接使用AI完成计算。适合复杂问题(速度较慢但错误率低)。
  • @ai_act:从自然语言描述生成Python代码。适合简单任务(速度快但错误率中等)。
  • @ai_code:自动选择最合适的处理方式(AI计算或代码生成),适用于任何问题(速度快且错误率低)。

工作原理

  1. 函数文档解析:AIpy解析函数的注释、参数和返回类型。
  2. 调用AI:根据分析,调用大语言模型生成:
    • 直接计算结果(@ai_func)
    • 可执行Python代码(@ai_act)
    • 优化后的代码生成(@ai_code)
  3. 结果处理:结果自动转换为预期类型并返回。

安装

pip install love-python

快速入门

示例:使用@ai_code解决问题

from lovepython import ai_code

@ai_code
def find_max(numbers: list[int]) -> int:
    """
    找出列表中的最大数。
    """

print(find_max([3, 1, 4, 1, 5]))  # 输出: 5

示例:使用@ai_func进行计算

from lovepython import ai_func

@ai_func
def calculate_area(radius: float) -> float:
    """
    计算给定半径的圆的面积。
    """

print(calculate_area(5.0))  # 输出: 78.53981633974483

模型配置

AIpy支持使用OpenAI格式的API进行计算和代码生成。您可以在./__pycache__/.aipy/meta.json中配置当前路径下使用的模型信息,或修改本库所在的文件目录中的meta.json的文件,以设置默认配置。如无修改,默认使用本地ollama作为推理API,样例如下:

{
    "model": "qwen2.5:32b",
    "url": "http://localhost:11434/",
    "temperature": 0.7,
    "api_key": "ollama"
}

性能优化

为减少Token消耗和运行时间,AIpy将生成的代码缓存到./__pycache__/.aipy/。如果函数名和提示词不变,将重用缓存代码。您可以在装饰器参数中设置force=True强制更新。

Metadata

Release files for love-python 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for love-python 0.1.2
File Size Uploaded Provenance
love_python-0.1.2.tar.gz 34.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for love-python 0.1.2
File Interpreter ABI Platform
love_python-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 63.6 kB

Release files / love_python-0.1.2.tar.gz

Download URL love_python-0.1.2.tar.gz
Size 34.2 kB
Tags Source
SHA-256 checksum
How to use checksums
7b8b3552048a74a1f2d38a4aef0a0330470a74523b1041b3fabf7e538983e1bd
BLAKE2b-256 checksum
How to use checksums
a842ef133d92e59c89e19da09ee50338649a5e4ffa0f11060cb3385a03803156
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.9

Release files / love_python-0.1.2-py3-none-any.whl

Download URL love_python-0.1.2-py3-none-any.whl
Size 29.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
021615f7de69c26f22d526815f9076be1ef7874ad69aeaf26afafc66dc00c1b4
BLAKE2b-256 checksum
How to use checksums
ca74bfddf026a3355ce87615d2941a12bf176fb220d59081e1f0df84f57dba6b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.9

Security for love-python 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page