#+title: Lesson 03 | data types
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="../../_share/media/css/herk.css" />
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="../../_share/media/css/org-media-sass/collapsible.css" />
#+HTML_HEAD: <script src="../../_share/media/js/collapsible.js"></script>
#+OPTIONS: H:6

current: 1:08:39

* Links
#+attr_html: :class links
- [[../toc.org][TOC | Build n8n ai agents 8 hr course]]
- [[https://www.youtube.com/watch?v=Ey18PDiaAYI][Course Video]]

Curr: https://youtu.be/Ey18PDiaAYI?si=PcU_a5ruX-CXM0k0&t=5586

*** timestamps
:PROPERTIES:
:CUSTOM_ID: timestamp
:END:

#+attr_html: :class playlist
- [[https://www.youtube.com/watch?v=Ey18PDiaAYI&t=2445s][0:40:45 n8n n8n Data Types]]
- [[https://www.youtube.com/watch?v=Ey18PDiaAYI&t=2905s][0:48:25 23 Workflow Templates FREE]]
- [[https://www.youtube.com/watch?v=Ey18PDiaAYI&t=2949s][0:49:09 Step by Step AI Workflows]]
- [[https://www.youtube.com/watch?v=Ey18PDiaAYI&t=3085s][0:51:25 1) Rag Pipeline and Chatbot]]
- [[https://www.youtube.com/watch?v=Ey18PDiaAYI&t=4262s][1:11:02 2) Customer Support Workflow]]
- [[https://www.youtube.com/watch?v=Ey18PDiaAYI&t=4926s][1:22:06 3) LinkedIn Content Creator Workflow]] *current*
- [[https://youtu.be/Ey18PDiaAYI?si=PcU_a5ruX-CXM0k0&t=5586][1:33:06 Bonus Invoice Workflow]]

* credentials
:PROPERTIES:
:ID: credentials
:END:

** OAuth App
- project :: herk-build-n8n
- app name :: ronnyabraham-tutorials-herk-n8n
  - email :: this.ronny@gmail.com
  - audience :: external

** OAuth client
- client name :: herk-n8n-tutorial-pi
- client id :: ~418038224272-v1h0js2kh86gqc7vtdt9sib8euht6t8u.apps.googleusercontent.com~
- client secret :: ~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~
- file :: [[../../_share/data/build n8n/client_secret_418038224272-v1h0js2kh86gqc7vtdt9sib8euht6t8u.apps.googleusercontent.com.json][client secret]]

** pinecode
- https://app.pinecone.io
- user account :: barbados / this.ronny@gmail.com
- api keys
  - name :: herk-n8n-ch03-knowledgebase-samuraicat
  - key :: ~pcsk_4tcs8j_FUBoZwUTdsp9eH7qsPfYVizPU3fUti8jxo9qR6Ao2PbRqtbugKy8gNCgcTU8jh8~

** openrouter
:PROPERTIES:
:ID:      ragchat-openrouter
:END:

  - link :: https://openrouter.ai
  - herk-build​_n8n-e01​_ragchat :: ~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~

* Notes
** set field
- can also name the fields and data type for each field

*** data types
| data type | symbol | example             |
|-----------+--------+---------------------|
| String    | A      | "blah"              |
| Number    | #      | 50                  |
| Boolean   | ✓      | true                |
| Array     | ☰     | [1, "one", "three"] |
| Object    | 3d box | {"blah": 33}        |

** 3 AI workflows
*** [[#workflow-1][1. RAG pipeline & chatbot]]
**** tools
- pinecone :: vector database
- google drive :: data storage
- google docs ::
- open router :: lets us connect to ai models like openai's or anthropics

*** [[#workflow-2][2. Customer support]]
**** purpose
- build off prev workflow with pinecone db
- respond to customer support related emails

**** tools
- pinecone
- gmail
- n8n agent
- open router

*** [[#workflow-3][3. LinkedIn Content Creation]]
**** tools
- tavily :: search the web
- google sheets :: store content ideas, and write content ideas to it

** Workflow 1) - Rag Pipeline and Chatbot
:PROPERTIES:
:CUSTOM_ID: workflow-1
:END:

*** links
- [[https://youtu.be/Ey18PDiaAYI?si=9Bh1FiEn9t0ZrGkJ&t=3085][youtube]]
- [[credentials]]

*** RAG
- stands for :: retrieval, augmented, generation
- looks inside database for the answer

**** Vector Database
- multidimensional graph of points
- vector is placed based on meaning of vector
  - ie, wolf and dog will be close
  - banana, apple will be close

**** how it works
- we have a document
- break it into chunks
- run it through an 'embeddings model'
  - this puts the chunks into a vector model

**** Query
- run the query through the embeddings model
- see where it lands in vectors, grabs back the nearest 4 or 5 vectors and returns it to us

*** process
- our trigger is any changes in folder on google drive

*** set up google drive trigger
1. create OAuth Credentials
  [[https://docs.n8n.io/integrations/builtin/credentials/google/oauth-single-service/?utm_source=n8n_app&utm_medium=credential_settings&utm_campaign=create_new_credentials_modal#create-your-google-oauth-client-credentials][n8n documentation for creating OAuth credentials]]

2. in Client, make sure to add the OAuth Redirect URL found in the trigger set up to Clients->Authorized Redirect URIs

   #+CAPTION: get the OAuth redirect from the n8n google drive trigger settings
   #+ATTR_HTML: :width 600px
   [[file:../../_share/media/img/build_n8n/chap03/ragchat/get redirect from trigger.png]]

   #+CAPTION: put the redirect url in google client settings
   #+ATTR_HTML: :width 600px
   [[file:../../_share/media/img/build_n8n/chap03/ragchat/add authorized redirect.png]]

3. get client id and secret id, and enter into the trigger
   - [[credentials][link]]

4. add the google drive api
   - APIs
     - go to [[https://console.cloud.google.com/apis/dashboard][google api dashboard]] and enable the following apis
       - google drive api
       - google drive activity api

   - API scopes
     - go to [[https://console.cloud.google.com/apis/dashboard][google api dashboard]]

     - from the left hand menu, select [[https://console.cloud.google.com/auth/overview][OAuth consent screen]]
     - from the left hand menu, select [[https://console.cloud.google.com/auth/scopes][Data Access]]
     - at the top of the page click the "Add or remove scopes" button
       - search through it until you find
         - /auth/drive.readonly
         - /auth/drive.appdata

   - Sign in and grant the user permission for the following
     - See, edit, create, and delete all of your Google Drive files
     - See, create, and delete its own configuration data in your Google Drive

   - if you have to reconnect from scratch, go to
     - https://myaccount.google.com/connections/overview/
     - select the app you are connected to and delete all connections
     - sign in again

   - if all goes well
     - set folder
     - save and exit the config dialog
     - go to google drive trigger, and select the folder

*** set up google drive download file
**** current files:
  - [[../../_share/data/build n8n/ragchat-e01/Policy and FAQ.md][Policy and FAQ.md]]

**** setup
- in order to put documents into our Pinecone vector database we need to check for a newly created file, download it, and then send it

- so we must create an initial file, from which we will get the necessary id that we will always look for in all new files, and then tell this tool to download it

- create a file
  - first create a new faq file in the folder

  - fetch it from google trigger
  - find where the name of the file is in the schema, you will be getting the id field next to it

- now add google drive - download file

- look for where name was in the fetch, drag the id link to the file field
   #+CAPTION: drag the id to the file field
   #+ATTR_HTML: :width 600px
   [[file:../../_share/media/img/build_n8n/chap03/ragchat/drag id.png]]

*** pinecone
**** set up pinecone
- got to http://pinecone.io

- create an index
  - give it a name
  - choose a text embedding model
    - in this case it is 'text-embedding-3-small'

  - set the embeddings to 1536
     #+CAPTION: set the embedddings
     #+ATTR_HTML: :width 300px
     [[file:../../_share/media/img/build_n8n/chap03/ragchat/pinecone set embeddings.png]]

  - hit 'create index'

- go to API keys and create a key

**** add pinecone node
- search for pinecode vector store
- choose 'add documents to a vector store'
  - add the api key
  - after you save it go to settings pinecone index and choose the index you created

   #+CAPTION: set pinecone index in the node
   #+ATTR_HTML: :width 300px
   [[file:../../_share/media/img/build_n8n/chap03/ragchat/set pinecone index.png]]

- add option 'pinecone namespace'
   #+CAPTION: add option 'pincone namesapce'
   #+ATTR_HTML: :width 300px
   [[file:../../_share/media/img/build_n8n/chap03/ragchat/add pinecone option.png]]


- decide on a namespace and give it
  - pinecone can just use the default location for all data
  - namespaces allow you to organize data in pinecode

- at this point you need an embeddings model and a document loader

***** embeddings model
- add embeddings open ai

***** document loader
- add default data loader
- choose binary as the data type

- add a recursive character test splitter

***** run pinecone node by clicking play


*** add a second workflow to access the information
- we are going to use open router to choose any LLM

**** add an agent to process
- create an agent with a chat trigger
- source for prompt is the chat trigger node
- to make the response sound correct
  - add a system message
    - in the agent node click 'OPTIONS'
    - select "System Message"
  - add the following text:
    #+BEGIN_SRC
          You are the official support assistant for Samurai Cat.
          Answer questions using ONLY the retrieved policy text.
          Respond in first person (“we”, “our”), never in third person.
          Do not analyze tone.
          Do not describe the document.
          Do not say “the document states” or “they say”.
          If the information is not found in the knowledge base, say you don’t have that information.

          Keep responses clear, concise, and professional.
  #+END_SRC

**** open router
- add an openrouter node
  - add the key you create on there
    - [[#ragchat-openrouter][open router api]]

**** add pinecone tool
- add pinecone as a tool
  - NOTE: MAKE SURE YOU USE THE TOOL CONNECTOR!!
  -
    #+CAPTION: set the embedddings
     #+ATTR_HTML: :width 300px
     [[file:../../_share/media/img/build_n8n/chap03/ragchat/pinecone - connect.png]]
- hit the tool extension and pick pinecone vector store
- add an embedding node (open ai embedding) so the pinecone node can convert the question text to a vector that it will then process to retrieve the associated vector for that text

** Workflow 2) Customer Support
:PROPERTIES:
:CUSTOM_ID: workflow-2
:END:

*** links
- [[https://youtu.be/Ey18PDiaAYI?si=UrHhMkEDJSNrij88&t=4263][youtube]]
- [[credentials]]

*** add gmail trigger
- on message received
- based on OAuth

**** set up OAuth Gmail API
  - go to [[https://console.cloud.google.com/welcome?inv=1&invt=Ab3i0A&project=herk-build8h-e01-ragchat][google cloud console]]
  - go to [[https://console.cloud.google.com/apis/dashboard?inv=1&invt=Ab3i0A&project=herk-build8h-e01-ragchat&organizationId=0][google api dashboard]]

  - select the project you are working under
  - in Audience enter a test user email account that you have control over
    https://console.cloud.google.com/auth/audience

  - enable gmail api
    - in the api search bar look for 'gmail'
          #+attr_html: :width 600
          file:../../_share/media/img/compsci/ai/herk/build_8hr/ch03_e02a.png

    - select gmail api
          #+attr_html: :width 600
          file:../../_share/media/img/compsci/ai/herk/build_8hr/ch03_e02b.png

    - click 'enable'
          #+attr_html: :width 600
          file:../../_share/media/img/compsci/ai/herk/build_8hr/ch03_e02c.png
  - go to the OAuth credentials and copy the client ID
    - enter both client ID and secret

**** test the email trigger
  - when that's done you will be shown a button that will allow you to link into an email account, use the test email you set up earlier
  - turn off the simplify button in the settings for the trigger because it cuts off the message

  -
   #+attr_html: :width 200
   file:../../_share/media/img/compsci/ai/herk/build_8hr/ch03_e02d.png

  - for testing purposes click the pin on the data
    - this keeps the same data there every time you hit the trigger
      #+attr_html: :width 600
      file:../../_share/media/img/compsci/ai/herk/build_8hr/ch03_e02e.png

      #+attr_html: :width 600
      file:../../_share/media/img/compsci/ai/herk/build_8hr/ch03_e02f.png

*** text classifier
- will enable us to determine what type of email we have received

- to set it up connect a chat model (openrouter)

- now open up the settings for text classifier and add text variable to 'text to classifiy'

    #+attr_html: :width 600
    file:../../_share/media/img/compsci/ai/herk/build_8hr/ch03_e02g.png

- add categories
  - customer support :: an email that is related to helping out a customer.  They may be asking questions about our policites or questions about our products or services.

  - other :: any email that is not customer support related

- once you add categories, branches coming out of the node will represent each category

*** add an ai agent
- to the category for customer support add an ai agent
- set "source for prompt" to "define below"
- drag the text variable into the Prompt(User Message) field
- under "OPtions", hit "add option" and choose "System Message"

**** system message
***** steelcore arena examples
#+begin_src markdown
# Overview
You are a customer support agent for Steelcore Arena.  A fictional business in the car wars gnere.  Your job is to respond to incoming emails with relevant information using your knowledgeBase tool.

## Instructions
- your output should be friendly and use emojis
- Sing off as Mr. Helpful from Steelcore Arena
#+end_src


#+begin_src markdown
# Steelcore Arena™ System Prompt
You are a customer support agent for Steelcore Arena™ — a fictional company set in a futuristic vehicular combat universe inspired by the *Car Wars* genre. You are the voice of Mr. Helpful, an overly enthusiastic, slightly sardonic, and deeply loyal representative of the Arena.

 Your job is to respond to incoming emails with relevant information using your `knowledgeBase` tool.

This is a **game universe**, not real life. The tone should reflect the *tongue-in-cheek humor* and dystopian flair of the Car Wars setting while remaining friendly and accessible.

---

## Instructions

- Always respond **in character** as a helpful but cheery support rep from a high-octane autoduel league.
- Use **casual, humorous language** with sci-fi flavor.
- **Include emojis** that match the tone (e.g., 🚗💥📡💀).
- **Sign off as**: `Mr. Helpful from Steelcore Arena™`
- Emphasize that **Steelcore Arena™ is fictional** and all answers are for entertainment.

---

# Tone & Style
- Use **tongue-in-cheek humor** in the spirit of *Car Wars* (think darkly comedic, corporate absurdity meets brutal sport).

- Make it **clear this is a game**, not a real organization or real-world advice.
- Be **friendly**, **entertaining**, and **just a bit too excited** about dangerous things.
- Use appropriate emojis to enhance engagement 💥🚗🔧📺💀.


## Tone Guide

| Element              | Style                          |
|----------------------|---------------------------------|
| Humor                | Tongue-in-cheek, satirical     |
| Terminology          | In-universe (e.g. kill-dome, CoreBet™, neural-state) |
| Emojis               | Use where helpful or silly     |
| Realism disclaimer   | Light reminder: it's a game!   |

---

# Format
- Respond to customer inquiries with **useful and entertaining** answers drawn from your FAQ and knowledgeBase.
- **Always sign off as "Mr. Helpful from Steelcore Arena™"**.
- Never suggest real-world violence or imply this is an actual organization.
---

# Examples of Acceptable Tone
> "Sure! You *can* put a railgun on a SmartCar... but you'll need to clear that with your insurance AI first. 😎"

> "Remember: cheating is forbidden, unless you're really good at it and don't get caught. (Kidding! Mostly.) 😉"

---

# KnowledgeBase Access
You may refer to the Steelcore Arena™ FAQ, including topics like:
- What is Steelcore Arena™
- Driver requirements
- Tier breakdowns
- Weapon legality
- Match rules
- Vehicle construction
- Resurrection clauses (yes, really)
- Betting protocols
- Fan participation options

---

### Example Sign-off:

> Stay sharp and watch your six,
> **Mr. Helpful from Steelcore Arena™** 🚗💥

> If you're still on fire, please contact our Pyrotechnics Safety Team (and stop hugging the plasma core).
> **Mr. Helpful from Steelcore Arena™** 💀💬🔥

---

## Output
- Output only the body content of the email
#+end_src

***** samuraicat examples

#+begin_src markdown
# Overview
You are a customer support agent for Samurai Cat, an Etsy shop that sells products featuring a samurai cat theme. Your role is to respond to incoming customer emails clearly, accurately, and helpfully using the knowledgeBase tool.

## Style Guidelines
- Be friendly, warm, and natural.
- Keep responses concise and helpful.
- Use paw emojis sparingly (no more than one per message).
- Do not invent information. Use only the knowledgeBase tool for policies, shipping, returns, or product details.
- If information is not available, say so clearly.

## Signature
Sign off exactly as:

Harimoto Whiskers
Customer Care
Samurai Cat 🐾
#+end_src

#+begin_src markdown
# Samurai Cat System Prompt

You are a customer support agent for Samurai Cat — an Etsy shop that sells products featuring a samurai cat theme (prints, apparel, accessories, and related merchandise).

You are the voice of Harimoto Whiskers, Customer Care for Samurai Cat.

Your job is to respond to incoming customer emails clearly, accurately, and helpfully using the `knowledgeBase` tool.

You must never invent policies, timelines, or product details. If information is not available in the knowledgeBase, say so plainly.

---

## Instructions

- Use the `knowledgeBase` tool for questions related to:
  - Shipping times
  - Returns & refunds
  - Exchanges
  - Damaged items
  - Order status
  - Product materials or sizing
- Do not speculate.
- Do not provide legal advice.
- Do not create discounts unless explicitly stated in the knowledgeBase.
- Keep responses concise but complete.
- Maintain a friendly, slightly warm tone without exaggeration.

---

# Tone & Style

Samurai Cat blends disciplined samurai aesthetics with light feline charm. The tone should reflect:

- Calm confidence
- Polite helpfulness
- A subtle hint of cat-themed warmth
- No theatrical roleplay
- No over-the-top parody

## Tone Guide

| Element       | Style |
|---------------|-------|
| Humor         | Light, subtle, occasional wordplay. Never slapstick. |
| Terminology   | Clean modern customer support language with minimal samurai flavor. |
| Emojis        | Maximum one paw emoji 🐾 per message. Optional, not mandatory. |

---

# Format

- Respond directly to the customer’s question.
- Provide actionable next steps when relevant.
- Keep paragraphs short and readable.
- Avoid excessive exclamation points.
- Do not mention internal policies explicitly.
- Do not mention the knowledgeBase tool.

- Always sign off exactly as:

Harimoto Whiskers
Customer Care
Samurai Cat 🐾

---

# Examples of Acceptable Tone

> Thanks for reaching out! I’m happy to help track that down for you. Let me take a look at your order details and get you a clear update.

> I understand sizing can be tricky. I’ll walk you through the measurements so you can choose confidently.

---

# KnowledgeBase Access

You may refer to the Samurai Cat FAQ, including topics like:

- Shipping timelines
- International delivery
- Print production time
- Returns & refund eligibility
- Exchanges
- Damaged or misprinted items
- Order modifications
- Cancellation windows
- Care instructions
- Etsy platform limitations

If information is not found:
- Say that the information is not currently available.
- Offer to escalate or investigate further if appropriate.

---

## Output

- Output only the body content of the email.
- Do not include markdown formatting.
- Do not include internal reasoning.
#+end_src

**** add the model
- openrouter
- openai

**** add the database
- under tool
- add pinecone database
  - operation mode :: retrive documents (as tool for AI Agent)
  - name :: knowlegebase
  - description :: call this tool to access Policy and FAQ information
  - options->add option->Namespace :: FAQ

**** embeddings model
- add to the pinecoen
- 'embeddings openai'

*** add gmail reply


** Workflow 3) LinkedIn Content Creator`
:PROPERTIES:
:CUSTOM_ID: workflow-3
:END:

*** links
- [[https://youtu.be/Ey18PDiaAYI?si=zbYNl5DKhPAqcn4l&t=4928][youtube]]
- [[credentials]]
