npm package: @jlinc/langchain
Description: A LangChain integration that logs events to the JLINC API.
Author: Ben Curtis
Latest version: 0.1.4

# JLINC Langchain Integration

The JLINC Langchain Integration is the official way to implement the zero-knowledge third-party auditing and authorization provided by the [JLINC Server](https://gitea.jlinc.io/jlinc-labs/jlinc-server) inside any Langchain-based infrastructure.

By embedding JLINC's trusted protocol directly into Langchain's tracing system, organizations can prove compliance, accountability, and data integrity without ever exposing sensitive information. This seamless integration enables developers to track, verify, and audit model interactions with full transparency while preserving confidentiality through cryptographically verifiable zero-knowledge proofs. Whether for regulated industries, enterprise governance, or AI safety applications, the JLINC Langchain Tracer ensures that trust, privacy, and accountability are built in from the ground up.

## Sample auditing application
The below code sample is a demonstration of the JLINC Langchain Integration in action. As data moves through the chain, it is cryptographically signed with a unique key for each element in the chain, and zero-knowledge audit records are delivered to the JLINC Archive Server.

```javascript
const { ChatOpenAI } = require("@langchain/openai");
const { awaitAllCallbacks } = require("@langchain/core/callbacks/promises");
const { Calculator } = require("@langchain/community/tools/calculator");
const { AgentExecutor, createToolCallingAgent } = require("langchain/agents");
const { ChatPromptTemplate } = require("@langchain/core/prompts");
const { JLINCTracer } = require("@jlinc/langchain");

async function main() {
  const config = {
    dataStoreApiUrl: "http://localhost:9090",
    dataStoreApiKey: process.env.JLINC_DATA_STORE_API_KEY,
    archiveApiUrl: "http://localhost:9090",
    archiveApiKey: process.env.JLINC_ARCHIVE_API_KEY,
    agreementId: "00000000-0000-0000-0000-000000000000",
    systemPrefix: "TracerTest",
    debug: true,
  }

  const tracer = new JLINCTracer(config);

  const llm = new ChatOpenAI({
    openAIApiKey: "n/a",
    configuration: {
      baseURL: "http://localhost:1234/v1",
    },
    modelName: "meta-llama-3.1-8b-instruct",
  });

  const calculator = new Calculator();
  const tools = [calculator];

  const prompt = ChatPromptTemplate.fromMessages([
    ["system", "You are a helpful assistant"],
    ["placeholder", "{chat_history}"],
    ["human", "{input}"],
    ["placeholder", "{agent_scratchpad}"],
  ]);

  const agent = createToolCallingAgent({ llm, tools, prompt });

  const agentExecutor = new AgentExecutor({
    agent,
    tools,
  });

  try {
    const r = await agentExecutor.invoke({ input: "Add 1 + 1" }, {callbacks: [tracer]});
    console.log(`\nResult`)
    console.log(`---------------------------------------------`)
    console.log(r)
  } catch (err) {
    console.error("Error calling LLM:", err);
  } finally {
    await awaitAllCallbacks();
  }
}

main()
```

## Sample authorization code
The JLINC integration also supports [AuthZEN](https://openid.net/wg/authzen/)-styled authorization pass through to any provider. For instance, the below modifications to the above code would add in authorization to determine if a "public" or "private" tool or LLM can be utilized:

```javascript
const { ChatOpenAI } = require("@langchain/openai");
const { awaitAllCallbacks } = require("@langchain/core/callbacks/promises");
const { Calculator } = require("@langchain/community/tools/calculator");
const { AgentExecutor, createToolCallingAgent } = require("langchain/agents");
const { ChatPromptTemplate } = require("@langchain/core/prompts");
const { JLINCTracer, JLINCAuthDecision, JLINCAuthBaseChatModel, JLINCAuthTool } = require("../src/index.js");

class CalculatorPrivate extends Calculator {
  static lc_name() {
    return "CalculatorPrivate";
  }
}

class CalculatorPublic extends Calculator {
  static lc_name() {
    return "CalculatorPublic";
  }
}

async function main() {
  const config = {
    dataStoreApiUrl: "http://localhost:9090",
    dataStoreApiKey: process.env.JLINC_DATA_STORE_API_KEY,
    archiveApiUrl: "http://localhost:9090",
    archiveApiKey: process.env.JLINC_ARCHIVE_API_KEY,
    agreementId: "00000000-0000-0000-0000-000000000000",
    systemPrefix: "TestTracerJlinc",
    debug: true,
  }
 
  const jlincAuthDecision = new JLINCAuthDecision(config);
  const auth = {
    subject: {
      type: "user",
      id: "tester",
    },
    action: {
      name: "read",
    },
    resource: {
      type: "data",
      id: "1234",
      properties: {
        ownerID: "tester@test.com",
      }
    }
  }
  await jlincAuthDecision.evaluate(auth);

  const tracer = new JLINCTracer(config);

  const authorizedLlm = new ChatOpenAI({
    openAIApiKey: "n/a",
    configuration: {
      baseURL: "http://localhost:1234/v1",
    },
    modelName: "meta-llama-3.1-8b-instruct",
  });
  const notAuthorizedLlm = new ChatOpenAI({
    openAIApiKey: "n/a",
    configuration: {
      baseURL: "http://localhost:1234/v1",
    },
    modelName: "hermes-3-llama-3.1-8b",
  });
  const llm = new JLINCAuthBaseChatModel({
    config,
    jlincAuthDecision,
    targetAuthorized: authorizedLlm,
    targetNotAuthorized: notAuthorizedLlm, // Optional
  });

  const calculatorPublic = new CalculatorPublic();
  calculatorPublic.name = 'calculator_public';
  const calculatorPrivate = new CalculatorPrivate();
  calculatorPrivate.name = 'calculator_private';
  const jlincAuthTool = new JLINCAuthTool({
    config,
    jlincAuthDecision,
    targetAuthorized: calculatorPublic,
    targetNotAuthorized: calculatorPrivate, // Optional
  });
  const tools = [jlincAuthTool];

  const prompt = ChatPromptTemplate.fromMessages([
    ["system", "You are a helpful assistant"],
    ["placeholder", "{chat_history}"],
    ["human", "{input}"],
    ["placeholder", "{agent_scratchpad}"],
  ]);

  const agent = createToolCallingAgent({ llm, tools, prompt });

  const agentExecutor = new AgentExecutor({
    agent,
    tools,
  });

  try {
    const r = await agentExecutor.invoke({ input: "Add 1 + 1. If a function call is used, tell me the output of the function call." }, { callbacks: [tracer] });

    // The next invocation requires a reauth for any future calls to the agent:
    auth.action.name = "write";
    jlincAuthDecision.evaluate(auth);

    console.log(`\nResult`)
    console.log(`---------------------------------------------`)
    console.log(r)
  } catch (err) {
    console.error("Error calling LLM:", err);
  } finally {
    await awaitAllCallbacks();
  }
}

main()
```


## Additional information

Full JLINC Documentation: https://docs.jlinc.io

Details of the JLINC protocol, schema, and context can be found at: https://protocol.jlinc.org/.