-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs[patch]: Use an example file for docs code examples (#3567)
* docs[patch]: Use an example file for docs code examples * cr * cr * cr
- Loading branch information
1 parent
f67a293
commit c0bb153
Showing
2 changed files
with
52 additions
and
22 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
45 changes: 45 additions & 0 deletions
45
examples/src/guides/expression_language/runnable_history_constructor_config.ts
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,45 @@ | ||
import { ChatOpenAI } from "langchain/chat_models/openai"; | ||
import { ChatMessageHistory } from "langchain/stores/message/in_memory"; | ||
import { ChatPromptTemplate, MessagesPlaceholder } from "langchain/prompts"; | ||
import { | ||
RunnableConfig, | ||
RunnableWithMessageHistory, | ||
} from "langchain/runnables"; | ||
|
||
// Construct your runnable with a prompt and chat model. | ||
const model = new ChatOpenAI({}); | ||
const prompt = ChatPromptTemplate.fromMessages([ | ||
["ai", "You are a helpful assistant"], | ||
new MessagesPlaceholder("history"), | ||
["human", "{input}"], | ||
]); | ||
const runnable = prompt.pipe(model); | ||
const messageHistory = new ChatMessageHistory(); | ||
|
||
// Define a RunnableConfig object, with a `configurable` key. | ||
const config: RunnableConfig = { configurable: { sessionId: "1" } }; | ||
const withHistory = new RunnableWithMessageHistory({ | ||
runnable, | ||
getMessageHistory: (_sessionId: string) => messageHistory, | ||
inputMessagesKey: "input", | ||
historyMessagesKey: "history", | ||
// Passing config through here instead of through the invoke method | ||
config, | ||
}); | ||
|
||
const output = await withHistory.invoke({ | ||
input: "Hello there, I'm Archibald!", | ||
}); | ||
console.log("output:", output); | ||
/** | ||
output: AIMessage { | ||
lc_namespace: [ 'langchain_core', 'messages' ], | ||
content: 'Hello, Archibald! How can I assist you today?', | ||
additional_kwargs: { function_call: undefined, tool_calls: undefined } | ||
} | ||
*/ | ||
|
||
/** | ||
* You can see the LangSmith traces here: | ||
* output @link https://smith.langchain.com/public/ee264a77-b767-4b5a-8573-efcbebaa5c80/r | ||
*/ |
c0bb153
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
langchainjs-api-refs β ./docs/api_refs
langchainjs-api-refs-langchain.vercel.app
langchainjs-api-docs.vercel.app
api.js.langchain.com
langchainjs-api-refs-git-main-langchain.vercel.app
c0bb153
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
langchainjs-docs β ./docs/core_docs/
langchainjs-docs-langchain.vercel.app
langchainjs-docs-ruddy.vercel.app
js.langchain.com
langchainjs-docs-git-main-langchain.vercel.app