AgentOS Server APIリファレンス
NewServer
HTTPサーバーを作成します。
シグネチャ:
go
func NewServer(config *Config) (*Server, error)
type Config struct {
Address string // サーバーアドレス (デフォルト: :8080)
SessionStorage session.Storage // セッションストレージ (デフォルト: memory)
Logger *slog.Logger // ロガー (デフォルト: slog.Default())
Debug bool // デバッグモード (デフォルト: false)
AllowOrigins []string // CORSオリジン
AllowMethods []string // CORSメソッド
AllowHeaders []string // CORSヘッダー
RequestTimeout time.Duration // リクエストタイムアウト (デフォルト: 30秒)
MaxRequestSize int64 // 最大リクエストサイズ (デフォルト: 10MB)
// ナレッジ API (オプション) / Knowledge API (optional)
VectorDBConfig *VectorDBConfig // ベクトルDB構成(例: chromadb)
EmbeddingConfig *EmbeddingConfig // 埋め込みモデル構成(例: OpenAI)
}
type VectorDBConfig struct {
Type string // 例: "chromadb"
BaseURL string // ベクトルDBエンドポイント
CollectionName string // 既定のコレクション
Database string // 任意のデータベース
Tenant string // 任意のテナント
}
type EmbeddingConfig struct {
Provider string // 例: "openai"
APIKey string
Model string // 例: "text-embedding-3-small"
BaseURL string // 例: "https://api.openai.com/v1"
}
例:
go
server, err := agentos.NewServer(&agentos.Config{
Address: ":8080",
Debug: true,
RequestTimeout: 60 * time.Second,
})
Server.RegisterAgent
エージェントを登録します。
シグネチャ:
go
func (s *Server) RegisterAgent(agentID string, ag *agent.Agent) error
例:
go
err := server.RegisterAgent("assistant", myAgent)
Server.Start / Shutdown
サーバーを起動および停止します。
シグネチャ:
go
func (s *Server) Start() error
func (s *Server) Shutdown(ctx context.Context) error
例:
go
go func() {
if err := server.Start(); err != nil {
log.Fatal(err)
}
}()
// グレースフルシャットダウン
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
server.Shutdown(ctx)
APIエンドポイント
完全なAPIドキュメントはOpenAPI仕様を参照してください。
コアエンドポイント:
GET /health
- ヘルスチェックPOST /api/v1/sessions
- セッション作成GET /api/v1/sessions/{id}
- セッション取得PUT /api/v1/sessions/{id}
- セッション更新DELETE /api/v1/sessions/{id}
- セッション削除GET /api/v1/sessions
- セッション一覧GET /api/v1/agents
- エージェント一覧POST /api/v1/agents/{id}/run
- エージェント実行
知識エンドポイント(オプション) / Knowledge Endpoints (optional):
POST /api/v1/knowledge/search
— ナレッジベースでベクトル類似検索 / Vector similarity searchGET /api/v1/knowledge/config
— 利用可能なチャンクャー、VectorDB、埋め込みモデル情報 / Available chunkers, VectorDBs, embedding modelPOST /api/v1/knowledge/content
— 最小取り込み(text/plain または application/json)
リクエスト例 / Example:
bash
curl -X POST http://localhost:8080/api/v1/knowledge/search \
-H "Content-Type: application/json" \
-d '{
"query": "エージェントの作成方法は?",
"limit": 5,
"filters": {"source": "documentation"}
}'
最小サーバー構成(ナレッジ API 有効化)/ Minimal server config (enable Knowledge API):
go
server, err := agentos.NewServer(&agentos.Config{
Address: ":8080",
VectorDBConfig: &agentos.VectorDBConfig{
Type: "chromadb",
BaseURL: os.Getenv("CHROMADB_URL"),
CollectionName: "agno_knowledge",
},
EmbeddingConfig: &agentos.EmbeddingConfig{
Provider: "openai",
APIKey: os.Getenv("OPENAI_API_KEY"),
Model: "text-embedding-3-small",
},
})
実行可能なサンプル / Runnable example: cmd/examples/knowledge_api/
ベストプラクティス
1. 常にContextを使用
go
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
output, err := ag.Run(ctx, input)
2. エラーを適切に処理
go
output, err := ag.Run(ctx, input)
if err != nil {
switch {
case types.IsInvalidInputError(err):
// 無効な入力を処理
case types.IsRateLimitError(err):
// バックオフして再試行
default:
// その他のエラーを処理
}
}
3. メモリを管理
go
// 新しいトピックを開始する際にクリア
ag.ClearMemory()
// または制限付きメモリを使用
mem := memory.NewInMemory(50)
4. 適切なタイムアウトを設定
go
server, _ := agentos.NewServer(&agentos.Config{
RequestTimeout: 60 * time.Second, // 複雑なエージェント用
})