Skip to main content

A package that allows for client-side OHTTP-wrapping over HTTP communication

Project description

ohttpy Package

Introduction

This package allows for client-side OHTTP-wrapping over HTTP communication. It acheives this by providing drop-in replacement classes for requests.Session and httpx.BaseTransport classes that abstract the OHTTP layer.

NOTE: This package utilizes a modified Rust library that implements OHTTP and relies on the chunked OHTTP protocol which has not been ratified into a standard at the time of writing.

NOTE: At the time of writing, this package is currently under active development and therefore, only compatible with chunked-OHTTP gateways that advertise public keys in a specific fashion.

Requests Session Example

To use OHTTP in a requests.Session setting, simply replace the object construction with ohttp.Session:

import ohttpy

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

# Use session like a request.Session.
response = session.post("http://ohttp-server/path/to/post", data="test data")
print(response.text)

HTTPX Transport Example

To use OHTTP in a httpx.Transport setting, simply replace the object construction with ohttp.Transport:

import httpx
import ohttpy

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

# Use client as normal including chunked-encoding response support.
method = "GET"
url = "http://ohttp-server/path/to/get/"
with httpx_client.stream(method, url) as resp:
    print(resp.status_code)
    print(resp.headers)
    for chunk in resp.stream:
        print(chunk.decode(), end="")

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

import httpx
import openai
import ohttpy

# Initialize httpx client with the ohttpy Transport that inherits from httpx.Transport
httpx_client = httpx.Client(transport=ohttpy.Transport())
base_url = "http://ohttp-server/v1"

# Configure OpenAI client with httpx client
client = openai.OpenAI(
    api_key="OPENAI_API_KEY",
    http_client=httpx_client, base_url=base_url)

# Use OpenAI SDK as normal for example llama chat (includingg stream capability)
is_stream=True
chat_completion_stream = client.chat.completions.create(
    model="meta-llama/Llama-3.2-3B-Instruct",
    messages=[
        {
            "role": "user",
            "content": "Create a song that captures the spirit and history of cybersecurity",
        }
    ],
    temperature=0.2,
    top_p=0.7,
    max_tokens=1024,
    stream=is_stream,
)
if is_stream:
    for chunk in chat_completion_stream:
        print(chunk.choices[0].delta.content or "", end="")
else:
    print(chat_completion_stream.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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

ohttpy-0.1a2-cp37-abi3-manylinux_2_34_x86_64.whl (3.9 MB view details)

Uploaded CPython 3.7+manylinux: glibc 2.34+ x86-64

File details

Details for the file ohttpy-0.1a2-cp37-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for ohttpy-0.1a2-cp37-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 5e22178f538c99de8bda43373fe6e15854b4ba3f931ff51116b71c1bcc1eb67d
MD5 a43457704e038565b125cd35a77f6489
BLAKE2b-256 6bed4bb461b384f7ab6df2e9a988bc486d377380e9e4420c0f1df8185d8d7009

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