Skip to main content

A decorator for easy LLM function calls with Pydantic parsing

Project description

Natural Programming with Python

A natural programming inspired decorator for easy LLM function calls with Pydantic parsing.

Installation

pip install naturalpy

Overview

natpy provides a seamless way to integrate LLMs into your Python functions using simple decorators. It transforms your functions into natural language interfaces to AI models, handling all the complexity of API calls and response parsing.

Built on top of the instructor library, natpy uses type annotations and docstrings to convert function calls into structured LLM queries and properly typed responses.

Features

  • Simple @natural decorator syntax
  • Uses function docstrings as prompts
  • Parameter substitution with ${param_name} syntax
  • Automatic response parsing based on return type annotations
  • Supports complex return types (Lists, Dicts, Pydantic models)
  • Configurable model parameters (temperature, max tokens, etc.)

Quick Start

First, set your OpenAI API key as an environment variable:

export OPENAI_API_KEY=your-api-key-here

Then, use the @natural decorator in your code:

from naturalpy import natural
from typing import List

@natural
def generate_ideas(topic: str, count: int) -> List[str]:
    """
    Generate ${count} creative ideas related to ${topic}.
    Each idea should be innovative and practical.
    """

# Call like a normal function
ideas = generate_ideas("sustainable urban gardening", 3)
print(ideas)  # ['Vertical hydroponic systems for balconies', ...]

Advanced Usage

Returning Complex Types

The @natural decorator supports a variety of return types:

from typing import Dict, List
from pydantic import BaseModel

# Return a dictionary
@natural
def analyze_sentiment(text: str, aspects: List[str]) -> Dict[str, float]:
    """
    Analyze the following text: "${text}"
    
    Give me a sentiment score for each of these aspects:
    ${aspects}
    
    For each aspect, provide a score between 0 and 1 where 0 is very negative
    and 1 is very positive.
    """

# Return a Pydantic model
class MovieRecommendation(BaseModel):
    title: str
    year: int
    director: str
    why_recommended: str

@natural
def recommend_movie(genres: List[str], mood: str) -> MovieRecommendation:
    """
    Recommend a movie that matches these genres: ${genres}
    The viewer is in a ${mood} mood.
    """

Customizing LLM Parameters

You can customize the LLM parameters by passing them to the decorator:

@natural(
    model="gpt-4o-2024-08-06",
    temperature=0.9,
    max_tokens=500
)
def write_story(plot: str, style: str) -> str:
    """
    Write a short story based on this plot:
    ${plot}
    
    Write in the style of ${style}.
    """

Error Handling

The decorator includes robust error handling:

  • Missing docstring: ValueError is raised if a function has no docstring
  • Missing return type: TypeError is raised if a function has no return type annotation
  • Invalid parameter references: RuntimeError is raised if the docstring references parameters that don't exist
  • API or parsing errors: RuntimeError is raised with details about the failure

How It Works

  1. The decorator extracts the function's docstring and uses it as a prompt
  2. It substitutes ${parameter_name} placeholders with actual argument values
  3. It determines the expected return type from the function's annotations
  4. It calls the OpenAI API with the assembled prompt
  5. The response is parsed according to the expected type using instructor
  6. The parsed result is returned from the function call

Requirements

  • Python 3.8+
  • OpenAI API key
  • instructor
  • pydantic

License

MIT License

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

naturalpy-0.1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

naturalpy-0.1.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file naturalpy-0.1.0.tar.gz.

File metadata

  • Download URL: naturalpy-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for naturalpy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 31f94ec27eba0b755e2a8d8caac6f69e91026198339f7115b3798a2529effdf6
MD5 5f6cb0bf594cfd20c5975f36fb7bcd5c
BLAKE2b-256 603ce8be06867627ee1c76f4cafe2de05f63bd2864c5165f3dea21a0607436e4

See more details on using hashes here.

File details

Details for the file naturalpy-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: naturalpy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for naturalpy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0761351ebbe2694edbc2d03a0273444ae70b800427548f85d486ff8e9e3258e1
MD5 0a1a75026d3e3ad3938d4505d8d70cc5
BLAKE2b-256 74b27c16ddef39da32b3bbbc0f78880ff2ff6ebc9d0c61a87920dd113346a824

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