Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

langchain[patch]: Implements support for Personal Access Token Authentication in the ConfluenceLoader #3409

Merged
merged 15 commits into from
Nov 30, 2023
14 changes: 12 additions & 2 deletions examples/src/document_loaders/confluence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ConfluencePagesLoader } from "langchain/document_loaders/web/confluence

yoHasse marked this conversation as resolved.
Show resolved Hide resolved
const username = process.env.CONFLUENCE_USERNAME;
const accessToken = process.env.CONFLUENCE_ACCESS_TOKEN;
const personalAccessToken = process.env.CONFLUENCE_PAT;

if (username && accessToken) {
const loader = new ConfluencePagesLoader({
Expand All @@ -13,8 +14,17 @@ if (username && accessToken) {

const documents = await loader.load();
console.log(documents);
} else {
} else if (personalAccessToken) {
const loader = new ConfluencePagesLoader({
baseUrl: "https://example.atlassian.net/wiki",
spaceKey: "~EXAMPLE362906de5d343d49dcdbae5dEXAMPLE",
personalAccessToken: personalAccessToken,
yoHasse marked this conversation as resolved.
Show resolved Hide resolved
});
const documents = await loader.load();
console.log(documents);
}
else {
console.log(
"You must provide a username and access token to run this example."
"You need either a username and access token, or a personal access token (PAT), to use this example."
);
}
3 changes: 3 additions & 0 deletions langchain/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,6 @@ NEO4J_USERNAME=ADD_YOURS_HERE
NEO4J_PASSWORD=ADD_YOURS_HERE
CLOSEVECTOR_API_KEY=ADD_YOURS_HERE
CLOSEVECTOR_API_SECRET=ADD_YOURS_HERE
CONFLUENCE_USERNAME=ADD_YOURS_HERE
CONFLUENCE_PASSWORD=ADD_YOURS_HERE
CONFLUENCE_PATH=ADD_YOURS_HERE
35 changes: 24 additions & 11 deletions langchain/src/document_loaders/web/confluence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import { BaseDocumentLoader } from "../base.js";
export interface ConfluencePagesLoaderParams {
baseUrl: string;
spaceKey: string;
username: string;
accessToken: string;
username?: string;
accessToken?: string;
personalAccessToken?: string;
limit?: number;
}

Expand Down Expand Up @@ -54,25 +55,40 @@ export class ConfluencePagesLoader extends BaseDocumentLoader {

public readonly spaceKey: string;

public readonly username: string;
public readonly username?: string;

public readonly accessToken: string;
public readonly accessToken?: string;

public readonly limit: number;

public readonly personalAccessToken?: string;

constructor({
baseUrl,
spaceKey,
username,
accessToken,
username = "",
accessToken = "",
limit = 25,
personalAccessToken = "",
yoHasse marked this conversation as resolved.
Show resolved Hide resolved
}: ConfluencePagesLoaderParams) {
super();
this.baseUrl = baseUrl;
this.spaceKey = spaceKey;
this.username = username;
this.accessToken = accessToken;
this.limit = limit;
this.personalAccessToken = personalAccessToken;
}

private authorizationHeader(): string {
yoHasse marked this conversation as resolved.
Show resolved Hide resolved
if (this.personalAccessToken) {
return `Bearer ${this.personalAccessToken}`;
} else {
const authToken = Buffer.from(
`${this.username}:${this.accessToken}`
).toString("base64");
return `Basic ${authToken}`;
}
}

/**
Expand All @@ -99,13 +115,10 @@ export class ConfluencePagesLoader extends BaseDocumentLoader {
url: string
): Promise<ConfluenceAPIResponse> {
try {
const authToken = Buffer.from(
`${this.username}:${this.accessToken}`
).toString("base64");

const authHeader = this.authorizationHeader();
const response = await fetch(url, {
headers: {
Authorization: `Basic ${authToken}`,
Authorization: authHeader,
"Content-Type": "application/json",
Accept: "application/json",
},
Expand Down
Loading