Ai apiRag mode

Using the Inkeep RAG API with the OpenAI SDK

Here's an example of how to use Structured Output Parsing with Inkeep's rag model to retrieve structured documents from your knowledge base. Please ensure you have an API key (don't have one? Get one here).

Example

index.ts
import OpenAI from 'openai';
import { z } from 'zod';
import dotenv from 'dotenv';
import { zodResponseFormat } from 'openai/helpers/zod';
dotenv.config();
 
if (!process.env.INKEEP_API_KEY) {
  throw new Error('INKEEP_API_KEY is required');
}
 
const client = new OpenAI({
  baseURL: 'https://api.inkeep.com/v1/',
  apiKey: process.env.INKEEP_API_KEY,
  // dangerouslyAllowBrowser: true, use this setting if you are using this in browser
});
 
// Define the RAG document schema
const InkeepSourceContentSchema = z.object({
  type: z.union([z.literal('text'), z.string()]),
  media_type: z.union([z.literal('text/plain'), z.string()]).optional(),
  text: z.string().optional(),
  data: z.string().optional(),
}).passthrough();
 
const InkeepSourceSchema = z.object({
  content: z.array(InkeepSourceContentSchema).optional(),
  type: z.union([z.literal('content'), z.string()]).optional(),
  media_type: z.string().optional(),
  data: z.string().optional(),
}).passthrough();
 
const InkeepRecordTypes = z.enum([
    'documentation',
    'site',
    'discourse_post',
    'github_issue',
    'github_discussion',
    'stackoverflow_question',
    'discord_forum_post',
    'discord_message',
    'custom_question_answer',
]);
 
const InkeepRAGDocumentSchema = z.object({
  type: z.string(),
  source: InkeepSourceSchema,
  title: z.string().optional(),
  context: z.string().optional(),
  record_type: z.union([z.string(), InkeepRecordTypes]).optional(),
  url: z.string().optional(),
}).passthrough();
 
const InkeepRAGResponseSchema = z.object({
  content: z.array(InkeepRAGDocumentSchema),
}).passthrough();
 
 
const completion = await client.beta.chat.completions.parse({
  model: "inkeep-rag",
  messages: [
    { role: "user", content: "How do I get started?" },
  ],
  response_format: zodResponseFormat(InkeepRAGResponseSchema, "rag"),
});
 
const validationResult = completion.choices[0].message.parsed;
 
console.log("Schema validation successful!");
 
// Process the RAG documents
validationResult.content.forEach((doc, index) => {
  console.log(`Document ${index + 1}:`);
  console.log(`Title: ${doc.title || 'N/A'}`);
  console.log(`URL: ${doc.url || 'N/A'}`);
  console.log(`Context: ${doc.context || 'N/A'}`);
  console.log(`Record Type: ${doc.record_type || 'N/A'}`);
  console.log('---');
  });

This example demonstrates how to use the OpenAI Node SDK to retrieve structured RAG documents from Inkeep's knowledge base. The response includes an array of documents with context, source information, and URLs.

On this page