-
Notifications
You must be signed in to change notification settings - Fork 90
feat(js): retrieval/integration doc translations #692
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
925f83b
feat(js): retrieval/integration doc translations
hntrl 769bb92
feat: collapse text splitters
hntrl c2cc7e7
feat: knowledge base translations
hntrl 6a63708
chore: rollback js install snippet
hntrl 5f7cc5d
feat: js translations + middleware for rag docs
hntrl ad39b30
feat: agentic rag doc translations
hntrl aeb2198
cr
hntrl 97b78ee
Merge branch 'main' into hunter/js-retrieval
lnhsingh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
145 changes: 145 additions & 0 deletions
145
src/oss/integrations/splitters/character_text_splitter.mdx
This file contains hidden or 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,145 @@ | ||
--- | ||
title: Splitting by character | ||
--- | ||
|
||
Character-based splitting is the simplest approach to text splitting. It divides text using a specified character sequence (default: `"\n\n"`), with chunk length measured by the number of characters. | ||
|
||
**Key points**: | ||
1. **How text is split**: by a given character separator. | ||
2. **How chunk size is measured**: by character count. | ||
|
||
:::python | ||
You can choose between: | ||
- `.split_text` — returns plain string chunks. | ||
- `.create_documents` — returns LangChain @[Document] objects, useful when metadata needs to be preserved for downstream tasks. | ||
::: | ||
:::js | ||
You can choose between: | ||
- `.splitText` — returns plain string chunks. | ||
- `.createDocuments` — returns LangChain @[Document] objects, useful when metadata needs to be preserved for downstream tasks. | ||
::: | ||
|
||
:::python | ||
```python | ||
%pip install -qU langchain-text-splitters | ||
``` | ||
::: | ||
:::js | ||
<CodeGroup> | ||
```bash npm | ||
npm install @langchain/textsplitters | ||
``` | ||
|
||
```bash pnpm | ||
pnpm install @langchain/textsplitters | ||
``` | ||
|
||
```bash yarn | ||
yarn add @langchain/textsplitters | ||
``` | ||
|
||
```bash bun | ||
bun add @langchain/textsplitters | ||
``` | ||
</CodeGroup> | ||
::: | ||
|
||
:::python | ||
```python | ||
from langchain_text_splitters import CharacterTextSplitter | ||
|
||
# Load an example document | ||
with open("state_of_the_union.txt") as f: | ||
state_of_the_union = f.read() | ||
|
||
text_splitter = CharacterTextSplitter( | ||
separator="\n\n", | ||
chunk_size=1000, | ||
chunk_overlap=200, | ||
length_function=len, | ||
is_separator_regex=False, | ||
) | ||
texts = text_splitter.create_documents([state_of_the_union]) | ||
print(texts[0]) | ||
``` | ||
```output | ||
page_content='Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \n\nLast year COVID-19 kept us apart. This year we are finally together again. \n\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \n\nWith a duty to one another to the American people to the Constitution. \n\nAnd with an unwavering resolve that freedom will always triumph over tyranny. \n\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \n\nHe thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \n\nHe met the Ukrainian people. \n\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.' | ||
``` | ||
::: | ||
:::js | ||
```ts | ||
import { CharacterTextSplitter } from "@langchain/textsplitters"; | ||
import { readFileSync } from "fs"; | ||
|
||
// Example: read a long document | ||
const stateOfTheUnion = readFileSync("state_of_the_union.txt", "utf8"); | ||
|
||
const splitter = new CharacterTextSplitter({ | ||
separator: "\n\n", | ||
chunkSize: 1000, | ||
chunkOverlap: 200, | ||
}); | ||
const texts = splitter.createDocuments([{ pageContent: stateOfTheUnion }]); | ||
console.log(texts[0]); | ||
``` | ||
```output | ||
Document { | ||
pageContent: 'Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \n\nLast year COVID-19 kept us apart. This year we are finally together again. \n\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \n\nWith a duty to one another to the American people to the Constitution. \n\nAnd with an unwavering resolve that freedom will always triumph over tyranny. \n\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \n\nHe thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \n\nHe met the Ukrainian people. \n\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.' | ||
} | ||
``` | ||
::: | ||
|
||
:::python | ||
Use `.create_documents` to propagate metadata associated with each document to the output chunks: | ||
|
||
```python | ||
metadatas = [{"document": 1}, {"document": 2}] | ||
documents = text_splitter.create_documents( | ||
[state_of_the_union, state_of_the_union], metadatas=metadatas | ||
) | ||
print(documents[0]) | ||
``` | ||
```output | ||
page_content='Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \n\nLast year COVID-19 kept us apart. This year we are finally together again. \n\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \n\nWith a duty to one another to the American people to the Constitution. \n\nAnd with an unwavering resolve that freedom will always triumph over tyranny. \n\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \n\nHe thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \n\nHe met the Ukrainian people. \n\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.' metadata={'document': 1} | ||
``` | ||
::: | ||
:::js | ||
Use `.createDocuments` to propagate metadata associated with each document to the output chunks: | ||
|
||
```ts | ||
const metadatas = [{"document": 1}, {"document": 2}] | ||
const documents = splitter.createDocuments( | ||
[{ pageContent: stateOfTheUnion }, { pageContent: stateOfTheUnion }], | ||
{ metadatas: metadatas } | ||
); | ||
console.log(documents[0]); | ||
``` | ||
```output | ||
Document { | ||
pageContent: 'Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \n\nLast year COVID-19 kept us apart. This year we are finally together again. \n\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \n\nWith a duty to one another to the American people to the Constitution. \n\nAnd with an unwavering resolve that freedom will always triumph over tyranny. \n\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \n\nHe thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \n\nHe met the Ukrainian people. \n\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.', | ||
metadata: {'document': 1} | ||
} | ||
``` | ||
::: | ||
|
||
:::python | ||
Use `.split_text` to obtain the string content directly: | ||
|
||
```python | ||
text_splitter.split_text(state_of_the_union)[0] | ||
``` | ||
```output | ||
'Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \n\nLast year COVID-19 kept us apart. This year we are finally together again. \n\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \n\nWith a duty to one another to the American people to the Constitution. \n\nAnd with an unwavering resolve that freedom will always triumph over tyranny. \n\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \n\nHe thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \n\nHe met the Ukrainian people. \n\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.' | ||
``` | ||
|
||
::: | ||
:::js | ||
Use `.splitText` to obtain the string content directly: | ||
|
||
```ts | ||
splitter.splitText(stateOfTheUnion)[0] | ||
``` | ||
```output | ||
'Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \n\nLast year COVID-19 kept us apart. This year we are finally together again. \n\nTonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \n\nWith a duty to one another to the American people to the Constitution. \n\nAnd with an unwavering resolve that freedom will always triumph over tyranny. \n\nSix days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \n\nHe thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \n\nHe met the Ukrainian people. \n\nFrom President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.' | ||
``` | ||
::: |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Ideally, we'd have output/examples that are non-political and not American-centric to make our docs more accessible to a wider audience, but I think
stateOfTheUnion
was an existing example already. Just flagging - we can update this example laterThere 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.
yeah these are brought over