Skip to content

Commit

Permalink
#131: clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrueger12 committed Feb 12, 2024
1 parent 774baad commit 6ab5bb3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/bedrockruntime v1.5.6
github.com/cloudwego/hertz v0.7.3
github.com/go-playground/validator/v10 v10.17.0
github.com/google/uuid v1.6.0
github.com/hertz-contrib/logger/zap v1.1.0
github.com/hertz-contrib/swagger v0.1.0
github.com/joho/godotenv v1.5.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/henrylee2cn/ameda v1.4.8/go.mod h1:liZulR8DgHxdK+MEwvZIylGnmcjzQ6N6f2PlWe7nEO4=
Expand Down
27 changes: 10 additions & 17 deletions pkg/providers/bedrock/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"time"

//"glide/pkg/providers/clients"

"glide/pkg/api/schemas"

"go.uber.org/zap"

"github.com/google/uuid"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
Expand Down Expand Up @@ -81,10 +83,9 @@ func (c *Client) createChatRequestSchema(request *schemas.UnifiedChatRequest) *C
}

func (c *Client) doChatRequest(ctx context.Context, payload *ChatRequest) (*schemas.UnifiedChatResponse, error) {
// Build request payload
rawPayload, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("unable to marshal bedrock request payload: %w", err)
return nil, fmt.Errorf("unable to marshal chat request payload: %w", err)
}

cfg, _ := config.LoadDefaultConfig(ctx,
Expand All @@ -100,17 +101,10 @@ func (c *Client) doChatRequest(ctx context.Context, payload *ChatRequest) (*sche
Body: rawPayload,
})
if err != nil {
errMsg := err.Error()
if strings.Contains(errMsg, "no such host") {
c.telemetry.Logger.Error("Error: The Bedrock service is not available in the selected region. Please double-check the service availability for your region at https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/.\n")
} else if strings.Contains(errMsg, "Could not resolve the foundation model") {
c.telemetry.Logger.Error("Error: Could not resolve the foundation model from model identifier. Please verify that the requested model exists and is accessible within the specified region.\n")
} else {
c.telemetry.Logger.Error("Error: Couldn't invoke model. Here's why: %v\n", zap.Error(err))
}
c.telemetry.Logger.Error("Error: Couldn't invoke model. Here's why: %v\n", zap.Error(err))
return nil, err
}

// Parse the response JSON
var bedrockCompletion schemas.BedrockChatCompletion

err = json.Unmarshal(result.Body, &bedrockCompletion)
Expand All @@ -119,16 +113,15 @@ func (c *Client) doChatRequest(ctx context.Context, payload *ChatRequest) (*sche
return nil, err
}

// Map response to UnifiedChatResponse schema
response := schemas.UnifiedChatResponse{
ID: "",
Created: 0,
Provider: "bedrock",
ID: uuid.NewString(),
Created: int(time.Now().Unix()),
Provider: "aws-bedrock",
Model: c.config.Model,
Cached: false,
ModelResponse: schemas.ProviderResponse{
SystemID: map[string]string{
"system_fingerprint": "",
"system_fingerprint": "none",
},
Message: schemas.ChatMessage{
Role: "assistant",
Expand Down

0 comments on commit 6ab5bb3

Please sign in to comment.