promptpack-langchain
The promptpack-langchain package provides seamless integration between PromptPacks and LangChain. It converts PromptPack definitions into LangChain-compatible prompt templates and tools.
Installation
Section titled “Installation”pip install promptpack-langchainThis will also install the base promptpack library as a dependency.
Features
Section titled “Features”- Convert PromptPacks to LangChain ChatPromptTemplate
- Convert PromptPack tools to LangChain tool definitions
- Support for multimodal content (images)
- Variable validation and type coercion
- Compatible with LangChain agents and chains
PromptPackTemplate
Section titled “PromptPackTemplate”Create LangChain prompt templates from PromptPacks:
from promptpack import parse_promptpackfrom promptpack_langchain import PromptPackTemplate
pack = parse_promptpack("pack.json")
# Create template for a specific prompttemplate = PromptPackTemplate.from_promptpack(pack, "support")
# Format messagesmessages = template.format_messages( role="support agent", company="Acme Inc.")
# Use with LangChain LLMfrom langchain_openai import ChatOpenAIllm = ChatOpenAI()response = llm.invoke(messages)Accessing the Underlying Template
Section titled “Accessing the Underlying Template”# Get the LangChain ChatPromptTemplatechat_template = template.chat_template
# Get input variablesprint(template.input_variables) # ['role', 'company']Tool Conversion
Section titled “Tool Conversion”Convert PromptPack tools to LangChain format:
from promptpack import parse_promptpackfrom promptpack_langchain import convert_tools
pack = parse_promptpack("pack.json")
# Convert all toolstools = convert_tools(pack)
# Use with an agentfrom langchain_openai import ChatOpenAIfrom langchain.agents import create_tool_calling_agent, AgentExecutor
llm = ChatOpenAI()template = PromptPackTemplate.from_promptpack(pack, "agent")
agent = create_tool_calling_agent(llm, tools, template.chat_template)executor = AgentExecutor(agent=agent, tools=tools)
result = executor.invoke({"input": "Search for Python tutorials"})Custom Tool Implementations
Section titled “Custom Tool Implementations”The converted tools need implementations to be functional:
from promptpack_langchain import convert_toolsfrom langchain.tools import StructuredTool
# Get tool definitionstool_defs = convert_tools(pack)
# Create implementationsdef search_docs(query: str) -> str: # Your implementation return f"Results for: {query}"
# Create functional toolsearch_tool = StructuredTool.from_function( func=search_docs, name="search_docs", description=tool_defs[0].description, args_schema=tool_defs[0].args_schema)Multimodal Support
Section titled “Multimodal Support”Handle multimodal content including images:
from promptpack_langchain import PromptPackTemplatefrom promptpack_langchain.multimodal import process_multimodal_content
# Process content with imagescontent = process_multimodal_content({ "type": "image_url", "image_url": {"url": "https://example.com/image.png"}})Validators
Section titled “Validators”Validate inputs against PromptPack variable definitions:
from promptpack_langchain.validators import validate_variables
# Validate input variableserrors = validate_variables( variables=prompt.variables, inputs={"role": "agent", "company": "Acme"})
if errors: for error in errors: print(f"Validation error: {error}")Integration Example
Section titled “Integration Example”Complete example using PromptPack with LangChain:
from promptpack import parse_promptpackfrom promptpack_langchain import PromptPackTemplate, convert_toolsfrom langchain_openai import ChatOpenAIfrom langchain.agents import create_tool_calling_agent, AgentExecutor
# Load PromptPackpack = parse_promptpack("agent.json")
# Create template and toolstemplate = PromptPackTemplate.from_promptpack(pack, "main")tools = convert_tools(pack)
# Set up LLMllm = ChatOpenAI(model="gpt-4")
# Create agentagent = create_tool_calling_agent( llm=llm, tools=tools, prompt=template.chat_template)
# Create executorexecutor = AgentExecutor( agent=agent, tools=tools, verbose=True)
# Runresult = executor.invoke({ "input": "Help me find information about Python", "role": "assistant", "company": "TechCorp"})
print(result["output"])