Skip to main content

AIpython is designed to effortlessly embed AI capabilities into Python, allowing users to solve problems through natural language descriptions and gradually transition to writing code.

Project description

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强制更新。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

love_python-0.1.2.tar.gz (34.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

love_python-0.1.2-py3-none-any.whl (29.4 kB view details)

Uploaded Python 3

File details

Details for the file love_python-0.1.2.tar.gz.

File metadata

  • Download URL: love_python-0.1.2.tar.gz
  • Upload date:
  • Size: 34.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for love_python-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7b8b3552048a74a1f2d38a4aef0a0330470a74523b1041b3fabf7e538983e1bd
MD5 0bf5a33ee68644e76d625b8a48fb551f
BLAKE2b-256 a842ef133d92e59c89e19da09ee50338649a5e4ffa0f11060cb3385a03803156

See more details on using hashes here.

File details

Details for the file love_python-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: love_python-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 29.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for love_python-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 021615f7de69c26f22d526815f9076be1ef7874ad69aeaf26afafc66dc00c1b4
MD5 ce692440e2a0c5a47045258c2564cd88
BLAKE2b-256 ca74bfddf026a3355ce87615d2941a12bf176fb220d59081e1f0df84f57dba6b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page