### [AI / ML領域相關學習筆記入口頁面](https://hackmd.io/@YungHuiHsu/BySsb5dfp) #### [Deeplearning.ai GenAI/LLM系列課程筆記](https://learn.deeplearning.ai/) ##### GenAI - [Large Language Models with Semantic Search。大型語言模型與語義搜索 ](https://hackmd.io/@YungHuiHsu/rku-vjhZT) - [LangChain for LLM Application Development。使用LangChain進行LLM應用開發](https://hackmd.io/1r4pzdfFRwOIRrhtF9iFKQ) - [Finetuning Large Language Models。微調大型語言模型](https://hackmd.io/@YungHuiHsu/HJ6AT8XG6) ##### RAG - [Preprocessing Unstructured Data for LLM Applications。大型語言模型(LLM)應用的非結構化資料前處理](https://hackmd.io/@YungHuiHsu/BJDAbgpgR) - [Building and Evaluating Advanced RAG。建立與評估進階RAG](https://hackmd.io/@YungHuiHsu/rkqGpCDca) - [[GenAI][RAG] Multi-Modal Retrieval-Augmented Generation and Evaluaion。多模態的RAG與評估 ](https://hackmd.io/@YungHuiHsu/B1LJcOlfA) ##### AI Agents 原理可參考這篇綜論: [How Agents for LLM Perform Task Planning。大型語言模型的代理如何進行任務規劃](https://hackmd.io/@YungHuiHsu/rkK52BkQp) - 相關framework選擇 - [AI Agents in LangGraph](https://hackmd.io/@YungHuiHsu/BJTKpkEHC) - [Building Agentic RAG with LlamaIndex](https://learn.deeplearning.ai/courses/building-agentic-rag-with-llamaindex/lesson/1/introduction) - [Multi AI Agent Systems with crewAI](https://learn.deeplearning.ai/courses/multi-ai-agent-systems-with-crewai/lesson/1/introduction) - [Functions, Tools and Agents with LangChain](https://learn.deeplearning.ai/courses/functions-tools-agents-langchain/lesson/1/introduction) --- # [AI Agents in LangGraph](https://learn.deeplearning.ai/courses/ai-agents-in-langgraph/lesson/1/introduction) * [Build an Agent from Scratch。從零開始建立AI代理](https://hackmd.io/@YungHuiHsu/BJTKpkEHC) * [LangGraph Components](https://hackmd.io/@YungHuiHsu/BySTFuhSC) * Agentic Search Tools * Persistence and Streaming * Human in the loop * Essay Writer --- ## LanaGraph Components。 LanaGraph組件 回顧上一小節的流程 ![image](https://hackmd.io/_uploads/rkF7j_nHC.png =800x) - LangChain的PromptTemplate - [hub/hwchase17/react](https://smith.langchain.com/hub/hwchase17/react) - 使用方式 ```python= # set the LANGCHAIN_API_KEY environment variable (create key in settings) from langchain import hub prompt = hub.pull("hwchase17/react") ``` - 提示模板 - `{tools}`可動態帶入指定工具 - `[{tool_names}]` ```poython= Answer the following questions as best you can. You have access to the following tools: {tools} Use the following format: Question: the input question you must answer Thought: you should always think about what to do Action: the action to take, should be one of [{tool_names}] Action Input: the input to the action Observation: the result of the action ... (this Thought/Action/Action Input/Observation can repeat N times) Thought: I now know the final answer Final Answer: the final answer to the original input question Begin! Question: {input} Thought:{agent_scratchpad} ``` #### LangChain: Tools ![image](https://hackmd.io/_uploads/Hy0epdnSA.png =800x) - 從langchain_community中調用tools ```python= # get a tool from the library from langchain_community.tools.tavily_search import \ TavilySearchResults tool = TavilySearchResults(max_results=2) self.tools = {t.name: t for t in tools} self.model = model.bind_tools([tool]) ``` #### New in LangGraph - **Cyclic Graphs** - **Persistence** - **Human-in-the-loop** ![image](https://hackmd.io/_uploads/Hk7T6OhrR.png) * LangGraph is an extension of LangChain that supports graphs. * Single and Multi-agent flows are described and represented as graphs. * Allows for extremely controlled “flows” * Built-in persistence allows for human-in-the-loop workflows ### Graph 基本組件 * Nodes。節點: 代理或功能 * Edges。邊: 連接節點 * Conditional edges。條件邊: 決定 ![image](https://hackmd.io/_uploads/H1S8Aunr0.png =400x) - 極簡範例 ![image](https://hackmd.io/_uploads/SkNuC_nrC.png =600x) #### Data/State * **代理狀態**可被圖的所有部分訪問 Agent state Is accessible to all parts of the graph * 是圖的本地狀態 it is local to the graph * 可存儲在持久層中 Can be stored in a persistence layer ![image](https://hackmd.io/_uploads/HkjCkYhB0.png =400x) - 範例 - Simple - 當 `messages` 欄位被更新時,它不是覆蓋原來的值,而是將新的消息添加到已有的消息中。 通過 `Annotated` 和 `operator.add` 來實現的。 - Complex - 更複雜的 `AgentState` 結構,包括 `input`、`chat_history`、`agent_outcome` 和 `intermediate_steps` - `input`、`chat_history` 和 `agent_outcome` 這三個欄位都沒有進行特別的註釋,這意味著當這些欄位被更新時,新值會覆蓋現有的值 - 唯一例外的是 `intermediate_steps` 欄位,它被註釋了 `operator.add`,這意味著當更新這個欄位時,新的值會添加到現有的值中,而不是覆蓋它 - 為什麼 `intermediate_steps` 要添加而不是覆蓋 - `intermediate_steps` 欄位被設計為追蹤代理行動和觀察的過程 - 這些步驟在代理執行過程中不斷累加,所以每次更新時需要將新步驟添加到現有步驟中,而不是覆蓋掉 - 保留完整的執行記錄,幫助追蹤代理的行動和決策過程 :::info 類似RL中記錄所有STATE ::: ```python= # Simple class AgentState(TypedDict): messages: Annotated[Sequence[BaseMessage], operator.add] # Complex class AgentState(TypedDict): input: str chat_history: list[BaseMessage] agent_outcome: Union[AgentAction, AgentFinish, None] intermediate_steps: Annotatedf[list[tuple[AgentAction, str]], operator.add] ``` - State ![image](https://hackmd.io/_uploads/BysBQFnBC.png =500x) ```python= # State class AgentState(TypedDict): messages: Annotated[list[AnyMessage], operator.add] ```