Ai apiQuestion answer mode

Use the Question Answer API with the OpenAI SDK

Copy page

Here's an example of how to use Chat Completions with one of Inkeep's qa models

index.ts
import OpenAI from "openai";
import { zodFunction } from "openai/helpers/zod";
import { z } from "zod";

import dotenv from "dotenv";
dotenv.config();

/* provideLinks tool schema */
const InkeepRecordTypes = z.enum([
  "documentation",
  "site",
  "discourse_post",
  "github_issue",
  "github_discussion",
  "stackoverflow_question",
  "discord_forum_post",
  "discord_message",
  "custom_question_answer",
]);

const LinkType = z.union([
  InkeepRecordTypes,
  z.string(), // catch all
]);

const LinkSchema = z
  .looseObject({
    label: z.string().nullish(), // the value of the footnote, e.g. `1`
    url: z.string(),
    title: z.string().nullish(),
    description: z.string().nullish(),
    type: LinkType.nullish(),
    breadcrumbs: z.array(z.string()).nullish(),
  })

export const LinksSchema = z.array(LinkSchema).nullish();

export const LinksToolSchema = z.object({
  links: LinksSchema,
});

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,
});

async function getResponseFromAI() {
  const result = await client.chat.completions.create({
    model: "inkeep-qa-expert",
    messages: [{ role: "user", content: "How do I get started with Inkeep?" }],
    tools: [
        zodFunction({
            name: "provideLinks",
            description: "Provides links",
            parameters: LinksToolSchema,
        }),
    ],
    tool_choice: "auto",
  });
  return result
}

const response = await getResponseFromAI();
const choice = response?.choices?.[0];
const assistantMessage = choice?.message.content;
const toolCalls = choice?.message?.tool_calls ?? [];

console.log(assistantMessage);

for (const toolCall of toolCalls) {
    console.log(toolCall);
    if (toolCall.type === "function" && toolCall.function.name === "provideLinks") {
        const parsedToolCall = LinksToolSchema.parse(JSON.parse(toolCall.function.arguments));
        console.log(parsedToolCall);
    }
}