基于 MCP 和内置工具的代理式 AI 应用 Go 框架
GO for Large LanguagE Model (GOLLEM)
gollem provides:
go get github.com/gollem-dev/gollempackage main
import (
"context"
"fmt"
"os"
"github.com/gollem-dev/gollem"
"github.com/gollem-dev/gollem/llm/openai"
)
func main() {
ctx := context.Background()
// Create LLM client
client, err := openai.New(ctx, os.Getenv("OPENAI_API_KEY"))
if err != nil {
panic(err)
}
// Create session for one-time query
session, err := client.NewSession(ctx)
if err != nil {
panic(err)
}
// Generate content
result, err := session.Generate(ctx, []gollem.Input{gollem.Text("Hello, how are you?")})
if err != nil {
panic(err)
}
fmt.Println(result.Texts)
}Build conversational agents with automatic session management and tool integration. Learn more →
agent := gollem.New(client,
gollem.WithTools(&GreetingTool{}),
gollem.WithSystemPrompt("You are a helpful assistant."),
)
// Session is managed automatically across calls
agent.Execute(ctx, "Hello!")
agent.Execute(ctx, "What did I just say?") // remembers contextDefine custom tools for LLMs to call, or connect external tools via MCP. Tools → | MCP →
…Send images and PDFs alongside text prompts. Learn more →
img, _ := gollem.NewImage(imageBytes)
pdf, _ := gollem.NewPDFFromReader(file)
result, _ := session.Generate(ctx, []gollem.Input{img, pdf, gollem.Text("Describe these.")})Constrain LLM responses to a JSON Schema. Learn more →
schema, _ := gollem.ToSchema(UserProfile{})
session, _ := client.NewSession(ctx,
gollem.WithSessionContentType(gollem.ContentTypeJSON),
gollem.WithSessionResponseSchema(schema),
)
resp, _ := session.Generate(ctx, []gollem.Input{gollem.Text("Extract: John, 30, [email protected]")})
// resp.Texts[0] is valid JSON matching the schemaFor one-shot queries, Query[T]() combines schema generation, session creation, LLM call, and JSON parsing into a single generic function call with automatic retry on parse failures:
type UserProfile struct {
Name string `json:"name" description:"User's full name"`
Age int `json:"age" description:"Age in years"`
Email string `json:"email" description:"Email address"`
}
result, _ := gollem.Query[UserProfile](ctx, client, "Extract: John, 30, [email protected]",
gollem.WithQuerySystemPrompt("You are a data extractor."),
)
// result.Data is *UserProfile — type-safe, already parsedTo run a structured query on an existing session (preserving conversation history), use SessionQuery[T]():
// session already has conversation context from prior Generate calls
resp, _ := gollem.SessionQuery[UserProfile](ctx, session, "Who am I?")
// resp.Data is *UserProfile, parsed from the LLM's JSON response
// The session's history (including this exchange) is preservedMonitor, log, and control agent behavior with composable middleware. Learn more →
agent := gollem.New(client,
gollem.WithToolMiddleware(func(next gollem.ToolHandler) gollem.ToolHandler {
return func(ctx context.Context, req *gollem.ToolExecRequest) (*gollem.ToolExecResponse, error) {
log.Printf("Tool called: %s", req.Tool.Name)
return next(ctx, req)
}
}),
)Swap execution strategies: simple, ReAct, or Plan & Execute. Learn more →
import "github.com/gollem-dev/gollem/strategy/planexec"
agent := gollem.New(client,
gollem.WithStrategy(planexec.New(client)),
gollem.WithTools(&SearchTool{}, &AnalysisTool{}),
)Observe agent execution with pluggable backends (in-memory, OpenTelemetry). Learn more →
import "github.com/gollem-dev/gollem/trace"
rec := trace.New(trace.WithRepository(trace.NewFileRepository("./traces")))
agent := gollem.New(client, gollem.WithTrace(rec))Portable conversation history for stateless/distributed applications. Learn more →
// Export history for persistence
history := agent.Session().History()
data, _ := json.Marshal(history)
// Restore in another process
var restored gollem.History
json.Unmarshal(data, &restored)
agent := gollem.New(client, gollem.WithHistory(&restored))For automatic persistence, implement HistoryRepository and pass it via WithHistoryRepository. gollem then loads history at the start of a session and saves it after every LLM round-trip — no manual marshaling required.
agent := gollem.New(client,
gollem.WithHistoryRepository(repo, "session-id"),
)
// History is loaded automatically on first Execute, and saved after each round-trip
err := agent.Execute(ctx, gollem.Text("Hello!"))See the examples directory for complete working examples:
Query[T]()Apache 2.0 License. See LICENSE for details.
暂无开放 Issues,或尚未同步最近议题。