Ai apiQuestion answer mode

Custom Tool Calls for the Question Answer API

Inkeep's inkeep-qa API allows you to pass in custom tool calls to the AI assistant.

You can use this to:

  • determine an answer's confidence and escalate to a human if needed
  • route, categorize, or label the question
  • trigger actions based on the content of a question or answer

To effectively use tool calls, you should:

  1. Define a clear semantic schema for the tool call arguments
  2. Write detailed descriptions of each tool call and its intended use cases
  3. Experiment with multiple test cases

For more details on implementing function calling, see this guide on tool calling.

Note
Note
The examples below leverage the tool argument definitions found in the Common Tools guide.

Single Tool Call

You can provide a single tool definition to the AI assistant by passing a tool object to the tools array parameter in the chat.completions.create function.

index.ts
import OpenAI from 'openai';
import dotenv from 'dotenv';
import { provideAnswerConfidenceSchema } from './common-tool-schemas.ts';
import { zodToJsonSchema } from 'zod-to-json-schema';
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
});
 
const singleTool = [
  {
      "function": {
          "name": "provideAnswerConfidence",
          "description": "Determine how confident the AI assistant was and whether or not to escalate to humans.",
          "parameters": zodToJsonSchema(provideAnswerConfidenceSchema),
      },
      "type": "function",
  },
];
 
const completion = await client.chat.completions.create({
    model: "inkeep-qa-expert",
    messages: [{ role: "user", content: "How do I get started?" }],
    tools: singleTool,
});
 
console.log(completion.choices[0].message.tool_calls);

Multiple Tool Calls

We can also combine multiple tool calls. The LLM will choose all relevant tools. If you'd like a tool to always be invoked, include that in the description of the tool.

index.ts
import OpenAI from 'openai';
import dotenv from 'dotenv';
import { provideAnswerConfidenceSchema, detectedSalesSignal } from './common-tool-schemas.ts';
import { zodToJsonSchema } from 'zod-to-json-schema';
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
});
 
const completion = await client.chat.completions.create({
    model: "inkeep-qa-expert",
    messages: [{ role: "user", content: "How much does your enterprise plan cost for a team of 50?" }],
    tools:  [
        {
            "type": "function",
            "function": {
                "name": "provideAnswerConfidence",
                "description": "Grade how confident the AI assistant was in its answer.",
                "parameters": zodToJsonSchema(provideAnswerConfidenceSchema),
            },
        },
        {
            "type": "function",
            "function": {
                "name": "detectSalesSignal",
                "description": "Identify when users express interest in potentially purchasing a product.",
                "parameters": zodToJsonSchema(detectedSalesSignal),
            },
        },
    ]
});
 
console.log(completion.choices[0].message.tool_calls);

On this page