Skip to content

Commit 15e5ac7

Browse files
authored
Merge pull request #11 from Serverless-Devs/fix-add-include-usage
feat: 添加 Google ADK、CrewAI、LangChain 和 PydanticAI 模型适配器的流选项支持,更新数据 AP…
2 parents 448e975 + c48aa3c commit 15e5ac7

File tree

8 files changed

+155
-3
lines changed

8 files changed

+155
-3
lines changed

.github/workflows/release-test.yml

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
name: Release Test Package
2+
3+
on:
4+
# 支持手动触发,用户可以在 GitHub Actions 页面点击 "Run workflow"
5+
workflow_dispatch:
6+
inputs:
7+
version_bump:
8+
description: '版本递增类型'
9+
required: true
10+
default: 'patch'
11+
type: choice
12+
options:
13+
- patch # 0.0.1 -> 0.0.2
14+
- minor # 0.0.1 -> 0.1.0
15+
- major # 0.0.1 -> 1.0.0
16+
17+
jobs:
18+
release-test:
19+
runs-on: ubuntu-latest
20+
permissions:
21+
contents: write
22+
id-token: write
23+
steps:
24+
- uses: actions/checkout@v4
25+
with:
26+
fetch-depth: 0 # 获取所有历史和 tags
27+
28+
- name: Set up Python
29+
uses: actions/setup-python@v5
30+
with:
31+
python-version: '3.10'
32+
33+
- name: Get latest test version and calculate next version
34+
id: version
35+
run: |
36+
# 获取所有 agentrun-inner-test-v* 的 tags,找到最新版本
37+
LATEST_TAG=$(git tag -l "agentrun-inner-test-v*" | sort -V | tail -n 1)
38+
39+
if [ -z "$LATEST_TAG" ]; then
40+
# 如果没有找到任何 tag,从 0.0.0 开始
41+
CURRENT_VERSION="0.0.0"
42+
echo "No existing test tags found, starting from 0.0.0"
43+
else
44+
# 从 tag 中提取版本号
45+
CURRENT_VERSION="${LATEST_TAG#agentrun-inner-test-v}"
46+
echo "Latest test tag: $LATEST_TAG (version: $CURRENT_VERSION)"
47+
fi
48+
49+
# 解析版本号
50+
IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION"
51+
52+
# 根据用户选择递增版本
53+
BUMP_TYPE="${{ inputs.version_bump }}"
54+
case "$BUMP_TYPE" in
55+
major)
56+
MAJOR=$((MAJOR + 1))
57+
MINOR=0
58+
PATCH=0
59+
;;
60+
minor)
61+
MINOR=$((MINOR + 1))
62+
PATCH=0
63+
;;
64+
patch)
65+
PATCH=$((PATCH + 1))
66+
;;
67+
esac
68+
69+
NEW_VERSION="${MAJOR}.${MINOR}.${PATCH}"
70+
NEW_TAG="agentrun-inner-test-v${NEW_VERSION}"
71+
72+
echo "VERSION=${NEW_VERSION}" >> $GITHUB_OUTPUT
73+
echo "TAG=${NEW_TAG}" >> $GITHUB_OUTPUT
74+
echo "New version: ${NEW_VERSION}"
75+
echo "New tag: ${NEW_TAG}"
76+
77+
- name: Update package name and version in pyproject.toml
78+
run: |
79+
VERSION="${{ steps.version.outputs.VERSION }}"
80+
# 修改包名为 agentrun-inner-test
81+
sed -i 's/name = "agentrun-sdk"/name = "agentrun-inner-test"/' pyproject.toml
82+
# 修改版本号
83+
sed -i 's/version = "[^"]*"/version = "'${VERSION}'"/' pyproject.toml
84+
echo "Updated pyproject.toml:"
85+
head -10 pyproject.toml
86+
87+
- name: Update __version__ in __init__.py
88+
run: |
89+
VERSION="${{ steps.version.outputs.VERSION }}"
90+
if grep -q "__version__" agentrun/__init__.py; then
91+
sed -i 's/__version__ = "[^"]*"/__version__ = "'${VERSION}'"/' agentrun/__init__.py
92+
else
93+
sed -i '1a __version__ = "'${VERSION}'"' agentrun/__init__.py
94+
fi
95+
echo "Updated __init__.py version to ${VERSION}"
96+
grep "__version__" agentrun/__init__.py
97+
98+
- name: Build package
99+
run: |
100+
python -m pip install --upgrade pip
101+
pip install build twine
102+
python -m build
103+
echo "Package built successfully"
104+
ls -la dist/
105+
106+
- name: Verify package
107+
run: |
108+
python -m twine check dist/*
109+
echo "Package verification completed"
110+
111+
- name: Publish to PyPI
112+
uses: pypa/gh-action-pypi-publish@release/v1
113+
with:
114+
password: ${{ secrets.PYPI_API_TOKEN }}
115+
verify-metadata: false
116+
117+
- name: Create and push tag
118+
run: |
119+
TAG="${{ steps.version.outputs.TAG }}"
120+
git config --local user.email "[email protected]"
121+
git config --local user.name "GitHub Action"
122+
git tag -a "$TAG" -m "Release test package version ${{ steps.version.outputs.VERSION }}"
123+
git push origin "$TAG"
124+
echo "Created and pushed tag: $TAG"
125+
126+
- name: Summary
127+
run: |
128+
echo "## 🎉 Test Package Released!" >> $GITHUB_STEP_SUMMARY
129+
echo "" >> $GITHUB_STEP_SUMMARY
130+
echo "- **Package Name:** agentrun-inner-test" >> $GITHUB_STEP_SUMMARY
131+
echo "- **Version:** ${{ steps.version.outputs.VERSION }}" >> $GITHUB_STEP_SUMMARY
132+
echo "- **Tag:** ${{ steps.version.outputs.TAG }}" >> $GITHUB_STEP_SUMMARY
133+
echo "- **Commit:** ${{ github.sha }}" >> $GITHUB_STEP_SUMMARY
134+
echo "" >> $GITHUB_STEP_SUMMARY
135+
echo "Install with: \`pip install agentrun-inner-test==${{ steps.version.outputs.VERSION }}\`" >> $GITHUB_STEP_SUMMARY

agentrun/integration/agentscope/model_adapter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,5 @@ def wrap_model(self, common_model: CommonModel) -> Any:
5656
"base_url": info.base_url,
5757
"http_client": AsyncClient(headers=info.headers),
5858
},
59+
generate_kwargs={"stream_options": {"include_usage": True}},
5960
)

agentrun/integration/crewai/model_adapter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ def wrap_model(self, common_model: Any) -> Any:
2222
model=f"{info.provider or 'openai'}/{info.model}",
2323
base_url=info.base_url,
2424
default_headers=info.headers,
25+
stream_options={"include_usage": True},
2526
# async_client=AsyncClient(headers=info.headers),
2627
)

agentrun/integration/google_adk/model_adapter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ def wrap_model(self, common_model: CommonModel) -> Any:
3939
api_base=info.base_url,
4040
api_key=info.api_key,
4141
extra_headers=info.headers,
42+
stream_options={"include_usage": True},
4243
)

agentrun/integration/langchain/model_adapter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ def wrap_model(self, common_model: Any) -> Any:
3333
model=info.model,
3434
base_url=info.base_url,
3535
async_client=AsyncClient(headers=info.headers),
36+
stream_usage=True,
3637
)

agentrun/integration/pydantic_ai/model_adapter.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
"""PydanticAI 模型适配器 / PydanticAI Model Adapter"""
22

3-
from contextlib import asynccontextmanager
4-
import json
5-
from typing import Any, AsyncIterator
3+
from typing import Any
64

75
from agentrun.integration.utils.adapter import ModelAdapter
86
from agentrun.integration.utils.model import CommonModel
@@ -19,6 +17,7 @@ def wrap_model(self, common_model: CommonModel) -> Any:
1917
try:
2018
from pydantic_ai.models.openai import OpenAIChatModel
2119
from pydantic_ai.providers.openai import OpenAIProvider
20+
from pydantic_ai.settings import ModelSettings
2221
except Exception as e:
2322
raise ImportError(
2423
"PydanticAI is not installed. "
@@ -36,6 +35,9 @@ def wrap_model(self, common_model: CommonModel) -> Any:
3635
api_key=info.api_key,
3736
http_client=AsyncClient(headers=info.headers),
3837
),
38+
settings=ModelSettings(
39+
extra_body={"stream_options": {"include_usage": True}}
40+
),
3941
)
4042

4143

agentrun/model/api/data.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ def completions(
5454
**self.headers,
5555
**kwargs.get("headers", {}),
5656
}
57+
if kwargs.get("stream_options") is None:
58+
kwargs["stream_options"] = {}
59+
kwargs["stream_options"]["include_usage"] = True
60+
5761
from litellm import completion
5862

5963
return completion(
@@ -82,6 +86,9 @@ def responses(
8286
**self.headers,
8387
**kwargs.get("headers", {}),
8488
}
89+
if kwargs.get("stream_options") is None:
90+
kwargs["stream_options"] = {}
91+
kwargs["stream_options"]["include_usage"] = True
8592
from litellm import responses
8693

8794
return responses(

tests/unittests/integration/test_integration.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,16 @@ def fake_completion(*args, **kwargs):
5353
messages = kwargs.get("messages") or []
5454
tools_payload = kwargs.get("tools")
5555
assert kwargs.get("stream") in (None, False)
56+
assert pydash.get(kwargs, "stream_options.include_usage") is True
57+
5658
return self._build_model_response(messages, tools_payload)
5759

5860
async def fake_acompletion(*args, **kwargs):
5961
messages = kwargs.get("messages") or []
6062
tools_payload = kwargs.get("tools")
6163
assert kwargs.get("stream") in (None, False)
64+
assert pydash.get(kwargs, "stream_options.include_usage") is True
65+
6266
return self._build_model_response(messages, tools_payload)
6367

6468
monkeypatch.setattr("litellm.completion", fake_completion)

0 commit comments

Comments
 (0)