Microsoft Agent Framework Integration Guide¶
This document describes how to integrate and use the Microsoft Agent Framework within AgentScope Runtime to build agents that support multi-turn conversations, conversation memory, and streaming responses.
📦 Example Overview¶
The following example demonstrates how to use the Microsoft Agent Framework inside AgentScope Runtime:
Uses the Qwen-Plus model from DashScope
Supports multi-turn conversation and session memory
Employs streaming output (SSE) to return responses in real-time
Implements session history storage via an in-memory database (
InMemoryDb)Can be accessed through an OpenAI-compatible API mode
Here’s the core code:
# ms_agent.py
# -*- coding: utf-8 -*-
import os
from contextlib import asynccontextmanager
from fastapi import FastAPI
from agent_framework.openai import OpenAIChatClient
from agentscope_runtime.engine import AgentApp
from agentscope_runtime.engine.schemas.agent_schemas import AgentRequest
PORT = 8090
def run_app():
"""Start AgentApp and enable streaming output."""
@asynccontextmanager
async def lifespan(app: FastAPI):
app.state.thread_storage = {} # Only for testing
yield
agent_app = AgentApp(
app_name="Friday",
app_description="A helpful assistant",
lifespan=lifespan,
)
@agent_app.query(framework="ms_agent_framework")
async def query_func(
self,
msgs,
request: AgentRequest = None,
**kwargs,
):
"""Handle agent queries."""
session_id = request.session_id
user_id = request.user_id
# Export historical context
id_key = f"{user_id}_{session_id}"
thread = agent_app.state.thread_storage.get(id_key)
# Create agent
agent = OpenAIChatClient(
model_id="qwen-plus",
api_key=os.environ["DASHSCOPE_API_KEY"],
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
).create_agent(
instructions="You're a helpful assistant named Friday",
name="Friday",
)
# Restore or create conversation thread
if thread:
thread = await agent.deserialize_thread(thread)
else:
thread = agent.get_new_thread()
# Streaming responses
async for event in agent.run_stream(
msgs,
thread=thread,
):
yield event
# Save session state
serialized_thread = await thread.serialize()
agent_app.state.thread_storage[id_key] = serialized_thread
agent_app.run(host="127.0.0.1", port=PORT)
if __name__ == "__main__":
run_app()
⚙️ Prerequisites¶
Note
Before starting, make sure you have installed AgentScope Runtime and Microsoft Agent Framework, and configured the required API keys.
Install dependencies:
pip install "agentscope-runtime[ext]"
Set environment variables (DashScope provides the Qwen model API Key):
export DASHSCOPE_API_KEY="your-dashscope-api-key"
▶️ Run the Example¶
To run the example:
python ms_agent.py
🌐 API Interaction¶
1. Ask the Agent (/process)¶
You can send HTTP POST requests to interact with the agent, with support for SSE streaming responses:
curl -N \
-X POST "http://localhost:8090/process" \
-H "Content-Type: application/json" \
-d '{
"input": [
{
"role": "user",
"content": [
{ "type": "text", "text": "What is the capital of France?" }
]
}
],
"session_id": "session_1"
}'
2. OpenAI-Compatible Mode¶
This example also supports the OpenAI Compatible API:
from openai import OpenAI
client = OpenAI(base_url="http://127.0.0.1:8090/compatible-mode/v1")
resp = client.responses.create(
model="any_model",
input="Who are you?",
)
print(resp.response["output"][0]["content"][0]["text"])
🔧 Customization¶
You can extend the example in the following ways:
Change the model — Replace the
model_idinOpenAIChatClientwith another model, or use a client from another model provider.Add system prompts — Modify the
instructionsfield to create a different persona for the agent.