# Chain & Agent

## Chain

* Chains bring together different components of AI to create a cohesive system. They set up a sequence of calls between the components. These components can include models and memory

### Summarization Chain

* For summarization chain, it consists of several components - Loader (Document , Web loader), Html To Text, Text Splitter
* There is several strategies for implementing summarization for llm model&#x20;
* For the large data set, due to  token limit of model, it is needed to split into several steps

#### Map Reduce

<figure><img src="https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2F9pcQOqKL3imebG2lem0C%2Fimage.png?alt=media&#x26;token=80ff861e-2014-4acb-966d-ad6dcfd8a5ee" alt=""><figcaption></figcaption></figure>

* The map reduce documents chain first applies an LLM chain to each document individually (the Map step), treating the chain output as a new document. It then passes all the new documents to a separate combine documents chain to get a single output (the Reduce step).

#### Refine

<figure><img src="https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2F12GOuGdj4fig6Shlfauw%2Fimage.png?alt=media&#x26;token=d5858f9b-f00f-45e0-9614-8decd7460ae4" alt=""><figcaption></figcaption></figure>

* The refine documents chain constructs a response by looping over the input documents and iteratively updating its answer.
* Since the Refine chain only passes a single document to the LLM at a time, it is well-suited for tasks that require analyzing more documents than can fit in the model's context. The obvious tradeoff is that this chain will make far more LLM calls than

#### Stuff

<figure><img src="https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FygP2GejmlaIO7SdlPRvG%2Fimage.png?alt=media&#x26;token=90dd6569-5e97-4697-b657-8c7afdc0f2d8" alt=""><figcaption></figcaption></figure>

* The stuff documents chain ("stuff" as in "to stuff" or "to fill") is the most straightforward of the document chains. It takes a list of documents, inserts them all into a prompt and passes that prompt to an LLM.
* It is suitable for small data set

## Agent

* Agents can interact with its environment, collect data, and use the data to perform self-determined tasks to meet predetermined goals. Humans set goals, but an AI agent independently chooses the best actions it needs to perform to achieve those goals.
* They can determine the purpose of a query, choose the best tools to use, but also keep learning and self-reflection
* It is mainly composed of tool, memory and model
* Model act as a brain , memory act as a external data source, tools define the a series of action.

### ReAct

<figure><img src="https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FA2N6QqgAIXiVxwvbYNOZ%2Fimage.png?alt=media&#x26;token=be8e5027-4268-43c4-a1ec-dc0cbf7ffe66" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FzyxNMjGgfMgUmsKCrRuI%2Fimage.png?alt=media&#x26;token=b277ee0a-0bb9-44d1-b070-941ae4422cbb" alt=""><figcaption></figcaption></figure>

* ReAct is inspired by the synergies between "acting" and "reasoning" which allow humans to learn new tasks and make decisions or reasoning.
* As LLM have capability of reasoning (understand the meaning of prompt) and acting (to decide the next step) . ReAct prompts LLMs to generate verbal reasoning traces and actions for a task. This allows the system to perform dynamic reasoning to create, maintain, and adjust plans for acting while also enabling interaction to external environments.
* React helps to break a complex task into logical steps (reasoning) and perform operations, such as calling APIs or retrieving information (actions), to reach a solution.

<pre class="language-python"><code class="lang-python"><strong># Answer the following questions as best you can. You have access to the following tools:
</strong>
# {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}

prompt = hub.pull("hwchase17/react")

# Choose the LLM to use
llm = OpenAI()

# Construct the ReAct agent
agent = create_react_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

agent_executor.invoke({"input": "what is LangChain?"})
</code></pre>

