interlify
v1.0.5
Published
Connect your APIs to LLM in minutes
Maintainers
Readme
Interlify
Connect your APIs to LLM in minutes
Installation
Install the Interlify client in your project:
npm install interlifyUse the Interlify client with a few lines of code:
import { OpenAI } from "openai";
import { Interlify } from "Interlify";
const client = new OpenAI();
const MODEL = YOUR_LLM_MODEL;
// You Steps of getting ACCESS_TOKEN_FOR_YOUR_TOOLS
// ...
// Initilize client
const interlify = new Interlify({
apiKey: YOUR_INTERLIFY_API_KEY,
projectId: YOUR_INTERLIFY_PROJECT_ID,
authHeaders: [
{ Authorization: ACCESS_TOKEN_FOR_YOUR_TOOLS }
]
});
const chat = async () => {
// prepare tools
const tools = await interlify.tools();
// porivde tools to llm
const response = await client.chat.completions.create({
model: MODEL,
messages: message_list as any,
tools: tools,
tool_choice: 'auto',
});
const responseMessage = response.choices[0].message;
const toolCalls = responseMessage.tool_calls;
if (toolCalls) {
message_list.push(responseMessage);
for (const toolCall of toolCalls) {
// call the tool using interlify
const functionResponse = await interlify.callTool(toolCall.function);
message_list.push({
// @ts-ignore
tool_call_id: toolCall.id,
role: 'tool',
name: toolCall.function.name,
content: JSON.stringify(functionResponse.data),
});
}
const secondResponse = await client.chat.completions.create({
model: MODEL,
messages: message_list as any,
});
return secondResponse.choices[0].message.content;
}
return responseMessage.content;
}
const message = await chat();
console.log(message)
Explanation
In the above code, Interlify did the following things:
- Instantiate the client
const interlify = new Interlify({
apiKey: YOUR_INTERLIFY_API_KEY,
projectId: YOUR_INTERLIFY_PROJECT_ID,
authHeaders: [
{ Authorization: ACCESS_TOKEN_FOR_YOUR_TOOLS }
]
});The ACCESS_TOKEN_FOR_YOUR_TOOLS is the authorization header whole string value that will be used by your service to authorize LLM to access protected resources.
For Bearer token format: Authorization: Bearer <YOUR_TOKEN>
The ACCESS_TOKEN_FOR_YOUR_TOOLS should be "Bearer <YOUR_TOKEN>".
For Basic token format: Authorization: Basic <base64(username:password)>
The ACCESS_TOKEN_FOR_YOUR_TOOLS should be "Basic <YOUR_ENCODED_CREDENTIALS>".
If your API does not require token, you can remove the authHeaders, so the code would be:
const interlify = new Interlify({
apiKey: YOUR_INTERLIFY_API_KEY,
projectId: YOUR_INTERLIFY_PROJECT_ID
});- Prepare the tools
const tools = await interlify.tools();- Provide tools to LLM
const response = await client.chat.completions.create({
model: MODEL,
messages: message_list as any,
//@ts-ignore
tools: tools,
tool_choice: 'auto',
});- Call the tool
const functionResponse = await interlify.callTool(toolCall.function);Work with Vercel AI SDK
Vercel AI SDK is the TypeScript toolkit designed to help developers build AI-powered applications and agents with React, Next.js, Vue, Svelte, Node.js, and more.
Interlify now support Vercel AI for tools calling. Now you can easily integrate interlify in your Next.js project with Vercel AI.
To start, install Vercel AI, Vercel AI Groq client, dotenv, and interlify
npm install @ai-sdk/groq ai dotenv interlifyThen create a .env file set these variables:
API_KEY = <YOUR_GROQ_API_KEY>
INTERLIFY_API_KEY = <INTERLIFY_API_KEY>
PROJECT_ID = <INTERLIFY_PROJECT_ID>
ACCESS_TOKEN = <YOUR_API_ACCESS_TOKEN>The code is as simple as below:
import { Interlify } from "Interlify";
import { config } from 'dotenv';
import { createGroq } from '@ai-sdk/groq';
import { generateText, ToolSet } from 'ai';
config();
const client = createGroq({ apiKey: process.env.API_KEY });
const MODEL = client('llama-3.3-70b-versatile');
// Init the client
const interlify = new Interlify({
apiKey: process.env.INTERLIFY_API_KEY || "",
projectId: process.env.PROJECT_ID || "",
authHeaders: [
{ Authorization: `Bearer ${process.env.ACCESS_TOKEN}` }
]
});
// Get tools for Vercel AI
const tools = await interlify.aiTools() as ToolSet;
const history = [
{
role: "system",
content: "You are online shoes shop customer service. You help customer on equries."
},{
role: "user",
content: "what shoes do you have?"
}]
const { text, toolResults } = await generateText({
model: MODEL,
// Pass tools to LLM
tools: tools,
messages: history
});
console.log("text: ", text);
console.log("toolResults");
console.log(JSON.stringify(toolResults));
That's it!
GL & HF!
