samples updating with AgentScope-Runtime 1.0.0 (#43)
This commit is contained in:
committed by
GitHub
parent
67469f1caa
commit
68450961bb
@@ -1,70 +1,109 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import asyncio
|
||||
# pylint:disable=redefined-outer-name, unused-argument
|
||||
|
||||
|
||||
import os
|
||||
|
||||
from agentscope.formatter import DashScopeChatFormatter
|
||||
from agentscope.tool import Toolkit, execute_python_code
|
||||
from agentscope.pipeline import stream_printing_messages
|
||||
|
||||
from agentscope.agent import ReActAgent
|
||||
from agentscope.model import DashScopeChatModel
|
||||
from agentscope_runtime.engine import LocalDeployManager, Runner
|
||||
from agentscope_runtime.engine.agents.agentscope_agent import AgentScopeAgent
|
||||
from agentscope_runtime.engine.services.context_manager import ContextManager
|
||||
|
||||
from agentscope_runtime.engine import AgentApp
|
||||
from agentscope_runtime.engine.schemas.agent_schemas import AgentRequest
|
||||
from agentscope_runtime.adapters.agentscope.memory import (
|
||||
AgentScopeSessionHistoryMemory,
|
||||
)
|
||||
from agentscope_runtime.engine.services.agent_state import (
|
||||
InMemoryStateService,
|
||||
)
|
||||
from agentscope_runtime.engine.services.session_history import (
|
||||
InMemorySessionHistoryService,
|
||||
)
|
||||
|
||||
|
||||
def local_deploy():
|
||||
asyncio.run(_local_deploy())
|
||||
|
||||
|
||||
async def _local_deploy():
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
server_port = int(os.environ.get("SERVER_PORT", "8090"))
|
||||
server_endpoint = os.environ.get("SERVER_ENDPOINT", "agent")
|
||||
model = DashScopeChatModel(
|
||||
model_name="qwen-turbo",
|
||||
api_key=os.getenv("DASHSCOPE_API_KEY"),
|
||||
)
|
||||
agent = AgentScopeAgent(
|
||||
name="Friday",
|
||||
model=model,
|
||||
agent_config={
|
||||
"sys_prompt": "A simple LLM agent to generate a short response",
|
||||
},
|
||||
agent_builder=ReActAgent,
|
||||
# server_endpoint = os.environ.get("SERVER_ENDPOINT", "process")
|
||||
|
||||
agent_app = AgentApp(
|
||||
app_name="Friday",
|
||||
app_description="A simple LLM agent to generate a short response",
|
||||
)
|
||||
|
||||
context_manager = ContextManager()
|
||||
@agent_app.init
|
||||
async def init_func(self):
|
||||
self.state_service = InMemoryStateService()
|
||||
self.session_service = InMemorySessionHistoryService()
|
||||
|
||||
runner = Runner(
|
||||
agent=agent,
|
||||
context_manager=context_manager,
|
||||
)
|
||||
await self.state_service.start()
|
||||
await self.session_service.start()
|
||||
|
||||
deploy_manager = LocalDeployManager(host="localhost", port=server_port)
|
||||
try:
|
||||
deployment_info = await runner.deploy(
|
||||
deploy_manager,
|
||||
endpoint_path=f"/{server_endpoint}",
|
||||
@agent_app.shutdown
|
||||
async def shutdown_func(self):
|
||||
await self.state_service.stop()
|
||||
await self.session_service.stop()
|
||||
|
||||
@agent_app.query(framework="agentscope")
|
||||
async def query_func(
|
||||
self,
|
||||
msgs,
|
||||
request: AgentRequest = None,
|
||||
**kwargs,
|
||||
):
|
||||
session_id = request.session_id
|
||||
user_id = request.user_id
|
||||
|
||||
state = await self.state_service.export_state(
|
||||
session_id=session_id,
|
||||
user_id=user_id,
|
||||
)
|
||||
|
||||
print("✅ Service deployed successfully!")
|
||||
print(f" URL: {deployment_info['url']}")
|
||||
print(f" Endpoint: {deployment_info['url']}/{server_endpoint}")
|
||||
print("\nAgent Service is running in the background.")
|
||||
toolkit = Toolkit()
|
||||
toolkit.register_tool_function(execute_python_code)
|
||||
|
||||
while True:
|
||||
await asyncio.sleep(1)
|
||||
agent = ReActAgent(
|
||||
name="Friday",
|
||||
model=DashScopeChatModel(
|
||||
"qwen-turbo",
|
||||
api_key=os.getenv("DASHSCOPE_API_KEY"),
|
||||
enable_thinking=True,
|
||||
stream=True,
|
||||
),
|
||||
sys_prompt="You're a helpful assistant named Friday.",
|
||||
toolkit=toolkit,
|
||||
memory=AgentScopeSessionHistoryMemory(
|
||||
service=self.session_service,
|
||||
session_id=session_id,
|
||||
user_id=user_id,
|
||||
),
|
||||
formatter=DashScopeChatFormatter(),
|
||||
)
|
||||
|
||||
except (KeyboardInterrupt, asyncio.CancelledError):
|
||||
# This block will be executed when you press Ctrl+C.
|
||||
print("\nShutdown signal received. Stopping the service...")
|
||||
if deploy_manager.is_running:
|
||||
await deploy_manager.stop()
|
||||
print("✅ Service stopped.")
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {e}")
|
||||
if deploy_manager.is_running:
|
||||
await deploy_manager.stop()
|
||||
if state:
|
||||
agent.load_state_dict(state)
|
||||
|
||||
async for msg, last in stream_printing_messages(
|
||||
agents=[agent],
|
||||
coroutine_task=agent(msgs),
|
||||
):
|
||||
yield msg, last
|
||||
|
||||
state = agent.state_dict()
|
||||
|
||||
await self.state_service.save_state(
|
||||
user_id=user_id,
|
||||
session_id=session_id,
|
||||
state=state,
|
||||
)
|
||||
|
||||
agent_app.run(host="127.0.0.1", port=server_port)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user