Skip to main content

Helper functions for building AI Tools as IVCAP services

Project description

ivcap_ai_tool: A python library for building AI tools for the IVCAP platform

A python library containing various helper and middleware functions to simplify developing AI tools to be deployed on IVCAP.

Note: A template git repositiory using this library can be found on github ivcap-works/ivcap-python-ai-tool-template. You may clone that and start from there.

Content

Register a Tool Function

class Request(BaseModel):
    jschema: str = Field("urn:sd:schema:some_tool.request.1", alias="$schema")
    ...

class Result(BaseModel):
    jschema: str = Field("urn:sd:schema:some_tool.1", alias="$schema")
    ...

def some_tool(req: Request) -> Result:
    """
    Here should go a quite extensive description of what the tool can be
    used for so that an agent can work out if this tool is useful in
    a specific context.

    DO NOT ADD PARAMTER AND RETURN DECRIPTIONS -
       DESCRIBE THEM IN THE `Request` MODEL
    """
    ...

    return Result(...)

add_tool_api_route(app, "/", some_tool, opts=ToolOptions(tags=["Great Tool"]))

Start the Service

app = FastAPI(
  ..
)

if __name__ == "__main__":
    start_tool_server(app, some_tool)

JSON-RPC Middleware

This middleware will convert any POST / with a payload following the JSON-RPC specification to an internal POST /{method} and will return the result formatted according to the JSON-RPC spec.

from ivcap_fastapi import use_json_rpc_middleware

app = FastAPI(
  ..
)

use_json_rpc_middleware(app)

Try-Later Middleware

This middleware is supporting the use case where the execution of a requested service is taking longer than the caller is willing to wait. A typical use case is where the service is itself outsourcing the execution to some other long-running service but may immediately receive a reference to the eventual result.

In this case, raising a TryLaterException will return with a 204 status code and additional information on how to later check back for the result.

from ivcap_fastapi import TryLaterException, use_try_later_middleware
use_try_later_middleware(app)

@app.post("/big_job")
def big_job(req: Request) -> Response:
    jobID, expected_exec_time = scheduling_big_job(req)
    raise TryLaterException(f"/big_job/jobs/{jobID}", expected_exec_time)

@app.get("/big_job/jobs/{jobID}")
def get_job(jobID: str) -> Response:
    resp = find_result_for(job_id)
    return resp

Specifically, raising TryLaterException(location, delay) will return an HTTP response with a 204 status code with the additional HTTP headers Location and Retry-Later set to location and delay respectively.

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

ivcap_ai_tool-0.5.1.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

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

ivcap_ai_tool-0.5.1-py2.py3-none-any.whl (16.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file ivcap_ai_tool-0.5.1.tar.gz.

File metadata

  • Download URL: ivcap_ai_tool-0.5.1.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.9.16 Darwin/24.3.0

File hashes

Hashes for ivcap_ai_tool-0.5.1.tar.gz
Algorithm Hash digest
SHA256 61e2378c752f54e0854e0052ed7cb33d0c08e8eab12abf02ec040e9f0d36aa22
MD5 3ce28d53f2299506db9e1cf8e057f4f4
BLAKE2b-256 130bc8756302580576110fe531b61c019f248e167cf4db5a53a4fc2a1a53506e

See more details on using hashes here.

File details

Details for the file ivcap_ai_tool-0.5.1-py2.py3-none-any.whl.

File metadata

  • Download URL: ivcap_ai_tool-0.5.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 16.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.9.16 Darwin/24.3.0

File hashes

Hashes for ivcap_ai_tool-0.5.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4e86f190fa8e10875fcfba44905dc85d70aa014086efd3ac15da924bf2c97a9d
MD5 f0a15ff9eb63cba119521c7c0c14862f
BLAKE2b-256 33f7bae588511b11b47d92a282d6aade5e0643c50b5cbd1b585a3032116b271a

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