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.
langchain[patch]: Allow headers to be passed in remote runnable reque…
…sts, docs (langchain-ai#4107) * Allow headers to be passed in remote runnable requests, docs * Fix broken link
- Loading branch information
1 parent
d84cfb3
commit 9ed1b27
Showing
5 changed files
with
121 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# LangServe | ||
|
||
[LangServe](https://python.langchain.com/docs/langserve) is a Python framework that helps developers deploy LangChain [runnables and chains](/docs/expression_language/) | ||
as REST APIs. | ||
|
||
If you have a deployed LangServe route, you can use the [RemoteRunnable](https://api.js.langchain.com/classes/langchain_runnables_remote.RemoteRunnable.html) class to interact | ||
with it as if it were a local chain. This allows you to more easily call hosted LangServe instances from JavaScript environments (like in the browser on the frontend). | ||
|
||
You'll need to install or package LangChain into your frontend: | ||
|
||
```bash npm2yarn | ||
npm install langchain | ||
``` | ||
|
||
## Usage | ||
|
||
Then, you can use any of the [supported LCEL interface methods](/docs/expression_language/interface). | ||
Here's an example of how this looks: | ||
|
||
import CodeBlock from "@theme/CodeBlock"; | ||
import Example from "@examples/ecosystem/langsmith.ts"; | ||
|
||
<CodeBlock language="typescript">{Example}</CodeBlock> | ||
|
||
[`streamLog`](/docs/expression_language/interface) is a lower level method for streaming chain intermediate steps as partial JSONPatch chunks. | ||
This method allows for a few extra options as well to only include or exclude certain named steps. | ||
|
||
`@langchain/core` also provides an `applyPatch` utility for aggregating these chunks into a full output: | ||
|
||
import StreamLogExample from "@examples/ecosystem/langsmith_stream_log.ts"; | ||
|
||
<CodeBlock language="typescript">{StreamLogExample}</CodeBlock> | ||
|
||
### Configuration | ||
|
||
You can also pass in options for headers and timeouts into the constructor. These will be used when making incoming requests: | ||
|
||
import OptionsExample from "@examples/ecosystem/langsmith_options.ts"; | ||
|
||
<CodeBlock language="typescript">{OptionsExample}</CodeBlock> |
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,21 @@ | ||
import { RemoteRunnable } from "langchain/runnables/remote"; | ||
|
||
const remoteChain = new RemoteRunnable({ | ||
url: "https://your_hostname.com/path", | ||
}); | ||
|
||
const result = await remoteChain.invoke({ | ||
param1: "param1", | ||
param2: "param2", | ||
}); | ||
|
||
console.log(result); | ||
|
||
const stream = await remoteChain.stream({ | ||
param1: "param1", | ||
param2: "param2", | ||
}); | ||
|
||
for await (const chunk of stream) { | ||
console.log(chunk); | ||
} |
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,18 @@ | ||
import { RemoteRunnable } from "langchain/runnables/remote"; | ||
|
||
const remoteChain = new RemoteRunnable({ | ||
url: "https://your_hostname.com/path", | ||
options: { | ||
timeout: 10000, | ||
headers: { | ||
Authorization: "Bearer YOUR_TOKEN", | ||
}, | ||
}, | ||
}); | ||
|
||
const result = await remoteChain.invoke({ | ||
param1: "param1", | ||
param2: "param2", | ||
}); | ||
|
||
console.log(result); |
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,40 @@ | ||
import { RemoteRunnable } from "langchain/runnables/remote"; | ||
import { applyPatch } from "@langchain/core/utils/json_patch"; | ||
|
||
const remoteChain = new RemoteRunnable({ | ||
url: "https://your_hostname.com/path", | ||
}); | ||
|
||
const logStream = await remoteChain.streamLog( | ||
{ | ||
param1: "param1", | ||
param2: "param2", | ||
}, | ||
// LangChain runnable config properties | ||
{ | ||
configurable: { | ||
llm: "some_property", | ||
}, | ||
metadata: { | ||
conversation_id: "other_metadata", | ||
}, | ||
}, | ||
// Optional additional streamLog properties for filtering outputs | ||
{ | ||
includeNames: [], | ||
includeTags: [], | ||
includeTypes: [], | ||
excludeNames: [], | ||
excludeTags: [], | ||
excludeTypes: [], | ||
} | ||
); | ||
|
||
let streamedResponse: Record<string, any> = {}; | ||
|
||
for await (const chunk of logStream) { | ||
console.log(chunk); | ||
streamedResponse = applyPatch(streamedResponse, chunk.ops).newDocument; | ||
} | ||
|
||
console.log(streamedResponse); |
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