Skip to main content

Python SDK for the Context API

Project description

Context Python Library

PyPI version

The Context Python library provides a convenient way to interface with the Context APIs. We include pre-defined classes and operations to interact with API resources.

Installation

pip install --upgrade context-python

Usage

The library needs to be configured with your Context API key, which is available in the Context Settings Dashboard.

Synchronous Example

import getcontext
from getcontext.generated.models import Conversation, Message, MessageRole, Rating
from getcontext.token import Credential
import os

token = os.environ.get("GETCONTEXT_TOKEN")

c = getcontext.ContextAPI(credential=Credential(token))

c.log.conversation(
    body={
        "conversation": Conversation(
            messages=[
                Message(
                    message="You are a helpful assistant!",
                    role=MessageRole.SYSTEM,
                ),
                Message(
                    message="Hello, world!",
                    role=MessageRole.USER,
                ),
                Message(
                    message="Hi, how can I help?",
                    role=MessageRole.ASSISTANT,
                    rating=Rating.POSITIVE,
                ),
            ],
        )
    }
)

Async Example

import asyncio

import getcontext.generated.aio as getcontext
from getcontext.generated.models import Conversation, Message, MessageRole, Rating
from getcontext.token import AsyncCredential
import os

token = os.environ.get("GETCONTEXT_TOKEN")


async def log():
    async with getcontext.ContextAPI(credential=AsyncCredential(token)) as client:
        await client.log.conversation(
            body={
                "conversation": Conversation(
                    messages=[
                        Message(
                            message="You are a helpful assistant!",
                            role=MessageRole.SYSTEM,
                        ),
                        Message(
                            message="Hello, world!",
                            role=MessageRole.USER,
                        ),
                        Message(
                            message="Hi, how can I help?",
                            role=MessageRole.ASSISTANT,
                            rating=Rating.POSITIVE,
                        ),
                    ],
                )
            }
        )


loop = asyncio.get_event_loop()
loop.run_until_complete(log())
loop.close()

Appendix

python: true
output-folder: getcontext/generated/
no-namespace-folders: true
credential-default-policy-type: BearerTokenCredentialPolicy
black: true
python3-only: true
add-credential: true
credential-scopes: all
models-mode: msrest

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

context_python-0.18.0.tar.gz (42.0 kB view details)

Uploaded Source

Built Distribution

context_python-0.18.0-py3-none-any.whl (50.1 kB view details)

Uploaded Python 3

File details

Details for the file context_python-0.18.0.tar.gz.

File metadata

  • Download URL: context_python-0.18.0.tar.gz
  • Upload date:
  • Size: 42.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.7 Darwin/23.4.0

File hashes

Hashes for context_python-0.18.0.tar.gz
Algorithm Hash digest
SHA256 052ecc47e5b0484447827ab35415deaacaa436f68bf1acfb901566f0d4c35633
MD5 924399279e7ec50853553f09de7dec84
BLAKE2b-256 44c525e103fcd9a5a6ff865de06040a6cd5d857d7f2b6a11755ca949864ea836

See more details on using hashes here.

File details

Details for the file context_python-0.18.0-py3-none-any.whl.

File metadata

  • Download URL: context_python-0.18.0-py3-none-any.whl
  • Upload date:
  • Size: 50.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.7 Darwin/23.4.0

File hashes

Hashes for context_python-0.18.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c0b2fcd6e7111fdd621cca4d340aa0dd2a3d0d8c33989ad4c9472ceafa03766c
MD5 1bdea939ac4d739e01fc5dae0a6724a8
BLAKE2b-256 c213808b24ee09b90bc1b2f6e5877bb1247568e51b9d85b96be266063ff276f8

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