-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow runnables to implement transform streaming (#2156)
* Adds support for transform streaming on runnables * Adds comment * Fix types * Adds EncodingOutputParser * Rename to BytesOutputParser
- Loading branch information
1 parent
1c1274d
commit ae9895f
Showing
6 changed files
with
227 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,15 @@ | ||
import { StringOutputParser } from "../schema/output_parser.js"; | ||
import { BaseOutputParser } from "../schema/output_parser.js"; | ||
|
||
/** @deprecated Use StringOutputParser instead */ | ||
export class NoOpOutputParser extends StringOutputParser {} | ||
export class NoOpOutputParser extends BaseOutputParser<string> { | ||
lc_namespace = ["langchain", "output_parsers", "default"]; | ||
|
||
lc_serializable = true; | ||
|
||
parse(text: string): Promise<string> { | ||
return Promise.resolve(text); | ||
} | ||
|
||
getFormatInstructions(): string { | ||
return ""; | ||
} | ||
} |
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,35 @@ | ||
/* eslint-disable no-promise-executor-return */ | ||
|
||
import { test } from "@jest/globals"; | ||
import { LLM } from "../../llms/base.js"; | ||
import { GenerationChunk } from "../index.js"; | ||
import { BytesOutputParser } from "../output_parser.js"; | ||
|
||
class FakeStreamingLLM extends LLM { | ||
_llmType() { | ||
return "fake"; | ||
} | ||
|
||
async _call(prompt: string): Promise<string> { | ||
return prompt; | ||
} | ||
|
||
async *_streamResponseChunks(input: string) { | ||
for (const c of input) { | ||
await new Promise((resolve) => setTimeout(resolve, 50)); | ||
yield { text: c, generationInfo: {} } as GenerationChunk; | ||
} | ||
} | ||
} | ||
|
||
test("BytesOutputParser", async () => { | ||
const llm = new FakeStreamingLLM({}); | ||
const stream = await llm.pipe(new BytesOutputParser()).stream("Hi there!"); | ||
const chunks = []; | ||
const decoder = new TextDecoder(); | ||
for await (const chunk of stream) { | ||
chunks.push(decoder.decode(chunk)); | ||
} | ||
expect(chunks.length).toEqual("Hi there!".length); | ||
expect(chunks.join("")).toEqual("Hi there!"); | ||
}); |
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
ae9895f
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 – ./
js.langchain.com
langchainjs-docs-ruddy.vercel.app
langchainjs-docs-langchain.vercel.app
langchainjs-docs-git-main-langchain.vercel.app