> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gp.scale.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Search Chat App With State

> Build a Search Chat App With State.

## Overview

This YAML configuration describes a state machine designed to handle a chat interaction with capabilities for searching information using Google and Wikipedia.

## YAML Workflow

```yaml theme={null}
initial_state:
  messages:
    - role: system
      content: Today's date is January 7, 2025.
starting_node: search_chat
machine:
  search_chat:
    workflow_config:
      final_output_nodes:
        - llm_call
      user_input:
        message:
          type: string
      workflow:
        - name: get_new_user_message
          type: create_messages
          config:
            message_configs:
              - role: user
                content: user_message
          inputs:
            user_message: message
        - name: add_user_message
          type: insert_messages
          config:
            index: -1
          inputs:
            messages: messages
            new_messages: get_new_user_message.output
        - name: search_generation
          type: tool_generation
          config:
            model: openai/gpt-4o
            tools:
              - name: internal.google_search
              - name: internal.WikipediaSearch
                init_kwargs: {}
          inputs:
            messages: add_user_message.output
        - name: add_tool_and_asst_msgs
          type: processor
          config:
            return_key: concatenate
            function_specs:
              concatenate:
                path: concatenate
                kwargs:
                  list1: list1
                  list2: list2
          inputs:
            list1: add_user_message.output
            list2: search_generation.output
        - name: llm_call
          type: get_message
          config:
            index: -1
          inputs:
            messages: add_tool_and_asst_msgs.output
    write_to_state:
      messages: add_tool_and_asst_msgs
    next_node:

      default: search_chat
```

## Initial state

**Messages Initialization:** The initial state is set with a system message that specifies the current date, indicating a specific context or time frame for subsequent interactions. This could help in contextualizing queries or managing time-sensitive information.

## Starting Node

**search\_chat:** This node is the entry point of the workflow, indicating that the primary interaction focus is related to searching via chat.

## Workflow Breakdown

<img src="https://mintcdn.com/scalegp/2sSrpizRElJqluR6/images/search-chat-app-with-state.png?fit=max&auto=format&n=2sSrpizRElJqluR6&q=85&s=2b01783e812a7fd78a75c34cf6fcf686" alt="search-chat-app-with-state-workflow" width="3208" height="864" data-path="images/search-chat-app-with-state.png" />

| Step | Node Name                | Type              | Purpose                                           |
| ---- | ------------------------ | ----------------- | ------------------------------------------------- |
| 1    | `get_new_user_message`   | `create_messages` | Takes in the user input as a message              |
| 2    | `add_user_message`       | `insert_messages` | Adds user messge to the messages in the state     |
| 3    | `search_generation`      | `tool_generation` | Performs search with concatenated messages        |
| 4    | `add_tool_and_asst_msgs` | `processor`       | Combines the user message with the search results |
| 4    | `llm_call`               | `get_message`     | Generates the best answer                         |

1. **Get New User Message:**
   * **Create User Message:** A node to take user input (the message) and convert it into a structured message object with the role "user."
2. **Insert User Message:**
   * **Update Messages:** Appends the newly created user message to the list of messages. This helps in maintaining context across interactions and forms part of the dialogue history.
3. **Search Generation:**
   * **Tool Generation:** Uses tool\_generation to perform searches using two tools:
     * Internal Google Search.
     * Internal Wikipedia Search.
   * This involves generating completion candidates that best match user queries by leveraging these search tools.
4. **Concatenate Messages:**
   * **Combine Message List:** A processor node is used to concatenate outputs from the user message and tool-generated search results, combining them into a single message list (add\_tool\_and\_asst\_msgs).
   * The concatenate function merges these lists together.
5. **Retrieve Latest Message:**
   * **Get Latest Output:** This node extracts the most recent message from add\_tool\_and\_asst\_msgs, indicating that it could be looking to retrieve the most relevant or recent piece of information for further processing or output.

## State Updates and Transition

* **State Writing:**
  Updates the messages in the state with the concatenated messages, ensuring the state carries forward the full conversation context, including the latest tool responses.
* **Next Node:**
  The default path loops back to search\_chat, suggesting a continuous interaction model where the workflow keeps iterating over user input and generating responses through searches.

## Workflow Benefits over Simple Search

* **Iterative Search Chat Mechanism:** The setup is designed to handle chat interactions focused on searching, continuously updating the conversation context with new information via tool invocations.
* **Tool Flexibility:** The use of tools within search\_generation allows for dynamic query handling and response generation, which enhances the adaptability of chatbot responses based on external data.
* **Message Management:** The process keeps track of the dialogue and ensures that every tool-generated message is integrated into the conversation, indicative of a system trying to enrich the dialogue with relevant information.
