https://github.com/contextco/context-py
https://github.com/contextco/context-py
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/contextco/context-py
- Owner: contextco
- License: mit
- Created: 2023-05-31T21:45:46.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-08-05T12:51:56.000Z (almost 2 years ago)
- Last Synced: 2024-08-05T14:45:42.759Z (almost 2 years ago)
- Language: Python
- Size: 475 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Context Python Library
[](https://badge.fury.io/py/context-python)
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](https://go.getcontext.ai/settings).
### Synchronous Example
```python
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
```python
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
```yaml
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
```