-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
208 lines (166 loc) · 5.1 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
package main
import (
"context"
gobedrock "entest/gobedrock/bedrock"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/bedrockagentruntime"
"github.com/aws/aws-sdk-go-v2/service/bedrockruntime"
opensearch "github.com/opensearch-project/opensearch-go/v2"
requestsigner "github.com/opensearch-project/opensearch-go/v2/signer/awsv2"
"github.com/rs/cors"
)
// opensearch severless client
var AOSSClient *opensearch.Client
// bedrock runtime client
var BedrockClient *bedrockruntime.Client
// bedrock agent runtime client
var BedrockAgentRuntimeClient *bedrockagentruntime.Client
// create an init function to initializing opensearch client
func init() {
//
fmt.Println("init and create an opensearch client")
// load aws credentials from profile demo using config
awsCfg1, err := config.LoadDefaultConfig(context.Background(),
config.WithRegion(gobedrock.BEDROCK_REGION),
)
if err != nil {
log.Fatal(err)
}
awsCfg2, err := config.LoadDefaultConfig(context.Background(),
config.WithRegion(gobedrock.AOSS_REGION),
)
if err != nil {
log.Fatal(err)
}
// create a aws request signer using requestsigner
signer, err := requestsigner.NewSignerWithService(awsCfg2, "aoss")
if err != nil {
log.Fatal(err)
}
// create an opensearch client using opensearch package
AOSSClient, err = opensearch.NewClient(opensearch.Config{
Addresses: []string{gobedrock.AOSS_ENDPOINT},
Signer: signer,
})
if err != nil {
log.Fatal(err)
}
// create bedrock runtime client
BedrockClient = bedrockruntime.NewFromConfig(awsCfg1)
// create bedrock agent runtime client
BedrockAgentRuntimeClient = bedrockagentruntime.NewFromConfig(awsCfg1)
}
func main() {
// create handler multiplexer
mux := http.NewServeMux()
// frontend claude haiku
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
content, error := os.ReadFile("./static/claude-haiku.html")
if error != nil {
fmt.Println(error)
}
w.Write(content)
})
// backend claude haiku
mux.HandleFunc("/bedrock-haiku", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
gobedrock.HandleBedrockClaude3HaikuChat(w, r, BedrockClient)
}
})
// bedrock frontend for image analyzer
mux.HandleFunc("/image", func(w http.ResponseWriter, r *http.Request) {
content, error := os.ReadFile("./static/image.html")
if error != nil {
fmt.Println(error)
}
w.Write(content)
})
// bedrock backend to analyze image
mux.HandleFunc("/claude-haiku-image", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
gobedrock.HandleHaikuImageAnalyzer(w, r, BedrockClient)
}
})
// magic mirror frontend
mux.HandleFunc("/mirror", func(w http.ResponseWriter, r *http.Request) {
content, error := os.ReadFile("./static/mirror.html")
if error != nil {
fmt.Println(error)
}
w.Write(content)
})
// knowledge based retrieve frontend
mux.HandleFunc("/retrieve", func(w http.ResponseWriter, r *http.Request) {
content, error := os.ReadFile("./static/retrieve.html")
if error != nil {
fmt.Println(error)
}
w.Write(content)
})
// knowledge based retrieve backend
mux.HandleFunc("/knowledge-base-retrieve", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
gobedrock.HandleRetrieve(w, r, BedrockAgentRuntimeClient)
}
})
// knowledge based retrieve frontend
mux.HandleFunc("/retrieve-generate", func(w http.ResponseWriter, r *http.Request) {
content, error := os.ReadFile("./static/retrieve-and-generate.html")
if error != nil {
fmt.Println(error)
}
w.Write(content)
})
// knowledge based retrieve backend
mux.HandleFunc("/knowledge-base-retrieve-and-generate", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
gobedrock.HandleRetrieveAndGenerate(w, r, BedrockAgentRuntimeClient)
}
})
// handle aoss index frontend
mux.HandleFunc("/aoss-index", func(w http.ResponseWriter, r *http.Request) {
content, error := os.ReadFile("./static/aoss-index.html")
if error != nil {
fmt.Println(error)
}
w.Write(content)
})
// handle index to aoss
mux.HandleFunc("/aoss-index-backend", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
gobedrock.HandleAOSSIndex(w, r, AOSSClient, BedrockClient)
}
})
// handle aoss query frontend
mux.HandleFunc("/aoss-query", func(w http.ResponseWriter, r *http.Request) {
content, error := os.ReadFile("./static/aoss-query.html")
if error != nil {
fmt.Println(error)
}
w.Write(content)
})
// handle query to aoss backend
mux.HandleFunc("/aoss-query-backend", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
gobedrock.HandleAOSSQueryByTitle(w, r, AOSSClient, BedrockClient)
}
})
// allow cors
handler := cors.AllowAll().Handler(mux)
// create a http server using http
server := http.Server{
Addr: ":3000",
Handler: handler,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
MaxHeaderBytes: 1 << 20,
}
server.ListenAndServe()
}