mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: Fix Redis samples for session migration and configurable REDIS_URL (#4060)
* fix: update Redis samples for session migration and configurable REDIS_URL - Replace hardcoded redis://localhost:6379 with configurable REDIS_URL env var - Fix SessionContext usage: use input_messages kwarg instead of removed extend_messages - Remove obsolete scope_to_per_operation_thread_id parameter - Remove stale commented-out overwrite_redis_index/drop_redis_index params Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Remove docker commands from comments to avoid security scan flags Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
396807ab17
commit
6fd50464b0
@@ -37,6 +37,10 @@ from azure.identity import AzureCliCredential
|
||||
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
|
||||
@@ -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",
|
||||
@@ -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
|
||||
@@ -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",
|
||||
@@ -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",
|
||||
|
||||
@@ -23,6 +23,10 @@ from azure.identity import AzureCliCredential
|
||||
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.
|
||||
@@ -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",
|
||||
|
||||
@@ -35,6 +35,10 @@ from azure.identity import AzureCliCredential
|
||||
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.
|
||||
|
||||
@@ -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(
|
||||
@@ -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",
|
||||
@@ -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",
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user