Skip to content

Fix/rebase #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: beta/fc
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,13 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Install and configure Poetry
python-version: "3.10"
- name: Install and configure Uv
run: |
make poetry_install
poetry config virtualenvs.create false
make uv_install
- name: Install requirements
run: make install_ci
- name: Lint
run: make lint
- name: Test
run: make test
run: make test
9 changes: 4 additions & 5 deletions .github/workflows/publish_to_pypi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.9'
python-version: "3.10"

- name: Update Release Version From tag
run: |
export VERSION=${{ github.event.release.tag_name }}
python scripts/release_version.py ${VERSION}

- name: Install and configure Poetry
run: |
make poetry_install
poetry config virtualenvs.create false
make uv_install
- name: Install requirements
run: make install
- name: Build dists
Expand All @@ -32,4 +31,4 @@ jobs:
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
password: ${{ secrets.PYPI_API_TOKEN }}
44 changes: 23 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,29 @@
# Default target executed when no arguments are given to make.
all: help

POETRY_VERSION=1.6.1
# poetry command will be installed in ``${HOME}/.local/bin/`` directory.
UV_VERSION=0.6.5
# uv command will be installed in ``${HOME}/.local/bin/`` directory.
PATH = ${HOME}/.local/bin/:$(shell printenv PATH)
poetry_install:
@if ! command -v poetry >/dev/null; then \
echo "Install Poetry"; \
pip install poetry==$(POETRY_VERSION); \
uv_install:
@if ! command -v uv >/dev/null; then \
echo "Install uv"; \
pip install uv==$(UV_VERSION); \
else \
echo "Poetry has been installed"; \
echo "Uv has been installed"; \
fi

@echo "Poetry version: $(shell poetry --version)"
@echo "Uv version: $(shell uv --version)"

install:
poetry install
export UV_SYSTEM_PYTHON=1
uv sync

install_ci:
poetry install --with lint,typing,test
export UV_SYSTEM_PYTHON=1
uv sync --group lint --group typing --group test

build:
poetry build
uv build

clean:
rm -rf dist
Expand All @@ -32,7 +34,7 @@ clean:
TEST_FILE ?= tests/ut/

test:
poetry run pytest $(TEST_FILE)
uv run pytest $(TEST_FILE)

######################
# LINTING AND FORMATTING
Expand All @@ -49,18 +51,18 @@ lint_tests: PYTHON_FILES=tests
lint_tests: MYPY_CACHE=.mypy_cache_test

lint lint_diff lint_package lint_tests:
[ "$(PYTHON_FILES)" = "" ] || poetry run ruff $(PYTHON_FILES) --fix
[ "$(PYTHON_FILES)" = "" ] || poetry run ruff format $(PYTHON_FILES) --diff
[ "$(PYTHON_FILES)" = "" ] || poetry run ruff --select I $(PYTHON_FILES)
[ "$(PYTHON_FILES)" = "" ] || poetry run mypy $(PYTHON_FILES) --install-types --non-interactive
[ "$(PYTHON_FILES)" = "" ] || poetry run mypy $(PYTHON_FILES)
[ "$(PYTHON_FILES)" = "" ] || uv run ruff check --fix $(PYTHON_FILES)
[ "$(PYTHON_FILES)" = "" ] || uv run ruff format $(PYTHON_FILES) --diff
[ "$(PYTHON_FILES)" = "" ] || uv run ruff check --select I $(PYTHON_FILES)
[ "$(PYTHON_FILES)" = "" ] || uv run mypy $(PYTHON_FILES) --install-types --non-interactive
[ "$(PYTHON_FILES)" = "" ] || uv run mypy $(PYTHON_FILES)

format format_diff:
poetry run ruff format $(PYTHON_FILES)
poetry run ruff --select I --fix $(PYTHON_FILES)
uv run ruff format $(PYTHON_FILES)
uv run ruff check --select I --fix $(PYTHON_FILES)

spell_check:
poetry run codespell --toml pyproject.toml
uv run codespell --toml pyproject.toml

spell_fix:
poetry run codespell --toml pyproject.toml -w
uv run codespell --toml pyproject.toml -w
39 changes: 27 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ from typing import AsyncIterable, Union

from arkitect.core.component.llm import BaseChatLanguageModel

