Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,9 +6,25 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
6 |
import time, uuid, json, asyncio, requests
|
7 |
from bs4 import BeautifulSoup
|
8 |
from typing import Optional, List, Union
|
9 |
-
import re
|
10 |
|
11 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
SYSTEM_PROMPT = """
|
13 |
You are a highly advanced AI agent specializing in WordPress & WooCommerce development. You must follow a strict "Think, Act, Answer" workflow for every user request. Your primary directive is to be transparent, showing your thought process before taking any action.
|
14 |
|
@@ -32,20 +48,30 @@ You MUST structure your response within the following XML tags. This is not opti
|
|
32 |
- If you can answer the user's request WITHOUT using a tool, formulate the complete and final answer here.
|
33 |
- If you used a tool, leave this tag empty in your first response. You will be given the tool's output and asked to generate the final answer in a second step.
|
34 |
</final_answer>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
"""
|
36 |
|
37 |
-
#
|
38 |
-
|
|
|
|
|
|
|
39 |
DEVICE = "cpu"
|
40 |
-
print(f"Loading model: {MODEL_ID}")
|
41 |
-
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype=torch.bfloat16, device_map=DEVICE)
|
42 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, padding_side='left')
|
43 |
tokenizer.pad_token = tokenizer.eos_token
|
44 |
print("Model and tokenizer loaded successfully.")
|
45 |
|
46 |
app = FastAPI()
|
47 |
|
48 |
-
# --- Tool Execution Functions ---
|
49 |
def execute_browse_tool(url: str) -> str:
|
50 |
try:
|
51 |
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
|
@@ -58,7 +84,7 @@ def execute_browse_tool(url: str) -> str:
|
|
58 |
except Exception as e:
|
59 |
return f"Error browsing {url}: {str(e)}"
|
60 |
|
61 |
-
# --- Pydantic Models ---
|
62 |
class ContentPart(BaseModel): type: str; text: str
|
63 |
class ChatMessage(BaseModel): role: str; content: Union[str, List[ContentPart]]
|
64 |
class ChatCompletionRequest(BaseModel):
|
@@ -77,7 +103,7 @@ def parse_tag(tag: str, text: str) -> str:
|
|
77 |
# --- API Endpoints ---
|
78 |
@app.get("/models", response_model=ModelList)
|
79 |
async def list_models():
|
80 |
-
return ModelList(data=[ModelData(id=MODEL_ID)])
|
81 |
|
82 |
@app.post("/chat/completions")
|
83 |
async def create_chat_completion(request: ChatCompletionRequest):
|
@@ -94,26 +120,23 @@ async def create_chat_completion(request: ChatCompletionRequest):
|
|
94 |
response_id = f"chatcmpl-{uuid.uuid4()}"
|
95 |
|
96 |
def stream_chunk(content: str):
|
97 |
-
chunk = {"id": response_id, "object": "chat.completion.chunk", "created": int(time.time()), "model": MODEL_ID, "choices": [{"index": 0, "delta": {"content": content}, "finish_reason": None}]}
|
98 |
return f"data: {json.dumps(chunk)}\n\n"
|
99 |
|
100 |
-
# --- STEP 1: Planification ---
|
101 |
initial_messages = [{'role': 'system', 'content': SYSTEM_PROMPT}, {'role': 'user', 'content': user_prompt}]
|
102 |
formatted_prompt = tokenizer.apply_chat_template(initial_messages, tokenize=False, add_generation_prompt=True)
|
103 |
inputs = tokenizer(formatted_prompt, return_tensors="pt", padding=True).to(DEVICE)
|
104 |
-
outputs = model.generate(**inputs, max_new_tokens=
|
105 |
agent_plan = tokenizer.decode(outputs[0][len(inputs['input_ids'][0]):], skip_special_tokens=True)
|
106 |
|
107 |
thinking_text = parse_tag("thinking", agent_plan)
|
108 |
tool_code_text = parse_tag("tool_code", agent_plan)
|
109 |
final_answer_text = parse_tag("final_answer", agent_plan)
|
110 |
|
111 |
-
# --- STEP 2: Diffusion de la pensée ---
|
112 |
if thinking_text:
|
113 |
yield stream_chunk(f"🤔 **Thinking...**\n```thought\n{thinking_text}\n```\n\n")
|
114 |
await asyncio.sleep(0.1)
|
115 |
|
116 |
-
# --- STEP 3: Action & Synthèse ---
|
117 |
tool_call = None
|
118 |
if tool_code_text:
|
119 |
try:
|
@@ -122,7 +145,6 @@ async def create_chat_completion(request: ChatCompletionRequest):
|
|
122 |
pass
|
123 |
|
124 |
if tool_call and 'tool' in tool_call:
|
125 |
-
# --- Exécution et Diffusion de l'Action ---
|
126 |
if tool_call['tool'] == 'browse' and 'url' in tool_call:
|
127 |
url = tool_call['url']
|
128 |
yield stream_chunk(f"🔎 **Action:** Browsing `{url}`...\n\n")
|
@@ -131,7 +153,6 @@ async def create_chat_completion(request: ChatCompletionRequest):
|
|
131 |
else:
|
132 |
tool_context = "Unknown tool requested."
|
133 |
|
134 |
-
# --- Appel de synthèse ---
|
135 |
synthesis_messages = [
|
136 |
{'role': 'system', 'content': SYSTEM_PROMPT},
|
137 |
{'role': 'user', 'content': user_prompt},
|
@@ -140,18 +161,16 @@ async def create_chat_completion(request: ChatCompletionRequest):
|
|
140 |
]
|
141 |
synthesis_prompt = tokenizer.apply_chat_template(synthesis_messages, tokenize=False, add_generation_prompt=True)
|
142 |
synthesis_inputs = tokenizer(synthesis_prompt, return_tensors="pt", padding=True).to(DEVICE)
|
143 |
-
synthesis_outputs = model.generate(**synthesis_inputs, max_new_tokens=
|
144 |
final_response = tokenizer.decode(synthesis_outputs[0][len(synthesis_inputs['input_ids'][0]):], skip_special_tokens=True)
|
145 |
final_answer_text = parse_tag("final_answer", final_response)
|
146 |
|
147 |
-
# --- STEP 4: Diffusion de la Réponse Finale ---
|
148 |
if final_answer_text:
|
149 |
yield stream_chunk(f"✅ **Final Answer:**\n{final_answer_text}")
|
150 |
else:
|
151 |
yield stream_chunk("Agent could not generate a final answer.")
|
152 |
|
153 |
-
|
154 |
-
final_chunk = {"id": response_id, "object": "chat.completion.chunk", "created": int(time.time()), "model": MODEL_ID, "choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]}
|
155 |
yield f"data: {json.dumps(final_chunk)}\n\n"
|
156 |
yield "data: [DONE]\n\n"
|
157 |
|
@@ -159,4 +178,4 @@ async def create_chat_completion(request: ChatCompletionRequest):
|
|
159 |
|
160 |
@app.get("/")
|
161 |
def root():
|
162 |
-
return {"status": "
|
|
|
6 |
import time, uuid, json, asyncio, requests
|
7 |
from bs4 import BeautifulSoup
|
8 |
from typing import Optional, List, Union
|
9 |
+
import re
|
10 |
|
11 |
+
# ==============================================================================
|
12 |
+
# === BLOC DE CONFIGURATION DE L'AGENT ===
|
13 |
+
# === Un jour, changez les valeurs ici pour utiliser un nouveau modèle. ===
|
14 |
+
# ==============================================================================
|
15 |
+
|
16 |
+
MODEL_CONFIG = {
|
17 |
+
# L'identifiant du modèle depuis le Hub Hugging Face (pas de GGUF).
|
18 |
+
"MODEL_ID": "deepseek-ai/deepseek-coder-1.3b-instruct",
|
19 |
+
|
20 |
+
# Paramètres de génération (ajustez si nécessaire pour le nouveau modèle)
|
21 |
+
"MAX_TOKENS_PLAN": 1024, # Tokens max pour la phase de réflexion/planification.
|
22 |
+
"MAX_TOKENS_ANSWER": 1024, # Tokens max pour la réponse finale.
|
23 |
+
"TEMPERATURE": 0.1, # Contrôle la créativité (plus bas = plus déterministe).
|
24 |
+
}
|
25 |
+
|
26 |
+
# --- CERVEAU DE L'AGENT (SYSTEM PROMPT) ---
|
27 |
+
# Si vous changez de modèle, vous devrez peut-être adapter ce prompt à son format.
|
28 |
SYSTEM_PROMPT = """
|
29 |
You are a highly advanced AI agent specializing in WordPress & WooCommerce development. You must follow a strict "Think, Act, Answer" workflow for every user request. Your primary directive is to be transparent, showing your thought process before taking any action.
|
30 |
|
|
|
48 |
- If you can answer the user's request WITHOUT using a tool, formulate the complete and final answer here.
|
49 |
- If you used a tool, leave this tag empty in your first response. You will be given the tool's output and asked to generate the final answer in a second step.
|
50 |
</final_answer>
|
51 |
+
|
52 |
+
### AVAILABLE TOOLS ###
|
53 |
+
- **Web Browser:** To use it, populate the `<tool_code>` tag with a JSON object: `{"tool": "browse", "url": "your_url_here"}`
|
54 |
+
|
55 |
+
### CODING RULES (For the content inside <final_answer>) ###
|
56 |
+
- Always provide secure, efficient, and standard-compliant code.
|
57 |
+
- Explain where to place the code (`functions.php`, custom plugin, etc.).
|
58 |
"""
|
59 |
|
60 |
+
# ==============================================================================
|
61 |
+
# === FIN DU BLOC DE CONFIGURATION - NE PAS MODIFIER CI-DESSOUS ===
|
62 |
+
# ==============================================================================
|
63 |
+
|
64 |
+
# --- Configuration & Model Loading ---
|
65 |
DEVICE = "cpu"
|
66 |
+
print(f"Loading model: {MODEL_CONFIG['MODEL_ID']}")
|
67 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_CONFIG['MODEL_ID'], torch_dtype=torch.bfloat16, device_map=DEVICE)
|
68 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_CONFIG['MODEL_ID'], padding_side='left')
|
69 |
tokenizer.pad_token = tokenizer.eos_token
|
70 |
print("Model and tokenizer loaded successfully.")
|
71 |
|
72 |
app = FastAPI()
|
73 |
|
74 |
+
# --- Tool Execution Functions ---
|
75 |
def execute_browse_tool(url: str) -> str:
|
76 |
try:
|
77 |
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
|
|
|
84 |
except Exception as e:
|
85 |
return f"Error browsing {url}: {str(e)}"
|
86 |
|
87 |
+
# --- Pydantic Models ---
|
88 |
class ContentPart(BaseModel): type: str; text: str
|
89 |
class ChatMessage(BaseModel): role: str; content: Union[str, List[ContentPart]]
|
90 |
class ChatCompletionRequest(BaseModel):
|
|
|
103 |
# --- API Endpoints ---
|
104 |
@app.get("/models", response_model=ModelList)
|
105 |
async def list_models():
|
106 |
+
return ModelList(data=[ModelData(id=MODEL_CONFIG['MODEL_ID'])])
|
107 |
|
108 |
@app.post("/chat/completions")
|
109 |
async def create_chat_completion(request: ChatCompletionRequest):
|
|
|
120 |
response_id = f"chatcmpl-{uuid.uuid4()}"
|
121 |
|
122 |
def stream_chunk(content: str):
|
123 |
+
chunk = {"id": response_id, "object": "chat.completion.chunk", "created": int(time.time()), "model": MODEL_CONFIG['MODEL_ID'], "choices": [{"index": 0, "delta": {"content": content}, "finish_reason": None}]}
|
124 |
return f"data: {json.dumps(chunk)}\n\n"
|
125 |
|
|
|
126 |
initial_messages = [{'role': 'system', 'content': SYSTEM_PROMPT}, {'role': 'user', 'content': user_prompt}]
|
127 |
formatted_prompt = tokenizer.apply_chat_template(initial_messages, tokenize=False, add_generation_prompt=True)
|
128 |
inputs = tokenizer(formatted_prompt, return_tensors="pt", padding=True).to(DEVICE)
|
129 |
+
outputs = model.generate(**inputs, max_new_tokens=MODEL_CONFIG['MAX_TOKENS_PLAN'], eos_token_id=tokenizer.eos_token_id)
|
130 |
agent_plan = tokenizer.decode(outputs[0][len(inputs['input_ids'][0]):], skip_special_tokens=True)
|
131 |
|
132 |
thinking_text = parse_tag("thinking", agent_plan)
|
133 |
tool_code_text = parse_tag("tool_code", agent_plan)
|
134 |
final_answer_text = parse_tag("final_answer", agent_plan)
|
135 |
|
|
|
136 |
if thinking_text:
|
137 |
yield stream_chunk(f"🤔 **Thinking...**\n```thought\n{thinking_text}\n```\n\n")
|
138 |
await asyncio.sleep(0.1)
|
139 |
|
|
|
140 |
tool_call = None
|
141 |
if tool_code_text:
|
142 |
try:
|
|
|
145 |
pass
|
146 |
|
147 |
if tool_call and 'tool' in tool_call:
|
|
|
148 |
if tool_call['tool'] == 'browse' and 'url' in tool_call:
|
149 |
url = tool_call['url']
|
150 |
yield stream_chunk(f"🔎 **Action:** Browsing `{url}`...\n\n")
|
|
|
153 |
else:
|
154 |
tool_context = "Unknown tool requested."
|
155 |
|
|
|
156 |
synthesis_messages = [
|
157 |
{'role': 'system', 'content': SYSTEM_PROMPT},
|
158 |
{'role': 'user', 'content': user_prompt},
|
|
|
161 |
]
|
162 |
synthesis_prompt = tokenizer.apply_chat_template(synthesis_messages, tokenize=False, add_generation_prompt=True)
|
163 |
synthesis_inputs = tokenizer(synthesis_prompt, return_tensors="pt", padding=True).to(DEVICE)
|
164 |
+
synthesis_outputs = model.generate(**synthesis_inputs, max_new_tokens=MODEL_CONFIG['MAX_TOKENS_ANSWER'], do_sample=True, temperature=MODEL_CONFIG['TEMPERATURE'], top_k=50, top_p=0.95, eos_token_id=tokenizer.eos_token_id)
|
165 |
final_response = tokenizer.decode(synthesis_outputs[0][len(synthesis_inputs['input_ids'][0]):], skip_special_tokens=True)
|
166 |
final_answer_text = parse_tag("final_answer", final_response)
|
167 |
|
|
|
168 |
if final_answer_text:
|
169 |
yield stream_chunk(f"✅ **Final Answer:**\n{final_answer_text}")
|
170 |
else:
|
171 |
yield stream_chunk("Agent could not generate a final answer.")
|
172 |
|
173 |
+
final_chunk = {"id": response_id, "object": "chat.completion.chunk", "created": int(time.time()), "model": MODEL_CONFIG['MODEL_ID'], "choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}]}
|
|
|
174 |
yield f"data: {json.dumps(final_chunk)}\n\n"
|
175 |
yield "data: [DONE]\n\n"
|
176 |
|
|
|
178 |
|
179 |
@app.get("/")
|
180 |
def root():
|
181 |
+
return {"status": "Configurable Reasoning Agent is online", "model_id": MODEL_CONFIG['MODEL_ID']}
|