Using Writer with LangChain
The Writer LangChain integration allows you to leverage Writer’s capabilities within the LangChain ecosystem, making it easy to build sophisticated AI applications. In this tutorial, you’ll explore each component of the integration and understand how they work.
Prerequisites
Before you begin, make sure you have:
- Python 3.11 or higher installed
- A Writer AI Studio account and API key (you can use the API Quickstart to get one)
- Basic familiarity with Python and LangChain concepts
Installation and setup
First, install the necessary packages:
Next, create a .env
file with WRITER_API_KEY
set to your Writer API key:
Components of the Writer LangChain integration
The langchain-writer
package provides several key components:
ChatWriter
for text generation- Tool calling capabilities, including
GraphTool
for Knowledge Graph integration - Additional tools like
PDFParser
for parsing PDFs andWriterTextSplitter
for intelligent text splitting
ChatWriter
ChatWriter
is a LangChain chat model that provides access to Writer’s AI capabilities for text generation. It supports streaming, non-streaming, batching, and asynchronous operations. You can use any of the Palmyra chat models available in AI Studio.
See the full documentation for ChatWriter
to learn more about the available parameters.
Usage
This example uses ChatWriter
to ask the Palmyra X 004 model to explain what LangChain is in plain terms.
Streaming
Streaming allows you to receive the generated text in chunks as it’s being produced. This example shows how to stream a response from the Palmyra X 004 model using synchronous streaming:
You can also use async streaming with the async for
loop and the astream
method:
Batch processing
You can batch process multiple prompts for efficient processing. The following example batches three individual LLM invocations and runs them in parallel:
Note that batch
returns results in the same order as the inputs. You can use batch_as_completed
to return results as they complete. Results may arrive out of order, but each includes the input index for matching.
You can also optionally set the max_concurrency
parameter to control the number of concurrent requests, which can be useful when you want to limit the number of parallel calls to prevent overloading a server or API:
See the LangChain documentation on parallel execution for more information.
Tool calling
ChatWriter
supports tool calling, which allows the model to use external functions to enhance its capabilities. Tool calling is available with Palmyra X 004 and later.
Tool calling basics
To use tool calling, follow these steps:
- Define a function that will be called by the model and decorate it with the
@tool
decorator. - Bind the tool to the chat model using the
bind_tools
method. - Use the tool in a chat and append the response to the messages list.
- Execute the tool call with the arguments given by the model and append the response to the messages list.
- Invoke the chat model with the updated messages list to receive the final response.
Here’s an example of how to use tool calling:
GraphTool
GraphTool
is a LangChain tool that allows you to retrieve information from a Knowledge Graph to enhance its responses. The tool executes remotely, so you simply need to provide the IDs of the Knowledge Graph you want to use. For more details on the built-in Knowledge Graph chat tool in Writer, see the Knowledge Graph chat support guide.
Usage
Additional tools
PDFParser
PDFParser
is a document loader that uses Writer’s PDF parsing capabilities to extract text from PDF documents. It converts PDFs into LangChain Document objects that can be used in your applications. For more details on the underlying API, see the PDF parser API reference.
Usage
Here’s an example of how to use the PDFParser
:
Output formats
The PDFParser
supports different output formats:
text
: Plain text extractionmarkdown
: Structured markdown with preserved formatting
WriterTextSplitter
WriterTextSplitter
is a text splitter that uses Writer’s context-aware splitting capabilities to divide documents into semantically meaningful chunks. This is particularly useful for preparing documents for retrieval systems. For more details on the underlying API, see the context-aware splitting tool API reference.
Usage
Here’s an example of how to use the WriterTextSplitter
:
Splitting strategies
The WriterTextSplitter
supports different splitting strategies:
llm_split
: Uses language model for precise semantic splittingfast_split
: Uses heuristic-based approach for quick splittinghybrid_split
: Combines both approaches for a balance of speed and quality
Conclusion
In this tutorial, you’ve explored the LangChain integration with Writer, covering each of its components:
ChatWriter
for text generation- Tool calling capabilities, including
GraphTool
for Knowledge Graph integration - Additional tools like
PDFParser
for parsing PDFs andWriterTextSplitter
for intelligent text splitting
This integration provides a powerful foundation for building AI applications with Writer and LangChain. Check out the package README and documentation, as well as the LangChain Documentation for more information on how to use LangChain with Writer.
Was this page helpful?