Skip to content
Open
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
6 changes: 5 additions & 1 deletion .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 Expand Up @@ -158,4 +159,7 @@ config.json

.envrc
.cache/
.claude/
.claude/

# Kiro specs
.kiro/
67 changes: 67 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 with submodules
add_subdirectory(third_party)
Expand Down Expand Up @@ -58,6 +59,15 @@ configure_ai_component(ai-sdk-cpp-core)
configure_ai_component(ai-sdk-cpp-openai)
configure_ai_component(ai-sdk-cpp-anthropic)

# 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)
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 @@ -103,6 +113,26 @@ set(ANTHROPIC_SOURCES
target_sources(ai-sdk-cpp-openai PRIVATE ${OPENAI_SOURCES})
target_sources(ai-sdk-cpp-anthropic PRIVATE ${ANTHROPIC_SOURCES})

# 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
# Security components
src/providers/bedrock/input_validator.cpp
src/providers/bedrock/credential_redactor.cpp
src/providers/bedrock/secure_logger.cpp
src/providers/bedrock/circuit_breaker.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 @@ -135,6 +165,23 @@ foreach(provider openai anthropic)
)
endforeach()

# 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 @@ -149,15 +196,34 @@ target_link_libraries(ai-sdk-cpp
ai::anthropic
)

# 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
AI_SDK_HAS_OPENAI=1
AI_SDK_HAS_ANTHROPIC=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)
if(AI_SDK_ENABLE_BEDROCK)
list(APPEND COMPONENT_TARGETS ai-sdk-cpp-bedrock)
endif()

# Common compile options
if(MSVC)
Expand Down Expand Up @@ -254,5 +320,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 "")
147 changes: 145 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,25 +306,168 @@ 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 AWS credentials and IAM for authentication.

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

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

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

if (result) {
std::cout << result->text << 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;

// Circuit breaker settings
security.circuit_breaker_threshold = 5;
security.circuit_breaker_timeout = std::chrono::seconds(30);

// 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;
security.max_tokens_limit = 200000;

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

##### Available Bedrock Models

```cpp
// Claude models
ai::bedrock::models::kClaudeSonnet // anthropic.claude-3-5-sonnet-20241022-v2:0
ai::bedrock::models::kClaudeHaiku // anthropic.claude-3-5-haiku-20241022-v1:0
ai::bedrock::models::kClaudeOpus // anthropic.claude-3-opus-20240229-v1:0

// 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();

auto stream = client.stream_text({
.model = ai::bedrock::models::kClaudeSonnet,
.system = "You are a helpful assistant.",
.prompt = "Write a haiku about programming."
});

for (const auto& chunk : stream) {
if (chunk.text) {
std::cout << chunk.text.value() << std::flush;
}
}

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
- ✅ **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**: Production-ready AWS Bedrock integration with security features

### Coming Soon

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

## Examples
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)

# OpenRouter example (OpenAI-compatible)
add_ai_example(openrouter_example openrouter_example.cpp)
Expand All @@ -55,7 +56,8 @@ add_ai_example(embeddings_example embeddings_example.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 @@ -73,4 +75,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