```bash
[1m> Entering new AgentExecutor chain...[0m
[32;1m[1;3m I should research LangChain to learn more about it.
Action: tavily_search_results_json
Action Input: "LangChain"[0m[36;1m[1;3m[{'url': 'https://www.ibm.com/topics/langchain', 'content': 'LangChain is essentially a library of abstractions for Python and Javascript, representing common steps and concepts  LangChain is an open source orchestration framework for the development of applications using large language models  other LangChain features, like the eponymous chains.  LangChain provides integrations for over 25 different embedding methods, as well as for over 50 different vector storesLangChain is a tool for building applications using large language models (LLMs) like chatbots and virtual agents. It simplifies the process of programming and integration with external data sources and software workflows. It supports Python and Javascript languages and supports various LLM providers, including OpenAI, Google, and IBM.'}][0m[32;1m[1;3m I should read the summary and look at the different features and integrations of LangChain.
Action: tavily_search_results_json
Action Input: "LangChain features and integrations"[0m[36;1m[1;3m[{'url': 'https://www.ibm.com/topics/langchain', 'content': "LangChain provides integrations for over 25 different embedding methods, as well as for over 50 different vector stores  LangChain is an open source orchestration framework for the development of applications using large language models  other LangChain features, like the eponymous chains.  LangChain is essentially a library of abstractions for Python and Javascript, representing common steps and conceptsLaunched by Harrison Chase in October 2022, LangChain enjoyed a meteoric rise to prominence: as of June 2023, it was the single fastest-growing open source project on Github. 1 Coinciding with the momentous launch of OpenAI's ChatGPT the following month, LangChain has played a significant role in making generative AI more accessible to enthusias..."}][0m[32;1m[1;3m I should take note of the launch date and popularity of LangChain.
Action: tavily_search_results_json
Action Input: "LangChain launch date and popularity"[0m[36;1m[1;3m[{'url': 'https://www.ibm.com/topics/langchain', 'content': "LangChain is an open source orchestration framework for the development of applications using large language models  other LangChain features, like the eponymous chains.  LangChain provides integrations for over 25 different embedding methods, as well as for over 50 different vector stores  LangChain is essentially a library of abstractions for Python and Javascript, representing common steps and conceptsLaunched by Harrison Chase in October 2022, LangChain enjoyed a meteoric rise to prominence: as of June 2023, it was the single fastest-growing open source project on Github. 1 Coinciding with the momentous launch of OpenAI's ChatGPT the following month, LangChain has played a significant role in making generative AI more accessible to enthusias..."}][0m[32;1m[1;3m I now know the final answer.
Final Answer: LangChain is an open source orchestration framework for building applications using large language models (LLMs) like chatbots and virtual agents. It was launched by Harrison Chase in October 2022 and has gained popularity as the fastest-growing open source project on Github in June 2023.[0m

[1m> Finished chain.[0m

{'input': 'what is LangChain?',
 'output': 'LangChain is an open source orchestration framework for building applications using large language models (LLMs) like chatbots and virtual agents. It was launched by Harrison Chase in October 2022 and has gained popularity as the fastest-growing open source project on Github in June 2023.'}
```

### Tool Call

