Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions python/samples/02-agents/context_providers/redis/redis_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
from redisvl.extensions.cache.embeddings import EmbeddingsCache
from redisvl.utils.vectorize import OpenAITextVectorizer

# Default Redis URL for local Redis Stack.
# Override via the REDIS_URL environment variable for remote or authenticated instances.
REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379")


# NOTE: approval_mode="never_require" is for sample brevity.
# Use "always_require" in production; see samples/02-agents/tools/function_tool_with_approval.py
Expand Down Expand Up @@ -121,14 +125,14 @@ async def main() -> None:
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://localhost:6379"),
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://localhost:6379",
redis_url=REDIS_URL,
index_name="redis_basics",
application_id="matrix_of_kermits",
agent_id="agent_kermit",
Expand All @@ -151,16 +155,14 @@ async def main() -> None:
from agent_framework import AgentSession, SessionContext

session = AgentSession(session_id="runA")
context = SessionContext()
context.extend_messages("input", messages)
context = SessionContext(input_messages=messages)
state = session.state

# Store messages via after_run
await provider.after_run(agent=None, session=session, context=context, state=state)

# Retrieve relevant memories via before_run
query_context = SessionContext()
query_context.extend_messages("input", [Message("system", ["B: Assistant Message"])])
query_context = SessionContext(input_messages=[Message("system", ["B: Assistant Message"])])
await provider.before_run(agent=None, session=session, context=query_context, state=state)

# Inspect retrieved memories that would be injected into instructions
Expand All @@ -179,12 +181,12 @@ async def main() -> None:
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://localhost:6379"),
cache=EmbeddingsCache(name="openai_embeddings_cache", redis_url=REDIS_URL),
)
# Recreate a clean index so the next scenario starts fresh
provider = RedisContextProvider(
source_id="redis_context",
redis_url="redis://localhost:6379",
redis_url=REDIS_URL,
index_name="redis_basics_2",
prefix="context_2",
application_id="matrix_of_kermits",
Expand Down Expand Up @@ -232,7 +234,7 @@ async def main() -> None:
# Text-only provider (full-text search only). Omits vectorizer and related params.
provider = RedisContextProvider(
source_id="redis_context",
redis_url="redis://localhost:6379",
redis_url=REDIS_URL,
index_name="redis_basics_3",
prefix="context_3",
application_id="matrix_of_kermits",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
from redisvl.extensions.cache.embeddings import EmbeddingsCache
from redisvl.utils.vectorize import OpenAITextVectorizer

# Default Redis URL for local Redis Stack.
# Override via the REDIS_URL environment variable for remote or authenticated instances.
REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379")


async def main() -> None:
"""Walk through provider and chat message store usage.
Expand All @@ -34,12 +38,12 @@ async def main() -> None:
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://localhost:6379"),
cache=EmbeddingsCache(name="openai_embeddings_cache", redis_url=REDIS_URL),
)

provider = RedisContextProvider(
source_id="redis_context",
redis_url="redis://localhost:6379",
redis_url=REDIS_URL,
index_name="redis_conversation",
prefix="redis_conversation",
application_id="matrix_of_kermits",
Expand Down
20 changes: 10 additions & 10 deletions python/samples/02-agents/context_providers/redis/redis_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
from redisvl.extensions.cache.embeddings import EmbeddingsCache
from redisvl.utils.vectorize import OpenAITextVectorizer

# Default Redis URL for local Redis Stack.
# Override via the REDIS_URL environment variable for remote or authenticated instances.
REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379")

# Please set OPENAI_API_KEY to use the OpenAI vectorizer.
# For chat responses, also set AZURE_AI_PROJECT_ENDPOINT and AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME.

Expand All @@ -57,12 +61,11 @@ async def example_global_thread_scope() -> None:

provider = RedisContextProvider(
source_id="redis_context",
redis_url="redis://localhost:6379",
redis_url=REDIS_URL,
index_name="redis_threads_global",
application_id="threads_demo_app",
agent_id="threads_demo_agent",
user_id="threads_demo_user",
scope_to_per_operation_thread_id=False, # Share memories across all sessions
)

agent = client.as_agent(
Expand Down Expand Up @@ -106,19 +109,16 @@ async def example_per_operation_thread_scope() -> None:
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://localhost:6379"),
cache=EmbeddingsCache(name="openai_embeddings_cache", redis_url=REDIS_URL),
)

provider = RedisContextProvider(
source_id="redis_context",
redis_url="redis://localhost:6379",
redis_url=REDIS_URL,
index_name="redis_threads_dynamic",
# overwrite_redis_index=True,
# drop_redis_index=True,
application_id="threads_demo_app",
agent_id="threads_demo_agent",
user_id="threads_demo_user",
scope_to_per_operation_thread_id=True, # Isolate memories per session
redis_vectorizer=vectorizer,
vector_field_name="vector",
vector_algorithm="hnsw",
Expand Down Expand Up @@ -172,12 +172,12 @@ async def example_multiple_agents() -> None:
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://localhost:6379"),
cache=EmbeddingsCache(name="openai_embeddings_cache", redis_url=REDIS_URL),
)

personal_provider = RedisContextProvider(
source_id="redis_context",
redis_url="redis://localhost:6379",
redis_url=REDIS_URL,
index_name="redis_threads_agents",
application_id="threads_demo_app",
agent_id="agent_personal",
Expand All @@ -196,7 +196,7 @@ async def example_multiple_agents() -> None:

work_provider = RedisContextProvider(
source_id="redis_context",
redis_url="redis://localhost:6379",
redis_url=REDIS_URL,
index_name="redis_threads_agents",
application_id="threads_demo_app",
agent_id="agent_work",
Expand Down