-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
community[minor],docs[minor]: Add ChromeAI chat model #5903
Merged
Merged
Changes from 11 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0a874c4
community[minor]: Add ChromeAI chat model
bracesproul 007f5ee
extra
bracesproul 2f2ade8
chore: lint files
bracesproul b83c14c
move to experimental, add demo app and instructions
bracesproul 2d9303b
chore: lint files
bracesproul a9eb87a
cr
bracesproul a3e0c25
cr
bracesproul 611114c
chore: lint files
bracesproul ddcc08b
chore: lint files
bracesproul 80b3ffe
chore: lint files
bracesproul f86e10b
docs
bracesproul 1f2e0e1
fix docs link
bracesproul 133b341
Merge branch 'main' of https://github.com/langchain-ai/langchainjs in…
bracesproul 8e259b6
moved to community
bracesproul 6aa2a9d
nits
bracesproul 33a04d6
allow for custom prompt formatters
bracesproul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
--- | ||
sidebar_label: ChromeAI | ||
--- | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
|
||
# ChatChromeAI | ||
|
||
:::info | ||
This feature is **experimental** and is subject to change. | ||
::: | ||
:::note | ||
The `Built-in AI Early Preview Program` by Google is currently in beta. To apply for access or find more information, please visit [this link](https://developer.chrome.com/docs/ai/built-in). | ||
::: | ||
|
||
ChatChromeAI leverages the webGPU and Gemini Nano to run LLMs directly in the browser, without the need for an internet connection. | ||
This allows for running faster and private models without ever having data leave the consumers device. | ||
|
||
## Getting started | ||
|
||
Once you've been granted access to the program, follow all steps to download the model. | ||
|
||
Once downloaded, you can start using `ChatChromeAI` in the browser as follows: | ||
|
||
```typescript | ||
import { ChatChromeAI } from "langchain/experimental/chat_models/chrome_ai"; | ||
import { HumanMessage } from "@langchain/core/messages"; | ||
|
||
const model = new ChatChromeAI({ | ||
temperature: 0.5, // Optional, defaults to 0.5 | ||
topK: 40, // Optional, defaults to 40 | ||
}); | ||
|
||
const message = new HumanMessage("Write me a short poem please"); | ||
|
||
const response = await model.invoke([message]); | ||
``` | ||
|
||
### Streaming | ||
|
||
`ChatChromeAI` also supports streaming chunks: | ||
|
||
```typescript | ||
import { AIMessageChunk } from "@langchain/core/messages"; | ||
|
||
let fullMessage: AIMessageChunk | undefined = undefined; | ||
for await (const chunk of await model.stream([message])) { | ||
if (!fullMessage) { | ||
fullMessage = chunk; | ||
} else { | ||
fullMessage = fullMessage.concat(chunk); | ||
} | ||
console.log(fullMessage.content); | ||
} | ||
``` | ||
|
||
We also have a simple demo application which you can copy to instantly start running `ChatChromeAI` in your browser. | ||
Navigate to the [README.md](../../../../../langchain/src/experimental/chrome_ai/app/README.md) in the `./app` directory of the integration for more instructions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# ChatChromeAI | ||
|
||
This is a simple application designed to run in the browser that uses the webGPU and Gemini Nano. | ||
Gemini Nano is a LLM which Google Chrome has embedded in the browser. As of 06/26/2024 it is still in beta. To request access or find more information, please visit [this link](https://developer.chrome.com/docs/ai/built-in). | ||
|
||
## Getting Started | ||
|
||
To run this application, you'll first need to build the locally dependencies. From the root of the `langchain-ai/langchainjs` repo, run the following command: | ||
|
||
```bash | ||
yarn build --filter=langchain --filter=@langchain/openai | ||
``` | ||
|
||
Once the dependencies are built, navigate into this directory (`langchain/src/experimental/chrome_ai/app`) and run the following command: | ||
|
||
```bash | ||
yarn install # install the dependencies | ||
|
||
yarn start # start the application | ||
``` | ||
|
||
Then, open your browser and navigate to [`http://127.0.0.1:8080/src/chrome_ai.html`](http://127.0.0.1:8080/src/chrome_ai.html). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey there! 👋 I noticed that this PR introduces changes to the dev and hard dependencies in the package.json file. I've flagged this for your review to ensure everything aligns with the project's requirements. Keep up the great work! |
||
"name": "chrome_ai", | ||
"packageManager": "yarn@3.4.1", | ||
"scripts": { | ||
"start": "rm -rf ./dist && yarn webpack && yarn http-server -c-1 -p 8080" | ||
}, | ||
"devDependencies": { | ||
"http-server": "^14.0.1", | ||
"webpack": "^5.92.1", | ||
"webpack-cli": "^5.1.4" | ||
}, | ||
"dependencies": { | ||
"@langchain/openai": "file:../../../../../libs/langchain-openai", | ||
"langchain": "file:../../../../" | ||
} | ||
} |
111 changes: 111 additions & 0 deletions
111
langchain/src/experimental/chrome_ai/app/src/chrome_ai.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>ChatChromeAI Example</title> | ||
<style> | ||
body { | ||
font-family: Arial, sans-serif; | ||
max-width: 800px; | ||
margin: 0 auto; | ||
padding: 20px; | ||
background-color: #f0f0f0; | ||
} | ||
h1 { | ||
color: #333; | ||
text-align: center; | ||
} | ||
button { | ||
background-color: #4caf50; | ||
border: none; | ||
color: white; | ||
padding: 10px 20px; | ||
text-align: center; | ||
text-decoration: none; | ||
display: inline-block; | ||
font-size: 16px; | ||
margin: 4px 2px; | ||
cursor: pointer; | ||
border-radius: 4px; | ||
} | ||
#destroyButton { | ||
background-color: #f44336; | ||
} | ||
form { | ||
background-color: white; | ||
padding: 20px; | ||
border-radius: 8px; | ||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); | ||
} | ||
input[type="text"] { | ||
width: 100%; | ||
padding: 12px 20px; | ||
margin: 8px 0; | ||
box-sizing: border-box; | ||
border: 2px solid #ccc; | ||
border-radius: 4px; | ||
} | ||
#responseContainer { | ||
background-color: white; | ||
padding: 20px; | ||
border-radius: 8px; | ||
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); | ||
margin-top: 20px; | ||
} | ||
.stats { | ||
display: flex; | ||
justify-content: space-around; | ||
margin-top: 10px; | ||
} | ||
.stat-pill { | ||
padding: 5px 10px; | ||
border-radius: 20px; | ||
font-size: 14px; | ||
color: white; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>LangChain.js🦜🔗 - ChatChromeAI Example</h1> | ||
|
||
<button id="destroyButton">Destroy Model</button> | ||
|
||
<form id="inputForm"> | ||
<label for="inputField">Enter your input:</label><br /> | ||
<input | ||
type="text" | ||
id="inputField" | ||
name="inputField" | ||
autocomplete="off" | ||
/><br /> | ||
<button type="submit">Submit</button> | ||
</form> | ||
|
||
<div id="responseContainer"> | ||
<div id="responseText"></div> | ||
<div id="statsContainer"> | ||
<div class="stats"> | ||
<span | ||
class="stat-pill" | ||
style="background-color: #3498db" | ||
id="firstTokenTime" | ||
>First Token: -- ms</span | ||
> | ||
<span | ||
class="stat-pill" | ||
style="background-color: #2ecc71" | ||
id="totalTime" | ||
>Total Time: -- ms</span | ||
> | ||
<span | ||
class="stat-pill" | ||
style="background-color: #e74c3c" | ||
id="totalTokens" | ||
>Total Tokens: --</span | ||
> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<script src="../dist/bundle.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { ChatChromeAI } from "langchain/experimental/chat_models/chrome_ai"; | ||
import { encodingForModel } from "@langchain/core/utils/tiktoken"; | ||
|
||
const model = new ChatChromeAI(); | ||
const destroyButton = document.getElementById("destroyButton"); | ||
const inputForm = document.getElementById("inputForm"); | ||
const submitButton = inputForm.querySelector("button[type='submit']"); | ||
|
||
// Initialize the model when the page loads | ||
window.addEventListener("load", async () => { | ||
try { | ||
await model.initialize(); | ||
destroyButton.disabled = false; | ||
submitButton.disabled = false; | ||
} catch (error) { | ||
console.error("Failed to initialize model:", error); | ||
alert("Failed to initialize model. Please try refreshing the page."); | ||
} | ||
}); | ||
|
||
destroyButton.addEventListener("click", () => { | ||
model.destroy(); | ||
destroyButton.disabled = true; | ||
submitButton.disabled = true; | ||
}); | ||
|
||
inputForm.addEventListener("submit", async (event) => { | ||
event.preventDefault(); | ||
const input = document.getElementById("inputField").value; | ||
const humanMessage = ["human", input]; | ||
|
||
// Clear previous response | ||
const responseTextElement = document.getElementById("responseText"); | ||
responseTextElement.textContent = ""; | ||
|
||
let fullMsg = ""; | ||
let timeToFirstTokenMs = 0; | ||
let totalTimeMs = 0; | ||
try { | ||
const startTime = performance.now(); | ||
for await (const chunk of await model.stream(humanMessage)) { | ||
if (timeToFirstTokenMs === 0) { | ||
timeToFirstTokenMs = performance.now() - startTime; | ||
} | ||
fullMsg += chunk.content; | ||
// Update the response element with the new content | ||
responseTextElement.textContent = fullMsg; | ||
} | ||
totalTimeMs = performance.now() - startTime; | ||
} catch (error) { | ||
console.error("An error occurred:", error); | ||
responseTextElement.textContent = "An error occurred: " + error.message; | ||
} | ||
|
||
const encoding = await encodingForModel("gpt2"); | ||
const numTokens = encoding.encode(fullMsg).length; | ||
|
||
// Update the stat pills | ||
document.getElementById( | ||
"firstTokenTime" | ||
).textContent = `First Token: ${Math.round(timeToFirstTokenMs)} ms`; | ||
document.getElementById("totalTime").textContent = `Total Time: ${Math.round( | ||
totalTimeMs | ||
)} ms`; | ||
document.getElementById( | ||
"totalTokens" | ||
).textContent = `Total Tokens: ${numTokens}`; | ||
}); |
10 changes: 10 additions & 0 deletions
10
langchain/src/experimental/chrome_ai/app/webpack.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const path = require("path"); | ||
|
||
module.exports = { | ||
entry: "./src/index.js", | ||
output: { | ||
filename: "bundle.js", | ||
path: path.resolve(__dirname, "dist"), | ||
}, | ||
mode: "development", | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there! 👋 I noticed that this PR introduces a new dependency change related to the "chrome_ai" chat model. This comment is to flag the change for maintainers to review, as it impacts the project's dependencies. Great work, and looking forward to the review!