You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The window.ai.contextCache API allows web applications to efficiently manage and reuse context information for AI models. This is particularly useful for applications that involve ongoing conversations or require maintaining state across multiple AI interactions, such as chatbots, virtual assistants, or context-aware content generation tools.
By caching context, applications can:
Improve response relevance in multi-turn conversations
Reduce latency by avoiding the need to resend full conversation history
Optimize resource usage by managing context size
API Description
interfaceContextCacheOptions{maxSize?: number;// Maximum number of tokens or characters to storettl?: number;// Time-to-live in milliseconds}interfaceContextEntry{id: string;content: string;timestamp: number;}interfaceWindowAI{contextCache: {add(id: string,content: string): Promise<void>;get(id: string): Promise<string|null>;update(id: string,content: string): Promise<void>;delete(id: string): Promise<void>;clear(): Promise<void>;setOptions(options: ContextCacheOptions): Promise<void>;};}interfaceWindow{ai: WindowAI;}
Methods
add(id: string, content: string): Adds a new context entry to the cache.
get(id: string): Retrieves a context entry by its ID.
update(id: string, content: string): Updates an existing context entry.
delete(id: string): Removes a context entry from the cache.
asyncfunctionmanageConversationContext(conversationId,newMessage){// Configure cacheawaitwindow.ai.contextCache.setOptions({maxSize: 1000,ttl: 3600000});// Retrieve existing contextletcontext=awaitwindow.ai.contextCache.get(conversationId);// Update context with new messagecontext=(context ? context+"\n" : "")+newMessage;awaitwindow.ai.contextCache.update(conversationId,context);// Use updated context in AI interactionconstresponse=awaitsomeAIFunction(context);returnresponse;}
This API provides a simple yet flexible way to manage context information for AI interactions in web applications.
The text was updated successfully, but these errors were encountered:
Use Case
The
window.ai.contextCache
API allows web applications to efficiently manage and reuse context information for AI models. This is particularly useful for applications that involve ongoing conversations or require maintaining state across multiple AI interactions, such as chatbots, virtual assistants, or context-aware content generation tools.By caching context, applications can:
API Description
Methods
add(id: string, content: string)
: Adds a new context entry to the cache.get(id: string)
: Retrieves a context entry by its ID.update(id: string, content: string)
: Updates an existing context entry.delete(id: string)
: Removes a context entry from the cache.clear()
: Removes all entries from the cache.setOptions(options: ContextCacheOptions)
: Configures cache behavior.Example Usage
This API provides a simple yet flexible way to manage context information for AI interactions in web applications.
The text was updated successfully, but these errors were encountered: