Skip to content

Commit 94ed02e

Browse files
committed
Introduce config files, multiple backends, refactor
This commit introduces a major API change for `aiac` in both its CLI and library forms, as per feedback from the community: - `aiac` now uses a configuration file where various named backends may be defined, each targeting a specific LLM provider and environment. For example, one may configure a staging Amazon Bedrock environment, and a production environment. Users refer to these backends using the names they select for them. Command line flags such as `--api-key` and `--aws-profile` are no longer accepted and are now part of the configuration file. - `aiac` no longer hardcodes supported models for each LLM provider. Users can choose any model they want, and set a default model for each backend. The `--list-models` flag (previously the `list-models` subcommand) contacts the LLM provider's API to retrieve a list of supported models. - The `aiac` CLI is now simpler, and is no longer comprised of multiple subcommands, meaning flags such as `--model` no longer need to be provided before the "get" argument (which was actually a subcommand before), they will be accepted anywhere (outside of the actual prompt). The word "get" or "generate" no longer need to be used at all either. - `aiac` no longer errors out when the provider API response notes that the result was truncated. Apparently, this isn't always accurate. Instead, the "stop reason" returned by the API is returned to the user as part of the output. Refer to the [README.md](README.md) file for more information, including how to upgrade from a previous version. Resolves: #91, #121, #123
1 parent fee4609 commit 94ed02e

25 files changed

+773
-1067
lines changed

README.md

+237-107
Large diffs are not rendered by default.

demo.gif

-40.8 KB
Loading

demo.mp4

-1.64 MB
Binary file not shown.

go.mod

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
1-
module github.com/gofireflyio/aiac/v4
1+
module github.com/gofireflyio/aiac/v5
22

33
go 1.19
44

55
require (
6+
github.com/BurntSushi/toml v1.4.0
7+
github.com/adrg/xdg v0.4.0
68
github.com/alecthomas/kong v0.7.1
79
github.com/atotto/clipboard v0.1.4
8-
github.com/aws/aws-sdk-go-v2 v1.23.5
10+
github.com/aws/aws-sdk-go-v2 v1.30.0
911
github.com/aws/aws-sdk-go-v2/config v1.25.11
10-
github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.5.2
12+
github.com/aws/aws-sdk-go-v2/service/bedrock v1.9.1
13+
github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.11.0
1114
github.com/briandowns/spinner v1.19.0
1215
github.com/fatih/color v1.7.0
1316
github.com/ido50/requests v1.5.0
1417
github.com/manifoldco/promptui v0.9.0
15-
github.com/rodaine/table v1.1.0
1618
)
1719

