langsmith

package module
v0.0.0-...-20b2a55 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

README

Langsmith Callbacks

English | 简体中文

A Langsmith callback implementation for Eino that implements the Handler interface. This enables seamless integration with Eino's application for enhanced observability and tracing.

Features

  • Implements github.com/cloudwego/eino/internel/callbacks.Handler interface
  • Easy integration with Eino's application

Installation

go get github.com/cloudwego/eino-ext/callbacks/langsmith

Quick Start

package main
import (
	"context"
	"fmt"
	"log"

	"github.com/cloudwego/eino-ext/callbacks/langsmith"
	"github.com/cloudwego/eino/callbacks"
	"github.com/cloudwego/eino/compose"
	"github.com/google/uuid"
	
)

func main() {

	cfg := &langsmith.Config{
		APIKey: "your api key",
		APIURL: "your api url",
		IDGen: func(ctx context.Context) string { // optional. id generator. default is uuid.NewString
			return uuid.NewString()
		},
	}
	// ft := langsmith.NewFlowTrace(cfg)
	cbh, err := langsmith.NewLangsmithHandler(cfg)
	if err != nil {
		log.Fatal(err)
	}

	// Set global callback handler
	callbacks.AppendGlobalHandlers(cbh)
	
	ctx := context.Background()
	ctx = langsmith.SetTrace(ctx,
		langsmith.WithSessionName("your session name"), // Set langsmith project name for reporting
	)

	g := compose.NewGraph[string, string]()
	// ... add nodes and edges to your graph
	// add node and edage to your eino graph, here is an simple example
	g.AddLambdaNode("node1", compose.InvokableLambda(func(ctx context.Context, input string) (output string, err error) {
		return input, nil
	}), compose.WithNodeName("node1"))
	g.AddLambdaNode("node2", compose.InvokableLambda(func(ctx context.Context, input string) (output string, err error) {
		return "test output", nil
	}), compose.WithNodeName("node2"))
	g.AddEdge(compose.START, "node1")
	g.AddEdge("node1", "node2")
	g.AddEdge("node2", compose.END)

	runner, err := g.Compile(ctx)
	if err != nil {
		fmt.Println(err)
	}
	// Invoke the runner
	result, err := runner.Invoke(ctx, "test input\n")
	if err != nil {
		fmt.Println(err)
	}
	// Process the result
	log.Printf("Got result: %s", result)
	
}

Examples

See the examples directory for complete usage examples.

For More Details

Documentation

Index

Constants

View Source
const (
	// DefaultLangsmithAPIURL Langsmith default url
	DefaultLangsmithAPIURL = "https://api.smith.langchain.com"
)

Variables

This section is empty.

Functions

func SafeDeepCopyMetadata

func SafeDeepCopyMetadata(original map[string]interface{}) map[string]interface{}

func SafeDeepCopySyncMapMetadata

func SafeDeepCopySyncMapMetadata(original *sync.Map) map[string]interface{}

func SetTrace

func SetTrace(ctx context.Context, opts ...TraceOption) context.Context

SetTrace 将 trace 选项设置到 context 中

Types

type CallbackHandler

type CallbackHandler struct {
	// contains filtered or unexported fields
}

CallbackHandler implements eino's Handler interface

func NewLangsmithHandler

func NewLangsmithHandler(cfg *Config) (*CallbackHandler, error)

NewLangsmithHandler creates a new CallbackHandler

func (*CallbackHandler) OnEnd

OnEnd handles successful call completion event

func (*CallbackHandler) OnEndWithStreamOutput

OnEndWithStreamOutput handles streaming output completion

func (*CallbackHandler) OnError

func (c *CallbackHandler) OnError(ctx context.Context, info *callbacks.RunInfo, err error) context.Context

OnError handles call failure event

func (*CallbackHandler) OnStart

OnStart handles call start event

func (*CallbackHandler) OnStartWithStreamInput

func (c *CallbackHandler) OnStartWithStreamInput(ctx context.Context, info *callbacks.RunInfo, input *schema.StreamReader[callbacks.CallbackInput]) context.Context

OnStartWithStreamInput handles streaming input initialization

type Config

type Config struct {
	APIKey   string                           // langsmith api key
	APIURL   string                           // langsmith api url, default:https://api.smith.langchain.com
	RunIDGen func(ctx context.Context) string // langsmith run_id generator
}

Config LangsmithHandler configuration

type FlowTrace

type FlowTrace struct {
	// contains filtered or unexported fields
}

func NewFlowTrace

func NewFlowTrace(cfg *Config) *FlowTrace

func (*FlowTrace) FinishSpan

func (ft *FlowTrace) FinishSpan(ctx context.Context, runID string)

func (*FlowTrace) SpanToString

func (ft *FlowTrace) SpanToString(ctx context.Context) (string, error)

SpanToString parse ctx's LangsmithState to string

func (*FlowTrace) StartSpan

func (ft *FlowTrace) StartSpan(ctx context.Context, name string, state *LangsmithState) (context.Context, string, error)

func (*FlowTrace) StringToSpan

func (ft *FlowTrace) StringToSpan(val string) (*LangsmithState, error)

StringToSpan parse string to LangsmithState

type Langsmith

type Langsmith interface {
	CreateRun(ctx context.Context, run *Run) error
	UpdateRun(ctx context.Context, runID string, patch *RunPatch) error
}

Langsmith func interface

func NewLangsmith

func NewLangsmith(apiKey, apiUrl string) Langsmith

NewLangsmith create langsmith client

type LangsmithState

type LangsmithState struct {
	TraceID           string                 `json:"trace_id"`
	ParentRunID       string                 `json:"parent_run_id"`
	ParentDottedOrder string                 `json:"parent_dotted_order"`
	Metadata          *sync.Map              `json:"metadata"`
	Tags              []string               `json:"tags"`
	MarshalMetadata   map[string]interface{} `json:"marshal_metadata"`
}

LangsmithState maintains Langsmith call chain state

func GetOrInitState

func GetOrInitState(ctx context.Context) (context.Context, *LangsmithState)

func GetState

func GetState(ctx context.Context) (context.Context, *LangsmithState)

type Run

type Run struct {
	ID                 string                 `json:"id"`                             // Unique identifier for the span.
	Name               string                 `json:"name"`                           // The name associated with the run.
	RunType            RunType                `json:"run_type"`                       // Type of run, e.g., "llm", "chain", "tool".
	StartTime          time.Time              `json:"start_time"`                     // Start time of the run.
	EndTime            *time.Time             `json:"end_time,omitempty"`             // End time of the run.
	Inputs             map[string]interface{} `json:"inputs"`                         // A map or set of inputs provided to the run.
	Outputs            map[string]interface{} `json:"outputs,omitempty"`              // A map or set of outputs generated by the run.
	Error              *string                `json:"error,omitempty"`                // Error message if the run encountered an error.
	ParentRunID        *string                `json:"parent_run_id,omitempty"`        // Unique identifier of the parent run.
	TraceID            string                 `json:"trace_id,omitempty"`             // Unique identifier for the trace the run is a part of. This is also the id field of the root run of the trace
	Extra              map[string]interface{} `json:"extra,omitempty"`                // Any extra information run.
	SessionName        string                 `json:"session_name,omitempty"`         // langsmith session name
	ReferenceExampleID *string                `json:"reference_example_id,omitempty"` // ID of a reference example associated with the run. This is usually only present for evaluation runs.
	DottedOrder        string                 `json:"dotted_order,omitempty"`         // Ordering string, hierarchical. Format: run_start_timeZrun_uuid.child_run_start_timeZchild_run_uuid...
	Tags               []string               `json:"tags,omitempty"`                 // Tags or labels associated with the run.
}

type RunPatch

type RunPatch struct {
	EndTime *time.Time             `json:"end_time,omitempty"` // End time of the run.
	Inputs  map[string]interface{} `json:"inputs,omitempty"`   // A map or set of inputs provided to the run.
	Outputs map[string]interface{} `json:"outputs,omitempty"`  // A map or set of outputs generated by the run.
	Error   *string                `json:"error,omitempty"`    // Error message if the run encountered an error.
	Extra   map[string]interface{} `json:"extra,omitempty"`    // Any extra information run.
}

RunPatch update run when it is finished or failed, patch output or error msg.

type RunType

type RunType string
const (
	RunTypeChain RunType = "chain" // chain node
	RunTypeLLM   RunType = "llm"   // llm model node
	RunTypeTool  RunType = "tool"  // tool node
)

type TraceOption

type TraceOption func(*traceOptions)

func AddTag

func AddTag(tag string) TraceOption

AddTag 插入tag

func SetMetadata

func SetMetadata(metadata *sync.Map) TraceOption

SetMetadata 设置 trace 的元数据, 覆盖写入

func WithReferenceExampleID

func WithReferenceExampleID(id string) TraceOption

WithReferenceExampleID 关联到一个 example

func WithSessionName

func WithSessionName(name string) TraceOption

WithSessionName 设置 Langsmith 的项目名称

func WithTraceID

func WithTraceID(id string) TraceOption

WithTraceID 强制指定一个 trace ID

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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