• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kshard / chatter / 16538940978

26 Jul 2025 10:35AM UTC coverage: 67.584% (+44.9%) from 22.674%
16538940978

Pull #53

github

fogfish
update license
Pull Request #53: Enable multi-content I/O within prompts & responses

596 of 837 new or added lines in 27 files covered. (71.21%)

2 existing lines in 2 files now uncovered.

884 of 1308 relevant lines covered (67.58%)

0.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/provider/bedrock/service.go
1
package bedrock
2

3
import (
4
        "context"
5
        "encoding/json"
6

7
        "github.com/aws/aws-sdk-go-v2/aws"
8
        "github.com/aws/aws-sdk-go-v2/config"
9
        "github.com/aws/aws-sdk-go-v2/service/bedrockruntime"
10
        "github.com/fogfish/opts"
11
        "github.com/kshard/chatter/aio/provider"
12
)
13

14
//
15
// Configuration options for Bedrock
16
//
17

18
type Option = opts.Option[Client]
19

20
const defaultRegion = "us-west-2"
21

22
var (
23
        // Use aws.Config to config the client
24
        WithConfig = opts.FMap(optsFromConfig)
25

26
        // Use region for aws.Config
27
        WithRegion = opts.FMap(optsFromRegion)
28

29
        // Set AWS Bedrock Runtime
30
        WithRuntime = opts.ForType[Client, Runtime]()
31
)
32

NEW
33
func optsFromRegion(c *Client, region string) error {
×
NEW
34
        cfg, err := config.LoadDefaultConfig(
×
NEW
35
                context.Background(),
×
NEW
36
                config.WithRegion(region),
×
NEW
37
        )
×
NEW
38
        if err != nil {
×
NEW
39
                return err
×
NEW
40
        }
×
41

NEW
42
        return optsFromConfig(c, cfg)
×
43
}
44

NEW
45
func optsFromConfig(c *Client, cfg aws.Config) (err error) {
×
NEW
46
        if c.api == nil {
×
NEW
47
                c.api = bedrockruntime.NewFromConfig(cfg)
×
NEW
48
        }
×
49

NEW
50
        return
×
51
}
52

53
// AWS Bedrock Runtime API
54
type Runtime interface {
55
        InvokeModel(ctx context.Context, params *bedrockruntime.InvokeModelInput, optFns ...func(*bedrockruntime.Options)) (*bedrockruntime.InvokeModelOutput, error)
56
}
57

58
type Client struct{ api Runtime }
59

60
type Service[A, B any] struct {
61
        client    Client
62
        model     string
63
        undefined B
64
}
65

66
var _ provider.Service[int, int] = (*Service[int, int])(nil)
67

NEW
68
func New[A, B any](model string, opt ...Option) (*Service[A, B], error) {
×
NEW
69
        c := Client{}
×
NEW
70
        if err := opts.Apply(&c, opt); err != nil {
×
NEW
71
                return nil, err
×
NEW
72
        }
×
NEW
73
        if c.api == nil {
×
NEW
74
                if err := optsFromRegion(&c, defaultRegion); err != nil {
×
NEW
75
                        return nil, err
×
NEW
76
                }
×
77
        }
78

NEW
79
        return &Service[A, B]{
×
NEW
80
                client:    c,
×
NEW
81
                model:     model,
×
NEW
82
                undefined: *new(B),
×
NEW
83
        }, nil
×
84
}
85

NEW
86
func (s *Service[A, B]) Invoke(ctx context.Context, input A) (B, error) {
×
NEW
87
        req, err := json.Marshal(input)
×
NEW
88
        if err != nil {
×
NEW
89
                return s.undefined, err
×
NEW
90
        }
×
91

NEW
92
        inquiry := &bedrockruntime.InvokeModelInput{
×
NEW
93
                ModelId:     aws.String(s.model),
×
NEW
94
                ContentType: aws.String("application/json"),
×
NEW
95
                Body:        req,
×
NEW
96
        }
×
NEW
97

×
NEW
98
        result, err := s.client.api.InvokeModel(ctx, inquiry)
×
NEW
99
        if err != nil {
×
NEW
100
                return s.undefined, err
×
NEW
101
        }
×
102

NEW
103
        var reply B
×
NEW
104

×
NEW
105
        err = json.Unmarshal(result.Body, &reply)
×
NEW
106
        if err != nil {
×
NEW
107
                return s.undefined, err
×
NEW
108
        }
×
109

NEW
110
        return reply, nil
×
111
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc