.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "build_tutorial/monitor.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_build_tutorial_monitor.py: .. _configuring_and_monitoring: Configuring and Monitoring ================================== The main entry of AgentScope is `agentscope.init`, where you can configure your application. .. GENERATED FROM PYTHON SOURCE LINES 10-34 .. code-block:: Python 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 ) .. rst-class:: sphx-glr-script-out .. code-block:: none [] .. GENERATED FROM PYTHON SOURCE LINES 35-38 Exporting the configuration -------------------------------- The `state_dict` method can be used to export the configuration of the running application. .. GENERATED FROM PYTHON SOURCE LINES 38-43 .. code-block:: Python import json print(json.dumps(agentscope.state_dict(), indent=2, ensure_ascii=False)) .. rst-class:: sphx-glr-script-out .. code-block:: none { "project": "Project Alpha", "name": "Test-1", "disable_saving": false, "run_id": "run_20250310-033308_Test-1", "pid": 1973, "timestamp": "2025-03-10 03:33:08", "agentscope_version": "0.1.2", "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_20250310-033308_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_20250310-033308_Test-1/agentscope.db" } } .. GENERATED FROM PYTHON SOURCE LINES 44-48 Monitoring the Runtime -------------------------- AgentScope provides AgentScope Studio, a web visual interface to monitor and manage the running applications and histories. Refer to section :ref:`visual` for more details. .. GENERATED FROM PYTHON SOURCE LINES 50-55 .. _token_usage: Monitoring Token Usage ------------------------ `print_llm_usage` will print and return the token usage of the current running application. .. GENERATED FROM PYTHON SOURCE LINES 55-74 .. code-block:: Python 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)) .. rst-class:: sphx-glr-script-out .. code-block:: none { "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": 17, "total_tokens": 29 } ], "image": [] } .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.601 seconds) .. _sphx_glr_download_build_tutorial_monitor.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: monitor.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: monitor.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: monitor.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_