| 標題 | 作者 | 日期 |
|---|---|---|
使用Golang開發Genai應用 | 海姆特蘭 | 25/03/2024 |
我們在生產中經常看到的是關於GO,Java,.Net編寫的語言的多樣性,但是目前在Python和JavaScript中,大多數有關構建Genai(生成人工智能)應用程序的學習材料(生成人工智能)。該研討會展示瞭如何通過使用Langchain,簡化或React等框架來實施幾個基本示例的Amazon Bedrock。
示例包括:
您將僅使用基本編程概念實現這些功能,而無需學習新框架以幫助您真正理解和掌握基本概念。
建築學

該示例應用程序假設您已經設置了
請用您的
const BEDROCK_REGION = ""
const AOSS_REGION = ""
const KNOWLEDGE_BASE_REGION = ""
const KNOWLEDGE_BASE_ID = ""
const KNOWLEDGE_BASE_MODEL_ID = ""
const KNOWLEDGE_BASE_NUMBER_OF_RESULT = 6
const AOSS_ENDPOINT = ""
const AOSS_NOTE_APP_INDEX_NAME = "" 項目結構
| - - static
| - - aoss - index. html
| -- aoss - query. html
| -- claude - haiku. html
| -- image. html
| -- retrieve. html
| -- retrieve - generate. html
| -- bedrock
| -- aoss. go
| -- bedrock. go
| -- constants. go
| -- knowledge - based. go
| -- main. go
| -- go . mod
| -- go . sumMain.GO將HTTP服務器和路由請求實現到處理程序。 Bedrock.go和Aoss.go是呼籲召喚Amazon Bedrock和Amazon Opensearch無服務器(AOSS)的功能。靜態文件夾包含帶有JavaScript的簡單前端。
重要的
要使用AOSS,您需要創建一個OpenSearch集合,並在常數中提供其URL端點。此外,您需要在AOSS中為運行時間環境設置數據訪問(EC2配置文件,ECS TAKS角色,Lambda角色,.ETC)
按照此官方文件安裝Golang,然後運行該應用程序,如下
重要的
請安裝GO版本1.21.5
cd developing - genai - applications - with - golang
go run main . go 首先,最好根據Amazon Bedrock Claude3 API格式創建一些數據結構
type Content struct {
Type string `json:"type"`
Text string `json:"text"`
}
type Message struct {
Role string `json:"role"`
Content [] Content `json:"content"`
}
type Body struct {
MaxTokensToSample int `json:"max_tokens"`
Temperature float64 `json:"temperature,omitempty"`
AnthropicVersion string `json:"anthropic_version"`
Messages [] Message `json:"messages"`
}
// list of messages
messages := [] Message {{
Role : "user" ,
Content : [] Content {{ Type : "text" , Text : promt }},
}}
// form request body
payload := Body {
MaxTokensToSample : 2048 ,
Temperature : 0.9 ,
AnthropicVersion : "bedrock-2023-05-31" ,
Messages : messages ,
}然後將有效載荷轉換為字節並調用基岩客戶
payload := Body {
MaxTokensToSample : 2048 ,
Temperature : 0.9 ,
AnthropicVersion : "bedrock-2023-05-31" ,
Messages : messages ,
}
// marshal payload to bytes
payloadBytes , err := json . Marshal ( payload )
if err != nil {
fmt . Println ( err )
return
}
// create request to bedrock
output , error := BedrockClient . InvokeModelWithResponseStream (
context . Background (),
& bedrockruntime. InvokeModelWithResponseStreamInput {
Body : payloadBytes ,
ModelId : aws . String ( "anthropic.claude-3-haiku-20240307-v1:0" ),
ContentType : aws . String ( "application/json" ),
Accept : aws . String ( "application/json" ),
},
)
if error != nil {
fmt . Println ( error )
return
}最後,解析流回复響應並解碼為文本。當在HTTP服務器上部署時,我們需要對代碼進行一些修改以將響應的每個響應傳輸到客戶端。例如在這裡
output , error := BedrockClient . InvokeModelWithResponseStream (
context . Background (),
& bedrockruntime. InvokeModelWithResponseStreamInput {
Body : payloadBytes ,
ModelId : aws . String ( "anthropic.claude-3-haiku-20240307-v1:0" ),
ContentType : aws . String ( "application/json" ),
Accept : aws . String ( "application/json" ),
},
)
if error != nil {
fmt . Println ( error )
return
}
// parse response stream
for event := range output . GetStream (). Events () {
switch v := event .( type ) {
case * types. ResponseStreamMemberChunk :
//fmt.Println("payload", string(v.Value.Bytes))
var resp ResponseClaude3
err := json . NewDecoder ( bytes . NewReader ( v . Value . Bytes )). Decode ( & resp )
if err != nil {
fmt . Println ( err )
}
fmt . Println ( resp . Delta . Text )
case * types. UnknownUnionMember :
fmt . Println ( "unknown tag:" , v . Tag )
default :
fmt . Println ( "union is nil or unknown type" )
}
}該研討會不提供詳細的逐步部署應用程序。相反,它提供了整體體系結構和部署選項。在Amazon ECS上部署應用程序是直接的。

Dockerfile
# syntax = docker / dockerfile : 1
# Build the application from source
FROM golang : 1.21 . 5 AS build - stage
WORKDIR / app
COPY go . mod go . sum . /
RUN go mod download
COPY * . go . /
COPY bedrock . / bedrock
RUN CGO_ENABLED = 0 GOOS = linux go build - o / genaiapp
# Run the tests in the container
FROM build - stage AS run - test - stage
# Deploy the application binary into a lean image
FROM gcr . io / distroless / base - debian11 AS build - release - stage
WORKDIR /
COPY -- from = build - stage / genai - go - app / genaiapp
COPY static . / static
EXPOSE 3000
USER nonroot : nonroot
ENTRYPOINT [ "/genaiapp" ]ECS任務角色
任務角色需要權限
這是任務角色附加的IAM策略的樣本
{
"Version" : " 2012-10-17 " ,
"Statement" : [
{
"Action" : [
" bedrock:InvokeModel " ,
" bedrock:InvokeModelWithResponseStream " ,
" bedrock:InvokeAgent "
],
"Resource" : [
" arn:aws:bedrock:<REGION>::foundation-model/anthropic.claude-3-haiku-20240307-v1:0 " ,
" arn:aws:bedrock:<REGION>::foundation-model/amazon.titan-embed-text-v1 "
],
"Effect" : " Allow "
},
{
"Action" : [ " s3:GetObject " , " s3:PutObject " ],
"Resource" : [
" arn:aws:s3:::<BUCKET_NAME> " ,
" arn:aws:s3:::<BUCKET_NAME>/* "
],
"Effect" : " Allow "
},
{
"Action" : " aoss:APIAccessAll " ,
"Resource" : [
" arn:aws:aoss:<REGION>:<ACCOUNT_ID>:collection/<COLLECTION_ID> "
],
"Effect" : " Allow "
}
]
}AOSS數據訪問策略
為了允許任務角色訪問AOSS集合,我們需要更新集合的訪問策略。