Our Python SDK makes it straightforward to integrate with Context.ai.
Installation
Install the Context.ai Python SDK with Pip:
pipinstall--upgradecontext-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 getcontextfrom getcontext.generated.models import Conversation, Message, MessageRole, Rating, Threadfrom getcontext.token import Credentialimport ostoken = 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 asyncioimport getcontext.generated.aio as getcontextfrom getcontext.generated.models import Conversation, Message, MessageRole, Ratingfrom getcontext.token import AsyncCredentialimport ostoken = os.environ.get("GETCONTEXT_TOKEN")asyncdeflog():asyncwith 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()