fix: error handling

This commit is contained in:
vintro 2025-12-10 14:57:21 -05:00
parent 971b183f11
commit 93a7cee4c1
No known key found for this signature in database
1 changed files with 7 additions and 20 deletions

View File

@ -6,8 +6,11 @@ icon: 'messages'
The `get_context()` method is a powerful feature that retrieves formatted conversation context from sessions, making it easy to integrate with LLMs like OpenAI, Anthropic, and others. This guide covers everything you need to know about working with session context.
<Note>
By default, the context includes a blend of summary and messages ***which covers the entire session history of a peer***.
</Note>
By default, the context includes a blend of summary and messages which covers the entire history of the session. Summaries are automatically generated at intervals and recent messages are included depending on how many tokens the context is intended to be. You can specify any token limit you want, and can disable summaries to fill that limit entirely with recent messages. To get representation data, you need to specify a target peer.
Summaries are automatically generated at intervals and recent messages are included depending on how many tokens the context is intended to be. You can specify any token limit you want, and can disable summaries to fill that limit entirely with recent messages. To get representation data, you need to specify a target peer.
## Basic Usage
@ -601,40 +604,24 @@ anthropic_messages = context.to_anthropic(assistant=assistant)
### 3. Error Handling
Always handle potential errors when working with context:
Always handle potential errors when retrieving context:
<CodeGroup>
```python Python
try:
context = session.get_context(tokens=2000)
messages = context.to_openai(assistant=assistant)
# Use messages with LLM API
response = openai_client.chat.completions.create(
model="gpt-4",
messages=messages
)
except Exception as e:
print(f"Error getting context: {e}")
# Handle error appropriately
# Handle error appropriately (fallback to basic context, retry, etc.)
```
```typescript TypeScript
(async () => {
try {
const context = await session.getContext({ tokens: 2000 });
const messages = context.toOpenAI(assistant);
// Use messages with LLM API
const response = await openai.chat.completions.create({
model: "gpt-4",
messages: messages
});
} catch (error) {
console.error(`Error getting context: ${error}`);
// Handle error appropriately
// Handle error appropriately (fallback to basic context, retry, etc.)
}
})();
```