Skip to content

Commit 0d866e2

Browse files
committed
feat: 添加 Google ADK、CrewAI、LangChain 和 PydanticAI 模型适配器的流选项支持,更新数据 API 以包含使用情况,优化测试用例
Change-Id: If568ea68a8f888784070096876f3b61535ea87d7
1 parent fd9f65c commit 0d866e2

File tree

8 files changed

+87
-3
lines changed

8 files changed

+87
-3
lines changed

.github/workflows/release-test.yml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
name: Release Test Package
2+
3+
on:
4+
push:
5+
tags:
6+
- 'test-v*'
7+
8+
jobs:
9+
release-test:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: read
13+
id-token: write
14+
steps:
15+
- uses: actions/checkout@v4
16+
17+
- name: Set up Python
18+
uses: actions/setup-python@v5
19+
with:
20+
python-version: '3.10'
21+
22+
- name: Extract version from tag
23+
run: |
24+
TAG="${{ github.ref_name }}"
25+
# 从 test-v0.0.1 中提取 0.0.1
26+
VERSION="${TAG#test-v}"
27+
echo "VERSION=${VERSION}" >> $GITHUB_ENV
28+
echo "Extracted version: ${VERSION}"
29+
30+
- name: Update package name and version in pyproject.toml
31+
run: |
32+
# 修改包名为 agentrun-inner-test
33+
sed -i 's/name = "agentrun-sdk"/name = "agentrun-inner-test"/' pyproject.toml
34+
# 修改版本号
35+
sed -i 's/version = "[^"]*"/version = "'${VERSION}'"/' pyproject.toml
36+
echo "Updated pyproject.toml:"
37+
head -10 pyproject.toml
38+
39+
- name: Update __version__ in __init__.py
40+
run: |
41+
if grep -q "__version__" agentrun/__init__.py; then
42+
sed -i 's/__version__ = "[^"]*"/__version__ = "'${VERSION}'"/' agentrun/__init__.py
43+
else
44+
sed -i '1a __version__ = "'${VERSION}'"' agentrun/__init__.py
45+
fi
46+
echo "Updated __init__.py version to ${VERSION}"
47+
grep "__version__" agentrun/__init__.py
48+
49+
- name: Build package
50+
run: |
51+
python -m pip install --upgrade pip
52+
pip install build twine
53+
python -m build
54+
echo "Package built successfully"
55+
ls -la dist/
56+
57+
- name: Verify package
58+
run: |
59+
python -m twine check dist/*
60+
echo "Package verification completed"
61+
62+
- name: Publish to PyPI
63+
uses: pypa/gh-action-pypi-publish@release/v1
64+
with:
65+
password: ${{ secrets.PYPI_API_TOKEN }}
66+
verify-metadata: false
67+

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["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["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)