# Build Chatbots with Amazon Bedrock
Amazon Bedrock
# How it works

# Prerequisites
1. Download PDF Guide from [Guidelines](https://selling-guide.fanniemae.com/)
2. 在 Search 找到 **S3** 去 create 一個 create-customer-resources-kb-bucket
3. 接著在 Search 找到 **Bedrock** ,選擇 **Amazon Bedrock**
4. 在側邊欄位選擇 **Model access**

5. 選擇 **Enable specific models** 來選擇想要用的模型
# Knowledge base for Amazon Bedrock
## Create Knowledge base
1. 一樣在 **Amazon Bedrock** 的側欄選擇 **Knowledge bases**

2. 接著根據需求選擇要儲存的格式,那這裡可以先選用 Knowledge Base with vector store

3. 可以先根據此範例建立 Knowledge Base

4. 再建完 Knowledge Base 之後,可以上傳要放在 Knowledge Base 的資料,最後一定要記得 **Sync(同步)**

## Test Knowledge base
1. 在同步完之後可以先關掉 **Generate responses**

2. 這樣 Amazon Bedrock 只會傳回與查詢相關的資料來源中的文字區塊。
# Agents for Amazon Bedrock
## How it works

## Prerequisites
1. 先下載 [api-schema](https://ws-assets-prod-iad-r-pdx-f3b3f9f1a7d6a3d0.s3.us-west-2.amazonaws.com/1e5b6626-f63c-41d7-adcb-4a3cdfd279ac/api-schema.zip)
2. 解壓縮之後,只要將 loan_calculator.json 和 mls_lookup.json 上傳到剛剛在 S3 創建的 Knowledge Base 中

## Create an Agent
1. 在 Amazon Bedrock 的側欄選擇 **Agents**,接著按下 Create Agent

- 若想要多個 agent 並行,可以勾選 Enable multi-agent collaboration
2. 接著就是按照想要使用的模型和 agent 要執行的功能去做設定,以下為範例:

### Add Action Groups





### Add Knowledge Bases



# Create Prompts using Prompt Management in Amazon Bedrock
1. 在 Amazon Bedrock 的側欄選擇 **Prompt Management**

2. 範例 promp:
```
Generate a question asking if the user will qualify for a loan for the specified criteria.
Include instruction to base the answer on key features of the property retrived from MLS listing.
Start with "will an applicant...".
{ "income": {{income}}, "creditScore": {{creditScore}}, "totalDebt": {{totalDebt}}, "loanAmount": {{loanAmount}}, "mlsId": {{mlsId}} }
Include a second question on loan requirements an applicant with the below attributes should consider for a Fannie Mae backed loan (other than debt to income).
```

* {{variable}} 是用來來建立變數
* 所以從範例中可以發現他用了income、creditScore、totalDebt、loanAmount、mlsId五個變數
2. 接著可以用 Configurations ,在選擇 Model 之後,可以去調整 Model 的參數


* Temperature 介於 0 到 1 之間的數值,它調節 LLM 反應的創造力。如果想要更確定的回應,請使用較低的Temperature ;如果想要對來自 Amazon Bedrock 上的 LLM 的相同提示獲得更具創造性或不同的回應,請使用較高的Temperature
# Amazon Bedrock Flows
## How it works

## Create a guardrail
1. 可以跟著自己想要創造的邏輯來畫出的個 flow ,但又幾點要別注意:
* 型別 -> Name、Type、Expression
* 尤其 Expression 要注意要和輸入或是回傳的格式一樣,ex:
input的格式長這樣
```
{ "income": 80000, "totalDebt": 5000, "loanTerm": 30, "loanAmount": 600000, "creditScore": 750, "mlsId": "MLS-3456" }
```
需要用到loanTerm時,他的類別會是這樣:
```
Name: loanTerm
Type: Number
Expression: $.data.loanTerm
```
需要用到mlsId時,他的類別會是這樣:
```
Name: mlsId
Type: String
Expression: $.data.mlsId
```
2. 這是自製 flow 範例:

# Create a guardrail
## 在 Amazon Bedrock 側欄 的 Safeguards 選擇 **Guardrails**
1. 輸入名稱,接著就可以案next了,除非想要改封鎖提示的傳訊

2. 接著可以根據想要篩選的內容去調整 Configure content filters ,如果沒有也可以保留原設定

3. 接著可以加入想要 bolck 的 Topics,以下是範例:
* Name :Investment Advice
* Definition:
```
Investment advice refers to inquires, guidance or recommendations regarding the management or allocation of fund or asset with the goal of generating returns or achieving specific financial objectives
```
* Add sample phrases
`
Is investing in the stocks better than bonds?
`
`
Should I invest in gold?
`
4. 加入 Words Filters

以下是範例:
`
crypto currency
`
`
bitcoin
`
5. 加入 sensitive information filters
* 按下 **Add new PII**
* 接著找到 **Email** ,接著看需求是要 Block 還是 Mask 都可以
* 再找到 **Credit/Debit card number** ,接著看需求是要 Block 還是 Mask 都可以
6. 最後可以根據需求去調整 contextual grounding check
* Grounding:將檢查模型響應是否基於來源而真實且準確,並且是否以來源為依據。將 Grounding score threshold 設定為0.85

* Relevance:檢查模型回應是否與使用者查詢相關。將Relevance score threshold設定為0.5

## Test the guardrail
測試範例:
```
Please summarize the below call center transcript. Put the name, email and the customer ID to the top:
Agent: Welcome to ABC company. How can I help you today?
Customer: I want to cancel my hotel booking.
Agent: Sure, I can help you with the cancellation. Can you please provide your customer ID?
Customer: Yes, my customer Id is trx-1234.
Agent: Thank you. Can I have your name and email for confirmation?
Customer: My name is Jane Doe and my email is jane.doe@gmail.com
Agent: Thank you for confirming. I will go ahead and cancel your reservation.
```
測試結果:
