|
| 1 | +## Required Agent Structure |
| 2 | + |
| 3 | +**IMPORTANT**: All UiPath coded agents MUST follow this standard structure unless explicitly specified otherwise by the user. |
| 4 | + |
| 5 | +### Required Components |
| 6 | + |
| 7 | +Every agent implementation MUST include these three Pydantic models: |
| 8 | + |
| 9 | +```python |
| 10 | +from pydantic import BaseModel |
| 11 | + |
| 12 | +class Input(BaseModel): |
| 13 | + """Define input fields that the agent accepts""" |
| 14 | + # Add your input fields here |
| 15 | + pass |
| 16 | + |
| 17 | +class State(BaseModel): |
| 18 | + """Define the agent's internal state that flows between nodes""" |
| 19 | + # Add your state fields here |
| 20 | + pass |
| 21 | + |
| 22 | +class Output(BaseModel): |
| 23 | + """Define output fields that the agent returns""" |
| 24 | + # Add your output fields here |
| 25 | + pass |
| 26 | +``` |
| 27 | + |
| 28 | +### Required LLM Initialization |
| 29 | + |
| 30 | +Unless the user explicitly requests a different LLM provider, always use `UiPathChat`: |
| 31 | + |
| 32 | +```python |
| 33 | +from uipath_langchain.chat import UiPathChat |
| 34 | + |
| 35 | +llm = UiPathChat(model="gpt-4o-2024-08-06", temperature=0.7) |
| 36 | +``` |
| 37 | + |
| 38 | +**Alternative LLMs** (only use if explicitly requested): |
| 39 | +- `ChatOpenAI` from `langchain_openai` |
| 40 | +- `ChatAnthropic` from `langchain_anthropic` |
| 41 | +- Other LangChain-compatible LLMs |
| 42 | + |
| 43 | +### Standard Agent Template |
| 44 | + |
| 45 | +Every agent should follow this basic structure: |
| 46 | + |
| 47 | +```python |
| 48 | +from langchain_core.messages import SystemMessage, HumanMessage |
| 49 | +from langgraph.graph import START, StateGraph, END |
| 50 | +from uipath_langchain.chat import UiPathChat |
| 51 | +from pydantic import BaseModel |
| 52 | + |
| 53 | +# 1. Define Input, State, and Output models |
| 54 | +class Input(BaseModel): |
| 55 | + field: str |
| 56 | + |
| 57 | +class State(BaseModel): |
| 58 | + field: str |
| 59 | + result: str = "" |
| 60 | + |
| 61 | +class Output(BaseModel): |
| 62 | + result: str |
| 63 | + |
| 64 | +# 2. Initialize UiPathChat LLM |
| 65 | +llm = UiPathChat(model="gpt-4o-2024-08-06", temperature=0.7) |
| 66 | + |
| 67 | +# 3. Define agent nodes (async functions) |
| 68 | +async def process_node(state: State) -> State: |
| 69 | + response = await llm.ainvoke([HumanMessage(state.field)]) |
| 70 | + return State(field=state.field, result=response.content) |
| 71 | + |
| 72 | +async def output_node(state: State) -> Output: |
| 73 | + return Output(result=state.result) |
| 74 | + |
| 75 | +# 4. Build the graph |
| 76 | +builder = StateGraph(State, input=Input, output=Output) |
| 77 | +builder.add_node("process", process_node) |
| 78 | +builder.add_node("output", output_node) |
| 79 | +builder.add_edge(START, "process") |
| 80 | +builder.add_edge("process", "output") |
| 81 | +builder.add_edge("output", END) |
| 82 | + |
| 83 | +# 5. Compile the graph |
| 84 | +graph = builder.compile() |
| 85 | +``` |
| 86 | + |
| 87 | +**Key Rules**: |
| 88 | +1. Always use async/await for all node functions |
| 89 | +2. All nodes (except output) must accept and return `State` |
| 90 | +3. The final output node must return `Output` |
| 91 | +4. Use `StateGraph(State, input=Input, output=Output)` for initialization |
| 92 | +5. Always compile with `graph = builder.compile()` |
0 commit comments