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

Added Outlook integration toolkit #3465

Open
wants to merge 74 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 56 commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
12dd2a9
outlook integration
hahahafafa Nov 14, 2023
3388d62
Update outlookIntegration.ts
SimonLi1020 Nov 15, 2023
2694daf
Merge pull request #1 from hahahafafa/develop
hahahafafa Nov 21, 2023
884bab7
get message tips integrated
hahahafafa Nov 22, 2023
929f9f8
added some tests and added getToken for getting Access token
oscarchen178 Nov 27, 2023
44928c1
move our integration to outlook folder, extract authentication to oth…
oscarchen178 Nov 29, 2023
ba52666
add get credentials from env, remove unused package
oscarchen178 Nov 29, 2023
106868d
create entrypoints for outlook, create read and send tool extending b…
oscarchen178 Nov 29, 2023
3b54b6f
Update index.ts
oscarchen178 Nov 29, 2023
5c259c7
reformat the read email
Qi123123Li Nov 30, 2023
3d123c5
now our outlook tools depend on abstract authFlowBase not concrete au…
oscarchen178 Nov 30, 2023
df52718
added two authentication flow, make outlook tool construct more simpl…
oscarchen178 Nov 30, 2023
5fc9972
change the code to fit standard lint rules
Qi123123Li Nov 30, 2023
43388c7
add test case for send email
Qi123123Li Nov 30, 2023
497e486
fix some small bugs
Qi123123Li Nov 30, 2023
e29c32d
Merge branch 'langchain-ai:main' into main
hahahafafa Nov 30, 2023
11d91fc
add invalid token test
oscarchen178 Nov 30, 2023
d19b446
Merge branch 'dev2'
hahahafafa Nov 30, 2023
7091118
Update .env.example
SimonLi1020 Nov 30, 2023
d48ab61
Merge branch 'main' into main
SimonLi1020 Nov 30, 2023
6234417
Merge branch 'main' into main
SimonLi1020 Nov 30, 2023
d38f9f6
use typedoc
Qi123123Li Dec 1, 2023
1bc7761
Merge branch 'main' into run-typedoc
Qi123123Li Dec 1, 2023
0ff7f6d
Merge pull request #4 from hahahafafa/run-typedoc
Qi123123Li Dec 1, 2023
12d160f
add http and url in package
Qi123123Li Dec 1, 2023
cf52d46
Merge branch 'run-typedoc'
Qi123123Li Dec 1, 2023
7c03901
Merge branch 'main' into main
Qi123123Li Dec 1, 2023
083a8ae
Merge branch 'main' into main
oscarchen178 Dec 4, 2023
d70da41
remove optional deps, remove modules using opt depd from index entryp…
oscarchen178 Dec 4, 2023
918ad47
Merge branch 'main' into main
oscarchen178 Dec 4, 2023
9a6ef37
Merge branch 'main' into main
oscarchen178 Dec 5, 2023
43ee152
add to create-entrypoints
oscarchen178 Dec 5, 2023
d4bf536
change from openurl to open package
oscarchen178 Dec 5, 2023
d21fc40
Merge branch 'main' into main
oscarchen178 Dec 5, 2023
960d082
remove url from dependencies
oscarchen178 Dec 6, 2023
f4861fe
log the url for manually open instead of using open package
oscarchen178 Dec 6, 2023
bdb51ab
Merge branch 'main' into main
oscarchen178 Dec 6, 2023
c518e9f
Update langchain/src/tools/outlook/authFlowREST.ts
Qi123123Li Dec 6, 2023
3212a3c
Update langchain/src/tools/outlook/descriptions.ts
Qi123123Li Dec 6, 2023
767acbc
Update langchain/src/tools/outlook/descriptions.ts
Qi123123Li Dec 6, 2023
216c3cc
Update langchain/src/tools/tests/outlookIntegration.test.ts
Qi123123Li Dec 6, 2023
c94886e
fix the format
Qi123123Li Dec 7, 2023
c3f6c74
add the jsdoc
Qi123123Li Dec 7, 2023
3cec11e
add jsdoc
Qi123123Li Dec 7, 2023
9b1d3f4
change OutlookBase to abstract; remove `state` in rest; apply camel c…
oscarchen178 Dec 10, 2023
d92e771
Merge branch 'main' into main
oscarchen178 Dec 10, 2023
6ebb241
Merge branch 'main' of https://github.com/hwchase17/langchainjs into …
jacoblee93 Dec 12, 2023
da61a07
Merge
jacoblee93 Dec 12, 2023
ed41abe
Skip test
jacoblee93 Dec 12, 2023
ba2ec31
Merge branch 'main' into main
oscarchen178 Dec 20, 2023
d9ca68d
Merge branch 'main' into main
oscarchen178 Dec 21, 2023
04a7221
Merge branch 'main' into main
oscarchen178 Dec 22, 2023
23271fd
Merge branch 'main' of https://github.com/hwchase17/langchainjs into …
jacoblee93 Dec 22, 2023
562df12
Move/rename files around
jacoblee93 Dec 22, 2023
a9ba549
add a docs page for outlook toolkit
oscarchen178 Dec 22, 2023
7fa8d85
Update outlook.mdx
jacoblee93 Dec 23, 2023
c2a9eba
Update outlook.mdx
oscarchen178 Dec 23, 2023
c7eb5c6
Update outlook.mdx
oscarchen178 Dec 23, 2023
6718669
add getRefreshToken for AuthFlowREST
oscarchen178 Dec 23, 2023
4cbdeb9
Merge branch 'main' into main
Qi123123Li Dec 26, 2023
b300654
Merge branch 'main' into main
oscarchen178 Dec 28, 2023
1fb53e0
move getAuth from outlook tool to authFLow
oscarchen178 Dec 29, 2023
4719a52
format
oscarchen178 Dec 29, 2023
cdde1d4
update docs
oscarchen178 Dec 29, 2023
ab49c28
Merge branch 'main' into main
oscarchen178 Dec 29, 2023
175324c
Merge branch 'main' into main
oscarchen178 Jan 4, 2024
8751341
Merge branch 'main' into main
oscarchen178 Jan 6, 2024
42189f8
format outlook.mdx
oscarchen178 Jan 6, 2024
f94225a
Merge branch 'main' into main
oscarchen178 Jan 6, 2024
d1e961e
putting example in the examples/
oscarchen178 Jan 18, 2024
569b34e
Update outlook.mdx
oscarchen178 Jan 19, 2024
981242e
Update outlook.ts
oscarchen178 Jan 19, 2024
1aa5ee3
Merge remote-tracking branch 'upstream/main'
oscarchen178 Jan 20, 2024
d03bd77
Merge branch 'main' into main
oscarchen178 Jan 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions docs/core_docs/docs/integrations/toolkits/outlook.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# Outlook Toolkit

