Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: violations of expectations chain #2945

Merged
merged 26 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
edd2919
feat: violations of expectations chain
bracesproul Oct 17, 2023
cd57077
fix: final fact generation chain
bracesproul Oct 17, 2023
3b8d4e8
fix: improve prompting
bracesproul Oct 17, 2023
630539c
refactor: generateRevisedPrediction to its own method
bracesproul Oct 17, 2023
e4e7008
chore: lint files
bracesproul Oct 17, 2023
f5299bd
feat: added run manager for tracing
bracesproul Oct 17, 2023
848500e
nit: use class llm instead of passing through
bracesproul Oct 17, 2023
83d4019
chore: added jsdoc
bracesproul Oct 17, 2023
e0cb608
chore: more jsdoc
bracesproul Oct 17, 2023
68a4ba9
chore: lint files
bracesproul Oct 17, 2023
a5da8c3
feat: connect retriever to chain
bracesproul Oct 17, 2023
5b3db0e
feat: add .fromLLM static method
bracesproul Oct 17, 2023
5d01fd8
chore: added pure comment to pure exports, renamed to be plural
bracesproul Oct 17, 2023
af0a23b
chore: fix side effect build errors, properly export chain
bracesproul Oct 17, 2023
16e9a83
fix: remove dup strings from relevant docs, nit on testing data
bracesproul Oct 17, 2023
8b73975
nit: rename test .init -> .int
bracesproul Oct 17, 2023
ab6b62a
Merge branch 'main' into feat/voe-chain
bracesproul Oct 18, 2023
8c0a677
chore: type return values
bracesproul Oct 18, 2023
7026316
chore: lint files
bracesproul Oct 18, 2023
2fbd1e1
refactor: move to experimental
bracesproul Oct 18, 2023
1df1033
feat: documentation
bracesproul Oct 19, 2023
63f671d
Merge branch 'main' into feat/voe-chain
bracesproul Oct 19, 2023
36acea6
lint
bracesproul Oct 19, 2023
823bdb9
nit: method rename -> chunkMessagesByAIResponse
bracesproul Oct 19, 2023
9cb96f3
Merge branch 'main' of https://github.com/hwchase17/langchainjs into …
jacoblee93 Oct 19, 2023
180aca6
Small docs updates, run format
jacoblee93 Oct 19, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions environment_tests/test-exports-bun/src/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from "langchain/base_language";
export * from "langchain/tools";
export * from "langchain/chains";
export * from "langchain/chains/openai_functions";
export * from "langchain/chains/violation_of_expectations";
export * from "langchain/embeddings/base";
export * from "langchain/embeddings/cache_backed";
export * from "langchain/embeddings/fake";
Expand Down
1 change: 1 addition & 0 deletions environment_tests/test-exports-cf/src/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from "langchain/base_language";
export * from "langchain/tools";
export * from "langchain/chains";
export * from "langchain/chains/openai_functions";
export * from "langchain/chains/violation_of_expectations";
export * from "langchain/embeddings/base";
export * from "langchain/embeddings/cache_backed";
export * from "langchain/embeddings/fake";
Expand Down
1 change: 1 addition & 0 deletions environment_tests/test-exports-cjs/src/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const base_language = require("langchain/base_language");
const tools = require("langchain/tools");
const chains = require("langchain/chains");
const chains_openai_functions = require("langchain/chains/openai_functions");
const chains_violation_of_expectations = require("langchain/chains/violation_of_expectations");
const embeddings_base = require("langchain/embeddings/base");
const embeddings_cache_backed = require("langchain/embeddings/cache_backed");
const embeddings_fake = require("langchain/embeddings/fake");
Expand Down
1 change: 1 addition & 0 deletions environment_tests/test-exports-esbuild/src/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as base_language from "langchain/base_language";
import * as tools from "langchain/tools";
import * as chains from "langchain/chains";
import * as chains_openai_functions from "langchain/chains/openai_functions";
import * as chains_violation_of_expectations from "langchain/chains/violation_of_expectations";
import * as embeddings_base from "langchain/embeddings/base";
import * as embeddings_cache_backed from "langchain/embeddings/cache_backed";
import * as embeddings_fake from "langchain/embeddings/fake";
Expand Down
1 change: 1 addition & 0 deletions environment_tests/test-exports-esm/src/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as base_language from "langchain/base_language";
import * as tools from "langchain/tools";
import * as chains from "langchain/chains";
import * as chains_openai_functions from "langchain/chains/openai_functions";
import * as chains_violation_of_expectations from "langchain/chains/violation_of_expectations";
import * as embeddings_base from "langchain/embeddings/base";
import * as embeddings_cache_backed from "langchain/embeddings/cache_backed";
import * as embeddings_fake from "langchain/embeddings/fake";
Expand Down
1 change: 1 addition & 0 deletions environment_tests/test-exports-vercel/src/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from "langchain/base_language";
export * from "langchain/tools";
export * from "langchain/chains";
export * from "langchain/chains/openai_functions";
export * from "langchain/chains/violation_of_expectations";
export * from "langchain/embeddings/base";
export * from "langchain/embeddings/cache_backed";
export * from "langchain/embeddings/fake";
Expand Down
1 change: 1 addition & 0 deletions environment_tests/test-exports-vite/src/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from "langchain/base_language";
export * from "langchain/tools";
export * from "langchain/chains";
export * from "langchain/chains/openai_functions";
export * from "langchain/chains/violation_of_expectations";
export * from "langchain/embeddings/base";
export * from "langchain/embeddings/cache_backed";
export * from "langchain/embeddings/fake";
Expand Down
3 changes: 3 additions & 0 deletions langchain/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ chains/sql_db.d.ts
chains/graph_qa/cypher.cjs
chains/graph_qa/cypher.js
chains/graph_qa/cypher.d.ts
chains/violation_of_expectations.cjs
chains/violation_of_expectations.js
chains/violation_of_expectations.d.ts
embeddings/base.cjs
embeddings/base.js
embeddings/base.d.ts
Expand Down
8 changes: 8 additions & 0 deletions langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
"chains/graph_qa/cypher.cjs",
"chains/graph_qa/cypher.js",
"chains/graph_qa/cypher.d.ts",
"chains/violation_of_expectations.cjs",
"chains/violation_of_expectations.js",
"chains/violation_of_expectations.d.ts",
"embeddings/base.cjs",
"embeddings/base.js",
"embeddings/base.d.ts",
Expand Down Expand Up @@ -1403,6 +1406,11 @@
"import": "./chains/graph_qa/cypher.js",
"require": "./chains/graph_qa/cypher.cjs"
},
"./chains/violation_of_expectations": {
"types": "./chains/violation_of_expectations.d.ts",
"import": "./chains/violation_of_expectations.js",
"require": "./chains/violation_of_expectations.cjs"
},
"./embeddings/base": {
"types": "./embeddings/base.d.ts",
"import": "./embeddings/base.js",
Expand Down
1 change: 1 addition & 0 deletions langchain/scripts/create-entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const entrypoints = {
"chains/query_constructor/ir": "chains/query_constructor/ir",
"chains/sql_db": "chains/sql_db/index",
"chains/graph_qa/cypher": "chains/graph_qa/cypher",
"chains/violation_of_expectations": "chains/violation_of_expectations/violation_of_expectations_chain",
bracesproul marked this conversation as resolved.
Show resolved Hide resolved
// embeddings
"embeddings/base": "embeddings/base",
"embeddings/bedrock": "embeddings/bedrock",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ChatOpenAI } from "../../chat_models/openai.js";
import { OpenAIEmbeddings } from "../../embeddings/openai.js";
import { AIMessage, HumanMessage } from "../../schema/index.js";
import { HNSWLib } from "../../vectorstores/hnswlib.js";
import { ViolationOfExpectationsChain } from "../violation_of_expectations/violation_of_expectations_chain.js";

const dummyMessages = [
new HumanMessage(
"I've been thinking about the importance of time with myself to discover my voice. I feel like 1-2 hours is never enough."
),
new AIMessage(
"The concept of 'adequate time' varies. Have you tried different formats of introspection, such as morning pages or long-form writing, to see if they make the process more efficient?"
),
new HumanMessage(
"I have tried journaling but never consistently. Sometimes it feels like writing doesn't capture everything."
),
];

test("should respond with the proper schema", async () => {
const vectorStore = await HNSWLib.fromTexts(
[" "],
[{ id: 1 }],
new OpenAIEmbeddings()
);
const retriever = vectorStore.asRetriever();

const llm = new ChatOpenAI({
modelName: "gpt-4",
});
const chain = new ViolationOfExpectationsChain({
llm,
retriever,
});

const res = await chain.call({
chat_history: dummyMessages,
});

console.log({
res,
});
});
77 changes: 77 additions & 0 deletions langchain/src/chains/violation_of_expectations/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { BaseMessage, HumanMessage } from "../../schema/index.js";

/**
* Contains the chunk of messages, along with the
* users response, which is the next message after the chunk.
*/
export type MessageChunkResult = {
chunkedMessages: BaseMessage[];
/**
* User response can be undefined if the last message in
* the chat history was from the AI.
*/
userResponse?: HumanMessage;
};

export type PredictNextUserMessageResponse = {
userState: string;
predictedUserMessage: string;
insights: Array<string>;
};

export type GetPredictionViolationsResponse = {
userResponse?: HumanMessage;
revisedPrediction: string;
explainedPredictionErrors: Array<string>;
};

export const PREDICT_NEXT_USER_MESSAGE_FUNCTION = {
name: "predictNextUserMessage",
description: "Predicts the next user message, along with insights.",
parameters: {
type: "object",
properties: {
userState: {
type: "string",
description: "Concise reasoning about the users internal mental state.",
},
predictedUserMessage: {
type: "string",
description:
"Your prediction on how they will respond to the AI's most recent message.",
},
insights: {
type: "array",
items: {
type: "string",
},
description:
"A concise list of any additional insights that would be useful to improve prediction.",
},
},
required: ["userState", "predictedUserMessage", "insights"],
},
};

export const PREDICTION_VIOLATIONS_FUNCTION = {
name: "predictionViolations",
description:
"Generates violations, errors and differences between the predicted user response, and the actual response.",
parameters: {
type: "object",
properties: {
violationExplanation: {
type: "string",
description: "How was the predication violated?",
},
explainedPredictionErrors: {
type: "array",
items: {
type: "string",
},
description: "Explanations of how the prediction was violated and why",
},
},
required: ["violationExplanation", "explainedPredictionErrors"],
},
};
Loading