chat
Send chat messages to AI models with optional RAG capabilities for retrieving information from documents. Configure parameters like temperature and system prompts to customize responses.
Instructions
Chat with Prem AI - supports chat completions with optional RAG capabilities.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | The chat message to send | |
| system_prompt | No | Optional system prompt to guide the model's behavior | |
| model | No | Optional model to use for completion | |
| temperature | No | Optional temperature for response generation | |
| max_tokens | No | Optional maximum tokens to generate | |
| repository_ids | No | Optional array of repository IDs for RAG | |
| similarity_threshold | No | Optional similarity threshold for RAG | |
| limit | No | Optional limit of context chunks for RAG |
Implementation Reference
- src/index.ts:83-123 (handler)The handler function for the 'chat' tool. It constructs a PremChatRequest, calls the Prem API chat.completions.create, and returns the response as text content or error.const requestId = `chat-${Date.now()}-${Math.random().toString(36).substring(2, 7)}`; this.activeRequests.add(requestId); try { const chatRequest = { project_id: PROJECT_ID as string, messages: [{ role: "user", content: query }], ...(model && { model }), ...(system_prompt && { system_prompt }), ...(temperature && { temperature }), ...(max_tokens && { max_tokens }), ...(repository_ids && { repositories: { ids: repository_ids, similarity_threshold: similarity_threshold || 0.65, limit: limit || 3 } }) }; const response = await this.client.chat.completions.create(chatRequest as any); const responseData = 'choices' in response ? response : { choices: [] }; return { content: [{ type: "text" as const, text: JSON.stringify(responseData, null, 2) }] }; } catch (error) { return { content: [{ type: "text" as const, text: `Chat error: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } finally { this.activeRequests.delete(requestId); } }
- src/index.ts:69-124 (registration)Registration of the 'chat' tool with the MCP server using this.server.toolcall.this.server.tool( "chat", "Chat with Prem AI - supports chat completions with optional RAG capabilities.", { query: z.string().describe("The chat message to send"), system_prompt: z.string().optional().describe("Optional system prompt to guide the model's behavior"), model: z.string().optional().describe("Optional model to use for completion"), temperature: z.number().optional().describe("Optional temperature for response generation"), max_tokens: z.number().optional().describe("Optional maximum tokens to generate"), repository_ids: z.array(z.number()).optional().describe("Optional array of repository IDs for RAG"), similarity_threshold: z.number().optional().describe("Optional similarity threshold for RAG"), limit: z.number().optional().describe("Optional limit of context chunks for RAG") }, async ({ query, system_prompt, model, temperature, max_tokens, repository_ids, similarity_threshold, limit }) => { const requestId = `chat-${Date.now()}-${Math.random().toString(36).substring(2, 7)}`; this.activeRequests.add(requestId); try { const chatRequest = { project_id: PROJECT_ID as string, messages: [{ role: "user", content: query }], ...(model && { model }), ...(system_prompt && { system_prompt }), ...(temperature && { temperature }), ...(max_tokens && { max_tokens }), ...(repository_ids && { repositories: { ids: repository_ids, similarity_threshold: similarity_threshold || 0.65, limit: limit || 3 } }) }; const response = await this.client.chat.completions.create(chatRequest as any); const responseData = 'choices' in response ? response : { choices: [] }; return { content: [{ type: "text" as const, text: JSON.stringify(responseData, null, 2) }] }; } catch (error) { return { content: [{ type: "text" as const, text: `Chat error: ${error instanceof Error ? error.message : String(error)}` }], isError: true }; } finally { this.activeRequests.delete(requestId); } } );
- src/index.ts:72-82 (schema)Zod schema for validating inputs to the 'chat' tool.{ query: z.string().describe("The chat message to send"), system_prompt: z.string().optional().describe("Optional system prompt to guide the model's behavior"), model: z.string().optional().describe("Optional model to use for completion"), temperature: z.number().optional().describe("Optional temperature for response generation"), max_tokens: z.number().optional().describe("Optional maximum tokens to generate"), repository_ids: z.array(z.number()).optional().describe("Optional array of repository IDs for RAG"), similarity_threshold: z.number().optional().describe("Optional similarity threshold for RAG"), limit: z.number().optional().describe("Optional limit of context chunks for RAG") }, async ({ query, system_prompt, model, temperature, max_tokens, repository_ids, similarity_threshold, limit }) => {
- src/types.ts:49-58 (schema)TypeScript interface defining the arguments for the chat tool.export interface ChatArgs { query: string; system_prompt?: string; model?: string; temperature?: number; max_tokens?: number; repository_ids?: number[]; similarity_threshold?: number; limit?: number; }
- src/types.ts:15-25 (helper)Type definition for the PremChatRequest used in the handler.export interface PremChatRequest { project_id: string; messages: PremMessage[]; model?: string; system_prompt?: string; session_id?: string; temperature?: number; max_tokens?: number; stream?: boolean; repositories?: PremRepository; }