Skip to content

Commit 6776def

Browse files
committed
fix(llm): fix reasoning on custom llm
1 parent cc44448 commit 6776def

File tree

7 files changed

+51
-54
lines changed

7 files changed

+51
-54
lines changed

AgentCrew/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.8.4"
1+
__version__ = "0.8.5"

AgentCrew/modules/chat/message/handler.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,6 @@ def process_result(_tool_uses, _input_tokens, _output_tokens):
388388

389389
return await self.get_assistant_response()
390390

391-
if thinking_content:
392-
self._notify("agent_continue", self.agent.name)
393-
394391
# Add assistant response to messages
395392
if assistant_response.strip():
396393
self._messages_append(

AgentCrew/modules/console/console_ui.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
RICH_STYLE_GREEN,
2121
RICH_STYLE_BLUE,
2222
RICH_STYLE_YELLOW,
23-
RICH_STYLE_GREEN_BOLD,
2423
RICH_STYLE_YELLOW_BOLD,
2524
PROMPT_CHAR,
2625
)

AgentCrew/modules/custom_llm/deepinfra_service.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,32 @@ def _process_stream_chunk(
4545
thinking_data
4646
)
4747
"""
48-
chunk_text = None
48+
chunk_text = ""
4949
input_tokens = 0
5050
output_tokens = 0
5151
thinking_content = None # OpenAI doesn't support thinking mode
5252

53+
if (not chunk.choices) or (len(chunk.choices) == 0):
54+
return (
55+
assistant_response or " ",
56+
tool_uses,
57+
input_tokens,
58+
output_tokens,
59+
"",
60+
(thinking_content, None) if thinking_content else None,
61+
)
62+
63+
delta_chunk = chunk.choices[0].delta
5364
# Handle regular content chunks
65+
#
5466
if (
55-
chunk.choices
56-
and len(chunk.choices) > 0
57-
and hasattr(chunk.choices[0].delta, "content")
58-
and chunk.choices[0].delta.content is not None
67+
hasattr(delta_chunk, "reasoning_content")
68+
and delta_chunk.reasoning_content is not None
5969
):
60-
chunk_text = chunk.choices[0].delta.content
70+
thinking_content = delta_chunk.reasoning_content
71+
72+
if hasattr(delta_chunk, "content") and delta_chunk.content is not None:
73+
chunk_text = delta_chunk.content
6174
if "<think>" in chunk_text:
6275
self._is_thinking = True
6376

@@ -87,11 +100,7 @@ def _process_stream_chunk(
87100
output_tokens = chunk.usage.completion_tokens
88101

89102
# Handle tool call chunks
90-
if (
91-
chunk.choices
92-
and len(chunk.choices) > 0
93-
and hasattr(chunk.choices[0].delta, "tool_calls")
94-
):
103+
if hasattr(delta_chunk, "tool_calls"):
95104
delta_tool_calls = chunk.choices[0].delta.tool_calls
96105
if delta_tool_calls:
97106
# Process each tool call in the delta
@@ -162,14 +171,6 @@ def _process_stream_chunk(
162171
except json.JSONDecodeError:
163172
# Arguments JSON is still incomplete, keep accumulating
164173
pass
165-
return (
166-
assistant_response or " ",
167-
tool_uses,
168-
input_tokens,
169-
output_tokens,
170-
"",
171-
(thinking_content, None) if thinking_content else None,
172-
)
173174

174175
return (
175176
assistant_response or " ",

AgentCrew/modules/custom_llm/github_copilot_service.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,6 @@ def _convert_internal_format(self, messages: List[Dict[str, Any]]):
7979
thinking_block = None
8080
for i, msg in enumerate(messages):
8181
msg.pop("agent", None)
82-
if "tool_calls" in msg and msg.get("tool_calls", []):
83-
for tool_call in msg["tool_calls"]:
84-
tool_call["function"] = {}
85-
tool_call["function"]["name"] = tool_call.pop("name", "")
86-
tool_call["function"]["arguments"] = json.dumps(
87-
tool_call.pop("arguments", {})
88-
)
8982
if msg.get("role") == "assistant":
9083
if thinking_block:
9184
msg["reasoning_text"] = thinking_block.get("thinking", "")
@@ -103,6 +96,14 @@ def _convert_internal_format(self, messages: List[Dict[str, Any]]):
10396
)
10497
msg["content"] = []
10598

99+
if "tool_calls" in msg and msg.get("tool_calls", []):
100+
for tool_call in msg["tool_calls"]:
101+
tool_call["function"] = {}
102+
tool_call["function"]["name"] = tool_call.pop("name", "")
103+
tool_call["function"]["arguments"] = json.dumps(
104+
tool_call.pop("arguments", {})
105+
)
106+
106107
if msg.get("role") == "tool":
107108
# Special treatment for GitHub Copilot GPT-4.1 model
108109
# At the the time of writing, GitHub Copilot GPT-4.1 model cannot read tool results with array content
@@ -174,29 +175,32 @@ def _process_stream_chunk(
174175
thinking_content = None # OpenAI doesn't support thinking mode
175176
thinking_signature = None
176177

178+
if (not chunk.choices) or (len(chunk.choices) == 0):
179+
return (
180+
assistant_response or " ",
181+
tool_uses,
182+
input_tokens,
183+
output_tokens,
184+
"",
185+
(thinking_content, None) if thinking_content else None,
186+
)
187+
188+
delta_chunk = chunk.choices[0].delta
189+
177190
# Handle thinking content
178191
if (
179-
chunk.choices
180-
and len(chunk.choices) > 0
181-
and hasattr(chunk.choices[0].delta, "reasoning_text")
182-
and chunk.choices[0].delta.reasoning_text is not None
192+
hasattr(delta_chunk, "reasoning_text")
193+
and delta_chunk.reasoning_text is not None
183194
):
184-
thinking_content = chunk.choices[0].delta.reasoning_text
195+
thinking_content = delta_chunk.reasoning_text
185196

186197
if (
187-
chunk.choices
188-
and len(chunk.choices) > 0
189-
and hasattr(chunk.choices[0].delta, "reasoning_opaque")
190-
and chunk.choices[0].delta.reasoning_opaque is not None
198+
hasattr(delta_chunk, "reasoning_opaque")
199+
and delta_chunk.reasoning_opaque is not None
191200
):
192-
thinking_signature = chunk.choices[0].delta.reasoning_opaque
201+
thinking_signature = delta_chunk.reasoning_opaque
193202
# Handle regular content chunks
194-
if (
195-
chunk.choices
196-
and len(chunk.choices) > 0
197-
and hasattr(chunk.choices[0].delta, "content")
198-
and chunk.choices[0].delta.content is not None
199-
):
203+
if hasattr(delta_chunk, "content") and delta_chunk.content is not None:
200204
chunk_text = chunk.choices[0].delta.content
201205
assistant_response += chunk_text
202206

@@ -208,11 +212,7 @@ def _process_stream_chunk(
208212
output_tokens = chunk.usage.completion_tokens
209213

210214
# Handle tool call chunks
211-
if (
212-
chunk.choices
213-
and len(chunk.choices) > 0
214-
and hasattr(chunk.choices[0].delta, "tool_calls")
215-
):
215+
if hasattr(delta_chunk, "tool_calls"):
216216
delta_tool_calls = chunk.choices[0].delta.tool_calls
217217
if delta_tool_calls:
218218
# Process each tool call in the delta

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "agentcrew-ai"
3-
version = "0.8.4"
3+
version = "0.8.5"
44
requires-python = ">=3.12"
55
classifiers = [
66
"Programming Language :: Python :: 3",

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)