konen 0.2.0__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {konen-0.2.0 → konen-0.3.0}/PKG-INFO +1 -1
- {konen-0.2.0 → konen-0.3.0}/pyproject.toml +1 -1
- {konen-0.2.0 → konen-0.3.0}/pyproject.toml.orig +1 -1
- {konen-0.2.0 → konen-0.3.0}/src/konen/__init__.py +3 -1
- {konen-0.2.0 → konen-0.3.0}/src/konen/backend/__init__.py +2 -0
- {konen-0.2.0 → konen-0.3.0}/src/konen/backend/claude_engine.py +11 -3
- {konen-0.2.0 → konen-0.3.0}/src/konen/backend/engine_factory.py +9 -0
- {konen-0.2.0 → konen-0.3.0}/src/konen/backend/gemini_engine.py +11 -3
- {konen-0.2.0 → konen-0.3.0}/src/konen/backend/gpt_engine.py +11 -3
- konen-0.3.0/src/konen/backend/qwen_engine.py +191 -0
- {konen-0.2.0 → konen-0.3.0}/src/konen/backend/workspace.py +1 -1
- {konen-0.2.0 → konen-0.3.0}/src/konen/cli/app.py +11 -6
- {konen-0.2.0 → konen-0.3.0}/LICENSE +0 -0
- {konen-0.2.0 → konen-0.3.0}/README.md +0 -0
- {konen-0.2.0 → konen-0.3.0}/src/konen/backend/ai_agent.py +0 -0
- {konen-0.2.0 → konen-0.3.0}/src/konen/backend/ai_team.py +0 -0
- {konen-0.2.0 → konen-0.3.0}/src/konen/backend/engine_router.py +0 -0
- {konen-0.2.0 → konen-0.3.0}/src/konen/backend/interaction_round.py +0 -0
- {konen-0.2.0 → konen-0.3.0}/src/konen/backend/interaction_state.py +0 -0
- {konen-0.2.0 → konen-0.3.0}/src/konen/backend/kimi_engine.py +0 -0
- {konen-0.2.0 → konen-0.3.0}/src/konen/backend/llm_engine.py +0 -0
- {konen-0.2.0 → konen-0.3.0}/src/konen/backend/llm_pool.py +0 -0
- {konen-0.2.0 → konen-0.3.0}/src/konen/backend/llm_response.py +0 -0
- {konen-0.2.0 → konen-0.3.0}/src/konen/backend/orchestrator.py +0 -0
- {konen-0.2.0 → konen-0.3.0}/src/konen/backend/provider_history.py +0 -0
- {konen-0.2.0 → konen-0.3.0}/src/konen/backend/provider_round.py +0 -0
- {konen-0.2.0 → konen-0.3.0}/src/konen/backend/transcript_history.py +0 -0
- {konen-0.2.0 → konen-0.3.0}/src/konen/backend/transcript_round.py +0 -0
- {konen-0.2.0 → konen-0.3.0}/src/konen/cli/__init__.py +0 -0
- {konen-0.2.0 → konen-0.3.0}/src/konen/cli/commands.py +0 -0
- {konen-0.2.0 → konen-0.3.0}/src/konen/cli/completer.py +0 -0
- {konen-0.2.0 → konen-0.3.0}/src/konen/cli/main.py +0 -0
- {konen-0.2.0 → konen-0.3.0}/src/konen/cli/renderer.py +0 -0
- {konen-0.2.0 → konen-0.3.0}/src/konen/cli/session.py +0 -0
|
@@ -17,12 +17,13 @@ from .backend import (
|
|
|
17
17
|
Orchestrator,
|
|
18
18
|
ProviderHistory,
|
|
19
19
|
ProviderRound,
|
|
20
|
+
QwenEngine,
|
|
20
21
|
TranscriptHistory,
|
|
21
22
|
TranscriptRound,
|
|
22
23
|
Workspace,
|
|
23
24
|
)
|
|
24
25
|
|
|
25
|
-
__version__ = "0.
|
|
26
|
+
__version__ = "0.3.0"
|
|
26
27
|
|
|
27
28
|
__all__ = [
|
|
28
29
|
"AIAgent",
|
|
@@ -41,6 +42,7 @@ __all__ = [
|
|
|
41
42
|
"Orchestrator",
|
|
42
43
|
"ProviderHistory",
|
|
43
44
|
"ProviderRound",
|
|
45
|
+
"QwenEngine",
|
|
44
46
|
"TranscriptHistory",
|
|
45
47
|
"TranscriptRound",
|
|
46
48
|
"Workspace",
|
|
@@ -16,6 +16,7 @@ from .llm_response import LLMResponse
|
|
|
16
16
|
from .orchestrator import Orchestrator
|
|
17
17
|
from .provider_history import ProviderHistory
|
|
18
18
|
from .provider_round import ProviderRound
|
|
19
|
+
from .qwen_engine import QwenEngine
|
|
19
20
|
from .transcript_history import TranscriptHistory
|
|
20
21
|
from .transcript_round import TranscriptRound
|
|
21
22
|
from .workspace import Workspace
|
|
@@ -37,6 +38,7 @@ __all__ = [
|
|
|
37
38
|
"Orchestrator",
|
|
38
39
|
"ProviderHistory",
|
|
39
40
|
"ProviderRound",
|
|
41
|
+
"QwenEngine",
|
|
40
42
|
"TranscriptHistory",
|
|
41
43
|
"TranscriptRound",
|
|
42
44
|
"Workspace",
|
|
@@ -18,10 +18,18 @@ class ClaudeEngine(LLMEngine):
|
|
|
18
18
|
super().__post_init__()
|
|
19
19
|
|
|
20
20
|
def _initialize_client(self) -> Anthropic:
|
|
21
|
+
"""Initialize the Anthropic client from engine configuration."""
|
|
22
|
+
client_kwargs: dict[str, Any] = {}
|
|
23
|
+
|
|
21
24
|
api_key = self.config.get("api_key")
|
|
22
|
-
if api_key is None:
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
if api_key is not None:
|
|
26
|
+
client_kwargs["api_key"] = api_key
|
|
27
|
+
|
|
28
|
+
base_url = self.config.get("base_url")
|
|
29
|
+
if base_url is not None:
|
|
30
|
+
client_kwargs["base_url"] = base_url
|
|
31
|
+
|
|
32
|
+
return Anthropic(**client_kwargs)
|
|
25
33
|
|
|
26
34
|
def prepare_provider_state(
|
|
27
35
|
self, states: list[dict[str, Any]]
|
|
@@ -6,6 +6,7 @@ from .gemini_engine import GeminiEngine
|
|
|
6
6
|
from .gpt_engine import GPTEngine
|
|
7
7
|
from .kimi_engine import KimiEngine
|
|
8
8
|
from .llm_engine import LLMEngine
|
|
9
|
+
from .qwen_engine import QwenEngine
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
class EngineFactory:
|
|
@@ -80,8 +81,16 @@ class EngineFactory:
|
|
|
80
81
|
EngineFactory._validate_builder_kwargs(kwargs)
|
|
81
82
|
return KimiEngine(**kwargs)
|
|
82
83
|
|
|
84
|
+
@staticmethod
|
|
85
|
+
def _qwen_engine_builder(kwargs: dict[str, Any]) -> LLMEngine:
|
|
86
|
+
kwargs = dict(kwargs)
|
|
87
|
+
kwargs["model_family"] = "qwen"
|
|
88
|
+
EngineFactory._validate_builder_kwargs(kwargs)
|
|
89
|
+
return QwenEngine(**kwargs)
|
|
90
|
+
|
|
83
91
|
|
|
84
92
|
EngineFactory.register("claude", EngineFactory._claude_engine_builder)
|
|
85
93
|
EngineFactory.register("gemini", EngineFactory._gemini_engine_builder)
|
|
86
94
|
EngineFactory.register("gpt", EngineFactory._gpt_engine_builder)
|
|
87
95
|
EngineFactory.register("kimi", EngineFactory._kimi_engine_builder)
|
|
96
|
+
EngineFactory.register("qwen", EngineFactory._qwen_engine_builder)
|
|
@@ -17,10 +17,18 @@ class GeminiEngine(LLMEngine):
|
|
|
17
17
|
super().__post_init__()
|
|
18
18
|
|
|
19
19
|
def _initialize_client(self) -> genai.Client:
|
|
20
|
+
"""Initialize the Gemini client from engine configuration."""
|
|
21
|
+
client_kwargs: dict[str, Any] = {}
|
|
22
|
+
|
|
20
23
|
api_key = self.config.get("api_key")
|
|
21
|
-
if api_key is None:
|
|
22
|
-
|
|
23
|
-
|
|
24
|
+
if api_key is not None:
|
|
25
|
+
client_kwargs["api_key"] = api_key
|
|
26
|
+
|
|
27
|
+
base_url = self.config.get("base_url")
|
|
28
|
+
if base_url is not None:
|
|
29
|
+
client_kwargs["http_options"] = genai.types.HttpOptions(base_url=base_url)
|
|
30
|
+
|
|
31
|
+
return genai.Client(**client_kwargs)
|
|
24
32
|
|
|
25
33
|
def generate_response(
|
|
26
34
|
self,
|
|
@@ -17,10 +17,18 @@ class GPTEngine(LLMEngine):
|
|
|
17
17
|
super().__post_init__()
|
|
18
18
|
|
|
19
19
|
def _initialize_client(self) -> OpenAI:
|
|
20
|
+
"""Initialize the OpenAI client from engine configuration."""
|
|
21
|
+
client_kwargs: dict[str, Any] = {}
|
|
22
|
+
|
|
20
23
|
api_key = self.config.get("api_key")
|
|
21
|
-
if api_key is None:
|
|
22
|
-
|
|
23
|
-
|
|
24
|
+
if api_key is not None:
|
|
25
|
+
client_kwargs["api_key"] = api_key
|
|
26
|
+
|
|
27
|
+
base_url = self.config.get("base_url")
|
|
28
|
+
if base_url is not None:
|
|
29
|
+
client_kwargs["base_url"] = base_url
|
|
30
|
+
|
|
31
|
+
return OpenAI(**client_kwargs)
|
|
24
32
|
|
|
25
33
|
def generate_response(
|
|
26
34
|
self,
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import copy
|
|
2
|
+
import os
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from openai import OpenAI
|
|
7
|
+
|
|
8
|
+
from .llm_engine import LLMEngine
|
|
9
|
+
from .llm_response import LLMResponse
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(slots=True)
|
|
13
|
+
class QwenEngine(LLMEngine):
|
|
14
|
+
"""Qwen execution engine for OpenAI-compatible endpoints."""
|
|
15
|
+
|
|
16
|
+
def __post_init__(self) -> None:
|
|
17
|
+
if self.model_family != "qwen":
|
|
18
|
+
raise ValueError("QwenEngine requires model_family='qwen'.")
|
|
19
|
+
super().__post_init__()
|
|
20
|
+
|
|
21
|
+
def _initialize_client(self) -> OpenAI:
|
|
22
|
+
"""Initialize the Qwen client from configuration or environment."""
|
|
23
|
+
base_url = self.config.get("base_url")
|
|
24
|
+
if base_url is None:
|
|
25
|
+
base_url = os.getenv("QWEN_BASE_URL")
|
|
26
|
+
if not isinstance(base_url, str) or not base_url.strip():
|
|
27
|
+
raise ValueError(
|
|
28
|
+
"Set QWEN_BASE_URL or provide QwenEngine.config['base_url']."
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
api_key = self.config.get("api_key")
|
|
32
|
+
if api_key is None:
|
|
33
|
+
api_key = os.getenv("QWEN_API_KEY")
|
|
34
|
+
if not isinstance(api_key, str) or not api_key.strip():
|
|
35
|
+
raise ValueError(
|
|
36
|
+
"Set QWEN_API_KEY or provide QwenEngine.config['api_key']."
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
return OpenAI(base_url=base_url, api_key=api_key)
|
|
40
|
+
|
|
41
|
+
def prepare_provider_state(
|
|
42
|
+
self, states: list[dict[str, Any]]
|
|
43
|
+
) -> dict[str, Any] | None:
|
|
44
|
+
"""Prepare Qwen message history from prior provider states."""
|
|
45
|
+
if not states:
|
|
46
|
+
return None
|
|
47
|
+
|
|
48
|
+
messages: list[dict[str, Any]] = []
|
|
49
|
+
for state in states:
|
|
50
|
+
for key, role in (
|
|
51
|
+
("user_message", "user"),
|
|
52
|
+
("assistant_message", "assistant"),
|
|
53
|
+
):
|
|
54
|
+
message = state.get(key)
|
|
55
|
+
if not isinstance(message, dict) or message.get("role") != role:
|
|
56
|
+
raise ValueError(
|
|
57
|
+
f"Qwen provider state must contain '{key}' with role '{role}'."
|
|
58
|
+
)
|
|
59
|
+
messages.append(message)
|
|
60
|
+
|
|
61
|
+
return {"messages": copy.deepcopy(messages)}
|
|
62
|
+
|
|
63
|
+
def generate_response(
|
|
64
|
+
self,
|
|
65
|
+
prompt: str,
|
|
66
|
+
system_instruction: str | None = None,
|
|
67
|
+
provider_state: dict[str, Any] | None = None,
|
|
68
|
+
) -> LLMResponse:
|
|
69
|
+
"""Generate a response with the Chat Completions API."""
|
|
70
|
+
messages = self._previous_messages(provider_state)
|
|
71
|
+
if system_instruction is not None:
|
|
72
|
+
messages.insert(0, {"role": "system", "content": system_instruction})
|
|
73
|
+
user_message = {"role": "user", "content": prompt}
|
|
74
|
+
messages.append(user_message)
|
|
75
|
+
|
|
76
|
+
completion = self.client.chat.completions.create(
|
|
77
|
+
model=self.model_name, messages=messages, **self._request_options()
|
|
78
|
+
)
|
|
79
|
+
if not completion.choices:
|
|
80
|
+
raise RuntimeError("Qwen response did not return a completion choice.")
|
|
81
|
+
|
|
82
|
+
choice = completion.choices[0]
|
|
83
|
+
message = choice.message
|
|
84
|
+
if message.tool_calls or message.function_call:
|
|
85
|
+
raise RuntimeError(
|
|
86
|
+
"Qwen returned tool calls, but Konen tool continuation is not "
|
|
87
|
+
"implemented."
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
metadata = self._build_response_metadata()
|
|
91
|
+
metadata["provider_response_id"] = completion.id
|
|
92
|
+
metadata["finish_reason"] = choice.finish_reason
|
|
93
|
+
|
|
94
|
+
usage = completion.usage
|
|
95
|
+
if usage is not None:
|
|
96
|
+
token_usage = {
|
|
97
|
+
"input_token_count": usage.prompt_tokens,
|
|
98
|
+
"output_token_count": usage.completion_tokens,
|
|
99
|
+
"total_token_count": usage.total_tokens,
|
|
100
|
+
}
|
|
101
|
+
output_details = usage.completion_tokens_details
|
|
102
|
+
if (
|
|
103
|
+
output_details is not None
|
|
104
|
+
and output_details.reasoning_tokens is not None
|
|
105
|
+
):
|
|
106
|
+
token_usage["reasoning_token_count"] = output_details.reasoning_tokens
|
|
107
|
+
metadata["token_usage"] = token_usage
|
|
108
|
+
|
|
109
|
+
text = message.content or message.refusal or None
|
|
110
|
+
assistant_message = {"role": "assistant", "content": text or ""}
|
|
111
|
+
# Qwen servers can return either field name for the reasoning trace.
|
|
112
|
+
reasoning_content = getattr(message, "reasoning_content", None)
|
|
113
|
+
if reasoning_content is None:
|
|
114
|
+
reasoning_content = getattr(message, "reasoning", None)
|
|
115
|
+
if isinstance(reasoning_content, str):
|
|
116
|
+
assistant_message["reasoning_content"] = reasoning_content
|
|
117
|
+
|
|
118
|
+
return LLMResponse(
|
|
119
|
+
text=text,
|
|
120
|
+
metadata=metadata,
|
|
121
|
+
provider_state={
|
|
122
|
+
"user_message": user_message,
|
|
123
|
+
"assistant_message": assistant_message,
|
|
124
|
+
},
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
def _request_options(self) -> dict[str, Any]:
|
|
128
|
+
"""Return the configured Qwen request options."""
|
|
129
|
+
options: dict[str, Any] = {}
|
|
130
|
+
extra_body: dict[str, Any] = {}
|
|
131
|
+
|
|
132
|
+
reasoning_effort = self.config.get("reasoning_effort")
|
|
133
|
+
if reasoning_effort is not None:
|
|
134
|
+
if not isinstance(reasoning_effort, str) or not reasoning_effort.strip():
|
|
135
|
+
raise ValueError(
|
|
136
|
+
"QwenEngine.config['reasoning_effort'] must be a non-empty string."
|
|
137
|
+
)
|
|
138
|
+
options["reasoning_effort"] = reasoning_effort
|
|
139
|
+
|
|
140
|
+
chat_template_kwargs = self.config.get("chat_template_kwargs")
|
|
141
|
+
if chat_template_kwargs is not None:
|
|
142
|
+
if not isinstance(chat_template_kwargs, dict):
|
|
143
|
+
raise ValueError(
|
|
144
|
+
"QwenEngine.config['chat_template_kwargs'] must be a dictionary."
|
|
145
|
+
)
|
|
146
|
+
extra_body["chat_template_kwargs"] = copy.deepcopy(chat_template_kwargs)
|
|
147
|
+
|
|
148
|
+
thinking_token_budget = self.config.get("thinking_token_budget")
|
|
149
|
+
if thinking_token_budget is not None:
|
|
150
|
+
if (
|
|
151
|
+
not isinstance(thinking_token_budget, int)
|
|
152
|
+
or isinstance(thinking_token_budget, bool)
|
|
153
|
+
or thinking_token_budget < 0
|
|
154
|
+
):
|
|
155
|
+
raise ValueError(
|
|
156
|
+
"QwenEngine.config['thinking_token_budget'] must be a "
|
|
157
|
+
"non-negative integer."
|
|
158
|
+
)
|
|
159
|
+
extra_body["thinking_token_budget"] = thinking_token_budget
|
|
160
|
+
|
|
161
|
+
if extra_body:
|
|
162
|
+
options["extra_body"] = extra_body
|
|
163
|
+
|
|
164
|
+
return options
|
|
165
|
+
|
|
166
|
+
@staticmethod
|
|
167
|
+
def _previous_messages(
|
|
168
|
+
provider_state: dict[str, Any] | None,
|
|
169
|
+
) -> list[dict[str, Any]]:
|
|
170
|
+
"""Validate and return prior Qwen messages."""
|
|
171
|
+
if provider_state is None:
|
|
172
|
+
return []
|
|
173
|
+
|
|
174
|
+
messages = provider_state.get("messages")
|
|
175
|
+
if not isinstance(messages, list) or not messages:
|
|
176
|
+
raise ValueError(
|
|
177
|
+
"Qwen prepared provider state must contain a non-empty 'messages' list."
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
for message in messages:
|
|
181
|
+
if not isinstance(message, dict) or message.get("role") not in (
|
|
182
|
+
"user",
|
|
183
|
+
"assistant",
|
|
184
|
+
):
|
|
185
|
+
raise ValueError(
|
|
186
|
+
"Qwen provider state messages must have role 'user' or 'assistant'."
|
|
187
|
+
)
|
|
188
|
+
if not isinstance(message.get("content"), str):
|
|
189
|
+
raise ValueError("Qwen provider state messages must contain text.")
|
|
190
|
+
|
|
191
|
+
return copy.deepcopy(messages)
|
|
@@ -506,7 +506,7 @@ class Workspace:
|
|
|
506
506
|
"properties": {
|
|
507
507
|
"model_family": {
|
|
508
508
|
"type": "string",
|
|
509
|
-
"enum": ["claude", "gemini", "gpt", "kimi"],
|
|
509
|
+
"enum": ["claude", "gemini", "gpt", "kimi", "qwen"],
|
|
510
510
|
},
|
|
511
511
|
"model_name": {"type": "string"},
|
|
512
512
|
"config": {"type": "object"},
|
|
@@ -292,14 +292,15 @@ class KonenCLI:
|
|
|
292
292
|
def _add_engine(self) -> None:
|
|
293
293
|
"""Interactively add an engine to the workspace."""
|
|
294
294
|
engine_uid = self._prompt_required("Engine UID: ")
|
|
295
|
+
model_families = ("claude", "gemini", "gpt", "kimi", "qwen")
|
|
295
296
|
model_family = self.session.prompt(
|
|
296
297
|
"Model family: ",
|
|
297
|
-
completer=WordCompleter(
|
|
298
|
-
["claude", "gemini", "gpt", "kimi"], ignore_case=False
|
|
299
|
-
),
|
|
298
|
+
completer=WordCompleter(model_families, ignore_case=False),
|
|
300
299
|
).strip()
|
|
301
|
-
if model_family not in
|
|
302
|
-
raise ValueError(
|
|
300
|
+
if model_family not in model_families:
|
|
301
|
+
raise ValueError(
|
|
302
|
+
f"Model family must be one of: {', '.join(model_families)}."
|
|
303
|
+
)
|
|
303
304
|
|
|
304
305
|
model_name = self._prompt_required("Model name: ")
|
|
305
306
|
config_text = self.session.prompt(
|
|
@@ -313,7 +314,11 @@ class KonenCLI:
|
|
|
313
314
|
model_name=model_name,
|
|
314
315
|
config=config,
|
|
315
316
|
)
|
|
316
|
-
|
|
317
|
+
try:
|
|
318
|
+
self._reload_runtime()
|
|
319
|
+
except Exception:
|
|
320
|
+
self.workspace.remove_engine(engine_uid)
|
|
321
|
+
raise
|
|
317
322
|
self.renderer.show_info(f"Engine '{engine_uid}' added.")
|
|
318
323
|
|
|
319
324
|
def _remove_engine(self, engine_uid: str) -> None:
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|