Python SDK

Our Python SDK makes it straightforward to integrate with Context.ai.

Installation

Install the Context.ai Python SDK with Pip:

pip install --upgrade context-python

Usage

The Python SDK needs to be authenticated with your Context.ai API Key. You can generate this key from the onboarding page (if you have not yet sent any transcripts to Context.ai), or from the settings page if you are already integrated.

Once you have your API key, you can start sending requests immediately using one of the example code snippets below. We provide integrations for working with our API in both a synchronous and asynchronous way.

Synchronous Example

import getcontext
from getcontext.generated.models import Conversation, Message, MessageRole, Rating, Thread
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.",
                    role=MessageRole.USER,
                ),
                Message(
                    message="Hi, how can I help?",
                    role=MessageRole.ASSISTANT,
                    rating=Rating.POSITIVE,
                ),
            ],
            metadata={
                "model": "gpt-3.5-turbo",
                "user_id": "1234",
                "environment": "test",
            }
        )
    }
)

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

Asynchronous 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.",
                            role=MessageRole.USER,
                        ),
                        Message(
                            message="Hi, how can I help?",
                            role=MessageRole.ASSISTANT,
                            rating=Rating.POSITIVE,
                        ),
                    ],
                    metadata={
                        "model": "gpt-3.5-turbo",
                        "user_id": "1234",
                        "environment": "test",
                    }
                )
            }
        )


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

Github: https://github.com/contextco/context-py/tree/main

Last updated