.env
file with WRITER_API_KEY
set to your Writer API key:
langchain-writer
package provides several key components:
ChatWriter
for text generationGraphTool
for Knowledge Graph integrationNoCodeAppTool
for no-code applicationsLLMTool
for specialized model delegationPDFParser
for parsing PDFs and WriterTextSplitter
for intelligent text splittingChatWriter
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.
ChatWriter
to ask the Palmyra X5 model to explain what LangChain is in plain terms.
async for
loop and the astream
method:
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:
ChatWriter
supports tool calling, which allows the model to use external functions to enhance its capabilities. Tool calling is available with Palmyra X4 and later.
@tool
decorator.bind_tools
method.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.
NoCodeAppTool
is a specialized tool that enables access to Writer’s no-code applications as LLM tools. This tool allows language models to interact with pre-built applications to enhance their responses.
The tool is designed as a standard “function” type tool and requires manual execution. Unlike the GraphTool
and LLMTool
which are executed remotely by Writer’s servers, you’ll need to handle the execution of the NoCodeAppTool
in your code.
When initializing the tool, you must provide an app_id
(either directly or using an environment variable) that corresponds to a no-code application created in your Writer account. The tool automatically retrieves the input parameters for the app during initialization, so you don’t need to specify them manually.
You can customize the tool’s name (default is “No-code application”) and description (default is “No-code application powered by Palmyra”) to provide better context to the model. The API key is typically read from environment variables but can also be provided directly.
When working with the tool, remember that all required inputs must be provided when invoking it, or a ValueError
will be raised. Input values can be either strings or lists of strings, depending on what the no-code application expects.
This tool is particularly useful for generating content, performing specialized data transformations, creating custom outputs based on user inputs, integrating with domain-specific applications, enhancing responses with formatted content, or leveraging pre-built applications for common tasks.
NoCodeAppTool
:
LLMTool
is a specialized tool that enables delegation to specific Writer models. This tool allows language models to delegate calls to different Palmyra model types to enhance their responses.
Unlike most LangChain tools that use the standard “function” type, the LLMTool
has a type of llm
and is designed specifically for use within the Writer environment. Due to its remote execution nature, this tool doesn’t support direct invocation through the _run
method—attempting to call this method will raise a NotImplementedError
.
When initializing the tool, you can specify which Palmyra model to delegate to using the model_name
parameter. Options include general-purpose models like palmyra-x5
, as well as domain-specific models such as palmyra-med
for medical content, palmyra-fin
for financial analysis, and palmyra-creative
for creative content generation. The default model is palmyra-x5
. The description
parameter plays a crucial role in helping the model understand when to use this tool.
When the model uses the LLM tool, the execution happens remotely on Writer’s servers, and the response includes additional data in the additional_kwargs["llm_data"]
field that you can access in your application.
This tool is particularly valuable when you need specialized domain knowledge. For example, you might delegate medical questions to the palmyra-med
model, use palmyra-creative
for generating creative content, or leverage palmyra-fin
for financial analysis.
LLMTool
:
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.
PDFParser
:
PDFParser
supports different output formats:
text
: Plain text extractionmarkdown
: Structured markdown with preserved formattingWriterTextSplitter
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.
WriterTextSplitter
:
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 qualityChatWriter
for text generationGraphTool
for Knowledge Graph integrationNoCodeAppTool
for no-code applicationsLLMTool
for specialized model delegationPDFParser
for parsing PDFs and WriterTextSplitter
for intelligent text splitting