1.0.3 • Published 4 months ago

interlify v1.0.3

Weekly downloads
-
License
Apache License 2....
Repository
-
Last release
4 months ago

Interlify

Connect your APIs to LLM in minutes

Installation

Install the Interlify client in your project:

npm install interlify

Use 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:

  1. 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
});
  1. Prepare the tools
const tools = await interlify.tools();
  1. 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',
    });
  1. Call the tool
const functionResponse = await interlify.callTool(toolCall.function);

That's it!

GL & HF!

1.0.3

4 months ago

1.0.2

4 months ago

1.0.1

4 months ago

1.0.0

4 months ago