1820
require (
19-
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.5.3 // indirect
21+
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 // indirect
2022
github.com/aws/aws-sdk-go-v2/credentials v1.16.9 // indirect
2123
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.9 // indirect
22-
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.8 // indirect
23-
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.8 // indirect
24+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.12 // indirect
25+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.12 // indirect
2426
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.1 // indirect
2527
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.10.3 // indirect
2628
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.8 // indirect
2729
github.com/aws/aws-sdk-go-v2/service/sso v1.18.2 // indirect
2830
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.2 // indirect
2931
github.com/aws/aws-sdk-go-v2/service/sts v1.26.2 // indirect
30-
github.com/aws/smithy-go v1.18.1 // indirect
32+
github.com/aws/smithy-go v1.20.2 // indirect
3133
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
34+
github.com/google/go-cmp v0.5.9 // indirect
3235
github.com/mattn/go-colorable v0.1.2 // indirect
3336
github.com/mattn/go-isatty v0.0.16 // indirect
3437
go.uber.org/atomic v1.10.0 // indirect

go.sum

+19-17
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,37 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
3737
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
3838
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
3939
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
40+
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
41+
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
4042
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
43+
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
44+
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
4145
github.com/alecthomas/assert/v2 v2.1.0 h1:tbredtNcQnoSd3QBhQWI7QZ3XHOVkw1Moklp2ojoH/0=
4246
github.com/alecthomas/kong v0.7.1 h1:azoTh0IOfwlAX3qN9sHWTxACE2oV8Bg2gAwBsMwDQY4=
4347
github.com/alecthomas/kong v0.7.1/go.mod h1:n1iCIO2xS46oE8ZfYCNDqdR0b0wZNrXAIAqro/2132U=
4448
github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE=
4549
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
4650
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
47-
github.com/aws/aws-sdk-go-v2 v1.23.5 h1:xK6C4udTyDMd82RFvNkDQxtAd00xlzFUtX4fF2nMZyg=
48-
github.com/aws/aws-sdk-go-v2 v1.23.5/go.mod h1:t3szzKfP0NeRU27uBFczDivYJjsmSnqI8kIvKyWb9ds=
49-
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.5.3 h1:Zx9+31KyB8wQna6SXFWOewlgoY5uGdDAu6PTOEU3OQI=
50-
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.5.3/go.mod h1:zxbEJhRdKTH1nqS2qu6UJ7zGe25xaHxZXaC2CvuQFnA=
51+
github.com/aws/aws-sdk-go-v2 v1.30.0 h1:6qAwtzlfcTtcL8NHtbDQAqgM5s6NDipQTkPxyH/6kAA=
52+
github.com/aws/aws-sdk-go-v2 v1.30.0/go.mod h1:ffIFB97e2yNsv4aTSGkqtHnppsIJzw7G7BReUZ3jCXM=
53+
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 h1:x6xsQXGSmW6frevwDA+vi/wqhp1ct18mVXYN08/93to=
54+
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2/go.mod h1:lPprDr1e6cJdyYeGXnRaJoP4Md+cDBvi2eOj00BlGmg=
5155
github.com/aws/aws-sdk-go-v2/config v1.25.11 h1:RWzp7jhPRliIcACefGkKp03L0Yofmd2p8M25kbiyvno=
5256
github.com/aws/aws-sdk-go-v2/config v1.25.11/go.mod h1:BVUs0chMdygHsQtvaMyEOpW2GIW+ubrxJLgIz/JU29s=
5357
github.com/aws/aws-sdk-go-v2/credentials v1.16.9 h1:LQo3MUIOzod9JdUK+wxmSdgzLVYUbII3jXn3S/HJZU0=
5458
github.com/aws/aws-sdk-go-v2/credentials v1.16.9/go.mod h1:R7mDuIJoCjH6TxGUc/cylE7Lp/o0bhKVoxdBThsjqCM=
5559
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.9 h1:FZVFahMyZle6WcogZCOxo6D/lkDA2lqKIn4/ueUmVXw=
5660
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.9/go.mod h1:kjq7REMIkxdtcEC9/4BVXjOsNY5isz6jQbEgk6osRTU=
57-
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.8 h1:8GVZIR0y6JRIUNSYI1xAMF4HDfV8H/bOsZ/8AD/uY5Q=
58-
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.8/go.mod h1:rwBfu0SoUkBUZndVgPZKAD9Y2JigaZtRP68unRiYToQ=
59-
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.8 h1:ZE2ds/qeBkhk3yqYvS3CDCFNvd9ir5hMjlVStLZWrvM=
60-
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.8/go.mod h1:/lAPPymDYL023+TS6DJmjuL42nxix2AvEvfjqOBRODk=
61+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.12 h1:SJ04WXGTwnHlWIODtC5kJzKbeuHt+OUNOgKg7nfnUGw=
62+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.12/go.mod h1:FkpvXhA92gb3GE9LD6Og0pHHycTxW7xGpnEh5E7Opwo=
63+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.12 h1:hb5KgeYfObi5MHkSSZMEudnIvX30iB+E21evI4r6BnQ=
64+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.12/go.mod h1:CroKe/eWJdyfy9Vx4rljP5wTUjNJfb+fPz1uMYUhEGM=
6165
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.1 h1:uR9lXYjdPX0xY+NhvaJ4dD8rpSRz5VY81ccIIoNG+lw=
6266
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.1/go.mod h1:6fQQgfuGmw8Al/3M2IgIllycxV7ZW7WCdVSqfBeUiCY=
63-
github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.5.2 h1:bonPpjgMAJwOau/Tb2nNvKxf3hwVICOFqJrkse4efs4=
64-
github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.5.2/go.mod h1:S4sAnHGFOt/VqsFShOVNIJ9o0KhNC3vLo4v26dflnRU=
67+
github.com/aws/aws-sdk-go-v2/service/bedrock v1.9.1 h1:e2FZRSG0h0nA/sdRJJXtOnbMXPvAFUoyRNWG3oTioyc=
68+
github.com/aws/aws-sdk-go-v2/service/bedrock v1.9.1/go.mod h1:4e+Wd418o5s7L7NChi9vWNjMkJtm/yi6NmWy5efcACk=
69+
github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.11.0 h1:wHTY1k+myd0QIZevhf2XiKF4rLs37vlLguJV6LFjUQ0=
70+
github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.11.0/go.mod h1:vHk9LI9clsbT8DYUmHtBxinKBlnp4XvxqyaCXA7J2bY=
6571
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.10.3 h1:e3PCNeEaev/ZF01cQyNZgmYE9oYYePIMJs2mWSKG514=
6672
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.10.3/go.mod h1:gIeeNyaL8tIEqZrzAnTeyhHcE0yysCtcaP+N9kxLZ+E=
6773
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.8 h1:EamsKe+ZjkOQjDdHd86/JCEucjFKQ9T0atWKO4s2Lgs=
@@ -72,8 +78,8 @@ github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.2 h1:8dU9zqA77C5egbU6yd4hFLai
7278
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.2/go.mod h1:7Lt5mjQ8x5rVdKqg+sKKDeuwoszDJIIPmkd8BVsEdS0=
7379
github.com/aws/aws-sdk-go-v2/service/sts v1.26.2 h1:fFrLsy08wEbAisqW3KDl/cPHrF43GmV79zXB9EwJiZw=
7480
github.com/aws/aws-sdk-go-v2/service/sts v1.26.2/go.mod h1:7Ld9eTqocTvJqqJ5K/orbSDwmGcpRdlDiLjz2DO+SL8=
75-
github.com/aws/smithy-go v1.18.1 h1:pOdBTUfXNazOlxLrgeYalVnuTpKreACHtc62xLwIB3c=
76-
github.com/aws/smithy-go v1.18.1/go.mod h1:NukqUGpCZIILqqiV0NIjeFh24kd/FAa4beRb6nbIUPE=
81+
github.com/aws/smithy-go v1.20.2 h1:tbp628ireGtzcHDDmLT/6ADHidqnwgF57XOXZe6tp4Q=
82+
github.com/aws/smithy-go v1.20.2/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E=
7783
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
7884
github.com/briandowns/spinner v1.19.0 h1:s8aq38H+Qju89yhp89b4iIiMzMm8YN3p6vGpwyh/a8E=
7985
github.com/briandowns/spinner v1.19.0/go.mod h1:mQak9GHqbspjC/5iUx3qMlIho8xBS/ppAL/hX5SmPJU=
@@ -183,25 +189,20 @@ github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVc
183189
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
184190
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
185191
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
186-
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
187-
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
188192
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
189193
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
190194
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
191195
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
192196
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
193197
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
194198
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
195-
github.com/rodaine/table v1.1.0 h1:/fUlCSdjamMY8VifdQRIu3VWZXYLY7QHFkVorS8NTr4=
196-
github.com/rodaine/table v1.1.0/go.mod h1:Qu3q5wi1jTQD6B6HsP6szie/S4w1QUQ8pq22pz9iL8g=
197199
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
198200
github.com/spf13/afero v1.9.2 h1:j49Hj62F0n+DaZ1dDCvhABaPNSGNkt32oRFxI33IEMw=
199201
github.com/spf13/afero v1.9.2/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y=
200202
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
201203
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
202204
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
203205
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
204-
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
205206
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
206207
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
207208
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
@@ -352,6 +353,7 @@ golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7w
352353
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
353354
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
354355
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
356+
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
355357
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
356358
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
357359
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

libaiac/bedrock/bedrock.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package bedrock
2+
3+
import (
4+
"github.com/aws/aws-sdk-go-v2/aws"
5+
"github.com/aws/aws-sdk-go-v2/service/bedrock"
6+
"github.com/aws/aws-sdk-go-v2/service/bedrockruntime"
7+
)
8+
9+
// Bedrock is the struct that implements libaiac's Backend interface.
10+
type Bedrock struct {
11+
runtime *bedrockruntime.Client
12+
service *bedrock.Client
13+
}
14+
15+
const (
16+
// DefaultAWSRegion is the default AWS region to use if the backend does not
17+
// specify one
18+
DefaultAWSRegion = "us-east-1"
19+
20+
// DefaultAWSProfile is the default AWS profile to use if the backend does
21+
// not specify one
22+
DefaultAWSProfile = "default"
23+
)
24+
25+
// New constructs a new Bedrock object. It receives a standard aws.Config
26+
// object.
27+
func New(cfg aws.Config) *Bedrock {
28+
return &Bedrock{
29+
runtime: bedrockruntime.NewFromConfig(cfg),
30+
service: bedrock.NewFromConfig(cfg),
31+
}
32+
}

libaiac/bedrock/chat.go

+39-56
Original file line numberDiff line numberDiff line change
@@ -3,98 +3,81 @@ package bedrock
33
import (
44
"context"
55
"fmt"
6-
"strings"
76

87
"github.com/aws/aws-sdk-go-v2/aws"
98
"github.com/aws/aws-sdk-go-v2/service/bedrockruntime"
10-
"github.com/gofireflyio/aiac/v4/libaiac/types"
9+
bedrocktypes "github.com/aws/aws-sdk-go-v2/service/bedrockruntime/types"
10+
"github.com/gofireflyio/aiac/v5/libaiac/types"
1111
)
1212

1313
// Conversation is a struct used to converse with a Bedrock chat model. It
1414
// maintains all messages sent/received in order to maintain context.
1515
type Conversation struct {
16-
client *Client
17-
model types.Model
18-
messages []types.Message
16+
backend *Bedrock
17+
model string
18+
messages []bedrocktypes.Message
1919
}
2020

2121
// Chat initiates a conversation with a Bedrock chat model. A conversation
2222
// maintains context, allowing to send further instructions to modify the output
2323
// from previous requests.
24-
func (client *Client) Chat(model types.Model) types.Conversation {
25-
if model.Type != types.ModelTypeChat {
26-
return nil
27-
}
28-
24+
func (backend *Bedrock) Chat(model string) types.Conversation {
2925
return &Conversation{
30-
client: client,
31-
model: model,
26+
backend: backend,
27+
model: model,
3228
}
3329
}
3430

3531
// Send sends the provided message to the backend and returns a Response object.
3632
// To maintain context, all previous messages (whether from you to the API or
3733
// vice-versa) are sent as well, allowing you to ask the API to modify the
3834
// code it already generated.
39-
func (conv *Conversation) Send(ctx context.Context, prompt string, msgs ...types.Message) (
35+
func (conv *Conversation) Send(ctx context.Context, prompt string) (
4036
res types.Response,
4137
err error,
4238
) {
43-
if len(msgs) > 0 {
44-
conv.messages = append(conv.messages, msgs...)
45-
}
46-
47-
conv.messages = append(conv.messages, types.Message{
48-
Role: "user",
49-
Content: prompt,
39+
conv.messages = append(conv.messages, bedrocktypes.Message{
40+
Role: bedrocktypes.ConversationRoleUser,
41+
Content: []bedrocktypes.ContentBlock{
42+
&bedrocktypes.ContentBlockMemberText{Value: prompt},
43+
},
5044
})
5145

52-
var inputText strings.Builder
53-
for _, msg := range conv.messages {
54-
switch msg.Role {
55-
case "user":
56-
fmt.Fprint(&inputText, "\n\nHuman: ")
57-
default:
58-
fmt.Fprint(&inputText, "\n\nAssistant: ")
59-
}
60-
61-
fmt.Fprint(&inputText, msg.Content)
46+
input := bedrockruntime.ConverseInput{
47+
ModelId: aws.String(conv.model),
48+
Messages: conv.messages,
49+
InferenceConfig: &bedrocktypes.InferenceConfiguration{
50+
Temperature: aws.Float32(0.2),
51+
},
6252
}
6353

64-
fmt.Fprintf(&inputText, "\n\nAssistant:")
65-
66-
body, err := conv.client.generateInputJSON(conv.model, inputText.String())
54+
output, err := conv.backend.runtime.Converse(ctx, &input)
6755
if err != nil {
68-
return res, fmt.Errorf("failed generating input JSON: %w", err)
56+
return res, fmt.Errorf("failed sending prompt: %w", err)
6957
}
7058

71-
output, err := conv.client.backend.InvokeModel(
72-
ctx,
73-
&bedrockruntime.InvokeModelInput{
74-
Body: body,
75-
ModelId: aws.String(conv.model.Name),
76-
Accept: aws.String("application/json"),
77-
ContentType: aws.String("application/json"),
78-
},
79-
)
80-
if err != nil {
81-
return res, fmt.Errorf("failed sending prompt: %w", err)
59+
outputMsgMember, ok := output.Output.(*bedrocktypes.ConverseOutputMemberMessage)
60+
if !ok {
61+
return res, fmt.Errorf("Bedrock returned an unexpected response")
8262
}
8363

84-
res.FullOutput, res.TokensUsed, err = conv.client.parseOutputJSON(
85-
conv.model,
86-
output.Body,
87-
)
88-
if err != nil {
89-
return res, err
64+
if len(outputMsgMember.Value.Content) == 0 {
65+
return res, fmt.Errorf("Bedrock didn't return any message")
9066
}
9167

92-
conv.messages = append(conv.messages, types.Message{
93-
Role: "assistant",
94-
Content: res.FullOutput,
95-
})
68+
outputMsg := outputMsgMember.Value
69+
70+
outputTxt, ok := outputMsg.Content[0].(*bedrocktypes.ContentBlockMemberText)
71+
if !ok {
72+
return res, fmt.Errorf("Bedrock return an unexpected response")
73+
}
74+
75+
res.FullOutput = outputTxt.Value
76+
res.TokensUsed = int64(*output.Usage.TotalTokens)
77+
res.StopReason = string(output.StopReason)
78+
79+
conv.messages = append(conv.messages, outputMsg)
9680

97-
var ok bool
9881
if res.Code, ok = types.ExtractCode(res.FullOutput); !ok {
9982
res.Code = res.FullOutput
10083
}

libaiac/bedrock/client.go

-18
This file was deleted.

0 commit comments

Comments
 (0)