Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Build directories
build/
build-*/
build_*/
out/
bin/
lib/
Expand Down
68 changes: 68 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ set(CMAKE_CXX_EXTENSIONS OFF)
option(BUILD_TESTS "Build tests" OFF)
option(BUILD_EXAMPLES "Build example applications" ON)
option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
option(AI_SDK_ENABLE_BEDROCK "Enable Amazon Bedrock provider (requires AWS SDK)" OFF)

# Add third party directory (vendored and CMake-fetched dependencies)
add_subdirectory(third_party)
Expand Down Expand Up @@ -68,6 +69,16 @@ configure_ai_component(ai-sdk-cpp-openai)
configure_ai_component(ai-sdk-cpp-anthropic)
configure_ai_component(ai-sdk-cpp-langfuse)

# Bedrock provider (optional, requires AWS SDK)
if(AI_SDK_ENABLE_BEDROCK)
find_package(AWSSDK REQUIRED COMPONENTS core bedrock-runtime bedrock sts)

add_library(ai-sdk-cpp-bedrock)
set_target_properties(ai-sdk-cpp-bedrock PROPERTIES EXPORT_NAME bedrock)
add_library(ai::bedrock ALIAS ai-sdk-cpp-bedrock)
configure_ai_component(ai-sdk-cpp-bedrock)
endif()

