# AI Aggregation Layer

My app's primary AI model is Google Gemini, integrated via the [Google Gemini AI kit](https://startup.google.com/gemini/). Requests are dispatched in parallel to my Python FastAPI built backend served on [Google Cloud Run](https://cloud.google.com/free?utm_source=pmax\&utm_medium=display\&utm_campaign=Cloud-SS-DR-GCP-1713664-GCP-DR-APAC-AU-en-PMAX-Display-PMAX-Prospecting-GenericCloud\&utm_content=c--x--9071737-17821954699\&utm_term\&gclsrc=aw.ds&\&https://ad.doubleclick.net/ddm/trackclk/N5295.276639.GOOGLEADWORDS/B26943865.344329733;dc_trk_aid=535895606;dc_trk_cid%3D163098484;dc_lat%3D;dc_rdid%3D;tag_for_child_directed_treatment%3D;tfua%3D;ltd%3D\&gad_source=1\&gad_campaignid=17820972903\&gclid=Cj0KCQiAnJHMBhDAARIsABr7b84PUhCkxZMdmmEHQDSZtbAGqh6ODnkRpQQWp7UIRohjuHITiqIOYSUaAhbdEALw_wcB).&#x20;

{% hint style="warning" %}
KEY TERMS USED IN THIS POST

* agent = AI = gemini = server
* human = user = mimi = client&#x20;
  {% endhint %}

## Concurrent Processes in React

On the client side, the parallelism is implemented in React with `Promise.all` via the following example code snippet.

```ts

/**
 * Sends a chat request to both AI and tarot endpoints in parallel.
 * @param {{ message: string, history: Array<{ role: string, content: string }> }} payload
 * @param {AbortSignal} signal
 * @returns {Promise<{ ai: any, taro: any }>}
 */
const fetchData = async (payload, abortRef) => {
  const [aiRes, taroRes] = await Promise.all([
    fetch("/api/gemini/flash", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(payload),
      signal: abortRef.current?.signal,
    }),
    fetch("/api/taro", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(payload),
      signal: abortRef.current?.signal,
    }),
  ]);

  return { aiRes, taroRes };
};
```

## Controlled Variables

### 1.1 User Action Space

The graph below lists the labeled actions the agent can accept from the client. Users choose one of these actions, which is then sent back to the agent.

{% @mermaid/diagram content="graph TD
U\[User Receives Message]

U --> A\[Action]
U --> B\[Chat]
U --> C\[Pick]
" %}

### 1.2 Agent Action Space

Action labels identifying what actions available for the AI model to select and return to user. The main invokable actions by agent is **1) Ask a question** and **2) Converse**. For the sake of simplicity, the questions are quantified as binary instead rather than continuous states.

**Types of Questions**

* **Memory Recall**: Recommend a question based on insights aggregated from history interactions with the user.
* **Recommendation**: Questions that are more focused at what my app needs and may appear more customer-service level or role played. These questions are directly returned from the servicing AI model (Gemini AI). ***One can view this as chatting with Gemini/ChatGPT, just through a different chat server.***&#x20;
* **Clarifier**:  In linguistic terms, when one refuses to steer away from the current discussion topic and instead remains anchored to it. The intent behind this is to suggest questions where the user (and agent) are able to question themselves and surface hidden emotions and motifs.&#x20;

{% @mermaid/diagram content="graph TD
Z\[Agent]
Z --> Q\[Question]
Z --> R\[Converse]

Q --> M\[Memory Recall]
Q --> D\[Recommendation]
Q --> C\[Clarifier]
" fullWidth="false" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://whoamimi.gitbook.io/blog/projects/tarotarot-ai-fortune-teller/ai-aggregation-layer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
