address comments

This commit is contained in:
Twisha Bansal
2026-02-06 17:10:15 +05:30
parent fad11de9ff
commit 2ec6dac8bd
2 changed files with 22 additions and 1 deletions

View File

@@ -29,3 +29,13 @@ For more information, see the [LangChain Middleware documentation](https://docs.
You can also add model-level (`wrap_model`) and agent-level (`before_agent`, `after_agent`) hooks to intercept messages at different stages of the execution loop. See the [LangChain Middleware documentation](https://docs.langchain.com/oss/python/langchain/middleware/custom#wrap-style-hooks) for details on these additional hook types.
{{% /tab %}}
{{< /tabpane >}}
## Results
The output should look similar to the following. Note that exact responses may vary due to the non-deterministic nature of LLMs and differences between orchestration frameworks.
```
AI: Booking Confirmed! You earned 500 Loyalty Points with this stay.
AI: Error: Maximum stay duration is 14 days.
```

View File

@@ -47,7 +47,13 @@ async def enforce_business_rules(request, handler):
except ValueError:
pass # Ignore invalid date formats
return await handler(request)
# PRE: Code here runs BEFORE the tool execution
# EXEC: Execute the tool (or next middleware)
result = await handler(request)
# POST: Code here runs AFTER the tool execution
return result
# Post processing
@@ -58,8 +64,12 @@ async def enrich_response(request, handler):
Adds loyalty points information to successful bookings.
Standardizes output format.
"""
# PRE: Code here runs BEFORE the tool execution
# EXEC: Execute the tool (or next middleware)
result = await handler(request)
# POST: Code here runs AFTER the tool execution
if isinstance(result, ToolMessage):
content = str(result.content)
tool_name = request.tool_call["name"]
@@ -79,6 +89,7 @@ async def main():
system_prompt=system_prompt,
model=model,
tools=tools,
# add any pre and post processing methods
middleware=[enforce_business_rules, enrich_response],
)