# Configure main interface library
target_include_directories(ai-sdk-cpp
INTERFACE
Expand Down Expand Up @@ -125,6 +136,26 @@ target_sources(ai-sdk-cpp-langfuse
src/langfuse/tracer.cpp
)

# Bedrock provider sources (conditional)
if(AI_SDK_ENABLE_BEDROCK)
set(BEDROCK_SOURCES
src/providers/bedrock/bedrock_client.cpp
src/providers/bedrock/bedrock_request_mapper.cpp
src/providers/bedrock/bedrock_response_parser.cpp
src/providers/bedrock/bedrock_stream.cpp
src/providers/bedrock/bedrock_factory.cpp
src/providers/bedrock/bedrock_credentials.cpp
# Security components
src/providers/bedrock/input_validator.cpp
src/providers/bedrock/credential_redactor.cpp
src/providers/bedrock/secure_logger.cpp
src/providers/bedrock/model_discovery.cpp
# SDK lifecycle management
src/providers/bedrock/aws_sdk_manager.cpp
)
target_sources(ai-sdk-cpp-bedrock PRIVATE ${BEDROCK_SOURCES})
endif()

# Link dependencies
target_link_libraries(ai-sdk-cpp-core
PUBLIC
Expand Down Expand Up @@ -188,6 +219,23 @@ target_compile_definitions(ai-sdk-cpp-langfuse
${HTTPLIB_COMPILE_DEFS}
)

# Bedrock provider dependencies (uses AWS SDK instead of httplib)
if(AI_SDK_ENABLE_BEDROCK)
target_link_libraries(ai-sdk-cpp-bedrock
PUBLIC
ai::core
nlohmann_json::nlohmann_json
PRIVATE
${AWSSDK_LINK_LIBRARIES}
$<BUILD_INTERFACE:concurrentqueue>
)

target_compile_definitions(ai-sdk-cpp-bedrock
PUBLIC
AI_SDK_HAS_BEDROCK=1
)
endif()

# Core needs HTTP definitions too
target_compile_definitions(ai-sdk-cpp-core
PRIVATE
Expand All @@ -203,6 +251,14 @@ target_link_libraries(ai-sdk-cpp
ai::langfuse
)

# Conditionally link Bedrock to main library
if(AI_SDK_ENABLE_BEDROCK)
target_link_libraries(ai-sdk-cpp
INTERFACE
ai::bedrock
)
endif()

# Define all component availability for main library
target_compile_definitions(ai-sdk-cpp
INTERFACE
Expand All @@ -211,13 +267,24 @@ target_compile_definitions(ai-sdk-cpp
AI_SDK_HAS_LANGFUSE=1
)

# Add Bedrock availability definition when enabled
if(AI_SDK_ENABLE_BEDROCK)
target_compile_definitions(ai-sdk-cpp
INTERFACE
AI_SDK_HAS_BEDROCK=1
)
endif()

# List of all concrete component targets
set(COMPONENT_TARGETS
ai-sdk-cpp-core
ai-sdk-cpp-openai
ai-sdk-cpp-anthropic
ai-sdk-cpp-langfuse
)
if(AI_SDK_ENABLE_BEDROCK)
list(APPEND COMPONENT_TARGETS ai-sdk-cpp-bedrock)
endif()

# Common compile options
if(MSVC)
Expand Down Expand Up @@ -315,5 +382,6 @@ message(STATUS " Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS " C++ standard: ${CMAKE_CXX_STANDARD}")
message(STATUS " Build examples: ${BUILD_EXAMPLES}")
message(STATUS " Build tests: ${BUILD_TESTS}")
message(STATUS " Bedrock provider: ${AI_SDK_ENABLE_BEDROCK}")
message(STATUS " Install prefix: ${CMAKE_INSTALL_PREFIX}")
message(STATUS "")
148 changes: 147 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,25 +346,171 @@ This approach works with any OpenAI-compatible API provider. Simply provide:

See the [OpenRouter example](examples/openrouter_example.cpp) for a complete demonstration.

#### Amazon Bedrock Integration

Amazon Bedrock provides access to foundation models from Anthropic, Amazon, Meta, and others through AWS infrastructure. The Bedrock provider uses the [Converse API](https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference.html), including streaming and tool calling (with multi-step tool execution via `max_steps`), and uses AWS credentials and IAM for authentication. Assumed-role and web identity credentials are refreshed automatically.

```cpp
#include <ai/bedrock.h>
#include <iostream>

int main() {
// Uses AWS_REGION env var and default credential chain
auto client = ai::bedrock::create_client();

ai::GenerateOptions options(ai::bedrock::models::kClaudeSonnet,
"Explain cloud computing in simple terms.");
options.system = "You are a helpful assistant.";
auto result = client.generate_text(options);

if (result) {
std::cout << result.text << std::endl;
} else {
std::cerr << result.error_message() << std::endl;
}

return 0;
}
```

##### Bedrock Configuration Options

```cpp
#include <ai/bedrock.h>

int main() {
ai::bedrock::BedrockConfig config;

// AWS region (required - or set AWS_REGION env var)
config.region = "us-east-1";

// Optional: Use a specific AWS profile
config.profile = "my-profile";

// Optional: Custom endpoint for VPC endpoints or local testing
config.endpoint_override = "https://vpce-xxx.bedrock-runtime.us-east-1.vpce.amazonaws.com";

// Optional: Cross-account access via STS AssumeRole
config.role_arn = "arn:aws:iam::123456789012:role/BedrockAccessRole";
config.external_id = "my-external-id"; // Optional security enhancement

// Optional: EKS/OIDC web identity federation
config.web_identity_token_file = "/var/run/secrets/eks.amazonaws.com/serviceaccount/token";

auto client = ai::bedrock::create_client(config);

return 0;
}
```

##### Bedrock Security Configuration

```cpp
ai::bedrock::SecurityConfig security;

// Concurrency control
security.max_concurrent_requests = 10;

// Timeouts
security.connection_timeout = std::chrono::seconds(5);
security.request_timeout = std::chrono::seconds(120);

// Input validation limits
security.max_prompt_length = 200000;
security.max_message_length = 200000;

ai::bedrock::BedrockConfig config;
config.security = security;
auto client = ai::bedrock::create_client(config);
```

##### Available Bedrock Models

```cpp
// Claude models (current generation), as global inference profile IDs:
// current Claude models need an inference profile for on-demand invocation
ai::bedrock::models::kClaudeFable // global.anthropic.claude-fable-5-2
ai::bedrock::models::kClaudeOpus // global.anthropic.claude-opus-5-5
ai::bedrock::models::kClaudeSonnet // global.anthropic.claude-sonnet-5-5
ai::bedrock::models::kClaudeHaiku // global.anthropic.claude-haiku-5-5

// Geographic inference profiles are passed as plain strings,
// e.g. "us.anthropic.claude-sonnet-5-5" or "eu.anthropic.claude-opus-5-5"

// Amazon Titan models
ai::bedrock::models::kTitanTextExpress // amazon.titan-text-express-v1
ai::bedrock::models::kTitanTextLite // amazon.titan-text-lite-v1
ai::bedrock::models::kTitanTextPremier // amazon.titan-text-premier-v1:0

// Meta Llama models
ai::bedrock::models::kLlama3_8B // meta.llama3-8b-instruct-v1:0
ai::bedrock::models::kLlama3_70B // meta.llama3-70b-instruct-v1:0
```

##### Bedrock Streaming

```cpp
#include <ai/bedrock.h>
#include <iostream>

int main() {
auto client = ai::bedrock::create_client();

ai::GenerateOptions options(ai::bedrock::models::kClaudeSonnet,
"Write a haiku about programming.");
options.system = "You are a helpful assistant.";
auto stream = client.stream_text(ai::StreamOptions(options));

for (const auto& event : stream) {
if (event.is_text_delta()) {
std::cout << event.text_delta << std::flush;
} else if (event.is_error()) {
std::cerr << event.error.value_or("unknown error") << std::endl;
}
}

return 0;
}
```

##### Building with Bedrock Support

Bedrock support requires the AWS SDK for C++. Enable it with the CMake option:

```bash
cmake -B build -DAI_SDK_ENABLE_BEDROCK=ON
cmake --build build
```

On macOS with Homebrew:
```bash
brew install aws-sdk-cpp
cmake -B build -DAI_SDK_ENABLE_BEDROCK=ON
cmake --build build
```

## Features

### Currently Supported

- ✅ **Text Generation**: Generate text completions with OpenAI and Anthropic models
- ✅ **Text Generation**: Generate text completions with OpenAI, Anthropic, and Bedrock models
- ✅ **Streaming**: Real-time streaming of generated content
- ✅ **Multi-turn Conversations**: Support for conversation history
- ✅ **Error Handling**: Comprehensive error handling with optional types
- ✅ **Embeddings**: OpenAI text embedding support
- ✅ **Amazon Bedrock**: AWS-native access to Claude, Titan, and Llama models

### Recently Added

- ✅ **Tool Calling**: Function calling and tool integration with multi-step support
- ✅ **Async Tools**: Asynchronous tool execution with parallel processing
- ✅ **Configurable Retries**: Customizable retry behavior with exponential backoff
- ✅ **Bedrock Provider**: Amazon Bedrock integration via the Converse API (optional, requires the AWS SDK for C++)

### Coming Soon

- 🚧 **Additional Providers**: Google, Cohere, and other providers
- 🚧 **Bedrock Embeddings**: Text embedding support for Bedrock
- 🚧 **Image Generation**: Support for image generation models

## Examples
Expand Down
9 changes: 9 additions & 0 deletions cmake/ai-sdk-cpp-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,20 @@ set(AI_SDK_VERSION "@PROJECT_VERSION@")
# Find PUBLIC dependencies only
find_dependency(nlohmann_json CONFIG REQUIRED)

# The optional Bedrock component links the AWS SDK for C++
set(AI_SDK_ENABLE_BEDROCK @AI_SDK_ENABLE_BEDROCK@)
if(AI_SDK_ENABLE_BEDROCK)
find_dependency(AWSSDK REQUIRED COMPONENTS core bedrock-runtime bedrock sts)
endif()

# Include the targets file
include("${CMAKE_CURRENT_LIST_DIR}/ai-sdk-cpp-targets.cmake")

# Available components
set(AI_SDK_AVAILABLE_COMPONENTS core openai anthropic langfuse)
if(AI_SDK_ENABLE_BEDROCK)
list(APPEND AI_SDK_AVAILABLE_COMPONENTS bedrock)
endif()

# Check requested components
set(AI_SDK_REQUESTED_COMPONENTS ${ai-sdk-cpp_FIND_COMPONENTS})
Expand Down
5 changes: 4 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ add_ai_example(retry_config_example retry_config_example.cpp)
# Simple test examples
add_ai_example(test_openai test_openai.cpp)
add_ai_example(test_anthropic test_anthropic.cpp)
add_ai_example(test_bedrock test_bedrock.cpp)

# OpenAI Responses API example (tool calling with the GPT-6 models)
add_ai_example(openai_responses openai_responses.cpp)
Expand All @@ -63,7 +64,8 @@ add_ai_example(langfuse_tracing langfuse_tracing.cpp)

# Component-specific examples
add_subdirectory(components/openai)
add_subdirectory(components/anthropic)
add_subdirectory(components/anthropic)
add_subdirectory(components/bedrock)
add_subdirectory(components/all)

# Message to show how to run examples
Expand All @@ -87,4 +89,5 @@ message(STATUS "")
message(STATUS "Component-specific examples:")
message(STATUS " ./examples/components/openai/openai_component_demo (OpenAI + Core only)")
message(STATUS " ./examples/components/anthropic/anthropic_component_demo (Anthropic + Core only)")
message(STATUS " ./examples/components/bedrock/bedrock_component_demo (Bedrock + Core only)")
message(STATUS " ./examples/components/all/all_components_demo (All components)")
Loading