Design Document - Part II API
Purpose
This Design Document gives the complete design of the software implementation. This information should be in structured comments (e.g. Javadoc) in the source files. We encourage the use of a documentation generation tool to generate a draft of your API that you can augment to include the following details.
Requirements
In addition to the general documentation requirements the Design Document - Part II API will contain:
General review of the software architecture for each module specified in Design Document - Part I Architecture. Please include your class diagram as an important reference.
For each class define the data fields, methods.
RAGPipeline
Purpose: Orchestrates the complete Retrieval-Augmented Generation flow from query to response.
Data Fields:
retriever: DocumentRetriever- Document retrieval componentcontextManager: ContextManager- Conversation context handlerpromptBuilder: PromptBuilder- Prompt construction componentllmConnector: LLMConnector- LLM interfacechain: Chain- LangChain Chain object coordinating componentspipelineConfig: Map<String, Any>- Configuration settingsexecutionMetrics: ExecutionMetrics- Performance tracking
Methods:
| Method | Purpose | Parameters | Return Type | Pre-conditions | Post-conditions | Exceptions |
|---|---|---|---|---|---|---|
process(userQuery: String, userId: String): RAGResponse | Executes full RAG pipeline | userQuery: user question, userId: user identifier | RAGResponse object with answer | All components initialized, userId valid | Response generated, logged, metrics updated | RAGException, PipelineException |
retrieveContext(query: String): List<Document> | Retrieves relevant documents for query | query: search query string | List of retrieved Document objects | retriever initialized | Context docs selected and ranked | RetrievalException, VectorStoreException |
generateResponse(query: String, documents: List<Document>, context: ConversationContext): String | Generates response using LLM | query: user query, documents: context docs, context: history | Response string | LLM ready, documents provided, context valid | Response generated with citations | LLMException, GenerationException |
validateResponse(response: String, sourceDocuments: List<Document>): Boolean | Validates response quality and relevance | response: generated response, sourceDocuments: used docs | Boolean indicating validation result | response not empty, documents provided | Validation score computed and logged | ValidationException |
handleFallback(originalQuery: String, error: Exception): String | Handles pipeline failures with fallback | originalQuery: original user query, error: exception thrown | Fallback response string | original error caught, fallback ready | Fallback response returned, error logged | FallbackException |
ContextManager
Purpose: Maintains conversation history and manages context windows for multi-turn conversations.
Data Fields:
conversationHistory: List<Message>- Ordered list of messagescontextWindow: int- Maximum number of previous messages to keepuserId: String- User ID for this conversationsessionStartTime: LocalDateTime- When conversation startedlastMessageTime: LocalDateTime- Timestamp of last messagecontextSummary: String- Summary of earlier conversation
Methods:
| Method | Purpose | Parameters | Return Type | Pre-conditions | Post-conditions | Exceptions |
|---|---|---|---|---|---|---|
addMessage(role: Role, content: String): void | Adds user or assistant message to history | role: USER or ASSISTANT, content: message text | void | content not empty | Message appended, timestamp recorded | InvalidMessageException |
getContext(): ConversationContext | Retrieves current conversation context | none | ConversationContext object | history not empty | Context object returned with full history | none |
trimHistory(): void | Removes oldest messages if exceeding context window | none | void | history populated | Excess messages removed, summary updated | none |
getSummary(maxLength: int): String | Gets concise summary of conversation | maxLength: max chars for summary | Summary string | history not empty | Summary generated and cached | SummaryException |
getRelevantContext(query: String, maxMessages: int): List<Message> | Filters history for query-relevant messages | query: current query, maxMessages: limit | Filtered list of Message objects | history not empty, query provided | Most relevant messages selected | ContextFilterException |
clearHistory(): void | Clears conversation history | none | void | none | History cleared, session ended | none |
exportConversation(): List<Message> | Exports full conversation for logging | none | List of all Message objects | history not empty | Conversation exported, formatted | ExportException |
LLMConnector
Purpose: Abstracts LLM interactions using LangChain, supporting multiple providers (OpenAI, Claude, etc.).
Data Fields:
llmProvider: String- Provider name (OPENAI, ANTHROPIC, etc.)apiKey: String- API authentication keymodelName: String- Specific model identifiertemperature: Float- Response randomness (0-1)maxTokens: int- Maximum output lengthretryPolicy: RetryPolicy- Retry configurationrequestCache: Map<String, String>- Cache of responses
Methods:
| Method | Purpose | Parameters | Return Type | Pre-conditions | Post-conditions | Exceptions |
|---|---|---|---|---|---|---|
generate(prompt: String): String | Generates single response from prompt | prompt: full prompt text | Generated response string | LLM initialized, API key valid, prompt provided | Response returned and cached | LLMException, APIException, RateLimitException |
streamResponse(prompt: String, callback: StreamCallback): void | Streams response token-by-token for real-time UI | prompt: full prompt, callback: token handler | void | LLM ready, streaming supported | Tokens streamed to callback as generated | StreamingException, LLMException |
validateResponse(response: String, constraints: List<Constraint>): Boolean | Validates response meets quality constraints | response: generated text, constraints: validation rules | Boolean validation result | response not empty, constraints defined | Validation result computed | ValidationException |
setTemperature(temperature: Float): void | Configures response randomness | temperature: value 0.0-1.0 | void | 0 <= temperature <= 1 | Temperature parameter updated | InvalidParameterException |
setMaxTokens(maxTokens: int): void | Sets maximum response length | maxTokens: token limit | void | maxTokens > 0 | Token limit updated | InvalidParameterException |
testConnection(): Boolean | Verifies LLM API connectivity | none | true if connected, false otherwise | API key configured | Connection status determined | ConnectionException |
getUsageStats(): UsageStats | Retrieves API usage and cost information | none | UsageStats object with metrics | requests made | Usage stats returned | StatsException |
PromptBuilder
Purpose: Constructs optimized prompts for LLM by combining retrieved documents, context, and instructions.
Data Fields:
systemPromptTemplate: String- System role instruction templateuserPromptTemplate: String- User query templatedocumentTemplate: String- Template for formatting documentsexamplePrompts: Map<Intent, String>- Few-shot examples by intentpromptConfig: Map<String, Any>- Configuration parameterscitationFormat: CitationFormat- Format for citations
Methods:
| Method | Purpose | Parameters | Return Type | Pre-conditions | Post-conditions | Exceptions |
|---|---|---|---|---|---|---|
buildSystemPrompt(context: SystemContext): String | Builds system role and guidelines prompt | context: SystemContext with company info | System prompt string | context populated | System prompt formatted and ready | PromptException |
buildUserPrompt(query: String, documents: List<Document>, context: ConversationContext): String | Assembles full user prompt with context | query: user question, documents: retrieved docs, context: history | Complete user prompt string | query not empty, documents provided | Prompt assembled with formatting | PromptBuildException |
formatDocuments(documents: List<Document>): String | Formats documents for inclusion in prompt | documents: list of Document objects | Formatted document string | documents not empty | Documents formatted with metadata | FormattingException |
addCitations(documents: List<Document>): String | Adds source citations for documents | documents: source documents | Citation formatted string | documents have source URLs | Citations formatted according to style | CitationException |
getExamplePrompt(intent: Intent): String | Retrieves few-shot example for intent type | intent: detected intent | Example prompt string | examplePrompts loaded, intent valid | Example fetched for use in prompt | PromptNotFoundException |
optimizePromptLength(prompt: String, maxLength: int): String | Truncates prompt while preserving quality | prompt: full prompt, maxLength: char limit | Optimized prompt string | prompt not empty, maxLength > 0 | Prompt trimmed intelligently | OptimizationException |
validatePrompt(prompt: String): PromptValidation | Validates prompt structure and content | prompt: assembled prompt | PromptValidation with score and issues | prompt not empty | Validation result computed | ValidationException |
ChatbotOrchestrator
Purpose: Main controller orchestrating all RAG pipeline components and managing conversation flow.
Data Fields:
ragPipeline: RAGPipeline- RAG pipeline instancequeryProcessor: QueryProcessor- Query processing componentslackHandler: SlackBotHandler- Slack integrationuserProfileManager: UserProfileManager- User managementexpertMatcher: ExpertMatcher- Expert matchingfallbackSearchHandler: FallbackSearchHandler- Fallback searchconversationManagers: Map<String, ContextManager>- Per-user conversation contextsorchestrationMetrics: Metrics- Performance and usage metrics
Methods:
| Method | Purpose | Parameters | Return Type | Pre-conditions | Post-conditions | Exceptions |
|---|---|---|---|---|---|---|
handleQuery(query: String, userId: String, channelId: String): void | Main entry point handling user query | query: user question, userId: user ID, channelId: Slack channel | void | query not empty, userId/channelId valid | Response sent to user, interaction logged | OrchestrationException |
manageConversation(userId: String): ConversationContext | Gets or creates conversation context for user | userId: user identifier | ConversationContext object | userId not empty | Conversation context retrieved/created | ConversationException |
processUserQuery(query: String, userId: String): ProcessedQuery | Processes query through pipeline | query: user input, userId: user | ProcessedQuery with parsed intent | query not empty | Query fully processed | ProcessingException |
generateContextualResponse(query: String, context: ConversationContext): String | Creates response with context awareness | query: current query, context: conversation history | Response string | query and context provided | Response generated with context | ResponseException |
routeToExpert(query: String, userId: String): UserProfile | Routes complex queries to experts | query: user question, userId: requesting user | Expert UserProfile | query valid, user exists | Expert identified and notified | ExpertRoutingException |
handleSpecialCommands(command: String, userId: String): String | Processes special bot commands | command: command string, userId: user | Command response | command recognized | Command executed, response returned | CommandException |
getMetrics(): Metrics | Retrieves performance and usage metrics | none | Metrics object | orchestrator active | Current metrics returned | none |
shutdown(): void | Gracefully shuts down all components | none | void | orchestrator running | All components stopped, connections closed | ShutdownException |
initialize(): void | Initializes all components in dependency order | none | void | configuration loaded | All components ready to use | InitializationException |
FallbackSearchHandler
Purpose: Implements fallback search strategies when internal documentation is insufficient.
Data Fields:
searchEngines: Map<String, SearchProvider>- External search providerssourceValidator: SourceValidator- Validates search resultsfallbackConfig: Map<String, Any>- Fallback configurationresultCache: Map<String, List<SearchResult>>- Cache of search resultsallowedDomains: List<String>- Whitelisted domains for fallback
Methods:
| Method | Purpose | Parameters | Return Type | Pre-conditions | Post-conditions | Exceptions |
|---|---|---|---|---|---|---|
searchExternal(query: String, engines: List<String>): List<SearchResult> | Searches external sources | query: search query, engines: search providers | List of SearchResult objects | query not empty, engines valid | Results retrieved from engines | SearchException, ExternalServiceException |
validateSource(result: SearchResult): Boolean | Validates search result source credibility | result: SearchResult to validate | true if valid source | result has URL and content | Source validation completed | ValidationException |
rankResults(results: List<SearchResult>, originalQuery: String): List<SearchResult> | Ranks search results by relevance | results: unranked results, originalQuery: reference query | Ranked list of SearchResult objects | results not empty | Results sorted by relevance score | RankingException |
filterByDomain(results: List<SearchResult>): List<SearchResult> | Filters results to allowed domains | results: results to filter | Filtered SearchResult list | allowedDomains configured | Results filtered by domain whitelist | none |
summarizeExternalContent(content: String): String | Creates summary of external source | content: full text | Summary string | content not empty | Summary generated and returned | SummarizationException |
determineFallbackNeed(internalResults: List<Document>): Boolean | Checks if fallback search needed | internalResults: retrieved documents | true if fallback needed | internalResults evaluated | Fallback decision made | none |
addToWhitelist(domain: String, category: String): void | Adds domain to approved sources | domain: domain name, category: category | void | domain valid format | Domain added to whitelist | ValidationException |
removeFromWhitelist(domain: String): void | Removes domain from approved sources | domain: domain to remove | void | domain in whitelist | Domain removed from whitelist | DomainNotFoundException |
ErrorHandler
Purpose: Manages error handling, logging, and graceful fallbacks for various failure scenarios.
Data Fields:
errorStrategies: Map<ErrorType, ErrorStrategy>- Error type handlersfallbackResponses: Map<ErrorType, String>- Fallback response messagesnotificationManager: NotificationManager- Admin error notificationserrorLogger: Logger- Error logging instancerecoveryStrategies: Map<ErrorType, RecoveryStrategy>- Recovery procedureserrorThresholds: Map<ErrorType, int>- Error rate thresholds
Methods:
| Method | Purpose | Parameters | Return Type | Pre-conditions | Post-conditions | Exceptions |
|---|---|---|---|---|---|---|
handleTimeout(requestId: String, operation: String): String | Handles operation timeouts | requestId: operation ID, operation: operation name | Fallback response string | timeout logged | Timeout handled, error wrapped | TimeoutException |
handleNoResults(query: String, context: ConversationContext): String | Handles queries with no results | query: user query, context: conversation context | Response string | query not empty | No results handled gracefully | none |
handleRateLimiting(userId: String, service: String): void | Handles rate limit errors | userId: user ID, service: service name | void | none | Rate limit logged, user notified | RateLimitException |
alertAdmin(error: Exception, severity: Severity): void | Notifies admin of critical errors | error: exception, severity: error level | void | error not null, admin configured | Admin notified via configured channel | NotificationException |
handleDatabaseError(error: SQLException): String | Handles database errors | error: SQL exception | Fallback response | error not null | Error logged, offline mode activated if needed | DatabaseException |
handleLLMError(error: LLMException): String | Handles LLM API errors | error: LLM exception | Fallback response | error not null | Error logged, alternative LLM tried if available | LLMException |
logDetailedError(error: Exception, context: Map<String, Any>): void | Logs detailed error with full context | error: exception, context: contextual info | void | error not null | Error with full stack trace logged | LoggingException |
getErrorMetrics(): ErrorMetrics | Retrieves error rate and metrics | none | ErrorMetrics object | error tracking active | Error metrics computed | MetricsException |
registerErrorStrategy(errorType: ErrorType, strategy: ErrorStrategy): void | Registers custom error handler | errorType: error type, strategy: handler | void | errorType valid | Strategy registered for error type | none |
shouldRetry(error: Exception): Boolean | Determines if operation should be retried | error: exception raised | true if retryable, false otherwise | error type recognized | Retry decision made | none |
The purpose of the class.
The purpose of each data field.
The purpose of each method
Pre-conditions if any.
Post-conditions if any.
Parameters and data types
Return value and output variables
Exceptions thrown* (PLEASE see note below for details).
An example of an auto-generated and then augmented API specification is here (Fiscal Design Document 2_API.docx )
This group developed their API documentation by hand (Design Document Part 2 API-1_MovieMatch.docx )
*At the top level, or where appropriate, all exceptions should be caught and an error message that is meaningful to the user generated. It is not OK to say ("xxxx has encountered a problem and will now close (OK?)". Error messages and recovery procedures should be documented in the User’s Manual.