-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathollamachain.js
84 lines (74 loc) · 2.05 KB
/
ollamachain.js
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
import { ChatOllama } from "@langchain/community/chat_models/ollama";
import { OllamaEmbeddings } from "@langchain/community/embeddings/ollama";
import { PGVectorStore } from "@langchain/community/vectorstores/pgvector";
import { formatDocumentsAsString } from "langchain/util/document";
import { PromptTemplate } from "@langchain/core/prompts";
import {
RunnableSequence,
RunnablePassthrough,
} from "@langchain/core/runnables";
import { StringOutputParser } from "@langchain/core/output_parsers";
const embeddings = new OllamaEmbeddings({
model: "llama2", // default value
baseUrl: "http://localhost:11434", // default value
requestOptions: {
useMMap: true,
numThread: 6,
numGpu: 1,
},
});
const config = {
postgresConnectionOptions: {
type: "postgres",
host: "localhost",
port: 5432,
user: "postgres",
password: "password",
database: "jaxnodevector",
},
tableName: "vectordocs",
columns: {
idColumnName: "id",
vectorColumnName: "vector",
contentColumnName: "content",
metadataColumnName: "metadata",
},
};
const pgvectorStore = await PGVectorStore.initialize(
embeddings,
config
);
const model = new ChatOllama({
baseUrl: "http://localhost:11434", // Default value
model: "llama2", // Default value
debug: true,
callbacks: [
{
handleLLMNewToken(token) {
process.stdout.write(token, 'utf-8');
},
handleLLMError(e) {
console.error(e);
},
handleLLMEnd() {
console.log("");
},
},
],
});
const retriever = pgvectorStore.asRetriever();
const prompt = PromptTemplate.fromTemplate(`Answer the question based only on the following context:
{context}
Question: {question}`);
const chain = RunnableSequence.from([
{
context: retriever.pipe(formatDocumentsAsString),
question: new RunnablePassthrough(),
},
prompt,
model,
new StringOutputParser(),
]);
// const logStream =
await chain.invoke("How can I prevent XPath injection in my Java apps?");
pgvectorStore.end();