from arkitect.core.component.llm.model import (
from arkitect.types.llm.model import (
ArkChatCompletionChunk,
ArkChatParameters,
ArkChatRequest,
Expand Down Expand Up @@ -174,7 +174,7 @@ curl --location 'http://localhost:8080/api/v3/bots/chat/completions' \
}
```

### 插件调用
### 工具调用(Function Calling)

1. 安装 arkitect

Expand All @@ -197,37 +197,51 @@ import os
from typing import AsyncIterable, Union

from arkitect.core.component.llm import BaseChatLanguageModel

from arkitect.core.component.llm.model import (
from arkitect.core.component.tool.ark_tool import link_reader
from arkitect.types.llm.model import (
ArkChatCompletionChunk,
ArkChatParameters,
ArkChatRequest,
ArkChatResponse,
Response,
)
from arkitect.core.component.tool import Calculator, ToolPool
from arkitect.launcher.local.serve import launch_serve
from arkitect.telemetry.trace import task

endpoint_id = "<YOUR ENDPOINT ID>"


# you can define your own methods here and let LLM use as tools
def adder(a: int, b: int) -> int:
"""Add two integer numbers

Args:
a (int): first number
b (int): second number

Returns:
int: sum result
"""
print("calling adder")
return a + b


@task()
async def default_model_calling(
request: ArkChatRequest,
) -> AsyncIterable[Union[ArkChatCompletionChunk, ArkChatResponse]]:
parameters = ArkChatParameters(**request.__dict__)
ToolPool.register(Calculator())

llm = BaseChatLanguageModel(
endpoint_id=endpoint_id,
messages=request.messages,
parameters=parameters,
)
if request.stream:
async for resp in llm.astream(functions=ToolPool.all()):
async for resp in llm.astream(functions=[adder, link_reader]):
yield resp
else:
yield await llm.arun(functions=ToolPool.all())
yield await llm.arun(functions=[adder, link_reader])


@task()
Expand Down Expand Up @@ -265,7 +279,7 @@ curl --location 'http://localhost:8080/api/v3/bots/chat/completions' \
"messages": [
{
"role": "user",
"content": "老王要养马,他有这样一池水:如果养马30匹,8天可可以把水喝光;如果养马25匹,12天把水喝光。老王要养马23匹,那么几天后他要为马找水喝?"
"content": "帮我计算2341234 -13241234"
}
]
}'
Expand All @@ -284,16 +298,17 @@ curl --location 'http://localhost:8080/api/v3/bots/chat/completions' \
"index": 0,
"logprobs": null,
"message": {
"content": "\n首先计算出每天新增的水量,再算出池中原有的水量,最后根据养马数量计算水可以喝的天数,调用 `Calculator/Calculator` 工具进行计算。\n\n假设每匹马每天的饮水量为\\(1\\)份,我们先来求出每天新增的水量。\n\n\n\n假设每匹马每天的饮水量为1份。30匹马8天的饮水量为$30\\times8=240$份,25匹马12天的饮水量为$25\\times12=300$份。那么12天的总饮水量比8天的总饮水量多了$300-240=60$份,这60份水是$12-8=4$天新增加的水量,所以每天新增加的水量为$60\\div4=15$份。则水池原有的水量为$30\\times8-15\\times8=120$份。如果养23匹马,每天实际消耗原水池的水量为$23-15=8$份,所以喝完水池里的水需要$120\\div8=15$天\n15天后他要为马找水喝。",
"content": "用户需要计算2341234 - 13241234,调用`adder`函数实现减法,即`adder(2341234, -13241234)`。2341234 - 13241234的结果是 -10900000。",
"role": "assistant",
"function_call": null,
"tool_calls": null,
"audio": null
"audio": null,
"reasoning_content": null
}
}
],
"created": 1737022804,
"model": "doubao-pro-32k-241215",
"model": "doubao-1-5-pro-32k-250115",
"object": "chat.completion",
"usage": {
"completion_tokens": 558,
Expand Down
15 changes: 8 additions & 7 deletions arkitect/core/component/context/chat_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any, AsyncIterable, Dict, List, Literal, Mapping, Optional, Union
from typing import Any, AsyncIterable, Dict, List, Literal, Optional, Union

from volcenginesdkarkruntime import AsyncArk
from volcenginesdkarkruntime.resources.chat import AsyncChat
Expand All @@ -26,8 +26,10 @@
ChatCompletionMessage,
)

from arkitect.core.component.tool.tool_pool import ToolPool

from .hooks import ChatHook, default_chat_hook
from .model import State, ToolType
from .model import State


class _AsyncCompletions(AsyncCompletions):
Expand All @@ -43,18 +45,17 @@ async def create(
self,
messages: List[ChatCompletionMessageParam],
stream: Optional[Literal[True, False]] = True,
tools: Optional[Mapping[str, ToolType]] = None,
tool_pool: ToolPool | None = None,
**kwargs: Dict[str, Any],
) -> Union[ChatCompletion, AsyncIterable[ChatCompletionChunk]]:
parameters = (
self._state.parameters.__dict__
if self._state.parameters is not None
else {}
)
if tools is not None:
parameters["tools"] = [
tool.tool_schema().model_dump() for tool in tools.values() or []
]
if tool_pool:
tools = await tool_pool.list_tools()
parameters["tools"] = [t.model_dump() for t in tools]
for hook in self.hooks:
messages = await hook(self._state, messages)
resp = await super().create(
Expand Down
Loading
Loading