langchain-dev-utils 1.2.11__py3-none-any.whl → 1.2.12__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- __version__ = "1.2.11"
1
+ __version__ = "1.2.12"
@@ -1,3 +1,4 @@
1
+ from .format_prompt import format_prompt
1
2
  from .model_fallback import ModelFallbackMiddleware
2
3
  from .model_router import ModelRouterMiddleware
3
4
  from .plan import (
@@ -22,4 +23,5 @@ __all__ = [
22
23
  "LLMToolEmulator",
23
24
  "ModelRouterMiddleware",
24
25
  "ToolCallRepairMiddleware",
26
+ "format_prompt",
25
27
  ]
@@ -0,0 +1,66 @@
1
+ from langchain.agents.middleware import ModelRequest, dynamic_prompt
2
+ from langchain_core.prompts.string import get_template_variables
3
+
4
+
5
+ @dynamic_prompt
6
+ def format_prompt(request: ModelRequest) -> str:
7
+ """Format the system prompt with variables from state and context.
8
+
9
+ This middleware function extracts template variables from the system prompt
10
+ and populates them with values from the agent's state and runtime context.
11
+ Variables are first resolved from the state, then from the context if not found.
12
+
13
+ Example:
14
+ >>> from langchain_dev_utils.agents.middleware.format_prompt import format_prompt
15
+ >>> from langchain.agents import create_agent
16
+ >>> from langchain_core.messages import HumanMessage
17
+ >>> from dataclasses import dataclass
18
+ >>>
19
+ >>> @dataclass
20
+ ... class Context:
21
+ ... name: str
22
+ ... user: str
23
+ >>>
24
+ >>> agent=create_agent(
25
+ ... model=model,
26
+ ... tools=tools,
27
+ ... system_prompt="You are a helpful assistant. Your name is {name}. Your user is {user}.",
28
+ ... middleware=[format_prompt],
29
+ ... context_schema=Context,
30
+ ... )
31
+ >>> agent.invoke(
32
+ ... {
33
+ ... "messages": [HumanMessage(content="Hello")],
34
+ ... },
35
+ ... context=Context(name="assistant", user="tbice"),
36
+ ... )
37
+
38
+ """
39
+ system_msg = request.system_message
40
+ if system_msg is None:
41
+ raise ValueError(
42
+ "system_message must be provided,while use format_prompt in middleware."
43
+ )
44
+
45
+ system_prompt = "\n".join(
46
+ [content.get("text", "") for content in system_msg.content_blocks]
47
+ )
48
+ variables = get_template_variables(system_prompt, "f-string")
49
+
50
+ format_params = {}
51
+
52
+ state = request.state
53
+ for key in variables:
54
+ if var := state.get(key, None):
55
+ format_params[key] = var
56
+
57
+ other_var_keys = set(variables) - set(format_params.keys())
58
+
59
+ if other_var_keys:
60
+ context = request.runtime.context
61
+ if context is not None:
62
+ for key in other_var_keys:
63
+ if var := getattr(context, key, None):
64
+ format_params[key] = var
65
+
66
+ return system_prompt.format(**format_params)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: langchain-dev-utils
3
- Version: 1.2.11
3
+ Version: 1.2.12
4
4
  Summary: A practical utility library for LangChain and LangGraph development
5
5
  Project-URL: Source Code, https://github.com/TBice123123/langchain-dev-utils
6
6
  Project-URL: repository, https://github.com/TBice123123/langchain-dev-utils
@@ -1,4 +1,4 @@
1
- langchain_dev_utils/__init__.py,sha256=aBWZsCYiXXcSUsUJr3tOTQWsH7ZDqJzyMYdQbOd5Qtc,23
1
+ langchain_dev_utils/__init__.py,sha256=dbW85A2PinQCZabwD2DNDTfOE9315GDtQQKAsJP8IXk,23
2
2
  langchain_dev_utils/_utils.py,sha256=MFEzR1BjXMj6HEVwt2x2omttFuDJ_rYAEbNqe99r9pM,1338
3
3
  langchain_dev_utils/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
4
  langchain_dev_utils/agents/__init__.py,sha256=PJ-lSDZv_AXMYA3H4fx-HzJa14tPbkGmq1HX8LNfaPo,125
@@ -6,7 +6,8 @@ langchain_dev_utils/agents/factory.py,sha256=XdGjktksfTDys7X4SgfPrQz10HUo5fTNAWE
6
6
  langchain_dev_utils/agents/file_system.py,sha256=Yk3eetREE26WNrnTWLoiDUpOyCJ-rhjlfFDk6foLa1E,8468
7
7
  langchain_dev_utils/agents/plan.py,sha256=WwhoiJBmVYVI9bT8HfjCzTJ_SIp9WFil0gOeznv2omQ,6497
8
8
  langchain_dev_utils/agents/wrap.py,sha256=RuchoH_VotPmKFuYEn2SXoSgNxZhSA9jKM0Iv_8oHLk,4718
9
- langchain_dev_utils/agents/middleware/__init__.py,sha256=sAd0gehREpt0MB5deX5_YmTJPBoqmeSsjNQ-ta6R3EM,768
9
+ langchain_dev_utils/agents/middleware/__init__.py,sha256=EECbcYcHXQAMA-guJNRGwCVi9jG957d0nOaoIuyIKC0,832
10
+ langchain_dev_utils/agents/middleware/format_prompt.py,sha256=rfii98tmOqkjaNHxWy7hovhEYKXrF0CdzsMLO54_CDI,2359
10
11
  langchain_dev_utils/agents/middleware/model_fallback.py,sha256=nivtXXF4cwyOBv6p7RW12nXtNg87wjTWxO3BKIYiroI,1674
11
12
  langchain_dev_utils/agents/middleware/model_router.py,sha256=pOK-4PNTLrmjaQA9poHoQnsaVwoX0JeJrLVysulv9iU,7631
12
13
  langchain_dev_utils/agents/middleware/plan.py,sha256=0qDCmenxgY_zrwMfOyYlgLfhYNw-HszNLeeOkfj14NA,16002
@@ -31,7 +32,7 @@ langchain_dev_utils/pipeline/types.py,sha256=T3aROKKXeWvd0jcH5XkgMDQfEkLfPaiOhhV
31
32
  langchain_dev_utils/tool_calling/__init__.py,sha256=mu_WxKMcu6RoTf4vkTPbA1WSBSNc6YIqyBtOQ6iVQj4,322
32
33
  langchain_dev_utils/tool_calling/human_in_the_loop.py,sha256=7Z_QO5OZUR6K8nLoIcafc6osnvX2IYNorOJcbx6bVso,9672
33
34
  langchain_dev_utils/tool_calling/utils.py,sha256=S4-KXQ8jWmpGTXYZitovF8rxKpaSSUkFruM8LDwvcvE,2765
34
- langchain_dev_utils-1.2.11.dist-info/METADATA,sha256=4IGI_v0rIC8_SW13_j3h2JyPS_-rKPvdK6m6lTW2ymE,11853
35
- langchain_dev_utils-1.2.11.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
36
- langchain_dev_utils-1.2.11.dist-info/licenses/LICENSE,sha256=AWAOzNEcsvCEzHOF0qby5OKxviVH_eT9Yce1sgJTico,1084
37
- langchain_dev_utils-1.2.11.dist-info/RECORD,,
35
+ langchain_dev_utils-1.2.12.dist-info/METADATA,sha256=5MjLniCOhGN8a0L6cNLD69PUMNzf5K0nS4Cfifdl5CI,11853
36
+ langchain_dev_utils-1.2.12.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
37
+ langchain_dev_utils-1.2.12.dist-info/licenses/LICENSE,sha256=AWAOzNEcsvCEzHOF0qby5OKxviVH_eT9Yce1sgJTico,1084
38
+ langchain_dev_utils-1.2.12.dist-info/RECORD,,