## Overview

Using this toolkit, you can integrate Microsoft Graph API functionalities into your applications to manage email interactions.

## Learn Microsoft Graph

Microsoft Graph is a RESTful web API that enables you to access Microsoft Cloud service resources. With Microsoft Graph, you can integrate various Microsoft services and data, including users' emails, calendar, contacts, and more, into your applications.

After you register your app and get authentication tokens for a user or service, you can make requests to the Microsoft Graph API.

How to get authentication tokens for a user:
https://learn.microsoft.com/en-us/graph/auth-v2-user

## App register and set environment variables
Azure App Registration for Microsoft Outlook API Integration

1. Register an Application:
- Go to Azure Portal.
- Navigate to Azure Active Directory -> App registrations.
- Register a new application, note down the Client ID.

2. Generate Client Secret:
- In the application settings, go to Certificates & Secrets.
- Create a new client secret and note down the Client Secret.

3. Configure Redirect URI:
- In the application settings, go to Authentication.
- Add a Redirect URI and note down the Redirect URI.

4. Permissions and Admin Consent:
- In API permissions, grant necessary permissions for Microsoft Graph API or Outlook API.
- Grant admin consent for the added permissions.

6. Environment Variables:
- Set the following environment variables:

```env
OUTLOOK_CLIENT_ID=your_client_id
OUTLOOK_CLIENT_SECRET=your_client_secret
OUTLOOK_REDIRECT_URI=your_redirect_uri
OUTLOOK_ACCESS_TOKEN=your_access_token
OUTLOOK_REFRESH_TOKEN=your_refresh_token

```

**Keep these values secure and avoid exposing them in public repositories.**

## Setup

You only need an `accessToken` to use the Outlook Toolkit, but they expire after an hour.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you need to click a link every hour to use this integration?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically, with the integration, you don't need to manually click a link every hour. Instead, once you obtain an accessToken, there is usually a corresponding refreshToken provided. The AuthFlowREST class can utilize this refreshToken along with clientId, clientSecret, and redirectUri to refresh the expired accessToken automatically. This eliminates the need for manual intervention and keeps the process secure without the necessity for a continuous login or keeping a server open.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I don't need to click the link every hour. If you have refreshToken and other credentials, the authFlowRefresh class will get a new valid token for you without the login step. Or if using authFlowToken, the same instance of the class only need to login once and then it will make use of the refreshToken.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't fully understand from the docs - is there a way to just get tht refresh token during setup and just use that?

