-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
194 lines (170 loc) · 4.79 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import server from "bunrest";
import cors from "buncors";
import { NotionAPILoader } from "langchain/document_loaders/web/notionapi";
import { SupabaseVectorStore } from "langchain/vectorstores/supabase";
import { createClient } from "@supabase/supabase-js";
import { OpenAIEmbeddings, ChatOpenAI } from "@langchain/openai";
import axios from "axios";
import { combineDocuments, linkedInProfileToText } from "./utils";
import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
import { PromptTemplate } from "@langchain/core/prompts";
import { StringOutputParser } from "@langchain/core/output_parsers";
import {
RunnablePassthrough,
RunnableSequence,
} from "@langchain/core/runnables";
/**
*
* Server setup
*
*/
const app = server();
app.use(cors());
app.listen(3000, () => {
console.log("Server started on port 3000");
});
/**
*
* contstants
*
*/
// notion
const NOTION_API_KEY = "your-key-here";
const PAGE_ID = "your-key-here";
// supabase
const SUPABASE_PROJECT_URL = "your-url-here";
const SUPABASE_API_KEY = "your-key-here";
const SUPABASE_TABLE_NAME = "documents";
// openai
const OPENAI_KEY = "your-key-here";
// langchain
const CHUNK_SIZE = 500;
const SEPARATORS = ["\n\n", "\n", " ", ""];
const CHUNK_OVERLAP = 50;
// others
const LINKEDIN_USER = "gcalabria";
/**
*
*
* global instances
*
*/
// langchain
const embeddings = new OpenAIEmbeddings({ openAIApiKey: OPENAI_KEY });
const splitter = new RecursiveCharacterTextSplitter({
chunkSize: CHUNK_SIZE,
separators: SEPARATORS,
chunkOverlap: CHUNK_OVERLAP,
});
const llm = new ChatOpenAI({ openAIApiKey: OPENAI_KEY });
// prompt template
const standaloneQuestionTemplate =
"Given a question, convert it into a standalone question. Question: {question} Standalone question:";
// answer template
const answerTemplate = `
You are <Your-name-here>, a helpful and enthusiastic support
bot who can answer questions about your career. You are a also a
computer scientist specialized in AI Engineering. Try to find the
answer in the context. If you really do not know the answer, then say
"I am sorry, I do not know the answer for that." Do not try to make up
an answer. Speak as if you were chatting to a friend.
context: {context}
question: {question}
answer:
`;
const answerPrompt = PromptTemplate.fromTemplate(answerTemplate);
// prompt
const standaloneQuestionPrompt = PromptTemplate.fromTemplate(
standaloneQuestionTemplate
);
// supabase
const supabaseClient = createClient(SUPABASE_PROJECT_URL, SUPABASE_API_KEY);
const vectorStore = new SupabaseVectorStore(embeddings, {
client: supabaseClient,
tableName: SUPABASE_TABLE_NAME,
});
const retriever = vectorStore.asRetriever();
// chains
const standaloneQuestionChain = standaloneQuestionPrompt
.pipe(llm)
.pipe(new StringOutputParser());
const retrieverChain = RunnableSequence.from([
(prevResult) => prevResult.standalone_question,
retriever,
combineDocuments,
]);
const answerChain = answerPrompt.pipe(llm).pipe(new StringOutputParser());
// link up chain with .pipe methods
const chain = RunnableSequence.from([
{
standalone_question: standaloneQuestionChain,
original_input: new RunnablePassthrough(),
},
{
context: retrieverChain,
question: ({ original_input }) => original_input.question,
},
answerChain,
]);
/**
*
*
* routes
*
*/
app.get("/api/notion", async (req, res) => {
const pageLoader = new NotionAPILoader({
clientOptions: {
auth: NOTION_API_KEY,
},
id: PAGE_ID,
type: "page",
});
const pageDocs = await pageLoader.loadAndSplit();
const vectorStore = await SupabaseVectorStore.fromDocuments(
pageDocs,
embeddings,
{
client: supabaseClient,
tableName: SUPABASE_TABLE_NAME,
}
);
console.log(vectorStore);
res.status(200).json(pageDocs);
});
app.get("/api/linkedin", async (req, res) => {
// Crawl linkedin profile
const response = await axios.get("https://linkedin-api8.p.rapidapi.com/", {
headers: {
"X-RapidAPI-Key": "your-key-here",
"X-RapidAPI-Host": "linkedin-api8.p.rapidapi.com",
},
params: {
username: LINKEDIN_USER,
},
});
// Add documents (linkedin profile) to supabase vectorstore
const profile = response.data;
const text = linkedInProfileToText(profile);
const output = await splitter.createDocuments([text]);
const vectorStore = await SupabaseVectorStore.fromDocuments(
output,
embeddings,
{
client: supabaseClient,
tableName: SUPABASE_TABLE_NAME,
}
);
res.status(200).json(vectorStore);
});
app.post("/api/answer", async (req, res) => {
if (typeof req.body === "object") {
const question = req.body.question;
const answer = await chain.invoke({
question: question,
});
res.status(201).json({ answer });
} else {
res.status(400).json({ error: "Invalid question" });
}
});