Update src/txagent/txagent.py
Browse files- src/txagent/txagent.py +248 -165
src/txagent/txagent.py
CHANGED
@@ -1,178 +1,261 @@
|
|
1 |
-
#
|
2 |
import os
|
3 |
-
import
|
4 |
-
import json
|
5 |
-
import shutil
|
6 |
-
from fastapi import FastAPI, HTTPException, UploadFile, File
|
7 |
-
from fastapi.responses import JSONResponse
|
8 |
-
from fastapi.middleware.cors import CORSMiddleware
|
9 |
-
from typing import List, Dict, Optional
|
10 |
import torch
|
11 |
-
|
12 |
-
from
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
tool_cache_dir = os.path.join(persistent_dir, "tool_cache")
|
18 |
-
file_cache_dir = os.path.join(persistent_dir, "cache")
|
19 |
-
report_dir = os.path.join(persistent_dir, "reports")
|
20 |
-
|
21 |
-
# Create directories if they don't exist
|
22 |
-
os.makedirs(model_cache_dir, exist_ok=True)
|
23 |
-
os.makedirs(tool_cache_dir, exist_ok=True)
|
24 |
-
os.makedirs(file_cache_dir, exist_ok=True)
|
25 |
-
os.makedirs(report_dir, exist_ok=True)
|
26 |
-
|
27 |
-
# Set environment variables
|
28 |
-
os.environ["HF_HOME"] = model_cache_dir
|
29 |
-
os.environ["TRANSFORMERS_CACHE"] = model_cache_dir
|
30 |
-
|
31 |
-
# Set up Python path
|
32 |
-
current_dir = os.path.dirname(os.path.abspath(__file__))
|
33 |
-
src_path = os.path.abspath(os.path.join(current_dir, "src"))
|
34 |
-
sys.path.insert(0, src_path)
|
35 |
-
|
36 |
-
# Request models
|
37 |
-
class ChatRequest(BaseModel):
|
38 |
-
message: str
|
39 |
-
temperature: float = 0.7
|
40 |
-
max_new_tokens: int = 512
|
41 |
-
history: Optional[List[Dict]] = None
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
temperature: float = 0.7
|
46 |
-
max_new_tokens: int = 512
|
47 |
-
max_round: int = 5
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
allow_headers=["*"],
|
63 |
-
)
|
64 |
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
75 |
|
76 |
-
def
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
)
|
93 |
-
agent.init_model()
|
94 |
-
return agent
|
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 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
"
|
174 |
-
}
|
175 |
|
176 |
-
|
177 |
-
|
178 |
-
|
|
|
1 |
+
# txagent.py - Core TxAgent class (simplified but maintains key functionality)
|
2 |
import os
|
3 |
+
import logging
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
import torch
|
5 |
+
import json
|
6 |
+
from typing import Dict, Optional, List, Union
|
7 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
|
8 |
+
from sentence_transformers import SentenceTransformer
|
9 |
+
from tooluniverse import ToolUniverse
|
10 |
+
from .toolrag import ToolRAGModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
13 |
+
logger = logging.getLogger("TxAgent")
|
|
|
|
|
|
|
14 |
|
15 |
+
class TxAgent:
|
16 |
+
def __init__(self,
|
17 |
+
model_name: str,
|
18 |
+
rag_model_name: str,
|
19 |
+
tool_files_dict: Optional[Dict] = None,
|
20 |
+
enable_finish: bool = True,
|
21 |
+
enable_rag: bool = False,
|
22 |
+
enable_summary: bool = False,
|
23 |
+
init_rag_num: int = 0,
|
24 |
+
step_rag_num: int = 0,
|
25 |
+
summary_mode: str = 'step',
|
26 |
+
summary_skip_last_k: int = 0,
|
27 |
+
summary_context_length: Optional[int] = None,
|
28 |
+
force_finish: bool = True,
|
29 |
+
avoid_repeat: bool = True,
|
30 |
+
seed: Optional[int] = None,
|
31 |
+
enable_checker: bool = False,
|
32 |
+
enable_chat: bool = False,
|
33 |
+
additional_default_tools: Optional[List] = None):
|
34 |
+
|
35 |
+
# Initialization parameters
|
36 |
+
self.model_name = model_name
|
37 |
+
self.rag_model_name = rag_model_name
|
38 |
+
self.tool_files_dict = tool_files_dict or {}
|
39 |
+
self.enable_finish = enable_finish
|
40 |
+
self.enable_rag = enable_rag
|
41 |
+
self.enable_summary = enable_summary
|
42 |
+
self.summary_mode = summary_mode
|
43 |
+
self.summary_skip_last_k = summary_skip_last_k
|
44 |
+
self.summary_context_length = summary_context_length
|
45 |
+
self.init_rag_num = init_rag_num
|
46 |
+
self.step_rag_num = step_rag_num
|
47 |
+
self.force_finish = force_finish
|
48 |
+
self.avoid_repeat = avoid_repeat
|
49 |
+
self.seed = seed
|
50 |
+
self.enable_checker = enable_checker
|
51 |
+
self.enable_chat = enable_chat
|
52 |
+
self.additional_default_tools = additional_default_tools or []
|
53 |
+
|
54 |
+
# Device setup
|
55 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
56 |
+
|
57 |
+
# Models
|
58 |
+
self.model = None
|
59 |
+
self.tokenizer = None
|
60 |
+
self.rag_model = None
|
61 |
+
self.tooluniverse = None
|
62 |
+
|
63 |
+
# Prompts
|
64 |
+
self.prompt_multi_step = "You are a helpful assistant that solves problems through step-by-step reasoning."
|
65 |
+
self.self_prompt = "Strictly follow the instruction."
|
66 |
+
self.chat_prompt = "You are a helpful assistant for user chat."
|
67 |
+
|
68 |
+
logger.info(f"Initialized TxAgent with model: {model_name} on device: {self.device}")
|
69 |
|
70 |
+
def init_model(self):
|
71 |
+
"""Initialize all models and components"""
|
72 |
+
self.load_llm_model()
|
73 |
+
self.load_rag_model()
|
74 |
+
self.load_tooluniverse()
|
75 |
+
logger.info("All models initialized successfully")
|
|
|
|
|
76 |
|
77 |
+
def load_llm_model(self):
|
78 |
+
"""Load the main LLM model"""
|
79 |
+
try:
|
80 |
+
logger.info(f"Loading LLM model: {self.model_name}")
|
81 |
+
self.tokenizer = AutoTokenizer.from_pretrained(
|
82 |
+
self.model_name,
|
83 |
+
cache_dir=os.getenv("HF_HOME"),
|
84 |
+
trust_remote_code=True
|
85 |
+
)
|
86 |
+
|
87 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
88 |
+
self.model_name,
|
89 |
+
torch_dtype=torch.float16 if self.device.type == "cuda" else torch.float32,
|
90 |
+
device_map="auto",
|
91 |
+
cache_dir=os.getenv("HF_HOME"),
|
92 |
+
trust_remote_code=True
|
93 |
+
)
|
94 |
+
logger.info(f"LLM model loaded on {self.device}")
|
95 |
+
except Exception as e:
|
96 |
+
logger.error(f"Failed to load LLM model: {str(e)}")
|
97 |
+
raise
|
98 |
|
99 |
+
def load_rag_model(self):
|
100 |
+
"""Load the RAG model"""
|
101 |
+
try:
|
102 |
+
logger.info(f"Loading RAG model: {self.rag_model_name}")
|
103 |
+
self.rag_model = ToolRAGModel(self.rag_model_name)
|
104 |
+
logger.info("RAG model loaded successfully")
|
105 |
+
except Exception as e:
|
106 |
+
logger.error(f"Failed to load RAG model: {str(e)}")
|
107 |
+
raise
|
108 |
|
109 |
+
def load_tooluniverse(self):
|
110 |
+
"""Initialize the ToolUniverse"""
|
111 |
+
try:
|
112 |
+
logger.info("Loading ToolUniverse")
|
113 |
+
self.tooluniverse = ToolUniverse(tool_files=self.tool_files_dict)
|
114 |
+
self.tooluniverse.load_tools()
|
115 |
+
|
116 |
+
# Prepare special tools
|
117 |
+
special_tools = self.tooluniverse.prepare_tool_prompts(
|
118 |
+
self.tooluniverse.tool_category_dicts["special_tools"])
|
119 |
+
self.special_tools_name = [tool['name'] for tool in special_tools]
|
120 |
+
|
121 |
+
logger.info(f"ToolUniverse loaded with {len(self.special_tools_name)} special tools")
|
122 |
+
except Exception as e:
|
123 |
+
logger.error(f"Failed to load ToolUniverse: {str(e)}")
|
124 |
+
raise
|
|
|
|
|
|
|
125 |
|
126 |
+
def chat(self, message: str, history: Optional[List[Dict]] = None,
|
127 |
+
temperature: float = 0.7, max_new_tokens: int = 512) -> str:
|
128 |
+
"""Handle chat conversations"""
|
129 |
+
try:
|
130 |
+
conversation = []
|
131 |
+
|
132 |
+
# Initialize with system prompt
|
133 |
+
conversation.append({"role": "system", "content": self.chat_prompt})
|
134 |
+
|
135 |
+
# Add history if provided
|
136 |
+
if history:
|
137 |
+
for msg in history:
|
138 |
+
conversation.append({"role": msg["role"], "content": msg["content"]})
|
139 |
+
|
140 |
+
# Add current message
|
141 |
+
conversation.append({"role": "user", "content": message})
|
142 |
+
|
143 |
+
# Generate response
|
144 |
+
inputs = self.tokenizer.apply_chat_template(
|
145 |
+
conversation,
|
146 |
+
add_generation_prompt=True,
|
147 |
+
return_tensors="pt"
|
148 |
+
).to(self.device)
|
149 |
+
|
150 |
+
generation_config = GenerationConfig(
|
151 |
+
max_new_tokens=max_new_tokens,
|
152 |
+
temperature=temperature,
|
153 |
+
do_sample=True,
|
154 |
+
pad_token_id=self.tokenizer.eos_token_id
|
155 |
+
)
|
156 |
+
|
157 |
+
outputs = self.model.generate(
|
158 |
+
inputs,
|
159 |
+
generation_config=generation_config
|
160 |
+
)
|
161 |
+
|
162 |
+
# Decode and clean up response
|
163 |
+
response = self.tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)
|
164 |
+
return response.strip()
|
165 |
+
|
166 |
+
except Exception as e:
|
167 |
+
logger.error(f"Chat failed: {str(e)}")
|
168 |
+
raise RuntimeError(f"Chat failed: {str(e)}")
|
169 |
|
170 |
+
def run_multistep_agent(self, message: str, temperature: float = 0.7,
|
171 |
+
max_new_tokens: int = 512, max_round: int = 5) -> str:
|
172 |
+
"""Run multi-step reasoning agent"""
|
173 |
+
try:
|
174 |
+
conversation = [{"role": "system", "content": self.prompt_multi_step}]
|
175 |
+
conversation.append({"role": "user", "content": message})
|
176 |
+
|
177 |
+
for _ in range(max_round):
|
178 |
+
# Generate next step
|
179 |
+
inputs = self.tokenizer.apply_chat_template(
|
180 |
+
conversation,
|
181 |
+
add_generation_prompt=True,
|
182 |
+
return_tensors="pt"
|
183 |
+
).to(self.device)
|
184 |
+
|
185 |
+
generation_config = GenerationConfig(
|
186 |
+
max_new_tokens=max_new_tokens,
|
187 |
+
temperature=temperature,
|
188 |
+
do_sample=True,
|
189 |
+
pad_token_id=self.tokenizer.eos_token_id
|
190 |
+
)
|
191 |
+
|
192 |
+
outputs = self.model.generate(
|
193 |
+
inputs,
|
194 |
+
generation_config=generation_config
|
195 |
+
)
|
196 |
+
|
197 |
+
response = self.tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)
|
198 |
+
|
199 |
+
# Check for final answer
|
200 |
+
if "[FinalAnswer]" in response:
|
201 |
+
return response.split("[FinalAnswer]")[-1].strip()
|
202 |
+
|
203 |
+
# Add to conversation
|
204 |
+
conversation.append({"role": "assistant", "content": response})
|
205 |
+
|
206 |
+
# If max rounds reached
|
207 |
+
if self.force_finish:
|
208 |
+
return self._force_final_answer(conversation, temperature, max_new_tokens)
|
209 |
+
|
210 |
+
return "Reasoning rounds exceeded limit without reaching a final answer."
|
211 |
+
|
212 |
+
except Exception as e:
|
213 |
+
logger.error(f"Multi-step agent failed: {str(e)}")
|
214 |
+
raise RuntimeError(f"Multi-step agent failed: {str(e)}")
|
215 |
|
216 |
+
def _force_final_answer(self, conversation: List[Dict], temperature: float, max_new_tokens: int) -> str:
|
217 |
+
"""Force a final answer when max rounds reached"""
|
218 |
+
try:
|
219 |
+
# Add instruction to provide final answer
|
220 |
+
conversation.append({
|
221 |
+
"role": "user",
|
222 |
+
"content": "Provide your final answer now based on all previous reasoning."
|
223 |
+
})
|
224 |
+
|
225 |
+
inputs = self.tokenizer.apply_chat_template(
|
226 |
+
conversation,
|
227 |
+
add_generation_prompt=True,
|
228 |
+
return_tensors="pt"
|
229 |
+
).to(self.device)
|
230 |
+
|
231 |
+
generation_config = GenerationConfig(
|
232 |
+
max_new_tokens=max_new_tokens,
|
233 |
+
temperature=temperature,
|
234 |
+
do_sample=True,
|
235 |
+
pad_token_id=self.tokenizer.eos_token_id
|
236 |
+
)
|
237 |
+
|
238 |
+
outputs = self.model.generate(
|
239 |
+
inputs,
|
240 |
+
generation_config=generation_config
|
241 |
+
)
|
242 |
+
|
243 |
+
response = self.tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)
|
244 |
+
return response.strip()
|
245 |
+
|
246 |
+
except Exception as e:
|
247 |
+
logger.error(f"Failed to force final answer: {str(e)}")
|
248 |
+
return "Failed to generate final answer."
|
249 |
|
250 |
+
def cleanup(self):
|
251 |
+
"""Clean up resources"""
|
252 |
+
if hasattr(self, 'model'):
|
253 |
+
del self.model
|
254 |
+
if hasattr(self, 'rag_model'):
|
255 |
+
del self.rag_model
|
256 |
+
torch.cuda.empty_cache()
|
257 |
+
logger.info("TxAgent resources cleaned up")
|
|
|
258 |
|
259 |
+
def __del__(self):
|
260 |
+
"""Destructor to ensure proper cleanup"""
|
261 |
+
self.cleanup()
|