Copy link

@oscarchen178 oscarchen178 Dec 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the confusion, the athentication flow to get the refresh token is to send request with clientId, clientSecret and redirectUri, and then open the link from response and login, after that the server will receive a code, then you send the code with other credentials to request refreshToken and accessToken. The authFlowREST implements this flow, so for this class only need clientId, clientSecret and redirectUri in .env. But this class does not store the tokens in env. You can use other ways like postman to get the refreshToken. The default expiry time of refresh token is much longer.
This video may help, https://www.youtube.com/watch?v=NAtiNpwnivI&t=309s


If you only have a valid `accessToken`, the following two ways can create an Outlook tool instance.

```typescript
const authFlow = new AuthFlowToken(accessToken);
// if you have accessToken stored in .env
const authFlow = new AuthFlowToken();
const outlookReadMail = new OutlookReadMailTool(authFlow=authFlow);
```
```typescript
// you must have accessToken stored in .env
const outlookReadMail = new OutlookReadMailTool(choice="token");
```

If you do not have a valid accessToken, the `AuthFlowREST` class obtains the accessToken for you. A temporary server will be opened on your `redirectUri` and you need to manually open the printed link to login on behalf of the user. For every `AuthFlowREST` instance, you only need to login once.

You'll need to provide a `clientId`, `clientSecret`, and `redirectUri`.
You can either pass them to the constructor or put them in the environment.
```typescript
const authFlow = new AuthFlowREST();
const outlookReadMail = new OutlookReadMailTool(authFlow=authFlow);
```

```typescript
const outlookReadMail = new OutlookReadMailTool(choice="rest");
```

Usually when you obtain a accessToken you also get a refreshToken used to refresh expired accessToken. The `AuthFlowREST` class needs the following credentials: `clientId`, `clientSecret`, `redirectUri` and `refreshToken`. In this way you won't need to login, no server will be open.

```typescript
const authFlow = new AuthFlowRefresh();
const outlookReadMail = new OutlookReadMailTool(authFlow=authFlow);
```
```typescript
const outlookReadMail = new OutlookReadMailTool(choice="refresh");
```

## Usage

This example shows how to read emails by using agent.

```typescript
import { ChatOpenAI } from 'langchain/chat_models/openai';
import { initializeAgentExecutorWithOptions } from 'langchain/agents';
import { OutlookReadMailTool, AuthFlowREST, AuthFlowToken} from 'langchain_community/tools/outlook';

async function createAndRunAgent() {
const gpt4 = new ChatOpenAI({ modelName: 'gpt-4', openAIApiKey: getEnvironmentVariable("OPENAI_API_KEY")});

const outlookReadMail = new OutlookReadMailTool("token");

const executor = await initializeAgentExecutorWithOptions([outlookReadMail], gpt4, {
agentType: "openai-functions",
verbose: true,
});

const input = 'show emails sent to YOUR_EMAIL';
const result = await executor.invoke({ input });

console.log(result);
}

createAndRunAgent();
```

This example shows how to send emails by using agent.