* [Tool calling](https://python.langchain.com/v0.1/docs/modules/model_io/chat/function_calling/) allows a model to detect when one or more tools should be called and respond with the inputs that should be passed to those tools. In an API call, you can describe tools and have the model intelligently choose to output a structured object like JSON containing arguments to call these tools.&#x20;

```python
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain_core.prompts import ChatPromptTemplate

tools = [TavilySearchResults(max_results=1)]
prompt = ChatPromptTemplate.from_messages(
    [
        (
            "system",
            "You are a helpful assistant. Make sure to use the tavily_search_results_json tool for information.",
        ),
        ("placeholder", "{chat_history}"),
        ("human", "{input}"),
        ("placeholder", "{agent_scratchpad}"),
    ]
)

# Construct the Tools agent
agent = create_tool_calling_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
agent_executor.invoke({"input": "what is LangChain?"})
```

```````bash
[1m> Entering new AgentExecutor chain...[0m
``````output
/Users/bagatur/langchain/libs/partners/anthropic/langchain_anthropic/chat_models.py:347: UserWarning: stream: Tool use is not yet supported in streaming mode.
  warnings.warn("stream: Tool use is not yet supported in streaming mode.")
``````output
[32;1m[1;3m
Invoking: `tavily_search_results_json` with `{'query': 'LangChain'}`
responded: [{'id': 'toolu_01QxrrT9srzkYCNyEZMDhGeg', 'input': {'query': 'LangChain'}, 'name': 'tavily_search_results_json', 'type': 'tool_use'}]

[0m[36;1m[1;3m[{'url': 'https://github.com/langchain-ai/langchain', 'content': 'About\n⚡ Building applications with LLMs through composability ⚡\nResources\nLicense\nCode of conduct\nSecurity policy\nStars\nWatchers\nForks\nReleases\n291\nPackages\n0\nUsed by 39k\nContributors\n1,848\nLanguages\nFooter\nFooter navigation Latest commit\nGit stats\nFiles\nREADME.md\n🦜️🔗 LangChain\n⚡ Building applications with LLMs through composability ⚡\nLooking for the JS/TS library? ⚡ Building applications with LLMs through composability ⚡\nLicense\nlangchain-ai/langchain\nName already in use\nUse Git or checkout with SVN using the web URL.\n 📖 Documentation\nPlease see here for full documentation, which includes:\n💁 Contributing\nAs an open-source project in a rapidly developing field, we are extremely open to contributions, whether it be in the form of a new feature, improved infrastructure, or better documentation.\n What can you build with LangChain?\n❓ Retrieval augmented generation\n💬 Analyzing structured data\n🤖 Chatbots\nAnd much more!'}][0m
``````output
/Users/bagatur/langchain/libs/partners/anthropic/langchain_anthropic/chat_models.py:347: UserWarning: stream: Tool use is not yet supported in streaming mode.
  warnings.warn("stream: Tool use is not yet supported in streaming mode.")
``````output
[32;1m[1;3mLangChain is an open-source Python library that helps developers build applications with large language models (LLMs) through composability. Some key features of LangChain include:

- Retrieval augmented generation - Allowing LLMs to retrieve and utilize external data sources when generating outputs.

- Analyzing structured data - Tools for working with structured data like databases, APIs, PDFs, etc. and allowing LLMs to reason over this data.

- Building chatbots and agents - Frameworks for building conversational AI applications.

- Composability - LangChain allows you to chain together different LLM capabilities and data sources in a modular and reusable way.

The library aims to make it easier to build real-world applications that leverage the power of large language models in a scalable and robust way. It provides abstractions and primitives for working with LLMs from different providers like OpenAI, Anthropic, Cohere, etc. LangChain is open-source and has an active community contributing new features and improvements.[0m

[1m> Finished chain.[0m

{'input': 'what is LangChain?',
 'output': 'LangChain is an open-source Python library that helps developers build applications with large language models (LLMs) through composability. Some key features of LangChain include:\n\n- Retrieval augmented generation - Allowing LLMs to retrieve and utilize external data sources when generating outputs.\n\n- Analyzing structured data - Tools for working with structured data like databases, APIs, PDFs, etc. and allowing LLMs to reason over this data.\n\n- Building chatbots and agents - Frameworks for building conversational AI applications.\n\n- Composability - LangChain allows you to chain together different LLM capabilities and data sources in a modular and reusable way.\n\nThe library aims to make it easier to build real-world applications that leverage the power of large language models in a scalable and robust way. It provides abstractions and primitives for working with LLMs from different providers like OpenAI, Anthropic, Cohere, etc. LangChain is open-source and has an active community contributing new features and improvements.'}
```````

## Multi-Agent Architecture

### Sequential

* The easiest flow&#x20;

<figure><img src="https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2F4W25UncDm1PNYMXf5F1Q%2Fimage.png?alt=media&#x26;token=323a7e27-d8cd-4afa-a4fd-5d4bc3b93b61" alt=""><figcaption></figcaption></figure>

### Parallel

* Suitable for the breaking complex tasks and ensure the co-relation

<figure><img src="https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2F5HQbppJbxdlycfqEjm9T%2Fimage.png?alt=media&#x26;token=3a3768f1-c4df-442c-b48e-c89fb7c6ad52" alt=""><figcaption></figcaption></figure>

### Loop

<figure><img src="https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2Fal5QTCFmhmK5UmSsA4ii%2Fimage.png?alt=media&#x26;token=75308072-03f3-44cc-af39-38936de5440b" alt=""><figcaption></figcaption></figure>

### Hierarchical <a href="#id-61d9" id="id-61d9"></a>

<figure><img src="https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FQ5pUtgQi3QxuVpet0Q0V%2Fimage.png?alt=media&#x26;token=27b3ff29-0fe2-4c85-abbd-5e5088874abf" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FYKfbVxJ7raS9YR7PfE88%2Fimage.png?alt=media&#x26;token=a0e7347d-a8cf-4d7d-8b88-d5f493ee785b" alt=""><figcaption></figcaption></figure>

* There is a supervisor / manager llm that decide what task should be executed
* After finished the task, it can be looped back to manager llm and start the new execution again until the task is decided to be finished

{% embed url="https://medium.com/@vipra_singh/ai-agents-multi-agent-architectures-part-7-0f0e185bb083" %}

## Memory

* Memory is a key part of AI chat services. The memory keeps a history of previous messages, allowing for an ongoing conversation with the AI, rather than every interaction starting fresh.
* To add memory to your AI workflow you can use either:
* [Window Buffer Memory](https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.memorybufferwindow/): stores a customizable length of chat history for the current session. This is the easiest to get started with.
* One of the memory services that n8n provides nodes for. These include:
  * [Motorhead](https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.memorymotorhead/)
  * [Redis Chat Memory](https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.memoryredischat/)
  * [Postgres Chat Memory](https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.memorypostgreschat/)
  * [Xata](https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.memoryxata/)
  * [Zep](https://docs.n8n.io/integrations/builtin/cluster-nodes/sub-nodes/n8n-nodes-langchain.memoryzep/)

## Workflow vs Agent

* Workflows are systems where LLMs and tools are orchestrated through predefined code paths.
* Agents, on the other hand, are systems where LLMs dynamically direct their own processes and tool usage, maintaining control over how they accomplish tasks.
* Think of it like cooking:\
  A **workflow** is like following a recipe exactly, step by step.\
  An **agent** is like a chef who decides on the spot how to prepare a dish based on the ingredients and what tastes best.

## Agentic RAG

<figure><img src="https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FfgI6j05iNA0dijtf1BR4%2Fimage.png?alt=media&#x26;token=9924dd9a-c9e8-46d6-93f6-424f97c634eb" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2F78yx4EwvXyHbLlYkeDNI%2Fimage.png?alt=media&#x26;token=c401bc2e-9aa7-42ef-bc2d-3b55d65f40a2" alt=""><figcaption></figcaption></figure>

* Agentic RAG identifies what’s needed to complete a task without waiting for explicit instructions. For instance, if it encounters an incomplete dataset or a question requiring additional context, it autonomously determines the missing elements and seeks them out.
* Unlike traditional models that rely on static, pre-trained knowledge, agentic RAG dynamically accesses real-time data.
