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

feature: chat deployment implementation #39

Merged
merged 29 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
f6166e9
Rename auth method in docs
FilipZmijewski Jan 15, 2025
7e395cb
fix(core): Fix trim messages mutation bug (#7547)
jacoblee93 Jan 18, 2025
a52fde3
release(core): 0.3.31 (#7548)
jacoblee93 Jan 18, 2025
74c06a5
fix(community): Updated Embeddings URL (#7545)
Jacky3003 Jan 18, 2025
9804d8f
fix(community): make sure guardrailConfig can be added even with anth…
madmed88 Jan 18, 2025
904dcad
docs: Fix PGVectorStore import in install dependencies (TypeScript) e…
bploetz Jan 18, 2025
e0e2fc9
fix(community): Airtable url (#7532)
eduardconstantin Jan 18, 2025
9314ef1
docs: Fix typo in OpenAIModerationChain example (#7528)
JonathanVelkeneers Jan 18, 2025
e1c8212
docs: Resolves #7483, resolves #7274 (#7505)
ucev Jan 18, 2025
08f1a46
docs: Rename auth method in IBM docs (#7524)
FilipZmijewski Jan 18, 2025
900381a
docs: correct misspelling (#7522)
ucev Jan 18, 2025
f1dbe28
release(community): 0.3.25 (#7549)
jacoblee93 Jan 18, 2025
3a1131a
feat(azure-cosmosdb): add session context for a user mongodb (#7436)
crisjy Jan 18, 2025
9518c50
release(azure-cosmosdb): 0.2.7 (#7550)
jacoblee93 Jan 18, 2025
1546a2e
fix(ci): Fix build (#7551)
jacoblee93 Jan 18, 2025
94467fa
feat(anthropic): Add Anthropic PDF support (document type) in invoke …
adhambadr Jan 18, 2025
fca3d89
release(anthropic): 0.3.12 (#7552)
jacoblee93 Jan 18, 2025
b235062
chore(core,langchain,community): Relax langsmith deps (#7556)
jacoblee93 Jan 20, 2025
64c2b8f
release(community): 0.3.26 (#7557)
jacoblee93 Jan 20, 2025
bb33dca
release(core): 0.3.32 (#7558)
jacoblee93 Jan 20, 2025
c4f6122
Release 0.3.12 (#7559)
jacoblee93 Jan 20, 2025
828f5e6
Merge branch 'main' of https://github.com/FilipZmijewski/langchainjs
FilipZmijewski Jan 21, 2025
362f321
Merge branch 'main' of https://github.com/langchain-ai/langchainjs
FilipZmijewski Jan 21, 2025
797c0ee
Add deployment chat to chat class
FilipZmijewski Jan 28, 2025
ba8f272
Upadate Watsonx sdk
FilipZmijewski Jan 29, 2025
bf47045
Rework interfaces in llms as well
FilipZmijewski Jan 30, 2025
02fa92c
Bump watsonx-ai sdk version
FilipZmijewski Jan 30, 2025
ca04f75
Remove unused code
FilipZmijewski Jan 30, 2025
c4681e5
Add fake auth
FilipZmijewski Jan 30, 2025
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
4 changes: 2 additions & 2 deletions docs/core_docs/docs/how_to/tool_calling_parallel.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"});\n",
"\n",
"const multiplyTool = tool(async ({ a, b }) => {\n",
" return a + b;\n",
" return a * b;\n",
"}, {\n",
" name: \"multiply\",\n",
" description: \"Multiplies a and b\",\n",
Expand Down Expand Up @@ -220,4 +220,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
4 changes: 2 additions & 2 deletions docs/core_docs/docs/how_to/tool_results_pass_to_model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
":::\n",
"```\n",
"\n",
"Some models are capable of [**tool calling**](/docs/concepts/tool_calling) - generating arguments that conform to a specific user-provided schema. This guide will demonstrate how to use those tool cals to actually call a function and properly pass the results back to the model.\n",
"Some models are capable of [**tool calling**](/docs/concepts/tool_calling) - generating arguments that conform to a specific user-provided schema. This guide will demonstrate how to use those tool calls to actually call a function and properly pass the results back to the model.\n",
"\n",
"![](../../static/img/tool_invocation.png)\n",
"\n",
Expand Down Expand Up @@ -367,4 +367,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
2 changes: 1 addition & 1 deletion docs/core_docs/docs/integrations/tools/google_calendar.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import IntegrationInstallTooltip from "@mdx_components/integration_install_toolt
<IntegrationInstallTooltip></IntegrationInstallTooltip>

```bash npm2yarn
npm install @langchain/openai @langchain/core
npm install @langchain/openai @langchain/core @langchain/community @langchain/langgraph
```

<CodeBlock language="typescript">{ToolExample}</CodeBlock>
Expand Down
2 changes: 1 addition & 1 deletion docs/core_docs/src/theme/VectorStoreTabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const ${vectorStoreVarName} = new MongoDBAtlasVectorSearch(embeddings, {
{
value: "PGVector",
label: "PGVector",
text: `import PGVectorStore from "@langchain/community/vectorstores/pgvector";
text: `import { PGVectorStore } from "@langchain/community/vectorstores/pgvector";

const ${vectorStoreVarName} = await PGVectorStore.initialize(embeddings, {})`,
dependencies: "@langchain/community",
Expand Down
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"ioredis": "^5.3.2",
"js-yaml": "^4.1.0",
"langchain": "workspace:*",
"langsmith": "^0.2.8",
"langsmith": ">=0.2.8 <0.4.0",
"mongodb": "^6.3.0",
"pg": "^8.11.0",
"pickleparser": "^0.2.1",
Expand Down
52 changes: 52 additions & 0 deletions examples/src/models/chat/integration_anthropic_pdf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { ChatAnthropic } from "@langchain/anthropic";

import * as fs from "fs";

export const run = async () => {
const llm = new ChatAnthropic({
model: "claude-3-5-sonnet-20240620", // Only claude-3-5-sonnet-20240620 , claude-3-5-sonnet-20241022 as of Jan 2025 support PDF documents as in base64
});

// PDF needs to be in Base64.
const getLocalFile = async (path: string) => {
const localFile = await fs.readFileSync(path);
const base64File = localFile.toString("base64");
return base64File;
};

// Or remotely
const getRemoteFile = async (url: string) => {
const response = await fetch(url);
const arrayBuffer = await response.arrayBuffer();
const base64File = Buffer.from(arrayBuffer).toString("base64");
return base64File;
};

const base64 = await getRemoteFile(
"https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf"
);

const prompt = "Summarise the contents of this PDF";

const response = await llm.invoke([
{
role: "user",
content: [
{
type: "text",
text: prompt,
},
{
type: "document",
source: {
media_type: "application/pdf",
type: "base64",
data: base64,
},
},
],
},
]);
console.log(response.content);
return response.content;
};
21 changes: 13 additions & 8 deletions examples/src/tools/google_calendar.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { initializeAgentExecutorWithOptions } from "langchain/agents";
import { OpenAI } from "@langchain/openai";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { ChatOpenAI } from "@langchain/openai";
import { Calculator } from "@langchain/community/tools/calculator";
import {
GoogleCalendarCreateTool,
GoogleCalendarViewTool,
} from "@langchain/community/tools/google_calendar";

export async function run() {
const model = new OpenAI({
const model = new ChatOpenAI({
temperature: 0,
apiKey: process.env.OPENAI_API_KEY,
model: "gpt-4o-mini",
});

const googleCalendarParams = {
Expand All @@ -31,22 +32,26 @@ export async function run() {
new GoogleCalendarViewTool(googleCalendarParams),
];

const calendarAgent = await initializeAgentExecutorWithOptions(tools, model, {
agentType: "zero-shot-react-description",
verbose: true,
const calendarAgent = createReactAgent({
llm: model,
tools,
});

const createInput = `Create a meeting with John Doe next Friday at 4pm - adding to the agenda of it the result of 99 + 99`;

const createResult = await calendarAgent.invoke({ input: createInput });
const createResult = await calendarAgent.invoke({
messages: [{ role: "user", content: createInput }],
});
// Create Result {
// output: 'A meeting with John Doe on 29th September at 4pm has been created and the result of 99 + 99 has been added to the agenda.'
// }
console.log("Create Result", createResult);

const viewInput = `What meetings do I have this week?`;

const viewResult = await calendarAgent.invoke({ input: viewInput });
const viewResult = await calendarAgent.invoke({
messages: [{ role: "user", content: viewInput }],
});
// View Result {
// output: "You have no meetings this week between 8am and 8pm."
// }
Expand Down
4 changes: 2 additions & 2 deletions langchain-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@langchain/core",
"version": "0.3.30",
"version": "0.3.32",
"description": "Core LangChain.js abstractions and schemas",
"type": "module",
"engines": {
Expand Down Expand Up @@ -38,7 +38,7 @@
"camelcase": "6",
"decamelize": "1.2.0",
"js-tiktoken": "^1.0.12",
"langsmith": "^0.2.8",
"langsmith": ">=0.2.8 <0.4.0",
"mustache": "^4.2.0",
"p-queue": "^6.6.2",
"p-retry": "4",
Expand Down
83 changes: 82 additions & 1 deletion langchain-core/src/messages/tests/message_utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import {
mergeMessageRuns,
trimMessages,
} from "../transformers.js";
import { AIMessage } from "../ai.js";
import { AIMessage, AIMessageChunk } from "../ai.js";
import { ChatMessage } from "../chat.js";
import { HumanMessage } from "../human.js";
import { SystemMessage } from "../system.js";
import { ToolMessage } from "../tool.js";
import { BaseMessage } from "../base.js";
import {
getBufferString,
Expand Down Expand Up @@ -187,6 +188,7 @@ describe("trimMessages can trim", () => {
defaultMsgSuffixLen;
}
}
console.log(count);
return count;
};

Expand All @@ -196,6 +198,84 @@ describe("trimMessages can trim", () => {
};
};

it("should not mutate messages if no trimming occurs with strategy last", async () => {
const trimmer = trimMessages({
maxTokens: 128000,
strategy: "last",
startOn: [HumanMessage],
endOn: [AIMessage, ToolMessage],
tokenCounter: () => 1,
});
const messages = [
new HumanMessage({
content: "Fetch the last 5 emails from Flora Testington's inbox.",
additional_kwargs: {},
response_metadata: {},
}),
new AIMessageChunk({
id: "chatcmpl-abcdefg",
content: "",
additional_kwargs: {
tool_calls: [
{
function: {
name: "getEmails",
arguments: JSON.stringify({
inboxName: "flora@foo.org",
amount: 5,
folder: "Inbox",
searchString: null,
from: null,
subject: null,
}),
},
id: "foobarbaz",
index: 0,
type: "function",
},
],
},
response_metadata: {
usage: {},
},
tool_calls: [
{
name: "getEmails",
args: {
inboxName: "flora@foo.org",
amount: 5,
folder: "Inbox",
searchString: null,
from: null,
subject: null,
},
id: "foobarbaz",
type: "tool_call",
},
],
tool_call_chunks: [
{
name: "getEmails",
args: '{"inboxName":"flora@foo.org","amount":5,"folder":"Inbox","searchString":null,"from":null,"subject":null,"cc":[],"bcc":[]}',
id: "foobarbaz",
index: 0,
type: "tool_call_chunk",
},
],
invalid_tool_calls: [],
}),
new ToolMessage({
content: "a whole bunch of emails!",
name: "getEmails",
additional_kwargs: {},
response_metadata: {},
tool_call_id: "foobarbaz",
}),
];
const trimmedMessages = await trimmer.invoke(messages);
expect(trimmedMessages).toEqual(messages);
});

it("First 30 tokens, not allowing partial messages", async () => {
const { messages, dummyTokenCounter } = messagesAndTokenCounterFactory();
const trimmedMessages = await trimMessages(messages, {
Expand Down Expand Up @@ -319,6 +399,7 @@ describe("trimMessages can trim", () => {

it("Last 30 tokens, including system message, allowing partial messages, end on HumanMessage", async () => {
const { messages, dummyTokenCounter } = messagesAndTokenCounterFactory();
console.log(messages);
const trimmedMessages = await trimMessages(messages, {
maxTokens: 30,
tokenCounter: dummyTokenCounter,
Expand Down
18 changes: 11 additions & 7 deletions langchain-core/src/messages/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ function _mergeMessageRuns(messages: BaseMessage[]): BaseMessage[] {
}
const merged: BaseMessage[] = [];
for (const msg of messages) {
const curr = msg; // Create a shallow copy of the message
const curr = msg;
const last = merged.pop();
if (!last) {
merged.push(curr);
Expand Down Expand Up @@ -861,20 +861,24 @@ async function _lastMaxTokens(
...rest
} = options;

// Create a copy of messages to avoid mutation
let messagesCopy = [...messages];

if (endOn) {
const endOnArr = Array.isArray(endOn) ? endOn : [endOn];
while (
messages &&
!_isMessageType(messages[messages.length - 1], endOnArr)
messagesCopy.length > 0 &&
!_isMessageType(messagesCopy[messagesCopy.length - 1], endOnArr)
) {
messages.pop();
messagesCopy = messagesCopy.slice(0, -1);
}
}

const swappedSystem = includeSystem && messages[0]._getType() === "system";
const swappedSystem =
includeSystem && messagesCopy[0]?._getType() === "system";
let reversed_ = swappedSystem
? messages.slice(0, 1).concat(messages.slice(1).reverse())
: messages.reverse();
? messagesCopy.slice(0, 1).concat(messagesCopy.slice(1).reverse())
: messagesCopy.reverse();

reversed_ = await _firstMaxTokens(reversed_, {
...rest,
Expand Down
4 changes: 2 additions & 2 deletions langchain/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "langchain",
"version": "0.3.11",
"version": "0.3.12",
"description": "Typescript bindings for langchain",
"type": "module",
"engines": {
Expand Down Expand Up @@ -530,7 +530,7 @@
"js-tiktoken": "^1.0.12",
"js-yaml": "^4.1.0",
"jsonpointer": "^5.0.1",
"langsmith": "^0.2.8",
"langsmith": ">=0.2.8 <0.4.0",
"openapi-types": "^12.1.3",
"p-retry": "4",
"uuid": "^10.0.0",
Expand Down
2 changes: 1 addition & 1 deletion langchain/src/chains/openai_moderation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface OpenAIModerationChainInput
* OpenAIModerationChainInput interface.
* @example
* ```typescript
* const moderation = new ChatOpenAIModerationChain({ throwError: true });
* const moderation = new OpenAIModerationChain({ throwError: true });
*
* const badString = "Bad naughty words from user";
*
Expand Down
2 changes: 1 addition & 1 deletion libs/langchain-anthropic/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@langchain/anthropic",
"version": "0.3.11",
"version": "0.3.12",
"description": "Anthropic integrations for LangChain.js",
"type": "module",
"engines": {
Expand Down
7 changes: 7 additions & 0 deletions libs/langchain-anthropic/src/utils/message_inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ function _formatContent(content: MessageContent) {
source,
...(cacheControl ? { cache_control: cacheControl } : {}),
};
} else if (contentPart.type === "document") {
// PDF
return {
type: "document",
source: contentPart.source,
...(cacheControl ? { cache_control: cacheControl } : {}),
};
} else if (
textTypes.find((t) => t === contentPart.type) &&
"text" in contentPart
Expand Down
2 changes: 1 addition & 1 deletion libs/langchain-azure-cosmosdb/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@langchain/azure-cosmosdb",
"version": "0.2.6",
"version": "0.2.7",
"description": "Azure CosmosDB integration for LangChain.js",
"type": "module",
"engines": {
Expand Down
Loading
Loading