langchain 0.2.13 → 0.2.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agents/tool_calling/index.cjs +14 -3
- package/dist/agents/tool_calling/index.d.ts +2 -3
- package/dist/agents/tool_calling/index.js +14 -3
- package/dist/chains/retrieval_qa.cjs +1 -1
- package/dist/chains/retrieval_qa.d.ts +1 -1
- package/dist/chains/retrieval_qa.js +1 -1
- package/dist/vectorstores/memory.cjs +31 -14
- package/dist/vectorstores/memory.d.ts +10 -2
- package/dist/vectorstores/memory.js +32 -15
- package/package.json +3 -3
- package/dist/experimental/chrome_ai/app/dist/bundle.cjs +0 -1250
- package/dist/experimental/chrome_ai/app/dist/bundle.d.ts +0 -1
- package/dist/experimental/chrome_ai/app/dist/bundle.js +0 -1249
|
@@ -5,6 +5,11 @@ const runnables_1 = require("@langchain/core/runnables");
|
|
|
5
5
|
const agent_js_1 = require("../agent.cjs");
|
|
6
6
|
const output_parser_js_1 = require("./output_parser.cjs");
|
|
7
7
|
const tool_calling_js_1 = require("../format_scratchpad/tool_calling.cjs");
|
|
8
|
+
function _isBaseChatModel(x) {
|
|
9
|
+
const model = x;
|
|
10
|
+
return (typeof model._modelType === "function" &&
|
|
11
|
+
model._modelType() === "base_chat_model");
|
|
12
|
+
}
|
|
8
13
|
/**
|
|
9
14
|
* Create an agent that uses tools.
|
|
10
15
|
* @param params Params required to create the agent. Includes an LLM, tools, and prompt.
|
|
@@ -62,10 +67,16 @@ function createToolCallingAgent({ llm, tools, prompt, streamRunnable, }) {
|
|
|
62
67
|
`Found ${JSON.stringify(prompt.inputVariables)} instead.`,
|
|
63
68
|
].join("\n"));
|
|
64
69
|
}
|
|
65
|
-
|
|
66
|
-
|
|
70
|
+
let modelWithTools;
|
|
71
|
+
if (_isBaseChatModel(llm)) {
|
|
72
|
+
if (llm.bindTools === undefined) {
|
|
73
|
+
throw new Error(`This agent requires that the "bind_tools()" method be implemented on the input model.`);
|
|
74
|
+
}
|
|
75
|
+
modelWithTools = llm.bindTools(tools);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
modelWithTools = llm;
|
|
67
79
|
}
|
|
68
|
-
const modelWithTools = llm.bindTools(tools);
|
|
69
80
|
const agent = agent_js_1.AgentRunnableSequence.fromRunnables([
|
|
70
81
|
runnables_1.RunnablePassthrough.assign({
|
|
71
82
|
agent_scratchpad: (input) => (0, tool_calling_js_1.formatToToolMessages)(input.steps),
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { BaseChatModel } from "@langchain/core/language_models/chat_models";
|
|
2
1
|
import { ChatPromptTemplate } from "@langchain/core/prompts";
|
|
3
2
|
import { StructuredToolInterface } from "@langchain/core/tools";
|
|
4
|
-
import { ToolDefinition } from "@langchain/core/language_models/base";
|
|
3
|
+
import { LanguageModelLike, ToolDefinition } from "@langchain/core/language_models/base";
|
|
5
4
|
import { AgentRunnableSequence } from "../agent.js";
|
|
6
5
|
import { ToolsAgentStep } from "./output_parser.js";
|
|
7
6
|
/**
|
|
@@ -13,7 +12,7 @@ export type CreateToolCallingAgentParams = {
|
|
|
13
12
|
* so must either be an OpenAI model that supports that or a wrapper of
|
|
14
13
|
* a different model that adds in equivalent support.
|
|
15
14
|
*/
|
|
16
|
-
llm:
|
|
15
|
+
llm: LanguageModelLike;
|
|
17
16
|
/** Tools this agent has access to. */
|
|
18
17
|
tools: StructuredToolInterface[] | ToolDefinition[];
|
|
19
18
|
/** The prompt to use, must have an input key of `agent_scratchpad`. */
|
|
@@ -2,6 +2,11 @@ import { RunnablePassthrough } from "@langchain/core/runnables";
|
|
|
2
2
|
import { AgentRunnableSequence } from "../agent.js";
|
|
3
3
|
import { ToolCallingAgentOutputParser, } from "./output_parser.js";
|
|
4
4
|
import { formatToToolMessages } from "../format_scratchpad/tool_calling.js";
|
|
5
|
+
function _isBaseChatModel(x) {
|
|
6
|
+
const model = x;
|
|
7
|
+
return (typeof model._modelType === "function" &&
|
|
8
|
+
model._modelType() === "base_chat_model");
|
|
9
|
+
}
|
|
5
10
|
/**
|
|
6
11
|
* Create an agent that uses tools.
|
|
7
12
|
* @param params Params required to create the agent. Includes an LLM, tools, and prompt.
|
|
@@ -59,10 +64,16 @@ export function createToolCallingAgent({ llm, tools, prompt, streamRunnable, })
|
|
|
59
64
|
`Found ${JSON.stringify(prompt.inputVariables)} instead.`,
|
|
60
65
|
].join("\n"));
|
|
61
66
|
}
|
|
62
|
-
|
|
63
|
-
|
|
67
|
+
let modelWithTools;
|
|
68
|
+
if (_isBaseChatModel(llm)) {
|
|
69
|
+
if (llm.bindTools === undefined) {
|
|
70
|
+
throw new Error(`This agent requires that the "bind_tools()" method be implemented on the input model.`);
|
|
71
|
+
}
|
|
72
|
+
modelWithTools = llm.bindTools(tools);
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
modelWithTools = llm;
|
|
64
76
|
}
|
|
65
|
-
const modelWithTools = llm.bindTools(tools);
|
|
66
77
|
const agent = AgentRunnableSequence.fromRunnables([
|
|
67
78
|
RunnablePassthrough.assign({
|
|
68
79
|
agent_scratchpad: (input) => formatToToolMessages(input.steps),
|
|
@@ -23,7 +23,7 @@ const load_js_1 = require("./question_answering/load.cjs");
|
|
|
23
23
|
* documents,
|
|
24
24
|
* embeddings
|
|
25
25
|
* );
|
|
26
|
-
* const prompt = ChatPromptTemplate.fromTemplate(`Answer the user's question: {input}`);
|
|
26
|
+
* const prompt = ChatPromptTemplate.fromTemplate(`Answer the user's question: {input} based on the following context {context}`);
|
|
27
27
|
*
|
|
28
28
|
* const combineDocsChain = await createStuffDocumentsChain({
|
|
29
29
|
* llm,
|
|
@@ -35,7 +35,7 @@ export interface RetrievalQAChainInput extends Omit<ChainInputs, "memory"> {
|
|
|
35
35
|
* documents,
|
|
36
36
|
* embeddings
|
|
37
37
|
* );
|
|
38
|
-
* const prompt = ChatPromptTemplate.fromTemplate(`Answer the user's question: {input}`);
|
|
38
|
+
* const prompt = ChatPromptTemplate.fromTemplate(`Answer the user's question: {input} based on the following context {context}`);
|
|
39
39
|
*
|
|
40
40
|
* const combineDocsChain = await createStuffDocumentsChain({
|
|
41
41
|
* llm,
|
|
@@ -20,7 +20,7 @@ import { loadQAStuffChain, } from "./question_answering/load.js";
|
|
|
20
20
|
* documents,
|
|
21
21
|
* embeddings
|
|
22
22
|
* );
|
|
23
|
-
* const prompt = ChatPromptTemplate.fromTemplate(`Answer the user's question: {input}`);
|
|
23
|
+
* const prompt = ChatPromptTemplate.fromTemplate(`Answer the user's question: {input} based on the following context {context}`);
|
|
24
24
|
*
|
|
25
25
|
* const combineDocsChain = await createStuffDocumentsChain({
|
|
26
26
|
* llm,
|
|
@@ -4,6 +4,7 @@ exports.MemoryVectorStore = void 0;
|
|
|
4
4
|
const vectorstores_1 = require("@langchain/core/vectorstores");
|
|
5
5
|
const documents_1 = require("@langchain/core/documents");
|
|
6
6
|
const similarities_js_1 = require("../util/ml-distance/similarities.cjs");
|
|
7
|
+
const math_js_1 = require("../util/math.cjs");
|
|
7
8
|
/**
|
|
8
9
|
* Class that extends `VectorStore` to store vectors in memory. Provides
|
|
9
10
|
* methods for adding documents, performing similarity searches, and
|
|
@@ -56,17 +57,7 @@ class MemoryVectorStore extends vectorstores_1.VectorStore {
|
|
|
56
57
|
}));
|
|
57
58
|
this.memoryVectors = this.memoryVectors.concat(memoryVectors);
|
|
58
59
|
}
|
|
59
|
-
|
|
60
|
-
* Method to perform a similarity search in the memory vector store. It
|
|
61
|
-
* calculates the similarity between the query vector and each vector in
|
|
62
|
-
* the store, sorts the results by similarity, and returns the top `k`
|
|
63
|
-
* results along with their scores.
|
|
64
|
-
* @param query Query vector to compare against the vectors in the store.
|
|
65
|
-
* @param k Number of top results to return.
|
|
66
|
-
* @param filter Optional filter function to apply to the vectors before performing the search.
|
|
67
|
-
* @returns Promise that resolves with an array of tuples, each containing a `Document` and its similarity score.
|
|
68
|
-
*/
|
|
69
|
-
async similaritySearchVectorWithScore(query, k, filter) {
|
|
60
|
+
async _queryVectors(query, k, filter) {
|
|
70
61
|
const filterFunction = (memoryVector) => {
|
|
71
62
|
if (!filter) {
|
|
72
63
|
return true;
|
|
@@ -78,22 +69,48 @@ class MemoryVectorStore extends vectorstores_1.VectorStore {
|
|
|
78
69
|
return filter(doc);
|
|
79
70
|
};
|
|
80
71
|
const filteredMemoryVectors = this.memoryVectors.filter(filterFunction);
|
|
81
|
-
|
|
72
|
+
return filteredMemoryVectors
|
|
82
73
|
.map((vector, index) => ({
|
|
83
74
|
similarity: this.similarity(query, vector.embedding),
|
|
84
75
|
index,
|
|
76
|
+
metadata: vector.metadata,
|
|
77
|
+
content: vector.content,
|
|
78
|
+
embedding: vector.embedding,
|
|
85
79
|
}))
|
|
86
80
|
.sort((a, b) => (a.similarity > b.similarity ? -1 : 0))
|
|
87
81
|
.slice(0, k);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Method to perform a similarity search in the memory vector store. It
|
|
85
|
+
* calculates the similarity between the query vector and each vector in
|
|
86
|
+
* the store, sorts the results by similarity, and returns the top `k`
|
|
87
|
+
* results along with their scores.
|
|
88
|
+
* @param query Query vector to compare against the vectors in the store.
|
|
89
|
+
* @param k Number of top results to return.
|
|
90
|
+
* @param filter Optional filter function to apply to the vectors before performing the search.
|
|
91
|
+
* @returns Promise that resolves with an array of tuples, each containing a `Document` and its similarity score.
|
|
92
|
+
*/
|
|
93
|
+
async similaritySearchVectorWithScore(query, k, filter) {
|
|
94
|
+
const searches = await this._queryVectors(query, k, filter);
|
|
88
95
|
const result = searches.map((search) => [
|
|
89
96
|
new documents_1.Document({
|
|
90
|
-
metadata:
|
|
91
|
-
pageContent:
|
|
97
|
+
metadata: search.metadata,
|
|
98
|
+
pageContent: search.content,
|
|
92
99
|
}),
|
|
93
100
|
search.similarity,
|
|
94
101
|
]);
|
|
95
102
|
return result;
|
|
96
103
|
}
|
|
104
|
+
async maxMarginalRelevanceSearch(query, options) {
|
|
105
|
+
const queryEmbedding = await this.embeddings.embedQuery(query);
|
|
106
|
+
const searches = await this._queryVectors(queryEmbedding, options.fetchK ?? 20, options.filter);
|
|
107
|
+
const embeddingList = searches.map((searchResp) => searchResp.embedding);
|
|
108
|
+
const mmrIndexes = (0, math_js_1.maximalMarginalRelevance)(queryEmbedding, embeddingList, options.lambda, options.k);
|
|
109
|
+
return mmrIndexes.map((idx) => new documents_1.Document({
|
|
110
|
+
metadata: searches[idx].metadata,
|
|
111
|
+
pageContent: searches[idx].content,
|
|
112
|
+
}));
|
|
113
|
+
}
|
|
97
114
|
/**
|
|
98
115
|
* Static method to create a `MemoryVectorStore` instance from an array of
|
|
99
116
|
* texts. It creates a `Document` for each text and metadata pair, and
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { VectorStore } from "@langchain/core/vectorstores";
|
|
1
|
+
import { MaxMarginalRelevanceSearchOptions, VectorStore } from "@langchain/core/vectorstores";
|
|
2
2
|
import type { EmbeddingsInterface } from "@langchain/core/embeddings";
|
|
3
|
-
import { Document } from "@langchain/core/documents";
|
|
3
|
+
import { Document, DocumentInterface } from "@langchain/core/documents";
|
|
4
4
|
import { cosine } from "../util/ml-distance/similarities.js";
|
|
5
5
|
/**
|
|
6
6
|
* Interface representing a vector in memory. It includes the content
|
|
@@ -48,6 +48,13 @@ export declare class MemoryVectorStore extends VectorStore {
|
|
|
48
48
|
* @returns Promise that resolves when all vectors have been added.
|
|
49
49
|
*/
|
|
50
50
|
addVectors(vectors: number[][], documents: Document[]): Promise<void>;
|
|
51
|
+
protected _queryVectors(query: number[], k: number, filter?: this["FilterType"]): Promise<{
|
|
52
|
+
similarity: number;
|
|
53
|
+
index: number;
|
|
54
|
+
metadata: Record<string, any>;
|
|
55
|
+
content: string;
|
|
56
|
+
embedding: number[];
|
|
57
|
+
}[]>;
|
|
51
58
|
/**
|
|
52
59
|
* Method to perform a similarity search in the memory vector store. It
|
|
53
60
|
* calculates the similarity between the query vector and each vector in
|
|
@@ -59,6 +66,7 @@ export declare class MemoryVectorStore extends VectorStore {
|
|
|
59
66
|
* @returns Promise that resolves with an array of tuples, each containing a `Document` and its similarity score.
|
|
60
67
|
*/
|
|
61
68
|
similaritySearchVectorWithScore(query: number[], k: number, filter?: this["FilterType"]): Promise<[Document, number][]>;
|
|
69
|
+
maxMarginalRelevanceSearch(query: string, options: MaxMarginalRelevanceSearchOptions<this["FilterType"]>): Promise<DocumentInterface[]>;
|
|
62
70
|
/**
|
|
63
71
|
* Static method to create a `MemoryVectorStore` instance from an array of
|
|
64
72
|
* texts. It creates a `Document` for each text and metadata pair, and
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { VectorStore } from "@langchain/core/vectorstores";
|
|
1
|
+
import { VectorStore, } from "@langchain/core/vectorstores";
|
|
2
2
|
import { Document } from "@langchain/core/documents";
|
|
3
3
|
import { cosine } from "../util/ml-distance/similarities.js";
|
|
4
|
+
import { maximalMarginalRelevance } from "../util/math.js";
|
|
4
5
|
/**
|
|
5
6
|
* Class that extends `VectorStore` to store vectors in memory. Provides
|
|
6
7
|
* methods for adding documents, performing similarity searches, and
|
|
@@ -53,17 +54,7 @@ export class MemoryVectorStore extends VectorStore {
|
|
|
53
54
|
}));
|
|
54
55
|
this.memoryVectors = this.memoryVectors.concat(memoryVectors);
|
|
55
56
|
}
|
|
56
|
-
|
|
57
|
-
* Method to perform a similarity search in the memory vector store. It
|
|
58
|
-
* calculates the similarity between the query vector and each vector in
|
|
59
|
-
* the store, sorts the results by similarity, and returns the top `k`
|
|
60
|
-
* results along with their scores.
|
|
61
|
-
* @param query Query vector to compare against the vectors in the store.
|
|
62
|
-
* @param k Number of top results to return.
|
|
63
|
-
* @param filter Optional filter function to apply to the vectors before performing the search.
|
|
64
|
-
* @returns Promise that resolves with an array of tuples, each containing a `Document` and its similarity score.
|
|
65
|
-
*/
|
|
66
|
-
async similaritySearchVectorWithScore(query, k, filter) {
|
|
57
|
+
async _queryVectors(query, k, filter) {
|
|
67
58
|
const filterFunction = (memoryVector) => {
|
|
68
59
|
if (!filter) {
|
|
69
60
|
return true;
|
|
@@ -75,22 +66,48 @@ export class MemoryVectorStore extends VectorStore {
|
|
|
75
66
|
return filter(doc);
|
|
76
67
|
};
|
|
77
68
|
const filteredMemoryVectors = this.memoryVectors.filter(filterFunction);
|
|
78
|
-
|
|
69
|
+
return filteredMemoryVectors
|
|
79
70
|
.map((vector, index) => ({
|
|
80
71
|
similarity: this.similarity(query, vector.embedding),
|
|
81
72
|
index,
|
|
73
|
+
metadata: vector.metadata,
|
|
74
|
+
content: vector.content,
|
|
75
|
+
embedding: vector.embedding,
|
|
82
76
|
}))
|
|
83
77
|
.sort((a, b) => (a.similarity > b.similarity ? -1 : 0))
|
|
84
78
|
.slice(0, k);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Method to perform a similarity search in the memory vector store. It
|
|
82
|
+
* calculates the similarity between the query vector and each vector in
|
|
83
|
+
* the store, sorts the results by similarity, and returns the top `k`
|
|
84
|
+
* results along with their scores.
|
|
85
|
+
* @param query Query vector to compare against the vectors in the store.
|
|
86
|
+
* @param k Number of top results to return.
|
|
87
|
+
* @param filter Optional filter function to apply to the vectors before performing the search.
|
|
88
|
+
* @returns Promise that resolves with an array of tuples, each containing a `Document` and its similarity score.
|
|
89
|
+
*/
|
|
90
|
+
async similaritySearchVectorWithScore(query, k, filter) {
|
|
91
|
+
const searches = await this._queryVectors(query, k, filter);
|
|
85
92
|
const result = searches.map((search) => [
|
|
86
93
|
new Document({
|
|
87
|
-
metadata:
|
|
88
|
-
pageContent:
|
|
94
|
+
metadata: search.metadata,
|
|
95
|
+
pageContent: search.content,
|
|
89
96
|
}),
|
|
90
97
|
search.similarity,
|
|
91
98
|
]);
|
|
92
99
|
return result;
|
|
93
100
|
}
|
|
101
|
+
async maxMarginalRelevanceSearch(query, options) {
|
|
102
|
+
const queryEmbedding = await this.embeddings.embedQuery(query);
|
|
103
|
+
const searches = await this._queryVectors(queryEmbedding, options.fetchK ?? 20, options.filter);
|
|
104
|
+
const embeddingList = searches.map((searchResp) => searchResp.embedding);
|
|
105
|
+
const mmrIndexes = maximalMarginalRelevance(queryEmbedding, embeddingList, options.lambda, options.k);
|
|
106
|
+
return mmrIndexes.map((idx) => new Document({
|
|
107
|
+
metadata: searches[idx].metadata,
|
|
108
|
+
pageContent: searches[idx].content,
|
|
109
|
+
}));
|
|
110
|
+
}
|
|
94
111
|
/**
|
|
95
112
|
* Static method to create a `MemoryVectorStore` instance from an array of
|
|
96
113
|
* texts. It creates a `Document` for each text and metadata pair, and
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "langchain",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.15",
|
|
4
4
|
"description": "Typescript bindings for langchain",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -616,8 +616,8 @@
|
|
|
616
616
|
"@langchain/anthropic": "^0.2.8",
|
|
617
617
|
"@langchain/aws": "^0.0.5",
|
|
618
618
|
"@langchain/cohere": "^0.2.1",
|
|
619
|
-
"@langchain/google-genai": "^0.0.
|
|
620
|
-
"@langchain/google-vertexai": "^0.0.
|
|
619
|
+
"@langchain/google-genai": "^0.0.25",
|
|
620
|
+
"@langchain/google-vertexai": "^0.0.25",
|
|
621
621
|
"@langchain/groq": "^0.0.15",
|
|
622
622
|
"@langchain/mistralai": "^0.0.26",
|
|
623
623
|
"@langchain/ollama": "^0.0.2",
|