Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a27eb44f62 | |||
| 66b525a24b | |||
| 3dc06c73ef | |||
| 989427c4d3 | |||
| c9406974e9 | |||
| b2328f761a | |||
| 17000dc51e |
@@ -75,6 +75,12 @@ Add to your opencode config:
|
||||
|
||||
Run on multiple machines to combine their power:
|
||||
|
||||
### Features
|
||||
- **Parallel Execution**: Local and peers generate simultaneously for faster consensus
|
||||
- **Streaming Support**: Federation works with streaming responses
|
||||
- **Winner Tracking**: Logs which node (local or peer) won consensus voting
|
||||
- **Token Usage**: Reports accurate token counts for federated responses
|
||||
|
||||
```bash
|
||||
# Machine 1 (Windows with RTX 4060)
|
||||
python main.py --auto --federation
|
||||
@@ -88,6 +94,29 @@ python main.py --auto --federation
|
||||
|
||||
Machines auto-discover each other and vote together on every request.
|
||||
|
||||
### Consensus with Federation
|
||||
1. Your prompt goes to all LLM instances across all machines
|
||||
2. Local swarm and all peers generate **in parallel** (2x faster)
|
||||
3. Wait for **all** nodes to complete generation
|
||||
4. Run global consensus across all responses
|
||||
5. Use federated result (highest confidence from all nodes)
|
||||
|
||||
### Token Reporting
|
||||
|
||||
Federation now provides accurate token counts:
|
||||
- **Prompt tokens**: Counted using tiktoken (cl100k_base encoding)
|
||||
- **Completion tokens**: Counted using tiktoken for federated response
|
||||
- **Total tokens**: Sum of prompt + completion tokens
|
||||
- **Included in**: Final streaming chunk and non-streaming responses
|
||||
|
||||
### Federation with Streaming
|
||||
|
||||
Federation works with streaming responses:
|
||||
- Local swarm and all peers generate in parallel
|
||||
- Stream content from local while waiting for federation
|
||||
- Switch to federated result when consensus complete
|
||||
- Full token reporting in streaming mode
|
||||
|
||||
## How Consensus Works
|
||||
|
||||
1. Your prompt goes to all LLM instances
|
||||
|
||||
@@ -9,4 +9,6 @@ ARGUMENTS: {"url": "https://example.com", "format": "markdown"}
|
||||
|
||||
Available tools: bash, webfetch
|
||||
|
||||
IMPORTANT: Only webfetch URLs that actually exist and are provided by the user. NEVER hallucinate or guess URLs. If a URL returns 404, stop trying to fetch it.
|
||||
|
||||
No explanations. No numbered lists. No markdown. Only tool calls.
|
||||
|
||||
+93
-3
@@ -1,5 +1,6 @@
|
||||
"""OpenAI-compatible API routes for Local Swarm."""
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
@@ -544,10 +545,99 @@ async def chat_completions(request: ChatCompletionRequest, fastapi_request: Requ
|
||||
completion_id = f"chatcmpl-{uuid.uuid4().hex[:12]}"
|
||||
created = int(time.time())
|
||||
|
||||
# Calculate prompt tokens once for usage reporting
|
||||
prompt_tokens = len(TOKEN_ENCODING.encode(prompt))
|
||||
|
||||
if request.stream:
|
||||
# For streaming with tools, return tool_calls to client (opencode) for execution
|
||||
# This enables multi-turn conversations where client executes tools and sends results back
|
||||
if has_tools:
|
||||
# Check if federation is enabled with peers
|
||||
peers_count = 0
|
||||
if federated_swarm is not None and federated_swarm.discovery is not None:
|
||||
peers_count = len(federated_swarm.discovery.get_peers())
|
||||
use_federation = peers_count > 0
|
||||
|
||||
if use_federation:
|
||||
# Use federation for ALL requests (with or without tools)
|
||||
logger.debug(f"🌐 Using federation with {peers_count} peer(s) - waiting for consensus...")
|
||||
|
||||
# Run federation and get consensus result
|
||||
fed_result = await federated_swarm.generate_with_federation(
|
||||
prompt=prompt,
|
||||
max_tokens=request.max_tokens or 1024,
|
||||
temperature=request.temperature or 0.7,
|
||||
min_peers=0
|
||||
)
|
||||
|
||||
logger.debug(f" ✓ Federation consensus complete (strategy: {fed_result.strategy}, confidence: {fed_result.local_confidence:.2f})")
|
||||
logger.debug(f" 🏆 Winner: {fed_result.winner} (check logs above for details)")
|
||||
logger.debug(f" 📝 Using federated response from {len(fed_result.peer_votes) + 1} nodes")
|
||||
|
||||
# Use the federated consensus result
|
||||
content = fed_result.final_response
|
||||
|
||||
# Calculate token counts
|
||||
completion_tokens = len(TOKEN_ENCODING.encode(content)) if content else 0
|
||||
total_tokens = prompt_tokens + completion_tokens
|
||||
|
||||
# Stream the federated response
|
||||
async def federation_stream_generator() -> AsyncIterator[str]:
|
||||
"""Generate SSE stream with federated content."""
|
||||
# Send role chunk
|
||||
first_chunk = ChatCompletionStreamResponse(
|
||||
id=completion_id,
|
||||
created=created,
|
||||
model=request.model,
|
||||
choices=[
|
||||
ChatCompletionStreamChoice(
|
||||
delta={"role": "assistant"}
|
||||
)
|
||||
]
|
||||
)
|
||||
yield f"data: {first_chunk.model_dump_json()}\n\n"
|
||||
|
||||
# Send content in chunks
|
||||
chunk_size = 100
|
||||
for i in range(0, len(content), chunk_size):
|
||||
chunk = content[i:i+chunk_size]
|
||||
stream_chunk = ChatCompletionStreamResponse(
|
||||
id=completion_id,
|
||||
created=created,
|
||||
model=request.model,
|
||||
choices=[
|
||||
ChatCompletionStreamChoice(
|
||||
delta={"content": chunk}
|
||||
)
|
||||
]
|
||||
)
|
||||
yield f"data: {stream_chunk.model_dump_json()}\n\n"
|
||||
|
||||
# Send final chunk with usage
|
||||
from api.models import UsageInfo
|
||||
final_chunk = ChatCompletionStreamResponse(
|
||||
id=completion_id,
|
||||
created=created,
|
||||
model=request.model,
|
||||
choices=[
|
||||
ChatCompletionStreamChoice(
|
||||
delta={},
|
||||
finish_reason="stop"
|
||||
)
|
||||
],
|
||||
usage=UsageInfo(
|
||||
prompt_tokens=prompt_tokens,
|
||||
completion_tokens=completion_tokens,
|
||||
total_tokens=total_tokens
|
||||
)
|
||||
)
|
||||
yield f"data: {final_chunk.model_dump_json()}\n\n"
|
||||
yield "data: [DONE]\n\n"
|
||||
|
||||
return StreamingResponse(
|
||||
federation_stream_generator(),
|
||||
media_type="text/event-stream"
|
||||
)
|
||||
elif has_tools:
|
||||
# For streaming with tools, return tool_calls to client (opencode) for execution
|
||||
# This enables multi-turn conversations where client executes tools and sends results back
|
||||
logger.debug(" 🔧 Streaming with tools - returning tool_calls to client for execution...")
|
||||
# Collect full response
|
||||
full_response = ""
|
||||
|
||||
+63
-42
@@ -5,7 +5,7 @@ Handles communication between peer swarms for distributed consensus.
|
||||
|
||||
import asyncio
|
||||
import time
|
||||
from typing import List, Optional, Dict, Any
|
||||
from typing import List, Optional, Dict, Any, Tuple
|
||||
from dataclasses import dataclass
|
||||
|
||||
from network.discovery import PeerInfo
|
||||
@@ -29,6 +29,7 @@ class FederationResult:
|
||||
local_confidence: float
|
||||
peer_votes: List[PeerVote]
|
||||
strategy: str
|
||||
winner: str = "" # Name of the winning node ("local" or peer name)
|
||||
|
||||
|
||||
class FederationClient:
|
||||
@@ -172,6 +173,8 @@ class FederatedSwarm:
|
||||
) -> FederationResult:
|
||||
"""
|
||||
Generate with federation across peer swarms.
|
||||
|
||||
Optimized: Runs local and peer generation in parallel for maximum speed.
|
||||
|
||||
Args:
|
||||
prompt: Input prompt
|
||||
@@ -182,53 +185,68 @@ class FederatedSwarm:
|
||||
Returns:
|
||||
FederationResult with final response
|
||||
"""
|
||||
# Phase 1: Local generation and consensus
|
||||
print(f" 🏠 Local swarm generating...")
|
||||
local_result = await self.local_swarm.generate(
|
||||
peers = self.discovery.get_peers()
|
||||
|
||||
if len(peers) == 0:
|
||||
if min_peers > 0:
|
||||
raise RuntimeError(f"Federation requires {min_peers} peers, but none found")
|
||||
|
||||
# Solo mode - just run local generation
|
||||
print(f" 🏠 Solo mode - local swarm generating...")
|
||||
local_result = await self.local_swarm.generate(
|
||||
prompt=prompt,
|
||||
max_tokens=max_tokens,
|
||||
temperature=temperature,
|
||||
use_consensus=True
|
||||
)
|
||||
return FederationResult(
|
||||
final_response=local_result.selected_response.text,
|
||||
local_confidence=local_result.confidence,
|
||||
peer_votes=[],
|
||||
strategy="solo"
|
||||
)
|
||||
|
||||
# Parallel generation: Local swarm AND peers generate simultaneously
|
||||
print(f" 🏠 Local swarm AND {len(peers)} peer(s) generating in parallel...")
|
||||
|
||||
# Start local generation
|
||||
local_task = self.local_swarm.generate(
|
||||
prompt=prompt,
|
||||
max_tokens=max_tokens,
|
||||
temperature=temperature,
|
||||
use_consensus=True
|
||||
)
|
||||
|
||||
local_best = local_result.selected_response
|
||||
local_confidence = local_result.confidence
|
||||
|
||||
print(f" ✓ Local best (confidence: {local_confidence:.2f})")
|
||||
|
||||
# Phase 2: Collect peer votes
|
||||
peers = self.discovery.get_peers()
|
||||
|
||||
if len(peers) == 0:
|
||||
if min_peers > 0:
|
||||
raise RuntimeError(f"Federation requires {min_peers} peers, but none found")
|
||||
|
||||
# Solo mode - just return local result
|
||||
return FederationResult(
|
||||
final_response=local_best.text,
|
||||
local_confidence=local_confidence,
|
||||
peer_votes=[],
|
||||
strategy="solo"
|
||||
)
|
||||
|
||||
print(f" 🌐 Requesting votes from {len(peers)} peer(s)...")
|
||||
for peer in peers:
|
||||
print(f" → Contacting {peer.name} at {peer.api_url}")
|
||||
|
||||
peer_votes = []
|
||||
|
||||
# Start peer requests
|
||||
vote_tasks = [
|
||||
self.federation_client.request_vote(peer, prompt, max_tokens, temperature)
|
||||
for peer in peers
|
||||
]
|
||||
|
||||
results = await asyncio.gather(*vote_tasks, return_exceptions=True)
|
||||
|
||||
for peer, result in zip(peers, results):
|
||||
|
||||
# Run everything in parallel
|
||||
all_tasks = [local_task] + vote_tasks
|
||||
results = await asyncio.gather(*all_tasks, return_exceptions=True)
|
||||
|
||||
# Separate local result from peer votes
|
||||
local_result_raw = results[0]
|
||||
if isinstance(local_result_raw, Exception):
|
||||
print(f" ✗ Local swarm failed: {local_result_raw}")
|
||||
raise RuntimeError(f"Local generation failed: {local_result_raw}")
|
||||
|
||||
from swarm.manager import ConsensusResult
|
||||
local_result: ConsensusResult = local_result_raw # Now guaranteed not to be an exception
|
||||
local_best = local_result.selected_response
|
||||
local_confidence = local_result.confidence
|
||||
print(f" ✓ Local completed (confidence: {local_confidence:.2f})")
|
||||
|
||||
# Collect peer votes
|
||||
peer_votes = []
|
||||
for peer, result in zip(peers, results[1:]):
|
||||
if isinstance(result, Exception):
|
||||
print(f" ✗ Peer {peer.name} failed: {result}")
|
||||
elif result is not None:
|
||||
peer_votes.append(result)
|
||||
print(f" ✓ Peer {peer.name} voted (confidence: {result.confidence:.2f})")
|
||||
print(f" ✓ Peer {peer.name} completed (confidence: {result.confidence:.2f})")
|
||||
|
||||
if len(peer_votes) == 0:
|
||||
# No peers responded, use local result
|
||||
@@ -240,16 +258,16 @@ class FederatedSwarm:
|
||||
strategy="local_fallback"
|
||||
)
|
||||
|
||||
# Phase 3: Global consensus
|
||||
# Global consensus
|
||||
print(f" 🗳️ Running global consensus ({len(peer_votes) + 1} votes)...")
|
||||
|
||||
final_response = self._weighted_vote(local_best.text, local_confidence, peer_votes)
|
||||
final_response, winner = self._weighted_vote(local_best.text, local_confidence, peer_votes)
|
||||
|
||||
return FederationResult(
|
||||
final_response=final_response,
|
||||
local_confidence=local_confidence,
|
||||
peer_votes=peer_votes,
|
||||
strategy=self.consensus_strategy
|
||||
strategy=self.consensus_strategy,
|
||||
winner=winner
|
||||
)
|
||||
|
||||
def _weighted_vote(
|
||||
@@ -257,11 +275,14 @@ class FederatedSwarm:
|
||||
local_response: str,
|
||||
local_confidence: float,
|
||||
peer_votes: List[PeerVote]
|
||||
) -> str:
|
||||
) -> Tuple[str, str]:
|
||||
"""
|
||||
Select best response using weighted voting.
|
||||
|
||||
Weights by confidence score. Higher confidence = more weight.
|
||||
|
||||
Returns:
|
||||
Tuple of (selected_response, winner_name)
|
||||
"""
|
||||
# Collect all votes with their weights
|
||||
all_votes = [(local_response, local_confidence, "local")]
|
||||
@@ -292,12 +313,12 @@ class FederatedSwarm:
|
||||
best_idx = max(range(len(scores)), key=lambda i: scores[i])
|
||||
best = all_votes[best_idx]
|
||||
print(f" ✓ Selected response from {best[2]} (quality score: {scores[best_idx]:.2f})")
|
||||
return best[0]
|
||||
return best[0], best[2]
|
||||
|
||||
# Default: weighted selection - pick highest confidence
|
||||
best = max(all_votes, key=lambda x: x[1])
|
||||
print(f" ✓ Selected response from {best[2]} (confidence: {best[1]:.2f})")
|
||||
return best[0]
|
||||
return best[0], best[2]
|
||||
|
||||
async def get_federation_status(self) -> Dict[str, Any]:
|
||||
"""Get current federation status."""
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
"""Unit tests for federation functionality."""
|
||||
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
|
||||
|
||||
from network.federation import FederationResult, PeerVote
|
||||
|
||||
|
||||
def test_federation_result_creation():
|
||||
"""Test FederationResult dataclass creation."""
|
||||
result = FederationResult(
|
||||
final_response="Test response",
|
||||
local_confidence=0.9,
|
||||
peer_votes=[
|
||||
PeerVote(
|
||||
peer_name="peer1",
|
||||
response_text="Peer response 1",
|
||||
confidence=0.85,
|
||||
latency_ms=100,
|
||||
worker_count=1
|
||||
),
|
||||
PeerVote(
|
||||
peer_name="peer2",
|
||||
response_text="Peer response 2",
|
||||
confidence=0.92,
|
||||
latency_ms=120,
|
||||
worker_count=1
|
||||
)
|
||||
],
|
||||
strategy="similarity",
|
||||
winner="peer2"
|
||||
)
|
||||
|
||||
assert result.final_response == "Test response"
|
||||
assert result.local_confidence == 0.9
|
||||
assert len(result.peer_votes) == 2
|
||||
assert result.strategy == "similarity"
|
||||
assert result.winner == "peer2"
|
||||
assert result.peer_votes[0].peer_name == "peer1"
|
||||
assert result.peer_votes[0].confidence == 0.85
|
||||
|
||||
|
||||
def test_peer_vote_creation():
|
||||
"""Test PeerVote dataclass creation."""
|
||||
vote = PeerVote(
|
||||
peer_name="test-peer",
|
||||
response_text="Test response content",
|
||||
confidence=0.95,
|
||||
latency_ms=100,
|
||||
worker_count=1
|
||||
)
|
||||
|
||||
assert vote.peer_name == "test-peer"
|
||||
assert vote.response_text == "Test response content"
|
||||
assert vote.confidence == 0.95
|
||||
|
||||
|
||||
def test_federation_result_empty_peers():
|
||||
"""Test FederationResult with no peer votes."""
|
||||
result = FederationResult(
|
||||
final_response="Local response",
|
||||
local_confidence=1.0,
|
||||
peer_votes=[],
|
||||
strategy="local_only",
|
||||
winner="local"
|
||||
)
|
||||
|
||||
assert result.final_response == "Local response"
|
||||
assert result.local_confidence == 1.0
|
||||
assert len(result.peer_votes) == 0
|
||||
assert result.strategy == "local_only"
|
||||
assert result.winner == "local"
|
||||
|
||||
|
||||
def test_federation_result_multiple_peers():
|
||||
"""Test FederationResult with multiple peer votes."""
|
||||
votes = [
|
||||
PeerVote(f"peer{i}", f"Response {i}", 0.8 + (i * 0.05), 100, 1)
|
||||
for i in range(5)
|
||||
]
|
||||
|
||||
result = FederationResult(
|
||||
final_response="Best response",
|
||||
local_confidence=0.75,
|
||||
peer_votes=votes,
|
||||
strategy="majority",
|
||||
winner="peer3"
|
||||
)
|
||||
|
||||
assert len(result.peer_votes) == 5
|
||||
assert result.peer_votes[2].peer_name == "peer2"
|
||||
assert result.peer_votes[4].confidence == 1.0
|
||||
|
||||
|
||||
def test_federation_result_winner_tracking():
|
||||
"""Test that winner is properly tracked."""
|
||||
result = FederationResult(
|
||||
final_response="Selected response",
|
||||
local_confidence=0.88,
|
||||
peer_votes=[
|
||||
PeerVote("peer1", "Response 1", 0.7, 80, 1),
|
||||
PeerVote("peer2", "Response 2", 0.92, 95, 1),
|
||||
PeerVote("peer3", "Response 3", 0.65, 110, 1)
|
||||
],
|
||||
strategy="highest_confidence",
|
||||
winner="peer2"
|
||||
)
|
||||
|
||||
# Verify winner is in peer_votes
|
||||
winner_names = [v.peer_name for v in result.peer_votes]
|
||||
assert result.winner in winner_names
|
||||
assert result.winner == "peer2"
|
||||
# Verify winner has highest confidence
|
||||
assert result.peer_votes[winner_names.index("peer2")].confidence == 0.92
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Run all tests
|
||||
test_functions = [
|
||||
test_federation_result_creation,
|
||||
test_peer_vote_creation,
|
||||
test_federation_result_empty_peers,
|
||||
test_federation_result_multiple_peers,
|
||||
test_federation_result_winner_tracking,
|
||||
]
|
||||
|
||||
passed = 0
|
||||
failed = 0
|
||||
|
||||
for test_func in test_functions:
|
||||
try:
|
||||
test_func()
|
||||
print(f"✓ {test_func.__name__}")
|
||||
passed += 1
|
||||
except AssertionError as e:
|
||||
print(f"✗ {test_func.__name__}: {e}")
|
||||
failed += 1
|
||||
except Exception as e:
|
||||
print(f"✗ {test_func.__name__}: Exception - {e}")
|
||||
failed += 1
|
||||
|
||||
print(f"\n{passed} passed, {failed} failed")
|
||||
|
||||
if failed > 0:
|
||||
sys.exit(1)
|
||||
Reference in New Issue
Block a user