You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
shentong.martin 57cb494e77 chore: fix lint and formatting
Change-Id: I3b4f908dd24ce6b779fdb8cac3e7519c3295a21b
2 months ago
..
README.md feat(graphtool): add InvokableGraphTool and StreamableGraphTool for wrapping compose types as tools 4 months ago
main.go chore: fix lint and formatting 2 months ago

README.md

Example 1: Document Summarization with compose.Chain

This example demonstrates using InvokableGraphTool with compose.Chain to create a document summarization tool.

What This Example Shows

  • Using compose.Chain for sequential processing
  • Wrapping a chain as an InvokableTool
  • Multi-step LLM processing (extract key points → generate summary)
  • Integrating with ChatModelAgent

Architecture

Input Document
      │
      ▼
┌─────────────────┐
│ Extract Key     │  ← ChatModel extracts key points
│ Points          │
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│ Generate        │  ← ChatModel creates coherent summary
│ Summary         │
└────────┬────────┘
         │
         ▼
   Output Summary

Key Components

Input/Output Types

type SummarizeInput struct {
    Document string `json:"document"`
    MaxWords int    `json:"max_words"`
}

type SummarizeOutput struct {
    Summary   string   `json:"summary"`
    KeyPoints []string `json:"key_points"`
    WordCount int      `json:"word_count"`
}

Chain Construction

fullChain := compose.NewChain[*SummarizeInput, *SummarizeOutput]()
fullChain.
    AppendLambda(/* transform input */).
    AppendChatTemplate(extractKeyPointsPrompt).
    AppendChatModel(cm).
    AppendLambda(/* transform for next step */).
    AppendChatTemplate(condenseSummaryPrompt).
    AppendChatModel(cm).
    AppendLambda(/* format output */)

Tool Creation

tool, err := graphtool.NewInvokableGraphTool[*SummarizeInput, *SummarizeOutput](
    fullChain,
    "summarize_document",
    "Summarize a document by extracting key points and creating a coherent summary.",
)

Running the Example

# Set your OpenAI API key
export OPENAI_API_KEY=your-api-key

# Run the example
go run main.go

Expected Output

=== Document Summarization Example ===

[Agent calls summarize_document tool]
[Chain executes: extract key points → generate summary]
[Agent returns formatted summary to user]

Key Takeaways

  1. Chain for Sequential Processing: compose.Chain is ideal for linear pipelines where each step's output feeds the next
  2. Type Safety: Generic types [I, O] ensure compile-time type checking
  3. Prompt Templates: Use prompt.FromMessages with schema.FString for dynamic prompts
  4. Tool Integration: The wrapped chain appears as a single tool to the agent