Expand for full code snippet
Documents
into smaller chunks. This is useful both for indexing data and passing it into a model, as large chunks are harder to search over and won’t fit in a model’s finite context window.urllib
to load HTML from web URLs and BeautifulSoup
to
parse it to text. We can customize the HTML -> text parsing by passing
in parameters into the BeautifulSoup
parser via bs_kwargs
(see
BeautifulSoup
docs).
In this case only HTML tags with class “post-content”, “post-title”, or
“post-header” are relevant, so we’ll remove all others.
DocumentLoader
: Object that loads data from a source as list of Documents
.
Document
into chunks for embedding and
vector storage. This should help us retrieve only the most relevant parts
of the blog post at run time.
As in the semantic search tutorial, we use a
RecursiveCharacterTextSplitter,
which will recursively split the document using common separators like
new lines until each chunk is the appropriate size. This is the
recommended text splitter for generic text use cases.
TextSplitter
: Object that splits a list of Document
objects into smaller
chunks for storage and retrieval.
Embeddings
: Wrapper around a text embedding model, used for converting
text to embeddings.
VectorStore
: Wrapper around a vector database, used for storing and
querying embeddings.
query
argument, as in the above example. You can
force the LLM to specify additional search parameters by adding arguments— for example, a category:✅ Benefits | ⚠️ Drawbacks |
---|---|
Search only when needed – The LLM can handle greetings, follow-ups, and simple queries without triggering unnecessary searches. | Two inference calls – When a search is performed, it requires one call to generate the query and another to produce the final response. |
Contextual search queries – By treating search as a tool with a query input, the LLM crafts its own queries that incorporate conversational context. | Reduced control – The LLM may skip searches when they are actually needed, or issue extra searches when unnecessary. |
Multiple searches allowed – The LLM can execute several searches in support of a single user query. |
Returning source documents
create_agent
, we can easily
incorporate new features and go deeper: