Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	out_state
Browse files
    	
        agent.py
    CHANGED
    
    | 
         @@ -18,9 +18,12 @@ from tools import ( 
     | 
|
| 18 | 
         
             
            )
         
     | 
| 19 | 
         | 
| 20 | 
         
             
            # βββββββββββββββββββββββββββ Configuration βββββββββββββββββββββββββββββββ
         
     | 
| 21 | 
         
            -
             
     | 
| 
         | 
|
| 
         | 
|
| 22 | 
         
             
            MAX_TOOL_CALLS = 5
         
     | 
| 23 | 
         | 
| 
         | 
|
| 24 | 
         
             
            # βββββββββββββββββββββββββββ Helper utilities ββββββββββββββββββββββββββββ
         
     | 
| 25 | 
         | 
| 26 | 
         
             
            # βββββββββββββββββββββββββββ Agent state β¬ βββββββββββββββββββββββββββββββ
         
     | 
| 
         @@ -33,7 +36,7 @@ MAX_TOOL_CALLS = 5 
     | 
|
| 33 | 
         
             
            # βββββββββββββββββββββββββββ Graph wiring βββββββββββββββββββββββββββββββ
         
     | 
| 34 | 
         | 
| 35 | 
         
             
            def build_graph():
         
     | 
| 36 | 
         
            -
                 
     | 
| 37 | 
         | 
| 38 | 
         
             
                llm = ChatOpenAI(model_name="gpt-4.1-mini", temperature=0.3)
         
     | 
| 39 | 
         | 
| 
         @@ -45,6 +48,6 @@ def build_graph(): 
     | 
|
| 45 | 
         
             
                    analyze_code_tool,
         
     | 
| 46 | 
         
             
                ]
         
     | 
| 47 | 
         
             
                # llm = llm.bind_tools(llm_tools)
         
     | 
| 48 | 
         
            -
                agent = create_react_agent(llm, llm_tools)
         
     | 
| 49 | 
         | 
| 50 | 
         
             
                return agent 
         
     | 
| 
         | 
|
| 18 | 
         
             
            )
         
     | 
| 19 | 
         | 
| 20 | 
         
             
            # βββββββββββββββββββββββββββ Configuration βββββββββββββββββββββββββββββββ
         
     | 
| 21 | 
         
            +
            SYSTEM_PROMPT = """
         
     | 
| 22 | 
         
            +
            You are a general AI assistant. I will ask you a question. Report your thoughts, and finish your answer with the following template: FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
         
     | 
| 23 | 
         
            +
            """
         
     | 
| 24 | 
         
             
            MAX_TOOL_CALLS = 5
         
     | 
| 25 | 
         | 
| 26 | 
         
            +
             
     | 
| 27 | 
         
             
            # βββββββββββββββββββββββββββ Helper utilities ββββββββββββββββββββββββββββ
         
     | 
| 28 | 
         | 
| 29 | 
         
             
            # βββββββββββββββββββββββββββ Agent state β¬ βββββββββββββββββββββββββββββββ
         
     | 
| 
         | 
|
| 36 | 
         
             
            # βββββββββββββββββββββββββββ Graph wiring βββββββββββββββββββββββββββββββ
         
     | 
| 37 | 
         | 
| 38 | 
         
             
            def build_graph():
         
     | 
| 39 | 
         
            +
                
         
     | 
| 40 | 
         | 
| 41 | 
         
             
                llm = ChatOpenAI(model_name="gpt-4.1-mini", temperature=0.3)
         
     | 
| 42 | 
         | 
| 
         | 
|
| 48 | 
         
             
                    analyze_code_tool,
         
     | 
| 49 | 
         
             
                ]
         
     | 
| 50 | 
         
             
                # llm = llm.bind_tools(llm_tools)
         
     | 
| 51 | 
         
            +
                agent = create_react_agent(model=llm, tools=llm_tools, prompt=SYSTEM_PROMPT)
         
     | 
| 52 | 
         | 
| 53 | 
         
             
                return agent 
         
     | 
    	
        app.py
    CHANGED
    
    | 
         @@ -33,12 +33,9 @@ class BasicAgent: 
     | 
|
| 33 | 
         | 
| 34 | 
         
             
                    # IMPORTANT: invoke() returns a **new** state instance (or an AddableValuesDict),
         
     | 
| 35 | 
         
             
                    # not the object we pass in.  Use the returned value to fetch final_answer.
         
     | 
| 36 | 
         
            -
                    out_state = self.graph.invoke( 
     | 
| 37 | 
         
            -
             
     | 
| 38 | 
         
            -
                     
     | 
| 39 | 
         
            -
                        return out_state.get("final_answer", "No answer.")
         
     | 
| 40 | 
         
            -
                    else:                                  # If future versions return the dataclass
         
     | 
| 41 | 
         
            -
                        return getattr(out_state, "final_answer", "No answer.")
         
     | 
| 42 | 
         | 
| 43 | 
         | 
| 44 | 
         
             
            def run_and_submit_all( profile: gr.OAuthProfile | None):
         
     | 
| 
         | 
|
| 33 | 
         | 
| 34 | 
         
             
                    # IMPORTANT: invoke() returns a **new** state instance (or an AddableValuesDict),
         
     | 
| 35 | 
         
             
                    # not the object we pass in.  Use the returned value to fetch final_answer.
         
     | 
| 36 | 
         
            +
                    out_state = self.graph.invoke({"messages": [HumanMessage(content=question)]})
         
     | 
| 37 | 
         
            +
                    print("out_state: ", out_state)
         
     | 
| 38 | 
         
            +
                    return out_state
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 39 | 
         | 
| 40 | 
         | 
| 41 | 
         
             
            def run_and_submit_all( profile: gr.OAuthProfile | None):
         
     | 
    	
        state.py
    CHANGED
    
    | 
         @@ -1,24 +1,16 @@ 
     | 
|
| 1 | 
         
            -
            from  
     | 
| 2 | 
         
            -
            from typing import List, Dict, Any, Optional
         
     | 
| 3 | 
         
             
            import json
         
     | 
| 4 | 
         
            -
            from dataclasses import dataclass, field, asdict
         
     | 
| 5 | 
         
             
            from langchain.schema import SystemMessage, HumanMessage, AIMessage, BaseMessage
         
     | 
| 
         | 
|
| 6 | 
         | 
| 7 | 
         | 
| 8 | 
         
            -
             
     | 
| 9 | 
         
            -
            class AgentState:
         
     | 
| 10 | 
         
             
                """Single sourceβofβtruth context for one user query run."""
         
     | 
| 11 | 
         
            -
             
     | 
| 12 | 
         
             
                user_question: str
         
     | 
| 13 | 
         
            -
                task_id: Optional[str]  
     | 
| 14 | 
         
            -
                messages: List[BaseMessage]  
     | 
| 15 | 
         
            -
             
     | 
| 16 | 
         
            -
                 
     | 
| 17 | 
         
            -
                 
     | 
| 18 | 
         
            -
                 
     | 
| 19 | 
         
            -
                 
     | 
| 20 | 
         
            -
             
     | 
| 21 | 
         
            -
                final_answer: Optional[str] = None
         
     | 
| 22 | 
         
            -
             
     | 
| 23 | 
         
            -
                def add(self, *msgs: BaseMessage):
         
     | 
| 24 | 
         
            -
                    self.messages.extend(msgs)
         
     | 
| 
         | 
|
| 1 | 
         
            +
            from typing import List, Dict, Any, Optional, TypedDict, Annotated
         
     | 
| 
         | 
|
| 2 | 
         
             
            import json
         
     | 
| 
         | 
|
| 3 | 
         
             
            from langchain.schema import SystemMessage, HumanMessage, AIMessage, BaseMessage
         
     | 
| 4 | 
         
            +
            from langgraph.graph.message import add_messages
         
     | 
| 5 | 
         | 
| 6 | 
         | 
| 7 | 
         
            +
            class AgentState(TypedDict):
         
     | 
| 
         | 
|
| 8 | 
         
             
                """Single sourceβofβtruth context for one user query run."""
         
     | 
| 
         | 
|
| 9 | 
         
             
                user_question: str
         
     | 
| 10 | 
         
            +
                task_id: Optional[str] 
         
     | 
| 11 | 
         
            +
                messages: Annotated[List[BaseMessage], add_messages]
         
     | 
| 12 | 
         
            +
                next_action: Optional[str]      # wiki | ocr | audio | final
         
     | 
| 13 | 
         
            +
                query: Optional[str]            # wiki search term
         
     | 
| 14 | 
         
            +
                snippet: Optional[str]          # code snippet
         
     | 
| 15 | 
         
            +
                tool_calls: int
         
     | 
| 16 | 
         
            +
                final_answer: Optional[str]
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         |