forked from langchain-ai/langchainjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: chat deployment implementation (#39)
* Rename auth method in docs * fix(core): Fix trim messages mutation bug (langchain-ai#7547) * release(core): 0.3.31 (langchain-ai#7548) * fix(community): Updated Embeddings URL (langchain-ai#7545) * fix(community): make sure guardrailConfig can be added even with anthropic models (langchain-ai#7542) * docs: Fix PGVectorStore import in install dependencies (TypeScript) example (langchain-ai#7533) * fix(community): Airtable url (langchain-ai#7532) * docs: Fix typo in OpenAIModerationChain example (langchain-ai#7528) * docs: Resolves langchain-ai#7483, resolves langchain-ai#7274 (langchain-ai#7505) Co-authored-by: jacoblee93 <[email protected]> * docs: Rename auth method in IBM docs (langchain-ai#7524) * docs: correct misspelling (langchain-ai#7522) Co-authored-by: jacoblee93 <[email protected]> * release(community): 0.3.25 (langchain-ai#7549) * feat(azure-cosmosdb): add session context for a user mongodb (langchain-ai#7436) Co-authored-by: jacoblee93 <[email protected]> * release(azure-cosmosdb): 0.2.7 (langchain-ai#7550) * fix(ci): Fix build (langchain-ai#7551) * feat(anthropic): Add Anthropic PDF support (document type) in invoke (langchain-ai#7496) Co-authored-by: jacoblee93 <[email protected]> * release(anthropic): 0.3.12 (langchain-ai#7552) * chore(core,langchain,community): Relax langsmith deps (langchain-ai#7556) * release(community): 0.3.26 (langchain-ai#7557) * release(core): 0.3.32 (langchain-ai#7558) * Release 0.3.12 (langchain-ai#7559) * Add deployment chat to chat class * Upadate Watsonx sdk * Rework interfaces in llms as well * Bump watsonx-ai sdk version * Remove unused code * Add fake auth --------- Co-authored-by: Jacob Lee <[email protected]> Co-authored-by: Jacky Chen <[email protected]> Co-authored-by: Mohamed Belhadj <[email protected]> Co-authored-by: Brian Ploetz <[email protected]> Co-authored-by: Eduard-Constantin Ibinceanu <[email protected]> Co-authored-by: Jonathan V <[email protected]> Co-authored-by: ucev <[email protected]> Co-authored-by: crisjy <[email protected]> Co-authored-by: Adham Badr <[email protected]>
- Loading branch information
1 parent
464e56b
commit 82a239e
Showing
33 changed files
with
657 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -187,6 +188,7 @@ describe("trimMessages can trim", () => { | |
defaultMsgSuffixLen; | ||
} | ||
} | ||
console.log(count); | ||
return count; | ||
}; | ||
|
||
|
@@ -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: "[email protected]", | ||
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: "[email protected]", | ||
amount: 5, | ||
folder: "Inbox", | ||
searchString: null, | ||
from: null, | ||
subject: null, | ||
}, | ||
id: "foobarbaz", | ||
type: "tool_call", | ||
}, | ||
], | ||
tool_call_chunks: [ | ||
{ | ||
name: "getEmails", | ||
args: '{"inboxName":"[email protected]","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, { | ||
|
@@ -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, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.