|
| 1 | +"""Internal prompt resolution utilities. |
| 2 | +
|
| 3 | +This module provides utilities for resolving different types of prompt specifications |
| 4 | +into standardized message formats for language models. It supports both synchronous |
| 5 | +and asynchronous prompt resolution with automatic detection of callable types. |
| 6 | +
|
| 7 | +The module is designed to handle common prompt patterns across LangChain components, |
| 8 | +particularly for summarization chains and other document processing workflows. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import inspect |
| 14 | +from typing import TYPE_CHECKING, Callable, Union |
| 15 | + |
| 16 | +if TYPE_CHECKING: |
| 17 | + from collections.abc import Awaitable |
| 18 | + |
| 19 | + from langchain_core.messages import MessageLikeRepresentation |
| 20 | + from langgraph.runtime import Runtime |
| 21 | + |
| 22 | + from langchain._internal._typing import ContextT, StateT |
| 23 | + |
| 24 | + |
| 25 | +def resolve_prompt( |
| 26 | + prompt: Union[ |
| 27 | + str, |
| 28 | + None, |
| 29 | + Callable[[StateT, Runtime[ContextT]], list[MessageLikeRepresentation]], |
| 30 | + ], |
| 31 | + state: StateT, |
| 32 | + runtime: Runtime[ContextT], |
| 33 | + default_user_content: str, |
| 34 | + default_system_content: str, |
| 35 | +) -> list[MessageLikeRepresentation]: |
| 36 | + """Resolve a prompt specification into a list of messages. |
| 37 | +
|
| 38 | + Handles prompt resolution across different strategies. Supports callable functions, |
| 39 | + string system messages, and None for default behavior. |
| 40 | +
|
| 41 | + Args: |
| 42 | + prompt: The prompt specification to resolve. Can be: |
| 43 | + - Callable: Function taking (state, runtime) returning message list. |
| 44 | + - str: A system message string. |
| 45 | + - None: Use the provided default system message. |
| 46 | + state: Current state, passed to callable prompts. |
| 47 | + runtime: LangGraph runtime instance, passed to callable prompts. |
| 48 | + default_user_content: User content to include (e.g., document text). |
| 49 | + default_system_content: Default system message when prompt is None. |
| 50 | +
|
| 51 | + Returns: |
| 52 | + List of message dictionaries for language models, typically containing |
| 53 | + a system message and user message with content. |
| 54 | +
|
| 55 | + Raises: |
| 56 | + TypeError: If prompt type is not str, None, or callable. |
| 57 | +
|
| 58 | + Example: |
| 59 | + ```python |
| 60 | + def custom_prompt(state, runtime): |
| 61 | + return [{"role": "system", "content": "Custom"}] |
| 62 | +
|
| 63 | + messages = resolve_prompt(custom_prompt, state, runtime, "content", "default") |
| 64 | + messages = resolve_prompt("Custom system", state, runtime, "content", "default") |
| 65 | + messages = resolve_prompt(None, state, runtime, "content", "Default") |
| 66 | + ``` |
| 67 | +
|
| 68 | + Note: |
| 69 | + Callable prompts have full control over message structure and content |
| 70 | + parameter is ignored. String/None prompts create standard system + user |
| 71 | + structure. |
| 72 | + """ |
| 73 | + if callable(prompt): |
| 74 | + return prompt(state, runtime) |
| 75 | + if isinstance(prompt, str): |
| 76 | + system_msg = prompt |
| 77 | + elif prompt is None: |
| 78 | + system_msg = default_system_content |
| 79 | + else: |
| 80 | + msg = f"Invalid prompt type: {type(prompt)}. Expected str, None, or callable." |
| 81 | + raise TypeError(msg) |
| 82 | + |
| 83 | + return [ |
| 84 | + {"role": "system", "content": system_msg}, |
| 85 | + {"role": "user", "content": default_user_content}, |
| 86 | + ] |
| 87 | + |
| 88 | + |
| 89 | +async def aresolve_prompt( |
| 90 | + prompt: Union[ |
| 91 | + str, |
| 92 | + None, |
| 93 | + Callable[[StateT, Runtime[ContextT]], list[MessageLikeRepresentation]], |
| 94 | + Callable[ |
| 95 | + [StateT, Runtime[ContextT]], Awaitable[list[MessageLikeRepresentation]] |
| 96 | + ], |
| 97 | + ], |
| 98 | + state: StateT, |
| 99 | + runtime: Runtime[ContextT], |
| 100 | + default_user_content: str, |
| 101 | + default_system_content: str, |
| 102 | +) -> list[MessageLikeRepresentation]: |
| 103 | + """Async version of resolve_prompt supporting both sync and async callables. |
| 104 | +
|
| 105 | + Handles prompt resolution across different strategies. Supports sync/async callable |
| 106 | + functions, string system messages, and None for default behavior. |
| 107 | +
|
| 108 | + Args: |
| 109 | + prompt: The prompt specification to resolve. Can be: |
| 110 | + - Callable (sync): Function taking (state, runtime) returning message list. |
| 111 | + - Callable (async): Async function taking (state, runtime) returning |
| 112 | + awaitable message list. |
| 113 | + - str: A system message string. |
| 114 | + - None: Use the provided default system message. |
| 115 | + state: Current state, passed to callable prompts. |
| 116 | + runtime: LangGraph runtime instance, passed to callable prompts. |
| 117 | + default_user_content: User content to include (e.g., document text). |
| 118 | + default_system_content: Default system message when prompt is None. |
| 119 | +
|
| 120 | + Returns: |
| 121 | + List of message dictionaries for language models, typically containing |
| 122 | + a system message and user message with content. |
| 123 | +
|
| 124 | + Raises: |
| 125 | + TypeError: If prompt type is not str, None, or callable. |
| 126 | +
|
| 127 | + Example: |
| 128 | + ```python |
| 129 | + async def async_prompt(state, runtime): |
| 130 | + return [{"role": "system", "content": "Async"}] |
| 131 | +
|
| 132 | + def sync_prompt(state, runtime): |
| 133 | + return [{"role": "system", "content": "Sync"}] |
| 134 | +
|
| 135 | + messages = await aresolve_prompt( |
| 136 | + async_prompt, state, runtime, "content", "default" |
| 137 | + ) |
| 138 | + messages = await aresolve_prompt( |
| 139 | + sync_prompt, state, runtime, "content", "default" |
| 140 | + ) |
| 141 | + messages = await aresolve_prompt("Custom", state, runtime, "content", "default") |
| 142 | + ``` |
| 143 | +
|
| 144 | + Note: |
| 145 | + Callable prompts have full control over message structure and content |
| 146 | + parameter is ignored. Automatically detects and handles async |
| 147 | + callables. |
| 148 | + """ |
| 149 | + if callable(prompt): |
| 150 | + result = prompt(state, runtime) |
| 151 | + # Check if the result is awaitable (async function) |
| 152 | + if inspect.isawaitable(result): |
| 153 | + return await result |
| 154 | + return result |
| 155 | + if isinstance(prompt, str): |
| 156 | + system_msg = prompt |
| 157 | + elif prompt is None: |
| 158 | + system_msg = default_system_content |
| 159 | + else: |
| 160 | + msg = f"Invalid prompt type: {type(prompt)}. Expected str, None, or callable." |
| 161 | + raise TypeError(msg) |
| 162 | + |
| 163 | + return [ |
| 164 | + {"role": "system", "content": system_msg}, |
| 165 | + {"role": "user", "content": default_user_content}, |
| 166 | + ] |
0 commit comments