-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.py
More file actions
159 lines (127 loc) · 5.09 KB
/
service.py
File metadata and controls
159 lines (127 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""
SearchAgentService - AgentCompass compatible tool calling service.
Usage:
uvicorn service:app --host 0.0.0.0 --port 8083
Configuration (via AgentCompass):
service_url: "http://localhost:8083/api/tasks"
service_env_params:
MAX_ITERATIONS: "50"
TIMEOUT: "600"
SERPER_API_KEY: "your_serper_key"
JINA_API_KEY: "your_jina_key"
MODEL_NAME: "model_name_for_visit_tool"
BASE_URL: "llm_base_url_for_visit_tool"
API_KEY: "llm_api_key_for_visit_tool"
"""
import logging
from typing import Optional, Dict, Any, List
from fastapi import FastAPI
from pydantic import BaseModel
from fc_inferencer import AsyncFCInferencer, ChatMessage
from tools.registry import build_default_registry
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("SearchAgentService")
app = FastAPI(title="SearchAgentService")
class TaskRequest(BaseModel):
"""AgentCompass compatible request format."""
params: Optional[Dict[str, Any]] = None
benchmark: Optional[str] = None
llm_config: Optional[Dict[str, Any]] = None
modality: Optional[str] = None
service_env_params: Optional[Dict[str, str]] = None
class TaskResponse(BaseModel):
"""AgentCompass compatible response format."""
final_answer: str
trajectory: Optional[List[Dict]] = None
status: str = "completed"
error: Optional[str] = None
@app.post("/api/tasks", response_model=TaskResponse)
async def run_task(request: TaskRequest):
"""Run agent task (AgentCompass WAIT protocol)."""
payload = request.model_dump()
params = payload.get("params", {}) or {}
llm_config = payload.get("llm_config", {}) or {}
env_params = payload.get("service_env_params", {}) or {}
question = params.get("question", "")
if not question:
return TaskResponse(
final_answer="",
status="failed",
error="empty question"
)
if not llm_config.get("model_name") or not llm_config.get("url"):
return TaskResponse(
final_answer="",
status="failed",
error="llm_config must contain model_name and url"
)
model_config = {
"model": llm_config.get("model_name", ""),
"base_url": llm_config.get("url", ""),
"api_key": llm_config.get("api_key", ""),
}
model_infer_params = llm_config.get("model_infer_params", {}) or {}
sample_params = {
"temperature": model_infer_params.get("temperature", 0.7),
"top_p": model_infer_params.get("top_p", 1.0),
}
max_iterations = int(env_params.get("MAX_ITERATIONS", "50"))
timeout = int(env_params.get("TIMEOUT", "600"))
max_retry = int(env_params.get("MAX_RETRY", "50"))
sleep_interval = int(env_params.get("SLEEP_INTERVAL", "5"))
task_id = params.get("task_id", "unknown")
logger.info(f"Starting task {task_id}, model: {model_config['model']}")
# Extract tool API keys from service_env_params and build registry
tool_config = {
"SERPER_API_KEY": env_params.get("SERPER_API_KEY", ""),
"JINA_API_KEY": env_params.get("JINA_API_KEY", ""),
"MODEL_NAME": env_params.get("MODEL_NAME", "") or llm_config.get("model_name", ""),
"BASE_URL": env_params.get("BASE_URL", "") or llm_config.get("url", ""),
"API_KEY": env_params.get("API_KEY", "") or llm_config.get("api_key", "sk-admin"),
}
# Parse enabled tools list (comma-separated), default: search,visit
tools_str = env_params.get("TOOLS", "")
tools = [t.strip() for t in tools_str.split(",") if t.strip()] if tools_str else None
registry = build_default_registry(config=tool_config, tools=tools)
inferencer = AsyncFCInferencer(
model=model_config,
sample_params=sample_params,
registry=registry,
max_iterations=max_iterations,
timeout=timeout,
max_retry=max_retry,
sleep_interval=sleep_interval,
)
try:
messages = [ChatMessage(role="user", content=question)]
result = await inferencer.infer(messages)
final_answer = inferencer.extract_final_answer(result)
logger.info(f"Task {task_id} completed")
return TaskResponse(
final_answer=final_answer,
trajectory=result,
status="completed"
)
except Exception as e:
logger.error(f"Task {task_id} failed: {e}")
return TaskResponse(
final_answer="",
status="failed",
error=str(e)
)
finally:
await inferencer.close()
@app.get("/health")
async def health_check():
"""Health check endpoint."""
return {"status": "healthy", "service": "SearchAgentService"}
if __name__ == "__main__":
import uvicorn
import argparse
parser = argparse.ArgumentParser(description="SearchAgentService")
parser.add_argument("--host", default="0.0.0.0")
parser.add_argument("--port", type=int, default=8083)
parser.add_argument("--workers", type=int, default=4)
args = parser.parse_args()
logger.info(f"Starting on {args.host}:{args.port}")
uvicorn.run("service:app", host=args.host, port=args.port, workers=args.workers)