```typescript
import { ChatOpenAI } from 'langchain/chat_models/openai';
import { initializeAgentExecutorWithOptions } from 'langchain/agents';
import { OutlookSendMailTool, AuthFlowREST } from 'langchain_community/tools/outlook';


async function createAndRunAgent() {
const gpt4 = new ChatOpenAI({ modelName: 'gpt-4', openAIApiKey: getEnvironmentVariable("OPENAI_API_KEY")});

const sendMailTool = new OutlookSendMailTool("token");

const executor = await initializeAgentExecutorWithOptions([sendMailTool], gpt4, {
agentType: "openai-functions",
verbose: true,
});

const input = 'send an email to YOUR_EMAIL and say Hello';
const result = await executor.invoke({ input });

console.log(result);
}

createAndRunAgent();
```
5 changes: 5 additions & 0 deletions langchain/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ NEO4J_USERNAME=ADD_YOURS_HERE
NEO4J_PASSWORD=ADD_YOURS_HERE
CLOSEVECTOR_API_KEY=ADD_YOURS_HERE
CLOSEVECTOR_API_SECRET=ADD_YOURS_HERE
OUTLOOK_CLIENT_ID=ADD_YOURS_HERE
OUTLOOK_CLIENT_SECRET=ADD_YOURS_HERE
OUTLOOK_REDIRECT_URI=ADD_YOURS_HERE
OUTLOOK_REFRESH_TOKEN=ADD_YOURS_HERE
OUTLOOK_ACCESS_TOKEN=ADD_YOURS_HERE
GPLACES_API_KEY=ADD_YOURS_HERE
CONFLUENCE_USERNAME=ADD_YOURS_HERE
CONFLUENCE_PASSWORD=ADD_YOURS_HERE
Expand Down
3 changes: 3 additions & 0 deletions libs/langchain-community/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ tools/google_places.d.ts
tools/ifttt.cjs
tools/ifttt.js
tools/ifttt.d.ts
tools/outlook.cjs
tools/outlook.js
tools/outlook.d.ts
tools/searchapi.cjs
tools/searchapi.js
tools/searchapi.d.ts
Expand Down
8 changes: 8 additions & 0 deletions libs/langchain-community/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,11 @@
"import": "./tools/ifttt.js",
"require": "./tools/ifttt.cjs"
},
"./tools/outlook": {
"types": "./tools/outlook.d.ts",
"import": "./tools/outlook.js",
"require": "./tools/outlook.cjs"
},
"./tools/searchapi": {
"types": "./tools/searchapi.d.ts",
"import": "./tools/searchapi.js",
Expand Down Expand Up @@ -1292,6 +1297,9 @@
"tools/ifttt.cjs",
"tools/ifttt.js",
"tools/ifttt.d.ts",
"tools/outlook.cjs",
"tools/outlook.js",
"tools/outlook.d.ts",
"tools/searchapi.cjs",
"tools/searchapi.js",
"tools/searchapi.d.ts",
Expand Down
2 changes: 2 additions & 0 deletions libs/langchain-community/scripts/create-entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const entrypoints = {
"tools/google_custom_search": "tools/google_custom_search",
"tools/google_places": "tools/google_places",
"tools/ifttt": "tools/ifttt",
"tools/outlook": "tools/outlook/index",
"tools/searchapi": "tools/searchapi",
"tools/searxng_search": "tools/searxng_search",
"tools/serpapi": "tools/serpapi",
Expand Down Expand Up @@ -191,6 +192,7 @@ const requiresOptionalDependency = [
"tools/aws_lambda",
"tools/discord",
"tools/gmail",
"tools/outlook",
"agents/toolkits/aws_sfn",
"callbacks/handlers/llmonitor",
"embeddings/bedrock",
Expand Down
1 change: 1 addition & 0 deletions libs/langchain-community/src/load/import_constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const optionalImportEntrypoints = [
"langchain_community/tools/aws_sfn",
"langchain_community/tools/discord",
"langchain_community/tools/gmail",
"langchain_community/tools/outlook",
"langchain_community/agents/toolkits/aws_sfn",
"langchain_community/embeddings/bedrock",
"langchain_community/embeddings/cloudflare_workersai",
Expand Down
3 changes: 3 additions & 0 deletions libs/langchain-community/src/load/import_type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export interface OptionalImportMap {
"@langchain/community/tools/gmail"?:
| typeof import("../tools/gmail/index.js")
| Promise<typeof import("../tools/gmail/index.js")>;
"@langchain/community/tools/outlook"?:
| typeof import("../tools/outlook/index.js")
| Promise<typeof import("../tools/outlook/index.js")>;
"@langchain/community/agents/toolkits/aws_sfn"?:
| typeof import("../agents/toolkits/aws_sfn.js")
| Promise<typeof import("../agents/toolkits/aws_sfn.js")>;
Expand Down
52 changes: 52 additions & 0 deletions libs/langchain-community/src/tools/outlook/auth/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Base class for handling authentication flows.
*
* @class AuthFlowBase
*/
export abstract class AuthFlowBase {
/**
* The client ID used for authentication.
*
* @protected
* @type {string}
* @memberof AuthFlowBase
*/
protected clientId: string;

/**
* The access token obtained through authentication.
*
* @protected
* @type {string}
* @memberof AuthFlowBase
*/
protected accessToken = "";

/**
* Creates an instance of AuthFlowBase.
*
* @param {string} clientId - The client ID for authentication.
* @memberof AuthFlowBase
*/
constructor(clientId: string) {
this.clientId = clientId;
}

/**
* Abstract method to get the access token.
*
* @abstract
* @returns {Promise<string>} A promise that resolves to the access token.
* @memberof AuthFlowBase
*/
public abstract getAccessToken(): Promise<string>;

/**
* Abstract method to refresh the access token.
*
* @abstract
* @returns {Promise<string>} A promise that resolves to the refreshed access token.
* @memberof AuthFlowBase
*/
public abstract refreshAccessToken(): Promise<string>;
}
Loading
Loading