Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Redis supports different context patterns across SDKs. In .NET, connect Redis-backed search to the generic TextSearchProvider for RAG. The Agent Framework Redis package provides searchable memory and conversation-history providers for Python.
| Pattern | API | SDK | Behavior |
|---|---|---|---|
| RAG | TextSearchProvider with a Redis search adapter |
.NET | Retrieves relevant Redis content before invocation or through an on-demand search tool. |
| Searchable memory | RedisContextProvider |
Python | Extracts conversational details and retrieves relevant context with full-text or hybrid vector search. |
| Conversation history | RedisHistoryProvider |
Python | Persists and reloads the exact message transcript for a session. |
Add RAG with TextSearchProvider
Use the provider-independent TextSearchProvider pattern for .NET. Implement its search adapter with the Redis client or vector-store connector selected by your application, map the Redis results to TextSearchProvider.TextSearchResult, and attach the provider through AIContextProviders.
This approach supports Redis-backed RAG without requiring a Redis-specific Agent Framework context-provider package.
Install the package
pip install agent-framework-redis --pre
Add searchable memory
Use this pattern when an agent should recall selected relevant information rather than replay every previous message.
Prerequisites
- A Redis deployment with RediSearch support, such as Redis Stack or a compatible managed service.
- A Microsoft Foundry project and model deployment for the sample agent.
- An embedding provider when you enable hybrid vector search.
Configure searchable memory
Use application_id, agent_id, and user_id to partition memories. Add a Redis vectorizer and vector-field settings when you want hybrid retrieval.
# Create a provider with partition scope and OpenAI embeddings
# Please set OPENAI_API_KEY to use the OpenAI vectorizer.
# For chat responses, also set FOUNDRY_PROJECT_ENDPOINT and FOUNDRY_MODEL.
# We attach an embedding vectorizer so the provider can perform hybrid (text + vector)
# retrieval. If you prefer text-only retrieval, instantiate RedisContextProvider without the
# 'vectorizer' and vector_* parameters.
vectorizer = OpenAITextVectorizer(
model="text-embedding-ada-002",
api_config={"api_key": os.getenv("OPENAI_API_KEY")},
cache=EmbeddingsCache(name="openai_embeddings_cache", redis_url=REDIS_URL),
)
# The provider manages persistence and retrieval. application_id/agent_id/user_id
# scope data for multi-tenant separation; thread_id (set later) narrows to a
# specific conversation.
provider = RedisContextProvider(
source_id="redis_context",
redis_url=REDIS_URL,
index_name="redis_basics",
application_id="matrix_of_kermits",
agent_id="agent_kermit",
user_id="kermit",
redis_vectorizer=vectorizer,
vector_field_name="vector",
vector_algorithm="hnsw",
vector_distance_metric="cosine",
)
Attach memory to an agent
Add the provider to context_providers. The provider stores conversational details after a run and surfaces relevant context before later runs.
# Create chat client for the agent
client = create_chat_client()
# Create agent wired to the Redis context provider. The provider automatically
# persists conversational details and surfaces relevant context on each turn.
agent = Agent(
client=client,
name="MemoryEnhancedAssistant",
instructions=(
"You are a helpful assistant. Personalize replies using provided context. "
"Before answering, always check for stored context"
),
tools=[],
context_providers=[provider],
)
# Teach a user preference; the agent writes this to the provider's memory
query = "Remember that I enjoy glugenflorgle"
result = await agent.run(query)
print("User: ", query)
print("Agent: ", result)
# Ask the agent to recall the stored preference; it should retrieve from memory
query = "What do I enjoy?"
result = await agent.run(query)
Persist conversation history
Use this pattern when a session must recover its complete transcript after an application restart or on another instance.
Prerequisites
- A Redis deployment reachable through
REDIS_URL. - TLS and authenticated Redis users for production deployments.
Attach RedisHistoryProvider through context_providers. The provider stores messages for the session and can limit the retained message count.
async def example_manual_memory_store() -> None:
"""Basic example of using Redis history provider."""
print("=== Basic Redis History Provider Example ===")
# Create Redis history provider
redis_provider = RedisHistoryProvider(
source_id="redis_basic_chat",
redis_url=REDIS_URL,
)
# Create agent with Redis history provider
agent = Agent(
client=OpenAIChatClient(),
name="RedisBot",
instructions="You are a helpful assistant that remembers our conversation using Redis.",
context_providers=[redis_provider],
)
# Create session
session = agent.create_session()
# Have a conversation
print("\n--- Starting conversation ---")
query1 = "Hello! My name is Alice and I love pizza."
print(f"User: {query1}")
response1 = await agent.run(query1, session=session)
print(f"Agent: {response1.text}")
query2 = "What do you remember about me?"
print(f"User: {query2}")
response2 = await agent.run(query2, session=session)
print(f"Agent: {response2.text}")
Use a stable session ID and persist the serialized AgentSession in trusted application storage when clients must resume the same logical conversation after a process restart.
Note
Redis context-provider integration isn't currently documented for Agent Framework Go. See the Agent Framework Go repository for the latest status.
Production considerations
- Derive tenant, search, memory, and session scopes from authenticated application identity, not model output.
- Use TLS, Redis authentication, and network isolation.
- Use separate key prefixes or deployments where tenant isolation requires it.
- Configure persistence, backups, retention, and eviction for the required durability.
- Treat retrieved memory as untrusted input and mitigate indirect prompt injection.
- Redact sensitive content before persisting messages or indexing searchable content.
Next steps
Go deeper: