Skip to content

Commit

Permalink
Introduce config files, multiple backends, refactor
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ido50 committed Jun 26, 2024
1 parent fee4609 commit 94ed02e
Show file tree
Hide file tree
Showing 25 changed files with 773 additions and 1,067 deletions.
344 changes: 237 additions & 107 deletions README.md

Large diffs are not rendered by default.

Binary file modified demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed demo.mp4
Binary file not shown.
19 changes: 11 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
module github.com/gofireflyio/aiac/v4
module github.com/gofireflyio/aiac/v5

go 1.19

require (
github.com/BurntSushi/toml v1.4.0
github.com/adrg/xdg v0.4.0
github.com/alecthomas/kong v0.7.1
github.com/atotto/clipboard v0.1.4
github.com/aws/aws-sdk-go-v2 v1.23.5
github.com/aws/aws-sdk-go-v2 v1.30.0
github.com/aws/aws-sdk-go-v2/config v1.25.11
github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.5.2
github.com/aws/aws-sdk-go-v2/service/bedrock v1.9.1
github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.11.0
github.com/briandowns/spinner v1.19.0
github.com/fatih/color v1.7.0
github.com/ido50/requests v1.5.0
github.com/manifoldco/promptui v0.9.0
github.com/rodaine/table v1.1.0
)

require (
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.5.3 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.16.9 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.9 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.8 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.8 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.12 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.12 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.1 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.10.3 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.8 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.18.2 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.2 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.26.2 // indirect
github.com/aws/smithy-go v1.18.1 // indirect
github.com/aws/smithy-go v1.20.2 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
go.uber.org/atomic v1.10.0 // indirect
Expand Down
36 changes: 19 additions & 17 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,37 @@ cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9
cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/adrg/xdg v0.4.0 h1:RzRqFcjH4nE5C6oTAxhBtoE2IRyjBSa62SCbyPidvls=
github.com/adrg/xdg v0.4.0/go.mod h1:N6ag73EX4wyxeaoeHctc1mas01KZgsj5tYiAIwqJE/E=
github.com/alecthomas/assert/v2 v2.1.0 h1:tbredtNcQnoSd3QBhQWI7QZ3XHOVkw1Moklp2ojoH/0=
github.com/alecthomas/kong v0.7.1 h1:azoTh0IOfwlAX3qN9sHWTxACE2oV8Bg2gAwBsMwDQY4=
github.com/alecthomas/kong v0.7.1/go.mod h1:n1iCIO2xS46oE8ZfYCNDqdR0b0wZNrXAIAqro/2132U=
github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/aws/aws-sdk-go-v2 v1.23.5 h1:xK6C4udTyDMd82RFvNkDQxtAd00xlzFUtX4fF2nMZyg=
github.com/aws/aws-sdk-go-v2 v1.23.5/go.mod h1:t3szzKfP0NeRU27uBFczDivYJjsmSnqI8kIvKyWb9ds=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.5.3 h1:Zx9+31KyB8wQna6SXFWOewlgoY5uGdDAu6PTOEU3OQI=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.5.3/go.mod h1:zxbEJhRdKTH1nqS2qu6UJ7zGe25xaHxZXaC2CvuQFnA=
github.com/aws/aws-sdk-go-v2 v1.30.0 h1:6qAwtzlfcTtcL8NHtbDQAqgM5s6NDipQTkPxyH/6kAA=
github.com/aws/aws-sdk-go-v2 v1.30.0/go.mod h1:ffIFB97e2yNsv4aTSGkqtHnppsIJzw7G7BReUZ3jCXM=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 h1:x6xsQXGSmW6frevwDA+vi/wqhp1ct18mVXYN08/93to=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2/go.mod h1:lPprDr1e6cJdyYeGXnRaJoP4Md+cDBvi2eOj00BlGmg=
github.com/aws/aws-sdk-go-v2/config v1.25.11 h1:RWzp7jhPRliIcACefGkKp03L0Yofmd2p8M25kbiyvno=
github.com/aws/aws-sdk-go-v2/config v1.25.11/go.mod h1:BVUs0chMdygHsQtvaMyEOpW2GIW+ubrxJLgIz/JU29s=
github.com/aws/aws-sdk-go-v2/credentials v1.16.9 h1:LQo3MUIOzod9JdUK+wxmSdgzLVYUbII3jXn3S/HJZU0=
github.com/aws/aws-sdk-go-v2/credentials v1.16.9/go.mod h1:R7mDuIJoCjH6TxGUc/cylE7Lp/o0bhKVoxdBThsjqCM=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.9 h1:FZVFahMyZle6WcogZCOxo6D/lkDA2lqKIn4/ueUmVXw=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.9/go.mod h1:kjq7REMIkxdtcEC9/4BVXjOsNY5isz6jQbEgk6osRTU=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.8 h1:8GVZIR0y6JRIUNSYI1xAMF4HDfV8H/bOsZ/8AD/uY5Q=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.8/go.mod h1:rwBfu0SoUkBUZndVgPZKAD9Y2JigaZtRP68unRiYToQ=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.8 h1:ZE2ds/qeBkhk3yqYvS3CDCFNvd9ir5hMjlVStLZWrvM=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.8/go.mod h1:/lAPPymDYL023+TS6DJmjuL42nxix2AvEvfjqOBRODk=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.12 h1:SJ04WXGTwnHlWIODtC5kJzKbeuHt+OUNOgKg7nfnUGw=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.12/go.mod h1:FkpvXhA92gb3GE9LD6Og0pHHycTxW7xGpnEh5E7Opwo=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.12 h1:hb5KgeYfObi5MHkSSZMEudnIvX30iB+E21evI4r6BnQ=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.12/go.mod h1:CroKe/eWJdyfy9Vx4rljP5wTUjNJfb+fPz1uMYUhEGM=
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.1 h1:uR9lXYjdPX0xY+NhvaJ4dD8rpSRz5VY81ccIIoNG+lw=
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.1/go.mod h1:6fQQgfuGmw8Al/3M2IgIllycxV7ZW7WCdVSqfBeUiCY=
github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.5.2 h1:bonPpjgMAJwOau/Tb2nNvKxf3hwVICOFqJrkse4efs4=
github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.5.2/go.mod h1:S4sAnHGFOt/VqsFShOVNIJ9o0KhNC3vLo4v26dflnRU=
github.com/aws/aws-sdk-go-v2/service/bedrock v1.9.1 h1:e2FZRSG0h0nA/sdRJJXtOnbMXPvAFUoyRNWG3oTioyc=
github.com/aws/aws-sdk-go-v2/service/bedrock v1.9.1/go.mod h1:4e+Wd418o5s7L7NChi9vWNjMkJtm/yi6NmWy5efcACk=
github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.11.0 h1:wHTY1k+myd0QIZevhf2XiKF4rLs37vlLguJV6LFjUQ0=
github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.11.0/go.mod h1:vHk9LI9clsbT8DYUmHtBxinKBlnp4XvxqyaCXA7J2bY=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.10.3 h1:e3PCNeEaev/ZF01cQyNZgmYE9oYYePIMJs2mWSKG514=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.10.3/go.mod h1:gIeeNyaL8tIEqZrzAnTeyhHcE0yysCtcaP+N9kxLZ+E=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.10.8 h1:EamsKe+ZjkOQjDdHd86/JCEucjFKQ9T0atWKO4s2Lgs=
Expand All @@ -72,8 +78,8 @@ github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.2 h1:8dU9zqA77C5egbU6yd4hFLai
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.2/go.mod h1:7Lt5mjQ8x5rVdKqg+sKKDeuwoszDJIIPmkd8BVsEdS0=
github.com/aws/aws-sdk-go-v2/service/sts v1.26.2 h1:fFrLsy08wEbAisqW3KDl/cPHrF43GmV79zXB9EwJiZw=
github.com/aws/aws-sdk-go-v2/service/sts v1.26.2/go.mod h1:7Ld9eTqocTvJqqJ5K/orbSDwmGcpRdlDiLjz2DO+SL8=
github.com/aws/smithy-go v1.18.1 h1:pOdBTUfXNazOlxLrgeYalVnuTpKreACHtc62xLwIB3c=
github.com/aws/smithy-go v1.18.1/go.mod h1:NukqUGpCZIILqqiV0NIjeFh24kd/FAa4beRb6nbIUPE=
github.com/aws/smithy-go v1.20.2 h1:tbp628ireGtzcHDDmLT/6ADHidqnwgF57XOXZe6tp4Q=
github.com/aws/smithy-go v1.20.2/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/briandowns/spinner v1.19.0 h1:s8aq38H+Qju89yhp89b4iIiMzMm8YN3p6vGpwyh/a8E=
github.com/briandowns/spinner v1.19.0/go.mod h1:mQak9GHqbspjC/5iUx3qMlIho8xBS/ppAL/hX5SmPJU=
Expand Down Expand Up @@ -183,25 +189,20 @@ github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVc
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rodaine/table v1.1.0 h1:/fUlCSdjamMY8VifdQRIu3VWZXYLY7QHFkVorS8NTr4=
github.com/rodaine/table v1.1.0/go.mod h1:Qu3q5wi1jTQD6B6HsP6szie/S4w1QUQ8pq22pz9iL8g=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/spf13/afero v1.9.2 h1:j49Hj62F0n+DaZ1dDCvhABaPNSGNkt32oRFxI33IEMw=
github.com/spf13/afero v1.9.2/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down Expand Up @@ -352,6 +353,7 @@ golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
32 changes: 32 additions & 0 deletions libaiac/bedrock/bedrock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package bedrock

import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/bedrock"
"github.com/aws/aws-sdk-go-v2/service/bedrockruntime"
)

// Bedrock is the struct that implements libaiac's Backend interface.
type Bedrock struct {
runtime *bedrockruntime.Client
service *bedrock.Client
}

