Commit
·
809ad60
1
Parent(s):
4450782
fix(P0): Complete HuggingFace tool calling integration and document framework display bug
Browse files
docs/bugs/P0_HUGGINGFACE_TOOL_CALLING_BROKEN.md
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# P0 Bug: HuggingFace Free Tier Tool Calling Broken
|
| 2 |
+
|
| 3 |
+
**Severity**: P0 (Critical) - Free Tier cannot perform multi-turn tool-based research
|
| 4 |
+
**Status**: IN_PROGRESS - Root causes identified, fixes pending
|
| 5 |
+
**Discovered**: 2025-12-01
|
| 6 |
+
**Investigator**: Claude Code (Systematic First-Principles Analysis)
|
| 7 |
+
|
| 8 |
+
## Executive Summary
|
| 9 |
+
The HuggingFace Free Tier fails to execute tools end-to-end. While the API calls themselves are valid, the **integration** with the Microsoft Agent Framework is missing a critical middleware component (`@use_function_invocation`), and the conversation history serialization is incomplete.
|
| 10 |
+
|
| 11 |
+
## Root Causes
|
| 12 |
+
|
| 13 |
+
### 1. Missing Tool Execution Middleware (The "Silent Failure")
|
| 14 |
+
**Mechanism**:
|
| 15 |
+
- The `OpenAIChatClient` uses the `@use_function_invocation` decorator, which creates an internal loop:
|
| 16 |
+
1. LLM proposes tools.
|
| 17 |
+
2. Middleware executes tools.
|
| 18 |
+
3. Middleware feeds results back to LLM.
|
| 19 |
+
4. LLM generates final answer.
|
| 20 |
+
- The `HuggingFaceChatClient` **lacked this decorator**.
|
| 21 |
+
- Result: The client returned raw tool calls to the `ChatAgent`. The `ChatAgent` passed them to the `MagenticAgentExecutor`.
|
| 22 |
+
- **Cascade Failure**: The `MagenticAgentExecutor` (in the framework) has a bug/limitation where it handles tool-call-only messages by converting them to their string representation (`repr()`) because they lack text content. This led to the observed `<ChatMessage object ...>` corruption in the logs and history.
|
| 23 |
+
|
| 24 |
+
### 2. Framework Message Corruption (P1 - HIGH, External Bug)
|
| 25 |
+
**Mechanism**:
|
| 26 |
+
- When `MagenticAgentMessageEvent` (which carries agent responses) is generated by the `agent_framework`, the `ChatMessage` object it contains (specifically in `event.message` and its nested `TextContent`) often has its `.text` attribute populated with a Python object's `repr` string (e.g., `<agent_framework._types.ChatMessage object at 0x...>`) instead of the actual human-readable message.
|
| 27 |
+
- DeepBoner's `_extract_text` method correctly identifies these `repr` strings and filters them out.
|
| 28 |
+
- Result: The human-readable agent response is lost at the framework level before DeepBoner can process it for display, leading to empty or uninformative messages in the UI/logs (e.g., `searcher: ...`).
|
| 29 |
+
**Impact**: Display/Logging only. Does not prevent tool execution or core logic, but severely degrades user experience and debugging visibility.
|
| 30 |
+
**Root Cause**: This is an internal issue within the `agent_framework`'s event messaging mechanism, specifically how `ChatMessage` objects are constructed and passed through the `MagenticAgentMessageEvent`. DeepBoner cannot reliably recover the original message text when it has been replaced by a `repr` string by the framework itself.
|
| 31 |
+
**Fix**: Requires an upstream fix or alternative message extraction strategy within the `agent_framework`. Until then, DeepBoner's UI/logs will display truncated or empty messages for these specific events.
|
| 32 |
+
|
| 33 |
+
## Solution Plan
|
| 34 |
+
|
| 35 |
+
1. **Fix History Serialization**: Update `_convert_messages` in `src/clients/huggingface.py` to correctly serialize `tool_calls` (Assistant role) and `tool_call_id` (Tool role) to the HuggingFace / OpenAI format.
|
| 36 |
+
2. **Enable Middleware**: Decorate `HuggingFaceChatClient` with `@use_function_invocation` (and `@use_chat_middleware`, `@use_observability` for parity).
|
| 37 |
+
3. **Display Fix**: Update `AdvancedOrchestrator._extract_text` to gracefully handle any remaining object representations, just in case.
|
| 38 |
+
|
| 39 |
+
## Verification
|
| 40 |
+
- **Reproduction Script**: `reproduce_bugs.py` confirms the serialization failure.
|
| 41 |
+
- **End-to-End Test**: `verify_p0_fix.py` (or similar) will be used to confirm the agent effectively uses tools and synthesizes an answer.
|
| 42 |
+
|
| 43 |
+
## Verified Findings
|
| 44 |
+
|
| 45 |
+
### What WORKS (Confirmed via Testing)
|
| 46 |
+
|
| 47 |
+
1. **Tool Serialization**: `_convert_tools()` correctly converts `AIFunction` → OpenAI JSON format ✅
|
| 48 |
+
2. **First API Call**: HuggingFace returns tool calls on the first request ✅
|
| 49 |
+
3. **Tool Call Parsing**: `_parse_tool_calls()` correctly extracts `FunctionCallContent` ✅
|
| 50 |
+
4. **Function Invoking Marker**: `__function_invoking_chat_client__ = True` is present ✅
|
| 51 |
+
5. **Original P0 (JSON serialization)**: Fixed - no longer crashes with TypeError ✅
|
| 52 |
+
|
| 53 |
+
### What is BROKEN (Root Causes)
|
| 54 |
+
|
| 55 |
+
---
|
| 56 |
+
|
| 57 |
+
## BUG #1: Conversation History Serialization (P0 - CRITICAL)
|
| 58 |
+
|
| 59 |
+
### Symptom
|
| 60 |
+
Multi-turn conversations fail with `BadRequestError` from HuggingFace API.
|
| 61 |
+
|
| 62 |
+
### Root Cause
|
| 63 |
+
`_convert_messages()` in `src/clients/huggingface.py` only extracts `role` and `content` from messages:
|
| 64 |
+
|
| 65 |
+
```python
|
| 66 |
+
def _convert_messages(self, messages: MutableSequence[ChatMessage]) -> list[dict[str, Any]]:
|
| 67 |
+
hf_messages: list[dict[str, Any]] = []
|
| 68 |
+
for msg in messages:
|
| 69 |
+
content = msg.text or ""
|
| 70 |
+
# ... role extraction ...
|
| 71 |
+
hf_messages.append({"role": role_str, "content": content}) # MISSING tool_calls and tool_call_id!
|
| 72 |
+
return hf_messages
|
| 73 |
+
```
|
| 74 |
+
|
| 75 |
+
### What HuggingFace API Expects
|
| 76 |
+
|
| 77 |
+
```json
|
| 78 |
+
[
|
| 79 |
+
{"role": "user", "content": "Search for testosterone"},
|
| 80 |
+
{
|
| 81 |
+
"role": "assistant",
|
| 82 |
+
"content": null,
|
| 83 |
+
"tool_calls": [ // REQUIRED when assistant called a tool
|
| 84 |
+
{
|
| 85 |
+
"id": "call_123",
|
| 86 |
+
"type": "function",
|
| 87 |
+
"function": {"name": "search_pubmed", "arguments": "{\"query\": \"testosterone\"}"}
|
| 88 |
+
}
|
| 89 |
+
]
|
| 90 |
+
},
|
| 91 |
+
{
|
| 92 |
+
"role": "tool",
|
| 93 |
+
"content": "Found 10 papers...",
|
| 94 |
+
"tool_call_id": "call_123" // REQUIRED - must match the tool call id
|
| 95 |
+
}
|
| 96 |
+
]
|
| 97 |
+
```
|
| 98 |
+
|
| 99 |
+
### What We Send
|
| 100 |
+
|
| 101 |
+
```json
|
| 102 |
+
[
|
| 103 |
+
{"role": "user", "content": "Search for testosterone"},
|
| 104 |
+
{"role": "assistant", "content": ""}, // MISSING tool_calls!
|
| 105 |
+
{"role": "tool", "content": "Found 10 papers..."} // MISSING tool_call_id!
|
| 106 |
+
]
|
| 107 |
+
```
|
| 108 |
+
|
| 109 |
+
### Impact
|
| 110 |
+
- First LLM call works (tools called)
|
| 111 |
+
- Second LLM call fails (API rejects malformed history)
|
| 112 |
+
- Research loop never completes
|
| 113 |
+
|
| 114 |
+
### Fix Required
|
| 115 |
+
Update `_convert_messages()` to:
|
| 116 |
+
1. Extract `tool_calls` from `ChatMessage.contents` (list of `FunctionCallContent`)
|
| 117 |
+
2. Add `tool_call_id` to tool messages (requires tracking call IDs)
|
| 118 |
+
|
| 119 |
+
---
|
| 120 |
+
|
| 121 |
+
## BUG #2: Framework Message Corruption (P1 - HIGH)
|
| 122 |
+
|
| 123 |
+
### Symptom
|
| 124 |
+
`MagenticAgentMessageEvent.message.text` contains the repr string of a ChatMessage object:
|
| 125 |
+
```
|
| 126 |
+
'<agent_framework._types.ChatMessage object at 0x10c394210>'
|
| 127 |
+
```
|
| 128 |
+
|
| 129 |
+
### Verified Behavior
|
| 130 |
+
|
| 131 |
+
```python
|
| 132 |
+
# From workflow event inspection:
|
| 133 |
+
event.message.text = '<agent_framework._types.ChatMessage object at 0x...>'
|
| 134 |
+
event.message.contents[0] = TextContent(text='<agent_framework._types.ChatMessage object at 0x..>')
|
| 135 |
+
```
|
| 136 |
+
|
| 137 |
+
### Root Cause Hypothesis
|
| 138 |
+
Somewhere in the Microsoft Agent Framework's workflow orchestration, when converting tool call responses from our `HuggingFaceChatClient`, the framework is:
|
| 139 |
+
1. Taking our `ChatMessage` response
|
| 140 |
+
2. Calling `str()` on it (which gives repr)
|
| 141 |
+
3. Creating a NEW `ChatMessage` with the repr as text content
|
| 142 |
+
|
| 143 |
+
This may be due to:
|
| 144 |
+
- Missing or incompatible `raw_representation` field
|
| 145 |
+
- Framework expecting a specific message structure we don't provide
|
| 146 |
+
- Type coercion issue in the workflow layer
|
| 147 |
+
|
| 148 |
+
### Impact
|
| 149 |
+
- UI shows `<ChatMessage object at 0x...>` instead of actual content
|
| 150 |
+
- Users cannot see what the agent found/did
|
| 151 |
+
- Debugging is difficult
|
| 152 |
+
|
| 153 |
+
### Fix Required
|
| 154 |
+
Investigate `agent_framework`'s `ChatAgent` and `MagenticBuilder` to understand:
|
| 155 |
+
1. How they process `ChatResponse` from the client
|
| 156 |
+
2. What structure they expect in `raw_representation`
|
| 157 |
+
3. Whether there's a required serialization method we're not implementing
|
| 158 |
+
|
| 159 |
+
---
|
| 160 |
+
|
| 161 |
+
## Verification Matrix
|
| 162 |
+
|
| 163 |
+
| Component | Status | Test Command |
|
| 164 |
+
|-----------|--------|--------------|
|
| 165 |
+
| Tool Serialization | ✅ WORKS | `client._convert_tools([search_pubmed])` |
|
| 166 |
+
| First Tool Call | ✅ WORKS | Single-turn API call returns `FunctionCallContent` |
|
| 167 |
+
| Multi-turn History | ❌ BROKEN | BadRequestError on second call |
|
| 168 |
+
| Event Display | ❌ BROKEN | Shows repr instead of content |
|
| 169 |
+
| End-to-End Research | ❌ BROKEN | Max rounds reached, no synthesis |
|
| 170 |
+
|
| 171 |
+
## Reproduction Steps
|
| 172 |
+
|
| 173 |
+
### BUG #1: History Serialization
|
| 174 |
+
|
| 175 |
+
```python
|
| 176 |
+
import asyncio
|
| 177 |
+
from src.clients.huggingface import HuggingFaceChatClient
|
| 178 |
+
from src.agents.tools import search_pubmed
|
| 179 |
+
from agent_framework import ChatMessage, ChatOptions
|
| 180 |
+
from agent_framework._types import Role, ToolMode, FunctionCallContent
|
| 181 |
+
|
| 182 |
+
async def test():
|
| 183 |
+
client = HuggingFaceChatClient()
|
| 184 |
+
|
| 185 |
+
# Round 1: Get tool call
|
| 186 |
+
messages_r1 = [
|
| 187 |
+
ChatMessage(role=Role.USER, text='Search for testosterone'),
|
| 188 |
+
]
|
| 189 |
+
response_r1 = await client._inner_get_response(
|
| 190 |
+
messages=messages_r1,
|
| 191 |
+
chat_options=ChatOptions(tools=[search_pubmed], tool_choice=ToolMode.AUTO),
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
# Round 2: Include tool history (FAILS)
|
| 195 |
+
messages_r2 = [
|
| 196 |
+
ChatMessage(role=Role.USER, text='Search for testosterone'),
|
| 197 |
+
response_r1.messages[0], # Assistant with tool call
|
| 198 |
+
ChatMessage(role=Role.TOOL, text='Found 10 papers...'),
|
| 199 |
+
ChatMessage(role=Role.USER, text='Now search for libido'),
|
| 200 |
+
]
|
| 201 |
+
|
| 202 |
+
# This will throw BadRequestError
|
| 203 |
+
response_r2 = await client._inner_get_response(
|
| 204 |
+
messages=messages_r2,
|
| 205 |
+
chat_options=ChatOptions(tools=[search_pubmed], tool_choice=ToolMode.AUTO),
|
| 206 |
+
)
|
| 207 |
+
|
| 208 |
+
asyncio.run(test())
|
| 209 |
+
```
|
| 210 |
+
|
| 211 |
+
### BUG #2: Event Display
|
| 212 |
+
|
| 213 |
+
```python
|
| 214 |
+
import asyncio
|
| 215 |
+
from src.orchestrators.advanced import AdvancedOrchestrator
|
| 216 |
+
from agent_framework import MagenticAgentMessageEvent
|
| 217 |
+
|
| 218 |
+
async def test():
|
| 219 |
+
orch = AdvancedOrchestrator(max_rounds=1)
|
| 220 |
+
async for event in orch._build_workflow().run_stream('Search for testosterone'):
|
| 221 |
+
if isinstance(event, MagenticAgentMessageEvent):
|
| 222 |
+
print(f"message.text = {event.message.text}") # Shows repr string
|
| 223 |
+
break
|
| 224 |
+
|
| 225 |
+
asyncio.run(test())
|
| 226 |
+
```
|
| 227 |
+
|
| 228 |
+
## Prior Fixes (Verified Working)
|
| 229 |
+
|
| 230 |
+
The following fixes from the `fix/p0-aifunction-serialization` branch ARE working:
|
| 231 |
+
|
| 232 |
+
1. **`_convert_tools()`**: Converts `AIFunction` objects to OpenAI-compatible JSON
|
| 233 |
+
2. **`_parse_tool_calls()`**: Converts HF response tool calls to `FunctionCallContent`
|
| 234 |
+
3. **Streaming accumulator**: Handles partial tool call deltas in streaming mode
|
| 235 |
+
4. **Function invoking marker**: `__function_invoking_chat_client__ = True`
|
| 236 |
+
|
| 237 |
+
These fixes solved the original P0 crash but revealed deeper issues.
|
| 238 |
+
|
| 239 |
+
## Files Requiring Changes
|
| 240 |
+
|
| 241 |
+
### Priority 1 (BUG #1)
|
| 242 |
+
- `src/clients/huggingface.py`
|
| 243 |
+
- `_convert_messages()` - Add tool_calls and tool_call_id serialization
|
| 244 |
+
|
| 245 |
+
### Priority 2 (BUG #2)
|
| 246 |
+
- Investigation needed into `agent_framework` behavior
|
| 247 |
+
- May require changes to `ChatResponse` structure
|
| 248 |
+
- May require implementing `raw_representation` field
|
| 249 |
+
|
| 250 |
+
## Risk Assessment
|
| 251 |
+
|
| 252 |
+
| Risk | Mitigation |
|
| 253 |
+
|------|------------|
|
| 254 |
+
| Breaking existing OpenAI flow | Test with OpenAI after changes |
|
| 255 |
+
| Framework incompatibility | Check agent_framework source/docs |
|
| 256 |
+
| Regression in serialization | Add unit tests for all message types |
|
| 257 |
+
|
| 258 |
+
## Timeline
|
| 259 |
+
|
| 260 |
+
- **BUG #1** can likely be fixed in 1-2 hours with proper test coverage
|
| 261 |
+
- **BUG #2** requires investigation of framework internals (unknown scope)
|
| 262 |
+
|
| 263 |
+
## References
|
| 264 |
+
|
| 265 |
+
- [HuggingFace Chat Completion API - Tool Use](https://huggingface.co/docs/huggingface_hub/package_reference/inference_client#huggingface_hub.InferenceClient.chat_completion)
|
| 266 |
+
- [OpenAI Function Calling](https://platform.openai.com/docs/guides/function-calling)
|
| 267 |
+
- Microsoft Agent Framework source code (internal)
|
src/clients/huggingface.py
CHANGED
|
@@ -6,6 +6,7 @@ an OpenAI API key.
|
|
| 6 |
"""
|
| 7 |
|
| 8 |
import asyncio
|
|
|
|
| 9 |
from collections.abc import AsyncIterable, MutableSequence
|
| 10 |
from functools import partial
|
| 11 |
from typing import Any, cast
|
|
@@ -17,8 +18,13 @@ from agent_framework import (
|
|
| 17 |
ChatOptions,
|
| 18 |
ChatResponse,
|
| 19 |
ChatResponseUpdate,
|
|
|
|
|
|
|
| 20 |
)
|
| 21 |
-
from agent_framework.
|
|
|
|
|
|
|
|
|
|
| 22 |
from huggingface_hub import InferenceClient
|
| 23 |
|
| 24 |
from src.utils.config import settings
|
|
@@ -26,6 +32,9 @@ from src.utils.config import settings
|
|
| 26 |
logger = structlog.get_logger()
|
| 27 |
|
| 28 |
|
|
|
|
|
|
|
|
|
|
| 29 |
class HuggingFaceChatClient(BaseChatClient): # type: ignore[misc]
|
| 30 |
"""Adapter for HuggingFace Inference API with full function calling support."""
|
| 31 |
|
|
@@ -63,15 +72,52 @@ class HuggingFaceChatClient(BaseChatClient): # type: ignore[misc]
|
|
| 63 |
"""Convert framework messages to HuggingFace format."""
|
| 64 |
hf_messages: list[dict[str, Any]] = []
|
| 65 |
for msg in messages:
|
| 66 |
-
# Basic conversion - extend as needed for multi-modal
|
| 67 |
-
content = msg.text or ""
|
| 68 |
# msg.role can be string or enum - extract .value for enums
|
| 69 |
-
# str(Role.USER) -> "Role.USER" (wrong), Role.USER.value -> "user" (correct)
|
| 70 |
if hasattr(msg.role, "value"):
|
| 71 |
role_str = str(msg.role.value)
|
| 72 |
else:
|
| 73 |
role_str = str(msg.role)
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
return hf_messages
|
| 76 |
|
| 77 |
def _convert_tools(self, tools: list[Any] | None) -> list[dict[str, Any]] | None:
|
|
@@ -112,12 +158,7 @@ class HuggingFaceChatClient(BaseChatClient): # type: ignore[misc]
|
|
| 112 |
return json_tools if json_tools else None
|
| 113 |
|
| 114 |
def _parse_tool_calls(self, message: Any) -> list[FunctionCallContent]:
|
| 115 |
-
"""Parse HuggingFace tool_calls into framework FunctionCallContent.
|
| 116 |
-
|
| 117 |
-
HF returns tool_calls as:
|
| 118 |
-
[ChatCompletionOutputToolCall(id='...', function=ChatCompletionOutputFunctionDefinition(
|
| 119 |
-
name='...', arguments='{"key": "value"}'), type='function')]
|
| 120 |
-
"""
|
| 121 |
contents: list[FunctionCallContent] = []
|
| 122 |
|
| 123 |
if not hasattr(message, "tool_calls") or not message.tool_calls:
|
|
@@ -303,6 +344,8 @@ class HuggingFaceChatClient(BaseChatClient): # type: ignore[misc]
|
|
| 303 |
if contents:
|
| 304 |
yield ChatResponseUpdate(
|
| 305 |
contents=contents,
|
|
|
|
|
|
|
| 306 |
)
|
| 307 |
|
| 308 |
except Exception as e:
|
|
|
|
| 6 |
"""
|
| 7 |
|
| 8 |
import asyncio
|
| 9 |
+
import json
|
| 10 |
from collections.abc import AsyncIterable, MutableSequence
|
| 11 |
from functools import partial
|
| 12 |
from typing import Any, cast
|
|
|
|
| 18 |
ChatOptions,
|
| 19 |
ChatResponse,
|
| 20 |
ChatResponseUpdate,
|
| 21 |
+
FinishReason,
|
| 22 |
+
Role,
|
| 23 |
)
|
| 24 |
+
from agent_framework._middleware import use_chat_middleware
|
| 25 |
+
from agent_framework._tools import use_function_invocation
|
| 26 |
+
from agent_framework._types import FunctionCallContent, FunctionResultContent
|
| 27 |
+
from agent_framework.observability import use_observability
|
| 28 |
from huggingface_hub import InferenceClient
|
| 29 |
|
| 30 |
from src.utils.config import settings
|
|
|
|
| 32 |
logger = structlog.get_logger()
|
| 33 |
|
| 34 |
|
| 35 |
+
@use_function_invocation
|
| 36 |
+
@use_observability
|
| 37 |
+
@use_chat_middleware
|
| 38 |
class HuggingFaceChatClient(BaseChatClient): # type: ignore[misc]
|
| 39 |
"""Adapter for HuggingFace Inference API with full function calling support."""
|
| 40 |
|
|
|
|
| 72 |
"""Convert framework messages to HuggingFace format."""
|
| 73 |
hf_messages: list[dict[str, Any]] = []
|
| 74 |
for msg in messages:
|
|
|
|
|
|
|
| 75 |
# msg.role can be string or enum - extract .value for enums
|
|
|
|
| 76 |
if hasattr(msg.role, "value"):
|
| 77 |
role_str = str(msg.role.value)
|
| 78 |
else:
|
| 79 |
role_str = str(msg.role)
|
| 80 |
+
|
| 81 |
+
content_str = msg.text or ""
|
| 82 |
+
tool_calls = []
|
| 83 |
+
tool_call_id = None
|
| 84 |
+
|
| 85 |
+
# Process contents for tool calls and results
|
| 86 |
+
if msg.contents:
|
| 87 |
+
for item in msg.contents:
|
| 88 |
+
if isinstance(item, FunctionCallContent):
|
| 89 |
+
# This is an assistant message invoking a tool
|
| 90 |
+
tool_calls.append(
|
| 91 |
+
{
|
| 92 |
+
"id": item.call_id,
|
| 93 |
+
"type": "function",
|
| 94 |
+
"function": {
|
| 95 |
+
"name": item.name,
|
| 96 |
+
"arguments": (
|
| 97 |
+
item.arguments
|
| 98 |
+
if isinstance(item.arguments, str)
|
| 99 |
+
else json.dumps(item.arguments)
|
| 100 |
+
),
|
| 101 |
+
},
|
| 102 |
+
}
|
| 103 |
+
)
|
| 104 |
+
elif isinstance(item, FunctionResultContent):
|
| 105 |
+
# This is a tool result message
|
| 106 |
+
role_str = "tool"
|
| 107 |
+
tool_call_id = item.call_id
|
| 108 |
+
# For tool results, the content is the result string
|
| 109 |
+
content_str = str(item.result) if item.result is not None else ""
|
| 110 |
+
|
| 111 |
+
message_dict: dict[str, Any] = {"role": role_str, "content": content_str}
|
| 112 |
+
|
| 113 |
+
if tool_calls:
|
| 114 |
+
message_dict["tool_calls"] = tool_calls
|
| 115 |
+
|
| 116 |
+
if tool_call_id:
|
| 117 |
+
message_dict["tool_call_id"] = tool_call_id
|
| 118 |
+
|
| 119 |
+
hf_messages.append(message_dict)
|
| 120 |
+
|
| 121 |
return hf_messages
|
| 122 |
|
| 123 |
def _convert_tools(self, tools: list[Any] | None) -> list[dict[str, Any]] | None:
|
|
|
|
| 158 |
return json_tools if json_tools else None
|
| 159 |
|
| 160 |
def _parse_tool_calls(self, message: Any) -> list[FunctionCallContent]:
|
| 161 |
+
"""Parse HuggingFace tool_calls into framework FunctionCallContent."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
contents: list[FunctionCallContent] = []
|
| 163 |
|
| 164 |
if not hasattr(message, "tool_calls") or not message.tool_calls:
|
|
|
|
| 344 |
if contents:
|
| 345 |
yield ChatResponseUpdate(
|
| 346 |
contents=contents,
|
| 347 |
+
role=Role.ASSISTANT,
|
| 348 |
+
finish_reason=FinishReason.TOOL_CALLS,
|
| 349 |
)
|
| 350 |
|
| 351 |
except Exception as e:
|