-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(kimi): clamp completion budget dynamically #2332
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
base: main
Are you sure you want to change the base?
Changes from all commits
34b527c
f26b1f7
a3ed772
d9e76f0
b0e4851
4a0f9ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| import mimetypes | ||
| import os | ||
| import uuid | ||
| from collections.abc import AsyncIterator, Sequence | ||
| from collections.abc import AsyncIterator, Mapping, Sequence | ||
| from typing import TYPE_CHECKING, Any, Literal, Self, Unpack, cast | ||
|
|
||
| import httpx | ||
|
|
@@ -85,7 +85,9 @@ class GenerationKwargs(TypedDict, total=False): | |
| See https://platform.moonshot.ai/docs/api/chat#request-body. | ||
| """ | ||
|
|
||
| max_completion_tokens: int | None | ||
| max_tokens: int | None | ||
| """Deprecated alias. Normalized to ``max_completion_tokens`` before requests.""" | ||
| temperature: float | None | ||
| top_p: float | None | ||
| n: int | None | ||
|
|
@@ -154,17 +156,21 @@ async def generate( | |
| system_prompt: str, | ||
| tools: Sequence[Tool], | ||
| history: Sequence[Message], | ||
| *, | ||
| generation_overrides: Mapping[str, Any] | None = None, | ||
| ) -> "KimiStreamedMessage": | ||
| messages: list[ChatCompletionMessageParam] = [] | ||
| if system_prompt: | ||
| messages.append({"role": "system", "content": system_prompt}) | ||
| messages.extend(_convert_message(message) for message in history) | ||
|
|
||
| generation_kwargs: dict[str, Any] = { | ||
| # default kimi generation kwargs | ||
| "max_tokens": 32000, | ||
| } | ||
| generation_kwargs.update(self._generation_kwargs) | ||
| generation_kwargs: dict[str, Any] = dict(self._generation_kwargs) | ||
| if generation_overrides: | ||
|
Comment on lines
+167
to
+168
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This change removes the provider-level Useful? React with 👍 / 👎. |
||
| generation_kwargs.update( | ||
| _normalize_generation_kwargs( | ||
| cast(Kimi.GenerationKwargs, dict(generation_overrides)) | ||
| ) | ||
| ) | ||
|
|
||
|
Comment on lines
+167
to
174
|
||
| try: | ||
| response = await self.client.chat.completions.create( | ||
|
|
@@ -221,7 +227,7 @@ def with_generation_kwargs(self, **kwargs: Unpack[GenerationKwargs]) -> Self: | |
| """ | ||
| new_self = copy.copy(self) | ||
| new_self._generation_kwargs = copy.deepcopy(self._generation_kwargs) | ||
| new_self._generation_kwargs.update(kwargs) | ||
| new_self._generation_kwargs.update(_normalize_generation_kwargs(kwargs)) | ||
| return new_self | ||
|
|
||
| def with_extra_body(self, extra_body: ExtraBody) -> Self: | ||
|
|
@@ -304,6 +310,14 @@ def _guess_filename(mime_type: str) -> str: | |
| return f"upload{extension}" | ||
|
|
||
|
|
||
| def _normalize_generation_kwargs(kwargs: Kimi.GenerationKwargs) -> Kimi.GenerationKwargs: | ||
| normalized: dict[str, Any] = dict(kwargs) | ||
| max_tokens = normalized.pop("max_tokens", None) | ||
| if max_tokens is not None and "max_completion_tokens" not in normalized: | ||
| normalized["max_completion_tokens"] = max_tokens | ||
| return cast(Kimi.GenerationKwargs, normalized) | ||
|
|
||
|
|
||
| def _convert_message(message: Message) -> ChatCompletionMessageParam: | ||
| message = message.model_copy(deep=True) | ||
| reasoning_content: str = "" | ||
|
|
@@ -507,7 +521,7 @@ async def _dev_main(): | |
| ] | ||
| stream = await chat.with_generation_kwargs( | ||
| temperature=0, | ||
| max_tokens=1000, | ||
| max_completion_tokens=1000, | ||
| ).generate(system_prompt, [], history) | ||
| async for part in stream: | ||
| print(part.model_dump(exclude_none=True)) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing the provider’s default cap here means any Kimi call path that does not add its own budget now sends no
max_completion_tokens/max_tokens. In this repo,/btwbypassesKimiSoul._stepand callskosong.stepdirectly withsoul._runtime.llm.chat_provider(src/kimi_cli/soul/btw.py,execute_side_question), so those requests are now unbounded unless users set an env var. That can cause very long generations and unexpectedly high latency/cost on side-question flows.Useful? React with 👍 / 👎.