diff --git a/fern/chat/non-streaming.mdx b/fern/chat/non-streaming.mdx index 492e3602..7ca58d88 100644 --- a/fern/chat/non-streaming.mdx +++ b/fern/chat/non-streaming.mdx @@ -10,9 +10,13 @@ Build a chat integration that receives complete responses after processing, perf **What You'll Build:** * Simple request-response chat patterns with immediate complete responses -* Session-based conversations that maintain context across multiple chats +* Context management using `previousChatId` for linked conversations * Basic integration with predictable response timing + +For comprehensive context management options including sessions, see **[Session management](/chat/session-management)**. + + ## Prerequisites * Completed [Chat quickstart](/chat/quickstart) tutorial @@ -21,7 +25,7 @@ Build a chat integration that receives complete responses after processing, perf ## Scenario -We'll build a help desk system for "TechFlow" that processes support messages through text chat and maintains conversation history using sessions. +We'll build a help desk system for "TechFlow" that processes support messages through text chat and maintains conversation history using `previousChatId`. --- @@ -101,103 +105,13 @@ We'll build a help desk system for "TechFlow" that processes support messages th --- -## 2. Context Management with Sessions - - - - Sessions allow multiple chats to share the same conversation context: - - ```bash title="Create Session" - curl -X POST https://api.vapi.ai/session \ - -H "Authorization: Bearer YOUR_API_KEY" \ - -H "Content-Type: application/json" \ - -d '{ - "assistantId": "your-assistant-id" - }' - ``` - - - Once you have a session ID, use it for related conversations: - - ```bash title="First Message with Session" - curl -X POST https://api.vapi.ai/chat \ - -H "Authorization: Bearer YOUR_API_KEY" \ - -H "Content-Type: application/json" \ - -d '{ - "sessionId": "session_abc123", - "input": "My account is locked and I can't access the dashboard" - }' - ``` - - ```bash title="Follow-up in Same Session" - curl -X POST https://api.vapi.ai/chat \ - -H "Authorization: Bearer YOUR_API_KEY" \ - -H "Content-Type: application/json" \ - -d '{ - "sessionId": "session_abc123", - "input": "I tried the suggestions but still can't get in" - }' - ``` - - - Build a session-aware chat manager: - - ```typescript title="session-manager.ts" - async function createChatSession(assistantId: string): Promise { - const response = await fetch('https://api.vapi.ai/session', { - method: 'POST', - headers: { - 'Authorization': 'Bearer YOUR_API_KEY', - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ assistantId }) - }); - - const session = await response.json(); - return session.id; - } - - async function sendSessionMessage( - sessionId: string, - message: string - ): Promise { - const response = await fetch('https://api.vapi.ai/chat', { - method: 'POST', - headers: { - 'Authorization': 'Bearer YOUR_API_KEY', - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - sessionId: sessionId, - input: message - }) - }); - - const chat = await response.json(); - return chat.output[0].content; - } - - // Usage example - const sessionId = await createChatSession('your-assistant-id'); - - const response1 = await sendSessionMessage(sessionId, "I need help with billing"); - console.log('Response 1:', response1); - - const response2 = await sendSessionMessage(sessionId, "Can you explain the charges?"); - console.log('Response 2:', response2); // Will remember the billing context - ``` - - - ---- - -## 3. Using previousChatId for Context +## 2. Context Management with previousChatId - - Alternative to sessions - link chats directly: + + Use `previousChatId` to maintain context across multiple chats: - ```typescript title="previous-chat-context.ts" + ```typescript title="conversation-chain.ts" async function createConversation() { let lastChatId: string | undefined; @@ -237,7 +151,7 @@ We'll build a help desk system for "TechFlow" that processes support messages th --- -## 4. Custom Assistant Configuration +## 3. Custom Assistant Configuration @@ -319,6 +233,7 @@ Enhance your non-streaming chat system further: * **[Add streaming capabilities](/chat/streaming)** - Upgrade to real-time responses for better UX * **[OpenAI compatibility](/chat/openai-compatibility)** - Use familiar OpenAI SDK patterns * **[Integrate tools](/tools)** - Enable your assistant to call external APIs and databases +* **[Session management](/chat/session-management)** - Learn about advanced context management with sessions * **[Add voice capabilities](/calls/outbound-calling)** - Extend your text chat to voice interactions diff --git a/fern/chat/quickstart.mdx b/fern/chat/quickstart.mdx index 8eaaa016..1bf2a3c6 100644 --- a/fern/chat/quickstart.mdx +++ b/fern/chat/quickstart.mdx @@ -273,6 +273,7 @@ Take your chat bot to the next level: * **[Streaming responses](/chat/streaming)** - Add real-time typing indicators and progressive responses * **[Non-streaming responses](/chat/non-streaming)** - Learn about sessions and complex conversation flows +* **[Session management](/chat/session-management)** - Learn advanced context management with sessions and previousChatId * **[OpenAI compatibility](/chat/openai-compatibility)** - Integrate with existing OpenAI workflows diff --git a/fern/chat/session-management.mdx b/fern/chat/session-management.mdx new file mode 100644 index 00000000..88f67052 --- /dev/null +++ b/fern/chat/session-management.mdx @@ -0,0 +1,245 @@ +--- +title: Session management +subtitle: Maintain conversation context using previousChatId vs sessionId +slug: chat/session-management +--- + +## Overview + +Vapi provides two approaches for maintaining conversation context across multiple chat interactions. + +**Two Context Management Methods:** +* **`previousChatId`** - Links individual chats in sequence +* **`sessionId`** - Groups multiple chats under a persistent session + + +`previousChatId` and `sessionId` are **mutually exclusive**. You cannot use both in the same request. + + +## Prerequisites + +* Completed [Chat quickstart](/chat/quickstart) tutorial +* Basic understanding of chat requests and responses + +--- + +## Method 1: Using previousChatId + +Link chats together by referencing the ID of the previous chat. + + + + ```bash title="Initial Chat" + curl -X POST https://api.vapi.ai/chat \ + -H "Authorization: Bearer YOUR_API_KEY" \ + -H "Content-Type: application/json" \ + -d '{ + "assistantId": "your-assistant-id", + "input": "Hello, my name is Sarah" + }' + ``` + + + ```json title="Response" + { + "id": "chat_abc123", + "output": [{"role": "assistant", "content": "Hello Sarah!"}] + } + ``` + + + ```bash title="Follow-up Chat" + curl -X POST https://api.vapi.ai/chat \ + -H "Authorization: Bearer YOUR_API_KEY" \ + -H "Content-Type: application/json" \ + -d '{ + "assistantId": "your-assistant-id", + "previousChatId": "chat_abc123", + "input": "What was my name again?" + }' + ``` + + + +Here's a TypeScript implementation of the conversation chain: + +```typescript title="conversation-chain.ts" +function createConversationChain() { + let lastChatId: string | null = null; + + return async function sendMessage(assistantId: string, input: string) { + const requestBody = { + assistantId, + input, + ...(lastChatId && { previousChatId: lastChatId }) + }; + + const response = await fetch('https://api.vapi.ai/chat', { + method: 'POST', + headers: { + 'Authorization': `Bearer ${process.env.VAPI_API_KEY}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify(requestBody) + }); + + const chat = await response.json(); + lastChatId = chat.id; + + return chat.output[0].content; + }; +} + +// Usage +const sendMessage = createConversationChain(); +await sendMessage("asst_123", "Hi, I'm Alice"); +await sendMessage("asst_123", "What's my name?"); // Remembers Alice +``` + +--- + +## Method 2: Using sessionId + +Create a persistent session that groups multiple chats. + + + + ```bash title="Create Session" + curl -X POST https://api.vapi.ai/session \ + -H "Authorization: Bearer YOUR_API_KEY" \ + -H "Content-Type: application/json" \ + -d '{"assistantId": "your-assistant-id"}' + ``` + + + ```json title="Session Response" + { + "id": "session_xyz789", + "assistantId": "your-assistant-id" + } + ``` + + + ```bash title="First Chat in Session" + curl -X POST https://api.vapi.ai/chat \ + -H "Authorization: Bearer YOUR_API_KEY" \ + -H "Content-Type: application/json" \ + -d '{ + "sessionId": "session_xyz789", + "input": "Hello, I need help with billing" + }' + ``` + + + +Here's a TypeScript implementation of the session manager: + +```typescript title="session-manager.ts" +async function createSession(assistantId: string) { + const response = await fetch('https://api.vapi.ai/session', { + method: 'POST', + headers: { + 'Authorization': `Bearer ${process.env.VAPI_API_KEY}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ assistantId }) + }); + + const session = await response.json(); + + return function sendMessage(input: string) { + return fetch('https://api.vapi.ai/chat', { + method: 'POST', + headers: { + 'Authorization': `Bearer ${process.env.VAPI_API_KEY}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ sessionId: session.id, input }) + }) + .then(response => response.json()) + .then(chat => chat.output[0].content); + }; +} + +// Usage +const sendMessage = await createSession("asst_123"); +await sendMessage("I need help with my account"); +await sendMessage("What was my first question?"); // Remembers context +``` + +--- + +## When to use each approach + +Use `previousChatId` when: +* Dealing with simple back-and-forth conversations +* Looking for a minimal setup + +Use `sessionId` when: +* Building complex multi-step workflows +* Long-running conversations +* Error resilience needed + + +Sessions are tied to one assistant. You cannot specify `assistantId` when using `sessionId`. + + +--- + +## Multi-Assistant Workflows + +For workflows with multiple assistants, create separate sessions for each assistant. + +```typescript title="multi-assistant-workflow.ts" +function createMultiAssistantWorkflow() { + const sessions = new Map(); + + return async function sendToAssistant(assistantId: string, input: string) { + let sessionId = sessions.get(assistantId); + + if (!sessionId) { + const response = await fetch('https://api.vapi.ai/session', { + method: 'POST', + headers: { + 'Authorization': `Bearer ${process.env.VAPI_API_KEY}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ assistantId }) + }); + + const session = await response.json(); + sessionId = session.id; + sessions.set(assistantId, sessionId); + } + + const response = await fetch('https://api.vapi.ai/chat', { + method: 'POST', + headers: { + 'Authorization': `Bearer ${process.env.VAPI_API_KEY}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ sessionId, input }) + }); + + const chat = await response.json(); + return chat.output[0].content; + }; +} + +// Usage +const sendToAssistant = createMultiAssistantWorkflow(); +await sendToAssistant("support_agent", "I have a billing issue"); +await sendToAssistant("billing_agent", "Can you help with this?"); +``` + +--- + +## Next Steps + +* **[Streaming responses](/chat/streaming)** - Add real-time responses to session-managed chats +* **[OpenAI compatibility](/chat/openai-compatibility)** - Use familiar OpenAI patterns with sessions +* **[Custom tools](/tools/custom-tools)** - Give assistants access to external APIs within sessions + + +Need help? Chat with the team on our [Discord](https://discord.com/invite/pUFNcf2WmH) or mention us on [X/Twitter](https://x.com/Vapi_AI). + diff --git a/fern/chat/streaming.mdx b/fern/chat/streaming.mdx index a9250542..083d5879 100644 --- a/fern/chat/streaming.mdx +++ b/fern/chat/streaming.mdx @@ -213,6 +213,7 @@ Enhance your streaming chat further: * **[OpenAI compatibility](/chat/openai-compatibility)** - Use OpenAI SDK for streaming with familiar syntax * **[Non-streaming patterns](/chat/non-streaming)** - Learn about sessions and complex conversation management +* **[Session management](/chat/session-management)** - Learn about context management with sessions and previousChatId in streaming * **[Add tools](/tools)** - Enable your assistant to call external APIs while streaming diff --git a/fern/docs.yml b/fern/docs.yml index ba111a1b..db7a075f 100644 --- a/fern/docs.yml +++ b/fern/docs.yml @@ -334,6 +334,9 @@ navigation: - page: OpenAI compatibility path: chat/openai-compatibility.mdx icon: fa-light fa-puzzle-piece + - page: Session management + path: chat/session-management.mdx + icon: fa-light fa-layer-group - section: Webhooks collapsed: true