You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: splunklib/ai/README.md
+78-8Lines changed: 78 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -422,7 +422,6 @@ and perform programmatic reasoning without relying on free-form text.
422
422
423
423
```py
424
424
from splunklib.ai import Agent, OpenAIModel
425
-
from splunklib.ai.messages import HumanMessage
426
425
from splunklib.client import connect
427
426
from typing import Literal
428
427
from pydantic import BaseModel, Field
@@ -451,12 +450,11 @@ async with Agent(
451
450
system_prompt="You are an agent, whose job is to determine the details of provided failure logs",
452
451
output_schema=Output,
453
452
) as agent:
454
-
result =await agent.invoke(
455
-
[
456
-
HumanMessage(
457
-
content=f"Analyze log: {log}",
458
-
)
459
-
]
453
+
# Use invoke_with_data when passing external data to the agent to reduce
454
+
# the risk of prompt injection.
455
+
result =await agent.invoke_with_data(
456
+
instructions="Analyze this log and determine the failure details.",
457
+
data=log,
460
458
)
461
459
462
460
# Make use of the output.
@@ -504,7 +502,7 @@ async with Agent(
504
502
await agent.invoke(...)
505
503
```
506
504
507
-
**Note**: Currently input schemas can only be used by subagents, not by regular agents.
505
+
**Note**: Input schemas can only be used by subagents, not by regular agents. When invoking agents with external data, see [Security](#security) for guidance on how to do this safely.
508
506
509
507
## Middleware
510
508
@@ -848,6 +846,78 @@ The agent emits logs for events such as: model interactions, tool calls, subagen
848
846
849
847
Additionally logs from local tools are also forwarded to this logger.
850
848
849
+
## Security
850
+
851
+
When invoking the agent with external data (log entries, alert payloads, API responses, etc.),
852
+
use `invoke_with_data` instead of `invoke`. It separates your instructions from the untrusted
853
+
data, reducing the risk of prompt injection:
854
+
855
+
```py
856
+
from splunklib.ai.messages import HumanMessage
857
+
858
+
# Use invoke for plain conversational messages.
859
+
result =await agent.invoke([HumanMessage(content="What are the top threats this week?")])
860
+
861
+
# Use invoke_with_data when passing external data to the agent.
862
+
result =await agent.invoke_with_data(
863
+
instructions="Summarize this security alert and assess its severity.",
864
+
data=alert_payload, # str or dict
865
+
)
866
+
```
867
+
868
+
If you prefer to build the message manually, `create_structured_prompt` gives you the same
869
+
separation and can be used directly inside a `HumanMessage`:
870
+
871
+
```py
872
+
from splunklib.ai import create_structured_prompt
873
+
from splunklib.ai.messages import HumanMessage
874
+
875
+
result =await agent.invoke([
876
+
HumanMessage(content=create_structured_prompt(
877
+
instructions="Summarize this security alert and assess its severity.",
878
+
data=alert_payload,
879
+
))
880
+
])
881
+
```
882
+
883
+
`truncate_input` caps the input length inline when constructing a message. `detect_injection`
884
+
scans for common injection patterns - one way to apply it consistently is via `agent_middleware`,
885
+
which gives you a single place to enforce the policy across every `invoke()` call. You decide
886
+
what to do when injection is detected:
887
+
888
+
```py
889
+
from typing import Any
890
+
from splunklib.ai import Agent, OpenAIModel, detect_injection, truncate_input
891
+
from splunklib.ai.middleware import (
892
+
agent_middleware,
893
+
AgentMiddlewareHandler,
894
+
AgentRequest,
895
+
)
896
+
from splunklib.ai.messages import AgentResponse, HumanMessage
0 commit comments