Skip to content

Commit a78a58c

Browse files
mjnoviceclaude
andcommitted
docs(samples): add memory-aware-agent sample
Demonstrates how a coded agent wires in UiPath Agent Memory: search a memory space, stitch the LLMOps-rendered system_prompt_injection into the LLM system prompt, then call the LLM Gateway. Includes a README clarifying that the SDK does not auto-wire memory based on is_agent_memory_enabled — recall and prompt injection are explicit calls in the agent code. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent de3b91c commit a78a58c

5 files changed

Lines changed: 197 additions & 0 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Memory-Aware Agent
2+
3+
A coded agent that recalls prior interactions from a UiPath **Agent Memory**
4+
space and uses them to augment its LLM system prompt before answering.
5+
6+
## What it demonstrates
7+
8+
1. Searching a memory space via `UiPath().memory.search_async`
9+
2. Stitching the LLMOps-rendered `system_prompt_injection` few-shot block
10+
into the system prompt
11+
3. Calling the UiPath LLM Gateway with the augmented context
12+
13+
The Python SDK does **not** auto-wire memory based on the
14+
`is_agent_memory_enabled` flag in agent definitions — recall and prompt
15+
injection are explicit calls in your agent code, as shown here.
16+
17+
## Prerequisites
18+
19+
Set the following environment variables (a `.env` file works too):
20+
21+
| Variable | Purpose |
22+
|----------|---------|
23+
| `UIPATH_URL` | Base URL, e.g. `https://cloud.uipath.com/<org>/<tenant>` |
24+
| `UIPATH_ACCESS_TOKEN` | PAT or service-to-service token |
25+
| `UIPATH_FOLDER_KEY` | Folder that owns the memory space |
26+
27+
You also need a memory space. Create one via the UI/CLI or programmatically:
28+
29+
```python
30+
from uipath.platform import UiPath
31+
sdk = UiPath()
32+
space = sdk.memory.create(
33+
name="customer-support-recall",
34+
description="Past resolved customer queries",
35+
folder_key="<your-folder-key>",
36+
)
37+
print(space.id) # pass this as memory_space_id
38+
```
39+
40+
## Run
41+
42+
```bash
43+
cd packages/uipath/samples/memory-aware-agent
44+
uv sync
45+
uipath run main '{"query": "How do I reset my password?", "memory_space_id": "<space-id>"}'
46+
```
47+
48+
## How it works
49+
50+
```
51+
AgentInput.query
52+
53+
54+
sdk.memory.search_async ──▶ MemorySearchResponse.system_prompt_injection
55+
│ │
56+
└──── augmented system prompt ◀┘
57+
58+
59+
sdk.llm.chat_completions ──▶ AgentOutput.response
60+
```
61+
62+
`MemorySearchResponse.system_prompt_injection` is a pre-rendered few-shot
63+
block produced by LLMOps — paste it directly into the system prompt and
64+
let the model do the rest. No extra templating required.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"version": "2.0",
3+
"resources": []
4+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
"""Memory-aware coded agent.
2+
3+
Searches a UiPath Agent Memory space for prior interactions relevant to
4+
the user's query, stitches the LLMOps-rendered few-shot injection into
5+
the LLM system prompt, then calls the UiPath LLM Gateway.
6+
7+
Required environment variables:
8+
UIPATH_URL UiPath base URL (e.g. https://cloud.uipath.com/org/tenant)
9+
UIPATH_ACCESS_TOKEN Personal access token or service token
10+
UIPATH_FOLDER_KEY Folder that owns the memory space (or pass folder_path)
11+
12+
The memory space itself is created out-of-band (UI, CLI, or
13+
``uipath.memory.create``) and its id is passed in via ``AgentInput``.
14+
"""
15+
16+
import os
17+
18+
from dotenv import load_dotenv
19+
from pydantic import BaseModel, Field
20+
21+
from uipath.platform import UiPath
22+
from uipath.platform.chat import ChatModels
23+
from uipath.platform.memory import (
24+
MemorySearchRequest,
25+
SearchField,
26+
SearchMode,
27+
SearchSettings,
28+
)
29+
from uipath.tracing import traced
30+
31+
load_dotenv()
32+
33+
BASE_SYSTEM_PROMPT = "You answer questions concisely and accurately."
34+
35+
36+
class AgentInput(BaseModel):
37+
"""Input model for the memory-aware agent."""
38+
39+
query: str = Field(description="User question")
40+
memory_space_id: str = Field(
41+
description="ID of the memory space to recall from (folder-scoped)"
42+
)
43+
44+
45+
class AgentOutput(BaseModel):
46+
"""Output model for the memory-aware agent."""
47+
48+
response: str = Field(description="Final LLM answer")
49+
matched_memories: int = Field(
50+
default=0, description="Number of memories returned by the search"
51+
)
52+
system_prompt_injection: str = Field(
53+
default="", description="The few-shot block stitched into the system prompt"
54+
)
55+
56+
57+
@traced()
58+
async def main(input: AgentInput) -> AgentOutput:
59+
"""Recall memories, augment the system prompt, then call the LLM."""
60+
base_url = os.environ.get("UIPATH_URL")
61+
access_token = os.environ.get("UIPATH_ACCESS_TOKEN")
62+
folder_key = os.environ.get("UIPATH_FOLDER_KEY")
63+
64+
if not base_url or not access_token:
65+
return AgentOutput(
66+
response=(
67+
"Missing required environment variables. "
68+
"Set UIPATH_URL and UIPATH_ACCESS_TOKEN."
69+
),
70+
)
71+
72+
sdk = UiPath()
73+
74+
# 1. Recall relevant prior interactions for this query.
75+
search_req = MemorySearchRequest(
76+
fields=[SearchField(key_path=["query"], value=input.query)],
77+
settings=SearchSettings(
78+
threshold=0.5,
79+
result_count=3,
80+
search_mode=SearchMode.Hybrid,
81+
),
82+
definition_system_prompt=BASE_SYSTEM_PROMPT,
83+
)
84+
recall = await sdk.memory.search_async(
85+
memory_space_id=input.memory_space_id,
86+
request=search_req,
87+
folder_key=folder_key,
88+
)
89+
90+
# 2. Stitch the LLMOps-rendered few-shot injection into the system prompt.
91+
system_prompt = BASE_SYSTEM_PROMPT
92+
if recall.system_prompt_injection:
93+
system_prompt = f"{BASE_SYSTEM_PROMPT}\n\n{recall.system_prompt_injection}"
94+
95+
# 3. Call the LLM with the augmented system prompt.
96+
chat = await sdk.llm.chat_completions(
97+
messages=[
98+
{"role": "system", "content": system_prompt},
99+
{"role": "user", "content": input.query},
100+
],
101+
model=ChatModels.gpt_4_1_mini_2025_04_14,
102+
max_tokens=400,
103+
temperature=0.2,
104+
)
105+
106+
answer = chat.choices[0].message.content or ""
107+
return AgentOutput(
108+
response=answer,
109+
matched_memories=len(recall.results),
110+
system_prompt_injection=recall.system_prompt_injection,
111+
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[project]
2+
name = "memory-aware-agent"
3+
version = "0.0.1"
4+
description = "Sample agent that augments LLM prompts with UiPath Agent Memory recall"
5+
requires-python = ">=3.11"
6+
dependencies = [
7+
"uipath",
8+
]
9+
10+
[dependency-groups]
11+
dev = [
12+
"uipath-dev",
13+
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"functions": {
3+
"main": "main.py:main"
4+
}
5+
}

0 commit comments

Comments
 (0)