Skip to content

Repository files navigation

🦜️🔗 LangGraphGo

go.dev reference

Quick Start

This is a simple example of how to use the library to create a simple chatbot that uses OpenAI to generate responses.

import (
"context""errors""fmt""testing""github.com/tmc/langchaingo/llms""github.com/tmc/langchaingo/llms/openai""github.com/tmc/langchaingo/schema""github.com/tmc/langgraphgo/graph"
)
funcmain() {
model, err:=openai.New()
iferr!=nil {
panic(err)
}
g:=graph.NewMessageGraph()
g.AddNode("oracle", func(ctx context.Context, state []llms.MessageContent) ([]llms.MessageContent, error) {
r, err:=model.GenerateContent(ctx, state, llms.WithTemperature(0.0))
iferr!=nil {
returnnil, err
}
returnappend(state,
llms.TextParts(schema.ChatMessageTypeAI, r.Choices[0].Content),
), nil
})
g.AddNode(graph.END, func(ctx context.Context, state []llms.MessageContent) ([]llms.MessageContent, error) {
returnstate, nil
})
g.AddEdge("oracle", graph.END)
g.SetEntryPoint("oracle")
runnable, err:=g.Compile()
iferr!=nil {
panic(err)
}
ctx:=context.Background()
// Let's run it!res, err:=runnable.Invoke(ctx, []llms.MessageContent{
llms.TextParts(schema.ChatMessageTypeHuman, "What is 1 + 1?"),
})
iferr!=nil {
panic(err)
}
fmt.Println(res)
// Output:// [{human [{What is 1 + 1?}]} {ai [{1 + 1 equals 2.}]}]
}

About

langgraph for Go

Topics

Resources

Stars

289 stars

Watchers

11 watching

Forks

Releases

Used by

Contributors

Languages