mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
5e056b672e
* Python: Provider-leading client design & OpenAI package extraction Major refactoring of the Python Agent Framework client architecture: - Extract OpenAI clients into new `agent-framework-openai` package - Core package no longer depends on openai, azure-identity, azure-ai-projects - Rename clients for discoverability: OpenAIResponsesClient → OpenAIChatClient, OpenAIChatClient → OpenAIChatCompletionClient - Unify `model_id`/`deployment_name`/`model_deployment_name` → `model` param - New FoundryChatClient for Azure AI Foundry Responses API - New FoundryAgent/FoundryAgentClient for connecting to pre-configured Foundry agents - Remove OpenAIBase/OpenAIConfigMixin from non-deprecated client MRO - Deprecate AzureOpenAI* clients, AzureAIClient, OpenAIAssistantsClient - Reorganize samples: azure_openai+azure_ai+azure_ai_agent → azure/ - ADR-0020: Provider-Leading Client Design Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix: missing Agent imports in samples, .model_id → .model in foundry_local sample Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix: CI failures — mypy errors, coverage targets, sample imports - azure-ai mypy: add type ignores for TypedDict total=, model arg, forward ref - Coverage: replace core.azure/openai targets with openai package target - project_provider: add type annotation for opts dict Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix: populate openai .pyi stub, fix broken README links, coverage targets Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fixes * updated observabilitty * reset azure init.pyi * fix errors * updated adr number * fix foundry local * fixed not renamed docstrings and comments, and added deprecated markers to old classes * fix tests and pyprojects * fix test vars * updated function tests * update durable * updated test setup for functions * Fix Foundry auth in workflow samples Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Stabilize Python integration workflows Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Update hosting samples for Foundry Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Trigger full CI rerun Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Trigger CI rerun again Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * trigger rerun * trigger rerun * fix for litellm * undo durabletask changes * Move Foundry APIs into foundry namespace Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix Foundry pyproject formatting Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Split provider samples by Foundry surface Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Restore hosting sample requirements Also fix the Foundry Local sample link after the provider sample move. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * updated tests * udpated foundry integration tests * removed dist from azurefunctions tests * Use separate Foundry clients for concurrent agents Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix client setup in azfunc and durable * disabled two tests * updated setup for some function and durable tests * improved azure openai setup with new clients * ignore deprecated * fixes * skip 11 * remove openai assistants int tests --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
125 lines
4.2 KiB
Python
125 lines
4.2 KiB
Python
# Copyright (c) Microsoft. All rights reserved.
|
|
|
|
"""Redis Context Provider: Basic usage and agent integration
|
|
|
|
This example demonstrates how to use the Redis context provider to persist
|
|
conversational details. Pass it as a constructor argument to create_agent.
|
|
|
|
Note: For session history persistence, see RedisHistoryProvider in the
|
|
conversations/redis_history_provider.py sample. RedisContextProvider is for
|
|
AI context (RAG, memories), while RedisHistoryProvider stores message history.
|
|
|
|
Requirements:
|
|
- A Redis instance with RediSearch enabled (e.g., Redis Stack)
|
|
- agent-framework with the Redis extra installed: pip install "agent-framework-redis"
|
|
- Optionally an OpenAI API key if enabling embeddings for hybrid search
|
|
|
|
Run:
|
|
python redis_conversation.py
|
|
"""
|
|
|
|
import asyncio
|
|
import os
|
|
|
|
from agent_framework import Agent
|
|
from agent_framework.foundry import FoundryChatClient
|
|
from agent_framework.redis import RedisContextProvider
|
|
from azure.identity import AzureCliCredential
|
|
from redisvl.extensions.cache.embeddings import EmbeddingsCache
|
|
from redisvl.utils.vectorize import OpenAITextVectorizer
|
|
|
|
# Copyright (c) Microsoft. All rights reserved.
|
|
|
|
|
|
# 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.
|
|
|
|
Helpful debugging (uncomment when iterating):
|
|
- print(await provider.redis_index.info())
|
|
- print(await provider.search_all())
|
|
"""
|
|
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),
|
|
)
|
|
|
|
provider = RedisContextProvider(
|
|
source_id="redis_context",
|
|
redis_url=REDIS_URL,
|
|
index_name="redis_conversation",
|
|
prefix="redis_conversation",
|
|
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",
|
|
)
|
|
|
|
# Create chat client for the agent
|
|
client = FoundryChatClient(
|
|
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
|
|
model=os.environ["FOUNDRY_MODEL"],
|
|
credential=AzureCliCredential(),
|
|
)
|
|
# 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],
|
|
)
|
|
|
|
# Create a session to manage conversation state
|
|
session = agent.create_session()
|
|
|
|
# Teach a user preference; the agent writes this to the provider's memory
|
|
query = "Remember that I enjoy gumbo"
|
|
result = await agent.run(query, session=session)
|
|
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, session=session)
|
|
print("User: ", query)
|
|
print("Agent: ", result)
|
|
|
|
query = "What did I say to you just now?"
|
|
result = await agent.run(query, session=session)
|
|
print("User: ", query)
|
|
print("Agent: ", result)
|
|
|
|
query = "Remember that I have a meeting at 3pm tomorro"
|
|
result = await agent.run(query, session=session)
|
|
print("User: ", query)
|
|
print("Agent: ", result)
|
|
|
|
query = "Tulips are red"
|
|
result = await agent.run(query, session=session)
|
|
print("User: ", query)
|
|
print("Agent: ", result)
|
|
|
|
query = "What was the first thing I said to you this conversation?"
|
|
result = await agent.run(query, session=session)
|
|
print("User: ", query)
|
|
print("Agent: ", result)
|
|
# Drop / delete the provider index in Redis
|
|
await provider.redis_index.delete()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|