add modelstudio_demos package with 2 demos (#76)
This commit is contained in:
216
modelstudio_demos/chat_demo/.gitignore
vendored
Normal file
216
modelstudio_demos/chat_demo/.gitignore
vendored
Normal file
@@ -0,0 +1,216 @@
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[codz]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py.cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
cover/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
.pybuilder/
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
# For a library or package, you might want to ignore these files since the code is
|
||||
# intended to run in multiple environments; otherwise, check them in:
|
||||
# .python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
# Pipfile.lock
|
||||
|
||||
# UV
|
||||
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||
# commonly ignored for libraries.
|
||||
# uv.lock
|
||||
|
||||
# poetry
|
||||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||
# commonly ignored for libraries.
|
||||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||
# poetry.lock
|
||||
# poetry.toml
|
||||
|
||||
# pdm
|
||||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
||||
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
||||
# pdm.lock
|
||||
# pdm.toml
|
||||
.pdm-python
|
||||
.pdm-build/
|
||||
|
||||
# pixi
|
||||
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
||||
# pixi.lock
|
||||
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
||||
# in the .venv directory. It is recommended not to include this directory in version control.
|
||||
.pixi
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# Redis
|
||||
*.rdb
|
||||
*.aof
|
||||
*.pid
|
||||
|
||||
# RabbitMQ
|
||||
mnesia/
|
||||
rabbitmq/
|
||||
rabbitmq-data/
|
||||
|
||||
# ActiveMQ
|
||||
activemq-data/
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.envrc
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
|
||||
# pytype static type analyzer
|
||||
.pytype/
|
||||
|
||||
# Cython debug symbols
|
||||
cython_debug/
|
||||
|
||||
# PyCharm
|
||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
||||
# .idea/
|
||||
|
||||
# Abstra
|
||||
# Abstra is an AI-powered process automation framework.
|
||||
# Ignore directories containing user credentials, local state, and settings.
|
||||
# Learn more at https://abstra.io/docs
|
||||
.abstra/
|
||||
|
||||
# Visual Studio Code
|
||||
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
||||
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
||||
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
||||
# you could uncomment the following to ignore the entire vscode folder
|
||||
# .vscode/
|
||||
|
||||
# Ruff stuff:
|
||||
.ruff_cache/
|
||||
|
||||
# PyPI configuration file
|
||||
.pypirc
|
||||
|
||||
# Marimo
|
||||
marimo/_static/
|
||||
marimo/_lsp/
|
||||
__marimo__/
|
||||
|
||||
# Streamlit
|
||||
.streamlit/secrets.toml
|
||||
45
modelstudio_demos/chat_demo/.pre-commit-config.yaml
Normal file
45
modelstudio_demos/chat_demo/.pre-commit-config.yaml
Normal file
@@ -0,0 +1,45 @@
|
||||
repos:
|
||||
# Pre-commit hooks for basic file checks
|
||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||
rev: v5.0.0
|
||||
hooks:
|
||||
- id: trailing-whitespace
|
||||
name: Trim trailing whitespace
|
||||
- id: end-of-file-fixer
|
||||
name: Fix end of files
|
||||
- id: check-yaml
|
||||
name: Check YAML files
|
||||
- id: check-added-large-files
|
||||
name: Check for large files
|
||||
args: ['--maxkb=1000']
|
||||
- id: check-json
|
||||
name: Check JSON files
|
||||
- id: check-toml
|
||||
name: Check TOML files
|
||||
- id: mixed-line-ending
|
||||
name: Check mixed line endings
|
||||
|
||||
# Python code formatting with Black
|
||||
- repo: https://github.com/psf/black
|
||||
rev: 24.10.0
|
||||
hooks:
|
||||
- id: black
|
||||
name: Format Python code with Black
|
||||
language_version: python3.10
|
||||
|
||||
# Python import sorting with isort
|
||||
- repo: https://github.com/PyCQA/isort
|
||||
rev: 5.13.2
|
||||
hooks:
|
||||
- id: isort
|
||||
name: Sort Python imports with isort
|
||||
args: ["--profile", "black"]
|
||||
|
||||
# Python linting with Ruff (fast alternative to flake8)
|
||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||
rev: v0.8.4
|
||||
hooks:
|
||||
- id: ruff
|
||||
name: Lint Python code with Ruff
|
||||
args: [--fix, --exit-non-zero-on-fix]
|
||||
|
||||
143
modelstudio_demos/chat_demo/README_en.md
Normal file
143
modelstudio_demos/chat_demo/README_en.md
Normal file
@@ -0,0 +1,143 @@
|
||||
# Alibaba Cloud Bailian High-Code Agent Starter
|
||||
|
||||
## Project Introduction
|
||||
|
||||
This is a starter project based on FastAPI Web framework and AgentScope, providing you with an initial template code package for deploying Agents locally or via Alibaba Cloud Bailian high-code cloud deployment.
|
||||
It supports direct local running and testing, and deployment to Alibaba Cloud Bailian via uvicorn, allowing you to freely code and assemble atomic capabilities such as LLM, MCP, RAG, memory, and search from Alibaba Cloud Bailian & AgentScope.
|
||||
|
||||
## Installing Dependencies
|
||||
|
||||
First, make sure you have Python 3.10 or higher installed.
|
||||
|
||||
## Local Startup and Testing
|
||||
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
### Dependency Description
|
||||
|
||||
- `fastapi`: For building Web APIs
|
||||
- `uvicorn`: For running FastAPI applications
|
||||
- `agentscope-runtime`: AgentScope runtime environment
|
||||
- `PyYAML`: PyYAML parsing package
|
||||
|
||||
## Configuration
|
||||
|
||||
### DashScope API Configuration
|
||||
|
||||
To use LLM features, you need to configure the Alibaba Cloud Bailian DashScope API KEY, which can also be added to the deployment machine's environment variables for subsequent cloud deployment:
|
||||
1. Set `DASHSCOPE_API_KEY` in the `deploy_starter/config.yml` file:
|
||||
```yaml
|
||||
DASHSCOPE_API_KEY: "your-api-key-here,sk-xxx"
|
||||
```
|
||||
|
||||
2. Or set it as an environment variable:
|
||||
```bash
|
||||
export DASHSCOPE_API_KEY="your-api-key-here,sk-xxx"
|
||||
```
|
||||
|
||||
## Running the Project
|
||||
|
||||
### Switch to project root directory and run directly
|
||||
|
||||
```bash
|
||||
cd current-project-root-directory, the directory where setup.py is located
|
||||
```
|
||||
|
||||
```bash
|
||||
python -m deploy_starter.main
|
||||
```
|
||||
|
||||
### Running with uvicorn
|
||||
|
||||
```bash
|
||||
uvicorn deploy_starter.main:app --host 127.0.0.1 --port 8080 --reload
|
||||
```
|
||||
|
||||
## API Endpoints
|
||||
|
||||
### Health Check
|
||||
|
||||
Check if the application is running properly:
|
||||
|
||||
```bash
|
||||
curl http://127.0.0.1:8080/health
|
||||
```
|
||||
|
||||
Expected response:
|
||||
```
|
||||
"OK"
|
||||
```
|
||||
|
||||
### Chat Endpoint
|
||||
|
||||
Chat with the LLM (requires DashScope API key configuration):
|
||||
|
||||
```bash
|
||||
curl -X POST http://127.0.0.1:8080/process \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"message": "Hello, world!"}'
|
||||
```
|
||||
|
||||
Expected response:
|
||||
```json
|
||||
{
|
||||
"response": "Hello! How can I help you?"
|
||||
}
|
||||
```
|
||||
|
||||
## Notes
|
||||
|
||||
1. Chat functionality will be unavailable if `DASHSCOPE_API_KEY` is not configured.
|
||||
2. The default model is `qwen-turbo`. You can change `DASHSCOPE_MODEL_NAME` in `config.yml` to switch models.
|
||||
|
||||
## Alibaba Cloud Bailian High-Code Cloud Deployment
|
||||
|
||||
### Preferably, you can directly upload the code package through the Alibaba Cloud Bailian High-Code Console
|
||||
[Create Application - High-Code Application](https://bailian.console.aliyun.com//app-center?tab=app#/app-center)
|
||||
|
||||

|
||||
|
||||
### Command-line console method for code upload and deployment - Better for quick code modifications and update deployments
|
||||
#### 1. Install Dependencies
|
||||
|
||||
```bash
|
||||
pip install agentscope-runtime==1.0.0
|
||||
pip install "agentscope-runtime[deployment]==1.0.0"
|
||||
```
|
||||
|
||||
#### 2. Set Environment Variables
|
||||
|
||||
```bash
|
||||
export ALIBABA_CLOUD_ACCESS_KEY_ID=... # Your Alibaba Cloud account AccessKey (required)
|
||||
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=... # Your Alibaba Cloud account SecurityKey (required)
|
||||
|
||||
# Optional: If you want to use separate OSS AK/SK, you can set the following (if not set, the account AK/SK above will be used). Please ensure the account has OSS read/write permissions.
|
||||
export MODELSTUDIO_WORKSPACE_ID=... # Your Bailian workspace ID
|
||||
export OSS_ACCESS_KEY_ID=...
|
||||
export OSS_ACCESS_KEY_SECRET=...
|
||||
export OSS_REGION=cn-beijing
|
||||
```
|
||||
|
||||
#### 3. Packaging and Deployment
|
||||
|
||||
##### Method A: Manually build wheel file
|
||||
|
||||
Ensure your project can be built into a wheel file. You can use setup.py, setup.cfg, or pyproject.toml.
|
||||
|
||||
Build the wheel file:
|
||||
```bash
|
||||
python setup.py bdist_wheel
|
||||
```
|
||||
|
||||
Deploy:
|
||||
```bash
|
||||
runtime-fc-deploy \
|
||||
--deploy-name [your app name] \
|
||||
--whl-path [relative path to your wheel file, e.g. "/dist/your_app.whl"]
|
||||
```
|
||||
|
||||

|
||||
|
||||
For details, please refer to the Alibaba Cloud Bailian High-Code Deployment Documentation: [Alibaba Cloud Bailian High-Code Deployment Documentation](https://bailian.console.aliyun.com/?tab=api#/api/?type=app&url=2983030)
|
||||
143
modelstudio_demos/chat_demo/README_zh.md
Normal file
143
modelstudio_demos/chat_demo/README_zh.md
Normal file
@@ -0,0 +1,143 @@
|
||||
# 阿里云百炼高代码Agent Starter
|
||||
|
||||
## 项目简介
|
||||
|
||||
这是一个基于FastAPI Web框架和AgentScope的启动项目,能给到你通过本地或者阿里云百炼高代码云端部署Agent的初始模版代码包。
|
||||
支持直接本地运行测试和通过 uvicorn 部署到阿里云百炼,自由代码编写组装阿里云百炼&AgentScope中的LLM、MCP、RAG、记忆、搜索等原子能力。
|
||||
|
||||
## 安装依赖
|
||||
|
||||
首先确保你已经安装了 Python 3.10 或更高版本。
|
||||
|
||||
## 本地启动测试
|
||||
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
### 依赖说明
|
||||
|
||||
- `fastapi`: 用于构建 Web API
|
||||
- `uvicorn`: 用于运行 FastAPI 应用
|
||||
- `agentscope-runtime`: AgentScope 运行时环境
|
||||
- `PyYAML`: PyYAML解析包
|
||||
|
||||
## 配置
|
||||
|
||||
### DashScope API 配置
|
||||
|
||||
要使用 LLM 功能,你需要配置阿里云百炼 DashScope API KEY,后续云端部署也可以添加到部署机器环境变量中:
|
||||
1. 在 `deploy_starter/config.yml` 文件中设置 `DASHSCOPE_API_KEY`:
|
||||
```yaml
|
||||
DASHSCOPE_API_KEY: "your-api-key-here,sk-xxx"
|
||||
```
|
||||
|
||||
2. 或者设置环境变量:
|
||||
```bash
|
||||
export DASHSCOPE_API_KEY="your-api-key-here,sk-xxx"
|
||||
```
|
||||
|
||||
## 运行项目
|
||||
|
||||
### 切换到项目根目录 直接运行
|
||||
|
||||
```bash
|
||||
cd 当前项目根目录,setup.py 文件所在的目录
|
||||
```
|
||||
|
||||
```bash
|
||||
python -m deploy_starter.main
|
||||
```
|
||||
|
||||
### 使用 uvicorn 运行
|
||||
|
||||
```bash
|
||||
uvicorn deploy_starter.main:app --host 127.0.0.1 --port 8080 --reload
|
||||
```
|
||||
|
||||
## API 接口
|
||||
|
||||
### 健康检查
|
||||
|
||||
检查应用是否正常运行:
|
||||
|
||||
```bash
|
||||
curl http://127.0.0.1:8080/health
|
||||
```
|
||||
|
||||
预期响应:
|
||||
```
|
||||
"OK"
|
||||
```
|
||||
|
||||
### 聊天接口
|
||||
|
||||
与 LLM 进行对话(需要配置 DashScope API 密钥):
|
||||
|
||||
```bash
|
||||
curl -X POST http://127.0.0.1:8080/process \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"message": "你好,世界!"}'
|
||||
```
|
||||
|
||||
预期响应:
|
||||
```json
|
||||
{
|
||||
"response": "你好!有什么我可以帮助你的吗?"
|
||||
}
|
||||
```
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. 如果未配置 `DASHSCOPE_API_KEY`,聊天功能将不可用。
|
||||
2. 默认使用 `qwen-turbo` 模型,可以在 `config.yml` 中修改 `DASHSCOPE_MODEL_NAME` 来切换模型。
|
||||
|
||||
## 阿里云百炼高代码 云端部署
|
||||
|
||||
### 优先可以选择阿里云百炼高代码控制台直接上传代码包
|
||||
[创建应用-高代码应用](https://bailian.console.aliyun.com//app-center?tab=app#/app-center)
|
||||
|
||||

|
||||
|
||||
### 命令行console方式进行代码上传部署-更适合快速修改代码进行更新部署
|
||||
#### 1. 安装依赖
|
||||
|
||||
```bash
|
||||
pip install agentscope-runtime==1.0.0
|
||||
pip install "agentscope-runtime[deployment]==1.0.0"
|
||||
```
|
||||
|
||||
#### 2. 设置环境变量
|
||||
|
||||
```bash
|
||||
export ALIBABA_CLOUD_ACCESS_KEY_ID=... # 你的阿里云账号AccessKey(必填)
|
||||
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=... # 你的阿里云账号SecurityKey(必填)
|
||||
|
||||
# 可选:如果你希望使用单独的 OSS AK/SK,可设置如下(未设置时将使用到上面的账号 AK/SK),请确保账号有 OSS 的读写权限
|
||||
export MODELSTUDIO_WORKSPACE_ID=... # 你的百炼业务空间id
|
||||
export OSS_ACCESS_KEY_ID=...
|
||||
export OSS_ACCESS_KEY_SECRET=...
|
||||
export OSS_REGION=cn-beijing
|
||||
```
|
||||
|
||||
#### 3. 打包和部署
|
||||
|
||||
##### 方式 A:手动构建 wheel 文件
|
||||
|
||||
确保你的项目可以被构建为 wheel 文件。你可以使用 setup.py、setup.cfg 或 pyproject.toml。
|
||||
|
||||
构建 wheel 文件:
|
||||
```bash
|
||||
python setup.py bdist_wheel
|
||||
```
|
||||
|
||||
部署:
|
||||
```bash
|
||||
runtime-fc-deploy \
|
||||
--deploy-name [你的应用名称] \
|
||||
--whl-path [到你的wheel文件的相对路径 如"/dist/your_app.whl"]
|
||||
```
|
||||
|
||||

|
||||
|
||||
具体请查看阿里云百炼高代码部署文档:[阿里云百炼高代码部署文档](https://bailian.console.aliyun.com/?tab=api#/api/?type=app&url=2983030)
|
||||
0
modelstudio_demos/chat_demo/__init__.py
Normal file
0
modelstudio_demos/chat_demo/__init__.py
Normal file
BIN
modelstudio_demos/chat_demo/deploy_by_cli.png
Normal file
BIN
modelstudio_demos/chat_demo/deploy_by_cli.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 494 KiB |
BIN
modelstudio_demos/chat_demo/deploy_by_ui.png
Normal file
BIN
modelstudio_demos/chat_demo/deploy_by_ui.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 101 KiB |
32
modelstudio_demos/chat_demo/deploy_starter/config.yml
Normal file
32
modelstudio_demos/chat_demo/deploy_starter/config.yml
Normal file
@@ -0,0 +1,32 @@
|
||||
# app config
|
||||
APP_NAME: "ModelStudio-Agent Starter"
|
||||
DEBUG: false
|
||||
|
||||
# Server Config
|
||||
FC_START_HOST: "0.0.0.0"
|
||||
HOST: "127.0.0.1"
|
||||
PORT: 8080
|
||||
RELOAD: true
|
||||
|
||||
# DashScope config
|
||||
DASHSCOPE_API_KEY:
|
||||
DASHSCOPE_MODEL_NAME: "qwen-turbo"
|
||||
|
||||
# log config
|
||||
LOG_LEVEL: "INFO"
|
||||
|
||||
# Setup config
|
||||
SETUP_PACKAGE_NAME: "deploy_starter"
|
||||
SETUP_MODULE_NAME: "main"
|
||||
SETUP_FUNCTION_NAME: "run_app"
|
||||
SETUP_COMMAND_NAME: "ModelStudio-Agent-starter"
|
||||
SETUP_NAME: "ModelStudio-Agent-starter"
|
||||
SETUP_VERSION: "0.1.0"
|
||||
SETUP_DESCRIPTION: "ModelStudio-Agent-starter"
|
||||
SETUP_LONG_DESCRIPTION: "ModelStudio-Agent-starter services, supporting both direct execution and uvicorn deployment"
|
||||
|
||||
# observability config
|
||||
TELEMETRY_ENABLE: TRUE
|
||||
|
||||
# FC run config
|
||||
FC_RUN_CMD: "python3 /code/python/deploy_starter/main.py"
|
||||
158
modelstudio_demos/chat_demo/deploy_starter/main.py
Normal file
158
modelstudio_demos/chat_demo/deploy_starter/main.py
Normal file
@@ -0,0 +1,158 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import asyncio
|
||||
import os
|
||||
|
||||
from agentscope.agent import ReActAgent
|
||||
from agentscope.formatter import DashScopeChatFormatter
|
||||
from agentscope.model import DashScopeChatModel
|
||||
from agentscope.pipeline import stream_printing_messages
|
||||
from agentscope.tool import Toolkit, execute_python_code
|
||||
from agentscope_runtime.adapters.agentscope.memory import (
|
||||
AgentScopeSessionHistoryMemory,
|
||||
)
|
||||
from agentscope_runtime.engine import AgentApp, LocalDeployManager
|
||||
from agentscope_runtime.engine.schemas.agent_schemas import AgentRequest
|
||||
from agentscope_runtime.engine.services.agent_state import InMemoryStateService
|
||||
from agentscope_runtime.engine.services.session_history import (
|
||||
InMemorySessionHistoryService,
|
||||
)
|
||||
from agentscope_runtime.engine.tracing import TraceType, trace
|
||||
|
||||
|
||||
# Read config.yml file
|
||||
def read_config():
|
||||
config_path = os.path.join(os.path.dirname(__file__), "config.yml")
|
||||
config_data = {}
|
||||
with open(config_path, "r", encoding="utf-8") as config_file:
|
||||
for line in config_file:
|
||||
line = line.strip()
|
||||
if line and not line.startswith("#"):
|
||||
if ":" in line:
|
||||
key, value = line.split(":", 1)
|
||||
key = key.strip()
|
||||
value = value.strip().strip("\"'")
|
||||
if value.lower() == "true":
|
||||
value = True
|
||||
elif value.lower() == "false":
|
||||
value = False
|
||||
elif value.isdigit():
|
||||
value = int(value)
|
||||
config_data[key] = value
|
||||
return config_data
|
||||
|
||||
|
||||
# Read configuration
|
||||
config = read_config()
|
||||
|
||||
agent_app = AgentApp(
|
||||
app_name=config.get("APP_NAME"),
|
||||
app_description="A helpful assistant",
|
||||
)
|
||||
|
||||
|
||||
# Initialize services (required)
|
||||
@agent_app.init
|
||||
async def init_func(self):
|
||||
self.state_service = InMemoryStateService()
|
||||
self.session_service = InMemorySessionHistoryService()
|
||||
await self.state_service.start()
|
||||
await self.session_service.start()
|
||||
|
||||
|
||||
@agent_app.shutdown
|
||||
async def shutdown_func(self):
|
||||
await self.state_service.stop()
|
||||
await self.session_service.stop()
|
||||
|
||||
|
||||
@agent_app.endpoint("/")
|
||||
def read_root():
|
||||
return {"hi, i'm running"}
|
||||
|
||||
|
||||
@agent_app.endpoint("/health")
|
||||
def health_check():
|
||||
return "OK"
|
||||
|
||||
|
||||
# Default interface implementation for /process
|
||||
@trace(trace_type=TraceType.LLM, trace_name="llm_func")
|
||||
@agent_app.query(framework="agentscope")
|
||||
async def query_func(
|
||||
self,
|
||||
msgs,
|
||||
request: AgentRequest = None,
|
||||
**kwargs,
|
||||
):
|
||||
assert kwargs is not None, "kwargs is Required for query_func"
|
||||
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,
|
||||
)
|
||||
|
||||
toolkit = Toolkit()
|
||||
toolkit.register_tool_function(execute_python_code)
|
||||
|
||||
agent = ReActAgent(
|
||||
name="Friday",
|
||||
model=DashScopeChatModel(
|
||||
config.get("DASHSCOPE_MODEL_NAME"),
|
||||
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(),
|
||||
)
|
||||
|
||||
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,
|
||||
)
|
||||
|
||||
|
||||
async def main():
|
||||
"""Deploy AgentScope Runtime using LocalDeployManager"""
|
||||
deployer = LocalDeployManager(
|
||||
host=config.get("FC_START_HOST", "127.0.0.1"),
|
||||
port=config.get("PORT", 8080),
|
||||
)
|
||||
|
||||
# Deploy agent_app
|
||||
await agent_app.deploy(deployer)
|
||||
|
||||
# Keep the service running
|
||||
print("Service started, press Ctrl+C to stop...")
|
||||
try:
|
||||
# Wait indefinitely until interrupted
|
||||
await asyncio.Event().wait()
|
||||
except KeyboardInterrupt:
|
||||
print("\nStopping service...")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
asyncio.run(main())
|
||||
except KeyboardInterrupt:
|
||||
print("\nService stopped")
|
||||
5
modelstudio_demos/chat_demo/requirements.txt
Normal file
5
modelstudio_demos/chat_demo/requirements.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
fastapi==0.116.1
|
||||
uvicorn
|
||||
agentscope-runtime==1.0.0
|
||||
setuptools
|
||||
PyYAML
|
||||
92
modelstudio_demos/chat_demo/setup.py
Normal file
92
modelstudio_demos/chat_demo/setup.py
Normal file
@@ -0,0 +1,92 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import os
|
||||
import uuid
|
||||
|
||||
from setuptools import setup
|
||||
|
||||
# Read dependencies from requirements.txt
|
||||
with open("requirements.txt", encoding="utf-8") as requirements_file:
|
||||
requirements = requirements_file.read().splitlines()
|
||||
|
||||
|
||||
# Read config.yml
|
||||
def read_config():
|
||||
config_path = os.path.join(
|
||||
os.path.dirname(__file__),
|
||||
"deploy_starter",
|
||||
"config.yml",
|
||||
)
|
||||
config_data = {}
|
||||
with open(config_path, "r", encoding="utf-8") as config_file:
|
||||
for line in config_file:
|
||||
line = line.strip()
|
||||
if line and not line.startswith("#"):
|
||||
if ":" in line:
|
||||
key, value = line.split(":", 1)
|
||||
key = key.strip()
|
||||
value = value.strip().strip("\"'")
|
||||
if value.lower() == "true":
|
||||
value = True
|
||||
elif value.lower() == "false":
|
||||
value = False
|
||||
elif value.isdigit():
|
||||
value = int(value)
|
||||
config_data[key] = value
|
||||
return config_data
|
||||
|
||||
|
||||
# Read README file if exists
|
||||
def read_readme():
|
||||
readme_files = ["README.md", "README.rst", "README.txt"]
|
||||
for filename in readme_files:
|
||||
if os.path.exists(filename):
|
||||
with open(filename, "r", encoding="utf-8") as readme_handle:
|
||||
return readme_handle.read()
|
||||
return "A FastAPI application with AgentScope runtime"
|
||||
|
||||
|
||||
# Load configuration
|
||||
config = read_config()
|
||||
|
||||
# Extract configuration values
|
||||
setup_package_name = config.get("SETUP_PACKAGE_NAME", "deploy_starter")
|
||||
setup_module_name = config.get("SETUP_MODULE_NAME", "main")
|
||||
setup_function_name = config.get("SETUP_FUNCTION_NAME", "run_app")
|
||||
setup_command_name = config.get(
|
||||
"SETUP_COMMAND_NAME",
|
||||
"ModelStudio-Agent-starter",
|
||||
)
|
||||
|
||||
# Generate package name with UUID suffix
|
||||
base_name = config.get("SETUP_NAME", "ModelStudio-Agent-starter")
|
||||
unique_name = f"{base_name}-{uuid.uuid4().hex[:8]}"
|
||||
|
||||
# Create package structure
|
||||
setup(
|
||||
name=unique_name,
|
||||
version=config.get("SETUP_VERSION", "0.1.0"),
|
||||
description=config.get("SETUP_DESCRIPTION", "ModelStudio-Agent-starter"),
|
||||
long_description=config.get(
|
||||
"SETUP_LONG_DESCRIPTION",
|
||||
(
|
||||
"ModelStudio-Agent-starter services, supporting both direct "
|
||||
"execution and uvicorn deployment"
|
||||
),
|
||||
),
|
||||
packages=[setup_package_name],
|
||||
package_dir={setup_package_name: setup_package_name},
|
||||
install_requires=requirements,
|
||||
python_requires=">=3.8",
|
||||
entry_points={
|
||||
"console_scripts": [
|
||||
(
|
||||
f"{setup_command_name}={setup_package_name}."
|
||||
f"{setup_module_name}:{setup_function_name}"
|
||||
),
|
||||
],
|
||||
},
|
||||
include_package_data=True,
|
||||
package_data={
|
||||
setup_package_name: ["config.yml"],
|
||||
},
|
||||
)
|
||||
Reference in New Issue
Block a user