快速开始

LangGraphGo 是一个用于构建有状态、多角色 LLM 应用程序的 Go 语言库,基于 LangChainGo 构建。它旨在实现与 Python LangGraph 库的功能对齐。

安装

使用 go get 安装库:

go get github.com/smallnest/langgraphgo

基本示例

下面是一个简单的示例,展示了如何创建一个基本的聊天图:

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/smallnest/langgraphgo/graph"
    "github.com/tmc/langchaingo/llms"
    "github.com/tmc/langchaingo/llms/openai"
)

func main() {
    ctx := context.Background()
    model, _ := openai.New()

    // 1. 创建图
    g := graph.NewStateGraph()

    // 2. 添加节点
    g.AddNode("generate", func(ctx context.Context, state interface{}) (interface{}, error) {
        messages := state.([]llms.MessageContent)
        response, _ := model.GenerateContent(ctx, messages)
        return append(messages, llms.TextParts("ai", response.Choices[0].Content)), nil
    })

    // 3. 定义边
    g.AddEdge("generate", graph.END)
    g.SetEntryPoint("generate")

    // 4. 编译
    runnable, _ := g.Compile()

    // 5. 调用
    initialState := []llms.MessageContent{
        llms.TextParts("human", "Hello, LangGraphGo!"),
    }
    result, _ := runnable.Invoke(ctx, initialState)
    
    fmt.Println(result)
}

特性概览