Skip to main content

LLM please cast to JSON

Project description

jsonllm


image

Installation


From PyPI

pip install python-jsonllm

From source

git clone https://github.com/ivanbelenky/jsonllm.git
cd jsonllm
python3 -m pip install -e .

Documentation


Schema type

class SchemaKey(TypedDict):
    name: Optional[str]
    type: Optional[type]
    default: JSONCompatible
    required: Optional[bool]
    instructions: Optional[str]
    valid: Optional[Caster] 
    caster: Optional[Validator]

class Schema(TypedDict):
    __key__: Union[SchemaKey, 'Schema'] # nested schemas

Example

import os

import jsonllm
import openai

# vertexai.init(project_id='jsonllm-rocks?', location='us-central1') | 
# openai.api_key = 'sk-...'
os.environ['ANTHROPIC_API_KEY'] = '...' # or load env

person = {
    'first_name': {
        'type': str,
        'required': True,
        'instructions': 'Find the first name, if not found input John'        
    },
    'last_name': {
        'type': str,
        'required': True,
        'instructions': 'Find the last name, if not found input Doe'
    },
    'date_of_birth': {
        'name': 'dob',
        'type': str,
        'instructions': 'Find the date of birth and cast it to isoformat'
    }
}

message = 'My name is John Connor, I think I was born 0 of Unix time.'
response = jsonllm.loads(message, person, model='claude-3-opus-20240229')
print(response.response)

{
    'first_name': 'John',
    'last_name': 'Connor',
    'dob': '1970-01-01'
}

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

python-jsonllm-0.0.2a0.tar.gz (7.8 kB view details)

Uploaded Source

File details

Details for the file python-jsonllm-0.0.2a0.tar.gz.

File metadata

  • Download URL: python-jsonllm-0.0.2a0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.8.13

File hashes

Hashes for python-jsonllm-0.0.2a0.tar.gz
Algorithm Hash digest
SHA256 14894c64c54929518b5952fabeb3a2847dbfcc13556a82a2f6add39058064a59
MD5 3321cd27ef165f002608e35a31f29c56
BLAKE2b-256 0a402ab38a1dea398d75734e0e6545377a33a2fa80364a36eabc83522f70cc61

See more details on using hashes here.

Supported by

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