langchaingo dynamodb chat history
1.0.0
Utilisez DynamoDB comme backend de mémoire / historique pour vos applications GO à l'aide de Langchain
Voici un exemple d'application que vous pouvez utiliser pour l'essayer.
Commencez par créer une table DynamoDB:
export DYNAMODB_TABLE_NAME=test-table
export PARTITION_KEY=chat_id
aws dynamodb create-table
--table-name $DYNAMODB_TABLE_NAME
--attribute-definitions AttributeName= $PARTITION_KEY ,AttributeType=S
--key-schema AttributeName= $PARTITION_KEY ,KeyType=HASH
--billing-mode PAY_PER_REQUEST L'exemple d'application ci-dessous, utilise le substratum rocheux d'Amazon (voir ligne - llm, err := claude.New(region, llm.DontUseHumanAssistantPrompt()) ). Pour que cela fonctionne, vous devrez assurer des autorisations IAM appropriées. Reportez-vous à la section "avant de commencer".
{
"Version" : " 2012-10-17 " ,
"Statement" : [
{
"Effect" : " Allow " ,
"Action" : " bedrock:* " ,
"Resource" : " * "
}
]
} Enregistrez le programme dans un fichier nommé main.go
Ensuite, créez un nouveau module Go, exécutez le programme et commencez à discuter.
go mod init demo
go mod tidy
export DYNAMODB_TABLE_NAME=test-table
export PARTITION_KEY=chat_id
go run main.goProgramme complet:
package main
import (
"bufio"
"context"
"fmt"
"log"
"os"
"strings"
ddbhist "github.com/abhirockzz/langchaingo-dynamodb-chat-history/dynamodb_chat_history"
"github.com/google/uuid"
"github.com/abhirockzz/amazon-bedrock-langchain-go/llm"
"github.com/abhirockzz/amazon-bedrock-langchain-go/llm/claude"
"github.com/tmc/langchaingo/chains"
"github.com/tmc/langchaingo/memory"
"github.com/tmc/langchaingo/outputparser"
"github.com/tmc/langchaingo/prompts"
)
const template = "{{.chat_history}} n n Human:{{.human_input}} n n Assistant:"
func main () {
region := "us-east-1"
llm , err := claude . New ( region , llm . DontUseHumanAssistantPrompt ())
if err != nil {
log . Fatal ( err )
}
//a random uuid is used as primary key value. typically, in an application this would be a unique identifier such as a session ID.
pkValue := uuid . New (). String ()
ddbcmh , err := ddbhist . New ( region , ddbhist . WithTableName ( os . Getenv ( "DYNAMODB_TABLE_NAME" )), ddbhist . WithPrimaryKeyName ( os . Getenv ( "PARTITION_KEY" )), ddbhist . WithPrimaryKeyValue ( pkValue ))
if err != nil {
log . Fatal ( err )
}
chain := chains. LLMChain {
Prompt : prompts . NewPromptTemplate (
template ,
[] string { "chat_history" , "human_input" },
),
LLM : llm ,
Memory : memory . NewConversationBuffer (
memory . WithMemoryKey ( "chat_history" ),
memory . WithAIPrefix ( " n n Assistant" ),
memory . WithHumanPrefix ( " n n Human" ),
memory . WithChatHistory ( ddbcmh ),
),
OutputParser : outputparser . NewSimple (),
OutputKey : "text" ,
}
ctx := context . Background ()
reader := bufio . NewReader ( os . Stdin )
for {
fmt . Print ( " n Enter your message: " )
input , _ := reader . ReadString ( 'n' )
input = strings . TrimSpace ( input )
_ , err := chains . Call ( ctx , chain , map [ string ] any { "human_input" : input }, chains . WithMaxTokens ( 8191 ),
chains . WithStreamingFunc (
func ( ctx context. Context , chunk [] byte ) error {
fmt . Print ( string ( chunk ))
return nil
},
))
if err != nil {
log . Fatal ( err )
}
}
}Vérifiez l'historique du chat dans DynamoDB:
export DYNAMODB_TABLE_NAME=test-table
aws dynamodb scan --table-name $DYNAMODB_TABLE_NAMEPour supprimer la table:
export DYNAMODB_TABLE_NAME=test-table
aws dynamodb delete-table --table-name $DYNAMODB_TABLE_NAME