Skip to content

Commit 51adfab

Browse files
committed
Use LangChain to automate autofill
1 parent 261cdfb commit 51adfab

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

autofill/autofillScript.ts

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,59 @@
1-
// This file will be run on the content page, and should include all the logic for inserting text into form items.
2-
const usernameField = document.querySelector('input[name="username"]');
3-
const passwordField = document.querySelector('input[name="password"]');
1+
import { AIMessage, ChatMessage, HumanMessage, SystemMessage } from "langchain/schema";
2+
import { chatModel } from "./llm";
3+
const user = {
4+
name: "samir s beall",
5+
6+
address: "1999 Burdett Ave, Troy, NY 12180",
7+
phone_number: "(123) 456-78910",
8+
};
49

5-
if (usernameField) (usernameField as HTMLInputElement).value = "yourUsername";
6-
if (passwordField) (passwordField as HTMLInputElement).value = "yourPassword";
10+
async function main (){
11+
12+
let template_text = "You are a helpful assistant that uses the given information to answer simple fields for a form. \
13+
When prompted with a message, simply respond with the relevant information concisely. Here is all the information formatted in a json format that you can respond with: ";
14+
15+
template_text += JSON.stringify(user)
16+
17+
console.log(template_text);
18+
19+
let messages = [new SystemMessage({ content: template_text })];
20+
21+
messages.push(new HumanMessage({content: "First Name"}));
22+
messages.push(new AIMessage({content: "Samir"}));
23+
24+
const inputFields = document.querySelectorAll('input');
25+
const questionsAndFields: { question: string, field: HTMLInputElement }[] = [];
26+
27+
inputFields.forEach((inputField: HTMLInputElement) => {
28+
const label = document.querySelector(`label[for="${inputField.id}"]`);
29+
if (label) {
30+
questionsAndFields.push({
31+
question: label.textContent || '',
32+
field: inputField
33+
});
34+
}
35+
});
36+
37+
console.log(questionsAndFields);
38+
39+
for (const qf of questionsAndFields){
40+
41+
console.log("querying chat")
42+
messages.push(new HumanMessage({content: qf.question}))
43+
44+
let chatModelResult = await chatModel.predictMessages(messages);
45+
messages.push(chatModelResult);
46+
47+
console.log("chat result: ")
48+
console.log(chatModelResult);
49+
50+
51+
qf.field.value = chatModelResult.content;
52+
}
53+
54+
console.log(messages);
55+
}
56+
57+
main();
758

859
export {}

autofill/llm.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { ChatOpenAI } from "langchain/chat_models/openai";
2+
3+
export const chatModel = new ChatOpenAI({openAIApiKey: process.env.OPENAI_API_KEY});

0 commit comments

Comments
 (0)