const (
// DefaultAWSRegion is the default AWS region to use if the backend does not
// specify one
DefaultAWSRegion = "us-east-1"

// DefaultAWSProfile is the default AWS profile to use if the backend does
// not specify one
DefaultAWSProfile = "default"
)

// New constructs a new Bedrock object. It receives a standard aws.Config
// object.
func New(cfg aws.Config) *Bedrock {
return &Bedrock{
runtime: bedrockruntime.NewFromConfig(cfg),
service: bedrock.NewFromConfig(cfg),
}
}
95 changes: 39 additions & 56 deletions libaiac/bedrock/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,98 +3,81 @@ package bedrock
import (
"context"
"fmt"
"strings"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/bedrockruntime"
"github.com/gofireflyio/aiac/v4/libaiac/types"
bedrocktypes "github.com/aws/aws-sdk-go-v2/service/bedrockruntime/types"
"github.com/gofireflyio/aiac/v5/libaiac/types"
)

// Conversation is a struct used to converse with a Bedrock chat model. It
// maintains all messages sent/received in order to maintain context.
type Conversation struct {
client *Client
model types.Model
messages []types.Message
backend *Bedrock
model string
messages []bedrocktypes.Message
}

// Chat initiates a conversation with a Bedrock chat model. A conversation
// maintains context, allowing to send further instructions to modify the output
// from previous requests.
func (client *Client) Chat(model types.Model) types.Conversation {
if model.Type != types.ModelTypeChat {
return nil
}

func (backend *Bedrock) Chat(model string) types.Conversation {
return &Conversation{
client: client,
model: model,
backend: backend,
model: model,
}
}

// Send sends the provided message to the backend and returns a Response object.
// To maintain context, all previous messages (whether from you to the API or
// vice-versa) are sent as well, allowing you to ask the API to modify the
// code it already generated.
func (conv *Conversation) Send(ctx context.Context, prompt string, msgs ...types.Message) (
func (conv *Conversation) Send(ctx context.Context, prompt string) (
res types.Response,
err error,
) {
if len(msgs) > 0 {
conv.messages = append(conv.messages, msgs...)
}

conv.messages = append(conv.messages, types.Message{
Role: "user",
Content: prompt,
conv.messages = append(conv.messages, bedrocktypes.Message{
Role: bedrocktypes.ConversationRoleUser,
Content: []bedrocktypes.ContentBlock{
&bedrocktypes.ContentBlockMemberText{Value: prompt},
},
})

var inputText strings.Builder
for _, msg := range conv.messages {
switch msg.Role {
case "user":
fmt.Fprint(&inputText, "\n\nHuman: ")
default:
fmt.Fprint(&inputText, "\n\nAssistant: ")
}

fmt.Fprint(&inputText, msg.Content)
input := bedrockruntime.ConverseInput{
ModelId: aws.String(conv.model),
Messages: conv.messages,
InferenceConfig: &bedrocktypes.InferenceConfiguration{
Temperature: aws.Float32(0.2),
},
}

fmt.Fprintf(&inputText, "\n\nAssistant:")

body, err := conv.client.generateInputJSON(conv.model, inputText.String())
output, err := conv.backend.runtime.Converse(ctx, &input)
if err != nil {
return res, fmt.Errorf("failed generating input JSON: %w", err)
return res, fmt.Errorf("failed sending prompt: %w", err)
}

output, err := conv.client.backend.InvokeModel(
ctx,
&bedrockruntime.InvokeModelInput{
Body: body,
ModelId: aws.String(conv.model.Name),
Accept: aws.String("application/json"),
ContentType: aws.String("application/json"),
},
)
if err != nil {
return res, fmt.Errorf("failed sending prompt: %w", err)
outputMsgMember, ok := output.Output.(*bedrocktypes.ConverseOutputMemberMessage)
if !ok {
return res, fmt.Errorf("Bedrock returned an unexpected response")
}

res.FullOutput, res.TokensUsed, err = conv.client.parseOutputJSON(
conv.model,
output.Body,
)
if err != nil {
return res, err
if len(outputMsgMember.Value.Content) == 0 {
return res, fmt.Errorf("Bedrock didn't return any message")
}

conv.messages = append(conv.messages, types.Message{
Role: "assistant",
Content: res.FullOutput,
})
outputMsg := outputMsgMember.Value

outputTxt, ok := outputMsg.Content[0].(*bedrocktypes.ContentBlockMemberText)
if !ok {
return res, fmt.Errorf("Bedrock return an unexpected response")
}

res.FullOutput = outputTxt.Value
res.TokensUsed = int64(*output.Usage.TotalTokens)
res.StopReason = string(output.StopReason)

conv.messages = append(conv.messages, outputMsg)

var ok bool
if res.Code, ok = types.ExtractCode(res.FullOutput); !ok {
res.Code = res.FullOutput
}
Expand Down
18 changes: 0 additions & 18 deletions libaiac/bedrock/client.go

This file was deleted.

Loading

0 comments on commit 94ed02e

Please sign in to comment.