Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100import torchimport jsonimport reimport datetimefrom flask import Flask, request, Response, render_templatefrom transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamerfrom threading import Threadapp = Flask(__name__)# 1. TOOL DEFINITIONSdef get_current_datetime(query: str = ""):"""Returns the current date and time."""return f"Observation: The current date and time is {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}."def simple_calculator(expression: str):"""An easy-to-construct tool for basic math (add, sub, mult, div)."""try:# Source 351: Calculators are essential tools for deterministic results.# Note: In production, use a safer math parser instead of eval.result = eval(expression, {"__builtins__": None}, {})return f"Observation: The calculation result is {result}."except Exception as e:return f"Observation: Error in calculation: {str(e)}."# Tool Registrytools = {"get_current_datetime": get_current_datetime,"simple_calculator": simple_calculator}# Load Modelmodel_id = "AshokGakr/model-tiny"tokenizer = AutoTokenizer.from_pretrained(model_id)model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")SYSTEM_PROMPT = """ROLE: You are a ReAct Agent. You solve tasks using this loop:Thought: (Reasoning about what to do)Action: (Tool name: 'get_current_datetime' or 'simple_calculator')Action Input: (Parameter for the tool)Observation: (Result from the tool - provided to you)... (Repeat Thought/Action/Observation if needed)Final Answer: (The final response to the user)AVAILABLE TOOLS:- get_current_datetime: Use this for any questions about the current date or time. No input needed.- simple_calculator: Use this for any math calculations. Input should be a math expression (e.g., '10 + 5')."""@app.route('/')def index():return render_template('index.html')@app.route('/chat', methods=['POST'])def chat():data = request.jsonuser_query = data.get("message", "")def generate_agent_response():# Source 13: Episodic memory maintains the conversation trajectory.history = [{"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": user_query}]for i in range(5): # Limit iterations to prevent infinite loops [5]input_ids = tokenizer.apply_chat_template(history, add_generation_prompt=True, return_tensors="pt").to(model.device)streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)thread = Thread(target=model.generate, kwargs={"input_ids": input_ids, "streamer": streamer, "max_new_tokens": 256})thread.start()full_turn_output = ""for new_text in streamer:full_turn_output += new_textyield new_text # Stream thoughts to the UI [6]# Check for Action [7]action_match = re.search(r"Action:\s*(\w+)", full_turn_output)input_match = re.search(r"Action Input:\s*(.*)", full_turn_output)if action_match and input_match:tool_name = action_match.group(1).strip()tool_input = input_match.group(1).strip()if tool_name in tools:obs = tools[tool_name](tool_input)yield f"\n{obs}\n"# Feed observation back into history [8, 9]history.append({"role": "assistant", "content": full_turn_output})history.append({"role": "user", "content": obs})else:breakelif "Final Answer:" in full_turn_output:breakelse:breakreturn Response(generate_agent_response(), mimetype='text/plain')if __name__ == '__main__':app.run(host='0.0.0.0', port=7860)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118import torchimport refrom typing import Annotated, TypedDict, Unionfrom flask import Flask, request, Response, render_templatefrom transformers import AutoModelForCausalLM, AutoTokenizerfrom langgraph.graph import StateGraph, ENDfrom langchain_core.tools import toolfrom pydantic import BaseModel, Fieldapp = Flask(__name__)# 1. DEFINE STRUCTURED TOOLS WITH PYDANTICclass CalcInput(BaseModel):expression: str = Field(description="The math expression to evaluate, e.g., '2 + 2'")@tool("simple_calculator", args_schema=CalcInput)def simple_calculator(expression: str):"""Useful for basic math calculations."""try:# Source 351: Tools provide deterministic results for agents.return str(eval(expression, {"__builtins__": None}, {}))except Exception as e:return f"Error: {str(e)}"@tool("get_time")def get_time():"""Returns the current system time."""from datetime import datetimereturn datetime.now().strftime("%H:%M:%S")tools = {"simple_calculator": simple_calculator, "get_time": get_time}# 2. LOAD REASONING ENGINEmodel_id = "AshokGakr/model-tiny"tokenizer = AutoTokenizer.from_pretrained(model_id)model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")# 3. DEFINE THE AGENT STATEclass AgentState(TypedDict):# Source 828: StateGraph acts as the system's real-time workflow tracker.messages: list[dict]next_step: str# 4. AGENT LOGIC NODESdef call_model(state: AgentState):# 1. Apply Chat Template to format the history# This prepares the context for the reasoning engine [5].inputs = tokenizer.apply_chat_template(state['messages'],add_generation_prompt=True,return_tensors="pt").to(model.device)# 2. FIX: Unpack the inputs using ** to pass tensors correctly# This prevents the KeyError: 'shape' by giving generate the specific tensors it needs.output_ids = model.generate(**inputs, # <--- CRITICAL FIX: Unpack the dictionarymax_new_tokens=256,do_sample=True,temperature=0.7)# 3. Decode only the newly generated tokens (skipping the original prompt)# inputs['input_ids'].shape[-1] provides the length of the input tokens.new_tokens = output_ids[inputs['input_ids'].shape[-1]:]response = tokenizer.decode(new_tokens, skip_special_tokens=True)# Identify if a tool needs to be called [1, 6]action_match = re.search(r"Action:\s*(\w+)", response)return {"messages": state['messages'] + [{"role": "assistant", "content": response}],"next_step": action_match.group(1) if action_match else "end"}def execute_tool(state: AgentState):tool_name = state['next_step']last_message = state['messages'][-1]['content']# Parse input (simplified for this model-tiny example)input_match = re.search(r"Action Input:\s*(.*)", last_message)arg = input_match.group(1).strip() if input_match else ""observation = tools[tool_name].run(arg)return {"messages": state['messages'] + [{"role": "user", "content": f"Observation: {observation}"}]}# 5. CONSTRUCT THE GRAPH# Source 96: Nodes represent actions; edges define the control flow.workflow = StateGraph(AgentState)workflow.add_node("agent", call_model)workflow.add_node("tools", execute_tool)workflow.set_entry_point("agent")workflow.add_conditional_edges("agent",lambda x: "tools" if x["next_step"] in tools else "end",{"tools": "tools", "end": END})workflow.add_edge("tools", "agent") # Create the ReAct Loop cycle [10]agent_app = workflow.compile()@app.route('/chat', methods=['POST'])def chat():user_input = request.json.get("message")# Execute the graph [5, 11]inputs = {"messages": [{"role": "user", "content": user_input}]}def run():for output in agent_app.stream(inputs):for key, value in output.items():# Stream the latest message content to the UI [12]yield value['messages'][-1]['content'] + "\n"return Response(run(), mimetype='text/plain')if __name__ == '__main__':app.run(host='0.0.0.0', port=7860)
app.py+92-74