https://github.com/tmc/langgraphgo
langgraph for Go
https://github.com/tmc/langgraphgo
langchain langchaingo
Last synced: 11 months ago
JSON representation
langgraph for Go
- Host: GitHub
- URL: https://github.com/tmc/langgraphgo
- Owner: tmc
- License: mit
- Created: 2024-01-27T17:25:24.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-23T17:04:58.000Z (about 2 years ago)
- Last Synced: 2024-12-01T10:23:34.778Z (over 1 year ago)
- Topics: langchain, langchaingo
- Language: Go
- Homepage:
- Size: 14.6 KB
- Stars: 58
- Watchers: 10
- Forks: 11
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🦜️🔗 LangGraphGo
[](https://pkg.go.dev/github.com/tmc/langgraphgo)
## Quick Start
This is a simple example of how to use the library to create a simple chatbot that uses OpenAI to generate responses.
```go
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"
)
func main() {
model, err := openai.New()
if err != 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))
if err != nil {
return nil, err
}
return append(state,
llms.TextParts(schema.ChatMessageTypeAI, r.Choices[0].Content),
), nil
})
g.AddNode(graph.END, func(ctx context.Context, state []llms.MessageContent) ([]llms.MessageContent, error) {
return state, nil
})
g.AddEdge("oracle", graph.END)
g.SetEntryPoint("oracle")
runnable, err := g.Compile()
if err != 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?"),
})
if err != nil {
panic(err)
}
fmt.Println(res)
// Output:
// [{human [{What is 1 + 1?}]} {ai [{1 + 1 equals 2.}]}]
}
```