bedrock

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 19, 2024 License: MIT Imports: 18 Imported by: 0

README

langchaingo-llm-bedrock

for github.com/tmc/langchaingo Amazon Bedrock LLM interface implement

GoDoc Go Report Card License

Example

package main

import (
	"context"
	_ "embed"
	"log"

	bedrock "github.com/mashiike/langchaingo-llm-bedrock"
	"github.com/tmc/langchaingo/llms"
	"github.com/tmc/langchaingo/schema"
)

//go:embed image.png
var image []byte

func main() {
	llm, err := bedrock.New(
		bedrock.WithModel(bedrock.Claude3Haiku),
	)
	if err != nil {
		log.Fatal(err)
	}
	ctx := context.Background()
	resp, err := llm.GenerateContent(ctx, []llms.MessageContent{
		{
			Role: schema.ChatMessageTypeHuman,
			Parts: []llms.ContentPart{
				llms.BinaryPart("image/png", image),
				llms.TextPart("この画像に書かれている内容をテキストにしてください。"),
			},
		},
	})
	if err != nil {
		log.Fatal(err)
	}
	for _, choice := range resp.Choices {
		log.Println(choice.Content)
	}
}

License

MIT License

Documentation

Index

Constants

View Source
const (
	TitanEmbeddingG1Text = "amazon.titan-embed-text-v1"
	Claude2              = "anthropic.claude-v2:1"
	ClaudeInstant        = "anthropic.claude-instant-v1"
	Claude3Sonnet        = "anthropic.claude-3-sonnet-20240229-v1:0"
	Claude3Haiku         = "anthropic.claude-3-haiku-20240307-v1:0"
)

https://docs.aws.amazon.com/bedrock/latest/userguide/model-ids-arns.html

Variables

This section is empty.

Functions

This section is empty.

Types

type BedrockClient

type BedrockClient interface {
	InvokeModel(ctx context.Context, params *bedrockruntime.InvokeModelInput, optFns ...func(*bedrockruntime.Options)) (*bedrockruntime.InvokeModelOutput, error)
}

type Claude2Request

type Claude2Request struct {
	Prompt            string   `json:"prompt"`
	MaxTokensToSample int      `json:"max_tokens_to_sample"`
	Temperature       float64  `json:"temperature,omitempty"`
	TopP              float64  `json:"top_p,omitempty"`
	TopK              int      `json:"top_k,omitempty"`
	StopSequences     []string `json:"stop_sequences,omitempty"`
}

type Claude2Response

type Claude2Response struct {
	Completion string `json:"completion"`
}

type Claude3Request

type Claude3Request struct {
	Temperature      float64                  `json:"temperature,omitempty"`
	TopP             float64                  `json:"top_p,omitempty"`
	TopK             int                      `json:"top_k,omitempty"`
	StopSequences    []string                 `json:"stop_sequences,omitempty"`
	System           string                   `json:"system,omitempty"`
	MaxTokens        int                      `json:"max_tokens,omitempty"`
	Messages         []*Claude3RequestMessage `json:"messages,omitempty"`
	AnthropicVersion string                   `json:"anthropic_version,omitempty"`
}

type Claude3RequestMessage

type Claude3RequestMessage struct {
	Role    string                         `json:"role"`
	Content []Claude3RequestMessageContent `json:"content"`
}

type Claude3RequestMessageContent

type Claude3RequestMessageContent interface {
	// contains filtered or unexported methods
}

type Claude3RequestMessageImageContent

type Claude3RequestMessageImageContent struct {
	Type   string                                     `json:"type,omitempty"`
	Source claoudelV3RequestMessageImageContentSource `json:"source,omitempty"`
}

type Claude3RequestMessageTextContent

type Claude3RequestMessageTextContent struct {
	Type string `json:"type,omitempty"`
	Text string `json:"text,omitempty"`
}

type Claude3Response

type Claude3Response struct {
	Content      []Claude3ResponseContent `json:"content"`
	ID           string                   `json:"id"`
	Model        string                   `json:"model"`
	Role         string                   `json:"role"`
	StopReason   string                   `json:"stop_reason"`
	StopSequence any                      `json:"stop_sequence"`
	Type         string                   `json:"type"`
	Usage        Claude3ResponseUsage     `json:"usage"`
}

type Claude3ResponseContent

type Claude3ResponseContent struct {
	Text string `json:"text"`
	Type string `json:"type"`
}

type Claude3ResponseUsage

type Claude3ResponseUsage struct {
	InputTokens  int `json:"input_tokens"`
	OutputTokens int `json:"output_tokens"`
}

type LLM

type LLM struct {
	CallbacksHandler callbacks.Handler
	// contains filtered or unexported fields
}

func New

func New(opts ...Option) (*LLM, error)

New returns a new Bedrock LLM.

func (*LLM) Call

func (l *LLM) Call(ctx context.Context, prompt string, options ...llms.CallOption) (string, error)

func (*LLM) CreateEmbedding

func (l *LLM) CreateEmbedding(ctx context.Context, texts []string) ([][]float32, error)

func (*LLM) GenerateContent

func (l *LLM) GenerateContent(ctx context.Context, messages []llms.MessageContent, options ...llms.CallOption) (*llms.ContentResponse, error)

type Option

type Option func(*options)

func WithAWSConfig

func WithAWSConfig(cfg aws.Config) Option

func WithClient

func WithClient(client BedrockClient) Option

func WithEmbeddingModel

func WithEmbeddingModel(embeddingModel string) Option

func WithLogger

func WithLogger(logger *slog.Logger) Option

func WithMaxTokens

func WithMaxTokens(maxTokens int) Option

func WithModel

func WithModel(model string) Option

func WithNumWorkers

func WithNumWorkers(workers int) Option

func WithRegion

func WithRegion(region string) Option

func WithStopWords

func WithStopWords(stopWords []string) Option

func WithTemperature

func WithTemperature(temperature float64) Option

func WithTopK

func WithTopK(topK int) Option

func WithTopP

func WithTopP(topP float64) Option

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL