Configuring and Monitoring

The main entry of AgentScope is agentscope.init, where you can configure your application.

import agentscope


agentscope.init(
    model_configs=[
        {  # The model configurations
            "config_name": "my-qwen-max",
            "model_type": "dashscope_chat",
            "model_name": "qwen-max",
        },
    ],
    project="Project Alpha",  # The project name
    name="Test-1",  # The runtime name
    disable_saving=False,  # The main switch to disable saving
    save_dir="./runs",  # The saving directory
    save_log=True,  # Save the logging or not
    save_code=False,  # Save the code for this runtime
    save_api_invoke=False,  # Save the API invocation
    cache_dir="~/.cache",  # The cache directory, used for caching embeddings and so on
    use_monitor=True,  # Monitor the token usage or not
    logger_level="INFO",  # The logger level
)
[]

Exporting the configuration

The state_dict method can be used to export the configuration of the running application.

import json

print(json.dumps(agentscope.state_dict(), indent=2, ensure_ascii=False))
{
  "project": "Project Alpha",
  "name": "Test-1",
  "disable_saving": false,
  "run_id": "run_20250113-053422_Test-1",
  "pid": 1938,
  "timestamp": "2025-01-13 05:34:22",
  "agentscope_version": "0.1.2.dev",
  "file": {
    "save_log": true,
    "save_code": false,
    "save_api_invoke": false,
    "base_dir": null,
    "run_dir": "/home/runner/work/agentscope/agentscope/docs/tutorial/en/source/tutorial/runs/run_20250113-053422_Test-1",
    "cache_dir": "~/.cache"
  },
  "model": {
    "model_configs": {
      "my-qwen-vl": {
        "config_name": "my-qwen-vl",
        "model_type": "dashscope_multimodal",
        "model_name": "qwen-vl-max"
      },
      "qwen_config": {
        "model_type": "dashscope_chat",
        "config_name": "qwen_config",
        "model_name": "qwen-max"
      },
      "my-qwen-max": {
        "config_name": "my-qwen-max",
        "model_type": "dashscope_chat",
        "model_name": "qwen-max"
      }
    }
  },
  "logger": {
    "level": "INFO"
  },
  "studio": {
    "active": false,
    "studio_url": null
  },
  "monitor": {
    "use_monitor": true,
    "path_db": "/home/runner/work/agentscope/agentscope/docs/tutorial/en/source/tutorial/runs/run_20250113-053422_Test-1/agentscope.db"
  }
}

Monitoring the Runtime

AgentScope provides AgentScope Studio, a web visual interface to monitor and manage the running applications and histories. Refer to section visual for more details.

Monitoring Token Usage

print_llm_usage will print and return the token usage of the current running application.

from agentscope.models import DashScopeChatWrapper

qwen_max = DashScopeChatWrapper(
    config_name="-",
    model_name="qwen-max",
)
qwen_plus = DashScopeChatWrapper(
    config_name="-",
    model_name="qwen-plus",
)

# Call qwen-max and qwen-plus to simulate the token usage
_ = qwen_max([{"role": "user", "content": "Hi!"}])
_ = qwen_plus([{"role": "user", "content": "Who are you?"}])

usage = agentscope.print_llm_usage()

print(json.dumps(usage, indent=2, ensure_ascii=False))
{
  "text_and_embedding": [
    {
      "model_name": "qwen-max",
      "times": 1,
      "prompt_tokens": 10,
      "completion_tokens": 9,
      "total_tokens": 19
    },
    {
      "model_name": "qwen-plus",
      "times": 1,
      "prompt_tokens": 12,
      "completion_tokens": 61,
      "total_tokens": 73
    }
  ],
  "image": []
}

Total running time of the script: (0 minutes 4.261 seconds)

Gallery generated by Sphinx-Gallery