Context.ai
  • What is Context.ai?
  • List of Trackable Metrics
  • Product Analytics
    • Overview
    • Topic Categorisation
      • LLM Topics
    • User Engagement Tracking
    • Foreign Language Support
    • PII Filtering
    • Custom Metadata Filtering
    • Backfill Analytics Data
    • Custom Events
    • API Ingestion Methods
      • Log Conversation
      • [deprecated] Upsert Conversation
      • Thread Conversation
      • Patch Thread Message
      • API Resources
        • Chat Message
        • [deprecated] Tool Message
        • Custom Event
        • Metadata
        • Conversation
        • Thread
    • Embedded API
      • Multi-Tenancy
      • Conversations
        • Series Data
  • Integrations
    • Getting Started
    • Python SDK
    • Javascript SDK
    • LangChain Plugin
    • Haystack Plugin
    • Authorization
Powered by GitBook
On this page
  • Installation
  • Usage
  • Synchronous Example
  • Asynchronous Example

Was this helpful?

  1. Integrations

Python SDK

PreviousGetting StartedNextJavascript SDK

Last updated 9 months ago

Was this helpful?

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 (if you have not yet sent any transcripts to Context.ai), or from the 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:

onboarding page
settings page
https://github.com/contextco/context-py/tree/main