Skip to content

Commit df43448

Browse files
authored
community[minor],langchain[minor]: Move base classes and reference implementations to core (#5187)
* Move toolkit declaration to core * Move more reference implementations to core * Move BaseChatMemory * Revert BaseChatMemory move * core[patch],langchain[patch]: Refactor self query (#5192) * Move BaseChain and LLMChain to legacy entrypoint in core * Fix build * Add deprecation strings * Revert BaseChain and LLMChain moves * Rename * Refactor * Revert * Update imports * Remove type * Remove peer dep * Update lock * Bump deps * Fix
1 parent 7fae59b commit df43448

File tree

22 files changed

+96
-984
lines changed

22 files changed

+96
-984
lines changed

langchain/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,7 @@
15131513
"dependencies": {
15141514
"@anthropic-ai/sdk": "^0.9.1",
15151515
"@langchain/community": "~0.0.47",
1516-
"@langchain/core": "~0.1.56",
1516+
"@langchain/core": "~0.1.60",
15171517
"@langchain/openai": "~0.0.28",
15181518
"@langchain/textsplitters": "~0.0.0",
15191519
"binary-extensions": "^2.2.0",

langchain/src/chains/query_constructor/index.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
21
import { z } from "zod";
2+
import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
33
import {
44
Example,
55
interpolateFString,
@@ -21,7 +21,6 @@ import {
2121
DEFAULT_SUFFIX,
2222
EXAMPLE_PROMPT,
2323
} from "./prompt.js";
24-
import { LLMChain } from "../llm_chain.js";
2524
import { AsymmetricStructuredOutputParser } from "../../output_parsers/structured.js";
2625
import { AttributeInfo } from "../../schema/query_constructor.js";
2726

@@ -168,7 +167,7 @@ function _getPrompt(
168167
/**
169168
* A type that represents options for the query constructor chain.
170169
*/
171-
export type QueryConstructorChainOptions = {
170+
export type QueryConstructorRunnableOptions = {
172171
llm: BaseLanguageModelInterface;
173172
documentContents: string;
174173
attributeInfo: AttributeInfo[];
@@ -177,16 +176,22 @@ export type QueryConstructorChainOptions = {
177176
allowedOperators?: Operator[];
178177
};
179178

180-
export function loadQueryConstructorChain(opts: QueryConstructorChainOptions) {
179+
/** @deprecated */
180+
export type QueryConstructorChainOptions = QueryConstructorRunnableOptions;
181+
182+
export function loadQueryConstructorRunnable(
183+
opts: QueryConstructorRunnableOptions
184+
) {
181185
const prompt = _getPrompt(
182186
opts.documentContents,
183187
opts.attributeInfo,
184188
opts.allowedComparators,
185189
opts.allowedOperators,
186190
opts.examples
187191
);
188-
return new LLMChain({
189-
llm: opts.llm,
190-
prompt,
191-
});
192+
const outputParser = StructuredQueryOutputParser.fromComponents(
193+
opts.allowedComparators,
194+
opts.allowedOperators
195+
);
196+
return prompt.pipe(opts.llm).pipe(outputParser);
192197
}
Lines changed: 1 addition & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -1,186 +1 @@
1-
import { VectorStore } from "@langchain/core/vectorstores";
2-
3-
/**
4-
* Represents logical AND operator.
5-
*/
6-
export type AND = "and";
7-
/**
8-
* Represents logical OR operator.
9-
*/
10-
export type OR = "or";
11-
/**
12-
* Represents logical NOT operator.
13-
*/
14-
export type NOT = "not";
15-
16-
/**
17-
* Represents a logical operator which can be AND, OR, or NOT.
18-
*/
19-
export type Operator = AND | OR | NOT;
20-
21-
/**
22-
* Represents equality comparison operator.
23-
*/
24-
export type EQ = "eq";
25-
/**
26-
* Represents inequality comparison operator.
27-
*/
28-
export type NE = "ne";
29-
/**
30-
* Represents less than comparison operator.
31-
*/
32-
export type LT = "lt";
33-
/**
34-
* Represents greater than comparison operator.
35-
*/
36-
export type GT = "gt";
37-
/**
38-
* Represents less than or equal to comparison operator.
39-
*/
40-
export type LTE = "lte";
41-
/**
42-
* Represents greater than or equal to comparison operator.
43-
*/
44-
export type GTE = "gte";
45-
46-
/**
47-
* Represents a comparison operator which can be EQ, NE, LT, GT, LTE, or
48-
* GTE.
49-
*/
50-
export type Comparator = EQ | NE | LT | GT | LTE | GTE;
51-
52-
export const Operators: { [key: string]: Operator } = {
53-
and: "and",
54-
or: "or",
55-
not: "not",
56-
};
57-
58-
export const Comparators: { [key: string]: Comparator } = {
59-
eq: "eq",
60-
ne: "ne",
61-
lt: "lt",
62-
gt: "gt",
63-
lte: "lte",
64-
gte: "gte",
65-
};
66-
67-
/**
68-
* Represents the result of visiting an operation or comparison
69-
* expression.
70-
*/
71-
export type VisitorResult = VisitorOperationResult | VisitorComparisonResult;
72-
73-
/**
74-
* Represents the result of visiting an operation expression.
75-
*/
76-
export type VisitorOperationResult = {
77-
[operator: string]: VisitorResult[];
78-
};
79-
80-
/**
81-
* Represents the result of visiting a comparison expression.
82-
*/
83-
export type VisitorComparisonResult = {
84-
[attr: string]: {
85-
[comparator: string]: string | number;
86-
};
87-
};
88-
89-
/**
90-
* Represents the result of visiting a structured query expression.
91-
*/
92-
export type VisitorStructuredQueryResult = {
93-
filter?: VisitorComparisonResult | VisitorOperationResult;
94-
};
95-
96-
/**
97-
* Abstract class for visiting expressions. Subclasses must implement
98-
* visitOperation, visitComparison, and visitStructuredQuery methods.
99-
*/
100-
export abstract class Visitor<T extends VectorStore = VectorStore> {
101-
declare VisitOperationOutput: object;
102-
103-
declare VisitComparisonOutput: object;
104-
105-
declare VisitStructuredQueryOutput: { filter?: T["FilterType"] };
106-
107-
abstract allowedOperators: Operator[];
108-
109-
abstract allowedComparators: Comparator[];
110-
111-
abstract visitOperation(operation: Operation): this["VisitOperationOutput"];
112-
113-
abstract visitComparison(
114-
comparison: Comparison
115-
): this["VisitComparisonOutput"];
116-
117-
abstract visitStructuredQuery(
118-
structuredQuery: StructuredQuery
119-
): this["VisitStructuredQueryOutput"];
120-
}
121-
122-
/**
123-
* Abstract class representing an expression. Subclasses must implement
124-
* the exprName property and the accept method.
125-
*/
126-
export abstract class Expression {
127-
abstract exprName: "Operation" | "Comparison" | "StructuredQuery";
128-
129-
accept(visitor: Visitor) {
130-
if (this.exprName === "Operation") {
131-
return visitor.visitOperation(this as unknown as Operation);
132-
} else if (this.exprName === "Comparison") {
133-
return visitor.visitComparison(this as unknown as Comparison);
134-
} else if (this.exprName === "StructuredQuery") {
135-
return visitor.visitStructuredQuery(this as unknown as StructuredQuery);
136-
} else {
137-
throw new Error("Unknown Expression type");
138-
}
139-
}
140-
}
141-
142-
/**
143-
* Abstract class representing a filter directive. It extends the
144-
* Expression class.
145-
*/
146-
export abstract class FilterDirective extends Expression {}
147-
148-
/**
149-
* Class representing a comparison filter directive. It extends the
150-
* FilterDirective class.
151-
*/
152-
export class Comparison extends FilterDirective {
153-
exprName = "Comparison" as const;
154-
155-
constructor(
156-
public comparator: Comparator,
157-
public attribute: string,
158-
public value: string | number
159-
) {
160-
super();
161-
}
162-
}
163-
164-
/**
165-
* Class representing an operation filter directive. It extends the
166-
* FilterDirective class.
167-
*/
168-
export class Operation extends FilterDirective {
169-
exprName = "Operation" as const;
170-
171-
constructor(public operator: Operator, public args?: FilterDirective[]) {
172-
super();
173-
}
174-
}
175-
176-
/**
177-
* Class representing a structured query expression. It extends the
178-
* Expression class.
179-
*/
180-
export class StructuredQuery extends Expression {
181-
exprName = "StructuredQuery" as const;
182-
183-
constructor(public query: string, public filter?: FilterDirective) {
184-
super();
185-
}
186-
}
1+
export * from "@langchain/core/structured_query";

langchain/src/chains/query_constructor/prompt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PromptTemplate } from "@langchain/core/prompts";
1+
import { PromptTemplate } from "../../prompts/index.js";
22

33
export const SONG_DATA_SOURCE = `\
44
\`\`\`json

langchain/src/chains/query_constructor/tests/query_chain.int.test.ts

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { test } from "@jest/globals";
22
import { OpenAI } from "@langchain/openai";
3-
import { loadQueryConstructorChain } from "../index.js";
3+
import { loadQueryConstructorRunnable } from "../index.js";
44
import {
55
Comparators,
66
Comparison,
@@ -76,54 +76,44 @@ test("Query Chain Test", async () => {
7676

7777
const allowedComparators = Object.values(Comparators);
7878
const allowedOperators = Object.values(Operators);
79-
const llm = new OpenAI({ modelName: "gpt-3.5-turbo", temperature: 0 });
80-
const queryChain = loadQueryConstructorChain({
79+
const llm = new OpenAI({
80+
modelName: "gpt-3.5-turbo-instruct",
81+
temperature: 0,
82+
});
83+
const queryChain = loadQueryConstructorRunnable({
8184
llm,
8285
documentContents,
8386
attributeInfo,
8487
allowedComparators,
8588
allowedOperators,
8689
});
8790

88-
const c1 = queryChain.call({
91+
const c1 = queryChain.invoke({
8992
query: "Which movies are less than 90 minutes?",
9093
});
91-
const c3 = queryChain.call({
94+
const c3 = queryChain.invoke({
9295
query: "Which movies are rated higher than 8.5?",
9396
});
94-
const c4 = queryChain.call({
97+
const c4 = queryChain.invoke({
9598
query: "Which movies are directed by Greta Gerwig?",
9699
});
97-
const c5 = queryChain.call({
100+
const c5 = queryChain.invoke({
98101
query:
99102
"Which movies are either comedy or drama and are less than 90 minutes?",
100103
});
101104

102-
const [
103-
{ [queryChain.outputKey]: r1 },
104-
{ [queryChain.outputKey]: r3 },
105-
{ [queryChain.outputKey]: r4 },
106-
{ [queryChain.outputKey]: r5 },
107-
] = await Promise.all([c1, c3, c4, c5]);
105+
const [r1, r3, r4, r5] = await Promise.all([c1, c3, c4, c5]);
108106

109107
expect(r1).toMatchObject(sq1);
110108
expect(r3).toMatchObject(sq3);
111109
expect(r4).toMatchObject(sq4);
112110
expect(r5).toMatchObject(sq5);
113111
const testTranslator = new BasicTranslator();
114112

115-
const { filter: parsedFilter1 } = testTranslator.visitStructuredQuery(
116-
r1 as StructuredQuery
117-
);
118-
const { filter: parsedFilter3 } = testTranslator.visitStructuredQuery(
119-
r3 as StructuredQuery
120-
);
121-
const { filter: parsedFilter4 } = testTranslator.visitStructuredQuery(
122-
r4 as StructuredQuery
123-
);
124-
const { filter: parsedFilter5 } = testTranslator.visitStructuredQuery(
125-
r5 as StructuredQuery
126-
);
113+
const { filter: parsedFilter1 } = testTranslator.visitStructuredQuery(r1);
114+
const { filter: parsedFilter3 } = testTranslator.visitStructuredQuery(r3);
115+
const { filter: parsedFilter4 } = testTranslator.visitStructuredQuery(r4);
116+
const { filter: parsedFilter5 } = testTranslator.visitStructuredQuery(r5);
127117

128118
expect(parsedFilter1).toMatchObject(filter1);
129119
expect(parsedFilter3).toMatchObject(filter3);

langchain/src/output_parsers/expression.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { BaseOutputParser } from "@langchain/core/output_parsers";
22
import { MasterHandler } from "./expression_type_handlers/factory.js";
33
import { ParsedType } from "./expression_type_handlers/types.js";
44
import { ASTParser } from "./expression_type_handlers/base.js";
5+
56
/**
6-
* okay so we need to be able to handle the following cases:
7+
* We need to be able to handle the following cases:
78
* ExpressionStatement
89
* CallExpression
910
* Identifier | MemberExpression

0 commit comments

Comments
 (0)