mirror of
https://github.com/langchain-ai/learning-langchain.git
synced 2026-07-01 16:06:32 -04:00
feat: more ch2 examples
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
import { TextLoader } from 'langchain/document_loaders/fs/text';
|
||||
import { RecursiveCharacterTextSplitter } from '@langchain/textsplitters';
|
||||
|
||||
const loader = new TextLoader('./test.txt'); // or any other loader
|
||||
const docs = await loader.load();
|
||||
|
||||
const splitter = new RecursiveCharacterTextSplitter({
|
||||
chunkSize: 1000,
|
||||
chunkOverlap: 200,
|
||||
});
|
||||
|
||||
const splittedDocs = await splitter.splitDocuments(docs);
|
||||
|
||||
console.log(splittedDocs);
|
||||
@@ -0,0 +1,12 @@
|
||||
import { RecursiveCharacterTextSplitter } from '@langchain/textsplitters';
|
||||
|
||||
const PYTHON_CODE = ` def hello_world(): print("Hello, World!") # Call the function hello_world() `;
|
||||
|
||||
const pythonSplitter = RecursiveCharacterTextSplitter.fromLanguage('python', {
|
||||
chunkSize: 50,
|
||||
chunkOverlap: 0,
|
||||
});
|
||||
|
||||
const pythonDocs = await pythonSplitter.createDocuments([PYTHON_CODE]);
|
||||
|
||||
console.log(pythonDocs);
|
||||
@@ -0,0 +1,15 @@
|
||||
import { RecursiveCharacterTextSplitter } from '@langchain/textsplitters';
|
||||
|
||||
const markdownText = ` # 🦜🔗 LangChain ⚡ Building applications with LLMs through composability ⚡ ## Quick Install \`\`\`bash pip install langchain \`\`\` As an open source project in a rapidly developing field, we are extremely open to contributions. `;
|
||||
|
||||
const mdSplitter = RecursiveCharacterTextSplitter.fromLanguage('markdown', {
|
||||
chunkSize: 60,
|
||||
chunkOverlap: 0,
|
||||
});
|
||||
|
||||
const mdDocs = await mdSplitter.createDocuments(
|
||||
[markdownText],
|
||||
[{ source: 'https://www.langchain.com' }]
|
||||
);
|
||||
|
||||
console.log(mdDocs);
|
||||
@@ -0,0 +1,12 @@
|
||||
import { OpenAIEmbeddings } from '@langchain/openai';
|
||||
|
||||
const model = new OpenAIEmbeddings();
|
||||
const embeddings = await model.embedDocuments([
|
||||
'Hi there!',
|
||||
'Oh, hello!',
|
||||
"What's your name?",
|
||||
'My friends call me World',
|
||||
'Hello World!',
|
||||
]);
|
||||
|
||||
console.log(embeddings);
|
||||
@@ -0,0 +1,21 @@
|
||||
import { TextLoader } from 'langchain/document_loaders/fs/text';
|
||||
import { RecursiveCharacterTextSplitter } from '@langchain/textsplitters';
|
||||
import { OpenAIEmbeddings } from '@langchain/openai';
|
||||
|
||||
const loader = new TextLoader('./test.txt');
|
||||
const docs = await loader.load();
|
||||
|
||||
// Split the document
|
||||
const splitter = new RecursiveCharacterTextSplitter({
|
||||
chunkSize: 1000,
|
||||
chunkOverlap: 200,
|
||||
});
|
||||
const chunks = await splitter.splitDocuments(docs);
|
||||
|
||||
console.log(chunks);
|
||||
|
||||
// Generate embeddings
|
||||
const model = new OpenAIEmbeddings();
|
||||
const embeddings = await model.embedDocuments(chunks.map((c) => c.pageContent));
|
||||
|
||||
console.log(embeddings);
|
||||
@@ -0,0 +1,9 @@
|
||||
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
||||
|
||||
from langchain_community.document_loaders import TextLoader
|
||||
|
||||
loader = TextLoader('./test.txt')
|
||||
docs = loader.load()
|
||||
|
||||
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
||||
splitted_docs = splitter.split_documents(docs)
|
||||
@@ -0,0 +1,14 @@
|
||||
from langchain_text_splitters import (
|
||||
Language,
|
||||
RecursiveCharacterTextSplitter,
|
||||
)
|
||||
|
||||
PYTHON_CODE = """ def hello_world(): print("Hello, World!") # Call the function hello_world() """
|
||||
|
||||
python_splitter = RecursiveCharacterTextSplitter.from_language(
|
||||
language=Language.PYTHON, chunk_size=50, chunk_overlap=0
|
||||
)
|
||||
|
||||
python_docs = python_splitter.create_documents([PYTHON_CODE])
|
||||
|
||||
print(python_docs)
|
||||
@@ -0,0 +1,14 @@
|
||||
from langchain_text_splitters import (
|
||||
Language,
|
||||
RecursiveCharacterTextSplitter,
|
||||
)
|
||||
markdown_text = """ # 🦜🔗 LangChain ⚡ Building applications with LLMs through composability ⚡ ## Quick Install ```bash pip install langchain ``` As an open source project in a rapidly developing field, we are extremely open to contributions. """
|
||||
|
||||
md_splitter = RecursiveCharacterTextSplitter.from_language(
|
||||
language=Language.MARKDOWN, chunk_size=60, chunk_overlap=0
|
||||
)
|
||||
|
||||
md_docs = md_splitter.create_documents(
|
||||
[markdown_text], [{"source": "https://www.langchain.com"}])
|
||||
|
||||
print(md_docs)
|
||||
@@ -0,0 +1,12 @@
|
||||
from langchain_openai import OpenAIEmbeddings
|
||||
|
||||
model = OpenAIEmbeddings()
|
||||
embeddings = model.embed_documents([
|
||||
"Hi there!",
|
||||
"Oh, hello!",
|
||||
"What's your name?",
|
||||
"My friends call me World",
|
||||
"Hello World!"
|
||||
])
|
||||
|
||||
print(embeddings)
|
||||
@@ -0,0 +1,19 @@
|
||||
from langchain_community.document_loaders import TextLoader
|
||||
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
||||
from langchain_openai import OpenAIEmbeddings
|
||||
|
||||
# Load the document
|
||||
loader = TextLoader("./test.txt")
|
||||
doc = loader.load()
|
||||
|
||||
# Split the document
|
||||
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
||||
chunks = splitter.split_documents(doc)
|
||||
|
||||
# Generate embeddings
|
||||
embeddings_model = OpenAIEmbeddings()
|
||||
embeddings = embeddings_model.embed_documents(
|
||||
chunk.page_content for chunk in chunks
|
||||
)
|
||||
|
||||
print(embeddings)
|
||||
@@ -1 +1,7 @@
|
||||
Test doc
|
||||
Life in ancient Greece was centered around the polis, or city-state, which served as the heart of social, political and economic activity. Citizens would gather in the agora - the central marketplace and meeting place - to trade goods, discuss politics, and engage in philosophical debates. The streets would buzz with activity as merchants sold their wares, from fresh fish and olive oil to handcrafted pottery and textiles.
|
||||
|
||||
Education was highly valued, though primarily available to male citizens. Young boys would attend schools to study reading, writing, mathematics, music, and physical education. The gymnasium was an important institution where men would exercise, compete in athletics, and engage in intellectual discussions. Meanwhile, most women managed households and raised children, though their roles varied between different city-states.
|
||||
|
||||
Religion permeated every aspect of daily life in ancient Greece. Citizens regularly participated in festivals honoring various gods and goddesses, with elaborate processions, sacrifices, athletic competitions, and theatrical performances. The temples, with their magnificent marble columns and sculptures, stood as the most prominent buildings in the city. Priests and priestesses held important roles as intermediaries between the people and the divine.
|
||||
|
||||
The Greeks were also great innovators in art, architecture, and engineering. Skilled craftsmen created beautiful pottery decorated with scenes from mythology and daily life. Architects designed impressive public buildings using precise mathematical principles. The ancient Greeks developed advanced water management systems, including aqueducts and public fountains, which improved urban living conditions significantly.
|
||||
|
||||
Reference in New Issue
Block a user