Skip to main content

A python package to interact with products from Lorica Cybersecurity

Project description

Lorica Package

Introduction

This package provides functionality for interaction with Lorica Cybersecurity products. The following capabilities are currently offered:

  • OHTTP encapsulation for secure interaction with Lorica AI deployment.

Lorica AI OHTTP Encapsulation using Requests Session

To encapsulate requests and responses through a requests.Session, simply replace the object construction with lorica.ohttp.Session:

import lorica.ohttp
import json

# Create lorica.ohttp.Session that inherits from requests.Session.
session = lorica.ohttp.Session()

deployment_url = "DEPLOYMENT_URL"
lorica_api_key = "LORICA_API_KEY"

# Use session like a request.Session including response streaming support.
stream = True
resp = session.post(
    f"{deployment_url}/v1/chat/completions",
    headers={"Authorization": f"Bearer {lorica_api_key}"},
    json={
        "model": "meta-llama/Llama-3.2-3B-Instruct",
        "messages": [
            {"role": "system", "content": "You are a helpful AI assistant."},
            {"role": "user", "content": "where does the sun rise from?"},
        ],
        "temperature": 0.7,
        "max_tokens": 1024,
        "stream": stream,
    },
    stream=stream
)
resp.raise_for_status()
if stream:
    for line in resp.iter_lines(decode_unicode=True):
        if not line or not line.startswith("data: "):
            continue

        data = line[len("data: "):].strip()
        if data == "[DONE]":
            break

        chunk = json.loads(data)
        print(chunk["choices"][0]["delta"]["content"], end="", flush=True)
else:
    print(resp.json()["choices"][0]["message"]["content"])

Lorica AI OHTTP Encapsulation using HTTPX Transport

To encapsulate requests and responses through a httpx.Transport, simply replace the object construction with lorica.ohttp.Transport:

import lorica.ohttp
import httpx
import json

# Initialize httpx client with the lorica.ohttp.Transport that inherits from httpx.Transport
httpx_client = httpx.Client(transport=lorica.ohttp.Transport())

deployment_url = "DEPLOYMENT_URL"
lorica_api_key = "LORICA_API_KEY"

# Use client as normal including chunked-encoding response support.
method = "POST"
url = deployment_url + "/v1/chat/completions"
stream = True
data = {
    "model": "meta-llama/Llama-3.2-3B-Instruct",
    "messages": [
        {"role": "system", "content": "You are a helpful AI assistant."},
        {"role": "user", "content": "where does the sun rise from?"},
    ],
    "temperature": 0.7,
    "max_tokens": 1024,
    "stream": stream,
}
headers = {"Authorization": f"Bearer {lorica_api_key}"}
if stream:
    with httpx_client.stream(method, url, json=data, headers=headers) as resp:
        resp.raise_for_status()
        for line in resp.iter_lines():
            if not line or not line.startswith("data: "):
                continue

            data = line[len("data: "):].strip()
            if data == "[DONE]":
                break

            chunk = json.loads(data)
            print(chunk["choices"][0]["delta"]["content"], end="", flush=True)
else:
    resp = httpx_client.post(url, json=data, headers=headers, timeout=30)
    resp.raise_for_status()
    print(resp.json()["choices"][0]["message"]["content"])

Lorica AI OHTTP Encapsulation using OpenAI Client

This is also applicable to clients that utilize httpx for their HTTP communication, for example openai client:

import lorica.ohttp
import httpx
import openai

# Initialize httpx client with lorica.ohttp.Transport that inherits from httpx.Transport
httpx_client = httpx.Client(transport=lorica.ohttp.Transport())
deployment_url = "DEPLOYMENT_URL"
lorica_api_key = "LORICA_API_KEY"

# Configure OpenAI client with httpx client
client = openai.OpenAI(
    api_key=lorica_api_key,
    http_client=httpx_client,
    base_url=deployment_url + "/v1")

# Use OpenAI SDK as normal for example llama chat (including stream capability)
stream = True
completion = client.chat.completions.create(
    model="meta-llama/Llama-3.2-3B-Instruct",
    messages=[
        {"role": "system", "content": "You are a helpful AI assistant."},
        {"role": "user", "content": "where does the sun rise from?"},
    ],
    temperature=0.7,
    max_tokens=1024,
    stream=stream,
)
if stream:
    for chunk in completion:
        print(chunk.choices[0].delta.content or "", end="", flush=True)
else:
    print(completion.choices[0].message.content)

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

lorica-0.1a6.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

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

lorica-0.1a6-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file lorica-0.1a6.tar.gz.

File metadata

  • Download URL: lorica-0.1a6.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for lorica-0.1a6.tar.gz
Algorithm Hash digest
SHA256 2cbb14231e4c1bed2672308d26cc8af7dececb89900712825cbb3cc1b796a1a6
MD5 79a2de43603a0dc291436bf01ff59f98
BLAKE2b-256 0ebc2cc1eb473a800e410bbfc92b81692f350c32a208af250671bc03d075d5c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for lorica-0.1a6.tar.gz:

Publisher: CI.yml on Lorica-Cyber/lorica

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lorica-0.1a6-py3-none-any.whl.

File metadata

  • Download URL: lorica-0.1a6-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for lorica-0.1a6-py3-none-any.whl
Algorithm Hash digest
SHA256 53dbefe5de7f94f640df329c9e844fc6c09709d51d19276c94890a3ef9ece0dd
MD5 a9f54d86d3a1e6d68e6dfd5090db6756
BLAKE2b-256 ac256f364dfdedd632189f43ab200956f6eaa4ec4a6a0bf74de3f6e46d287e5a

See more details on using hashes here.

Provenance

The following attestation bundles were made for lorica-0.1a6-py3-none-any.whl:

Publisher: CI.yml on Lorica-Cyber/lorica

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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