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
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ jobs:
run: ctest --build-config ${{ matrix.build_type == 'debug' && 'Debug' || 'Release' }} --output-on-failure --parallel
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
GEMINI_API_KEY: ${{ secrets.GEMINI_API_KEY }}
18 changes: 17 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ set(HTTPLIB_COMPILE_DEFS
add_library(ai-sdk-cpp-core)
add_library(ai-sdk-cpp-openai)
add_library(ai-sdk-cpp-anthropic)
add_library(ai-sdk-cpp-google)
add_library(ai-sdk-cpp-langfuse)
add_library(ai-sdk-cpp INTERFACE)

# Keep installed target names consistent with the build-tree aliases.
set_target_properties(ai-sdk-cpp-core PROPERTIES EXPORT_NAME core)
set_target_properties(ai-sdk-cpp-openai PROPERTIES EXPORT_NAME openai)
set_target_properties(ai-sdk-cpp-anthropic PROPERTIES EXPORT_NAME anthropic)
set_target_properties(ai-sdk-cpp-google PROPERTIES EXPORT_NAME google)
set_target_properties(ai-sdk-cpp-langfuse PROPERTIES EXPORT_NAME langfuse)
set_target_properties(ai-sdk-cpp PROPERTIES EXPORT_NAME sdk)

Expand All @@ -61,12 +63,14 @@ add_library(ai::sdk ALIAS ai-sdk-cpp)
add_library(ai::core ALIAS ai-sdk-cpp-core)
add_library(ai::openai ALIAS ai-sdk-cpp-openai)
add_library(ai::anthropic ALIAS ai-sdk-cpp-anthropic)
add_library(ai::google ALIAS ai-sdk-cpp-google)
add_library(ai::langfuse ALIAS ai-sdk-cpp-langfuse)

# Configure all component libraries
configure_ai_component(ai-sdk-cpp-core)
configure_ai_component(ai-sdk-cpp-openai)
configure_ai_component(ai-sdk-cpp-anthropic)
configure_ai_component(ai-sdk-cpp-google)
configure_ai_component(ai-sdk-cpp-langfuse)

# Bedrock provider (optional, requires AWS SDK)
Expand Down Expand Up @@ -127,8 +131,17 @@ set(ANTHROPIC_SOURCES
src/providers/anthropic/anthropic_factory.cpp
)

set(GOOGLE_SOURCES
src/providers/google/google_client.cpp
src/providers/google/google_request_builder.cpp
src/providers/google/google_response_parser.cpp
src/providers/google/google_stream.cpp
src/providers/google/google_factory.cpp
)

target_sources(ai-sdk-cpp-openai PRIVATE ${OPENAI_SOURCES})
target_sources(ai-sdk-cpp-anthropic PRIVATE ${ANTHROPIC_SOURCES})
target_sources(ai-sdk-cpp-google PRIVATE ${GOOGLE_SOURCES})

# Langfuse tracing component
target_sources(ai-sdk-cpp-langfuse
Expand Down Expand Up @@ -172,7 +185,7 @@ target_link_libraries(ai-sdk-cpp-core
)

# Provider components link to core and share common dependencies
foreach(provider openai anthropic)
foreach(provider openai anthropic google)
target_link_libraries(ai-sdk-cpp-${provider}
PUBLIC
ai::core
Expand Down Expand Up @@ -248,6 +261,7 @@ target_link_libraries(ai-sdk-cpp
ai::core
ai::openai
ai::anthropic
ai::google
ai::langfuse
)

Expand All @@ -264,6 +278,7 @@ target_compile_definitions(ai-sdk-cpp
INTERFACE
AI_SDK_HAS_OPENAI=1
AI_SDK_HAS_ANTHROPIC=1
AI_SDK_HAS_GOOGLE=1
AI_SDK_HAS_LANGFUSE=1
)

Expand All @@ -280,6 +295,7 @@ set(COMPONENT_TARGETS
ai-sdk-cpp-core
ai-sdk-cpp-openai
ai-sdk-cpp-anthropic
ai-sdk-cpp-google
ai-sdk-cpp-langfuse
)
if(AI_SDK_ENABLE_BEDROCK)
Expand Down
51 changes: 46 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# AI SDK CPP

The AI SDK CPP is a modern C++ toolkit designed to help you build AI-powered applications with popular model providers like OpenAI and Anthropic. It provides a unified, easy-to-use API that abstracts away the complexity of different provider implementations.
The AI SDK CPP is a modern C++ toolkit designed to help you build AI-powered applications with popular model providers like OpenAI, Anthropic, and Google Gemini. It provides a unified, easy-to-use API that abstracts away the complexity of different provider implementations.

## Motivation

Expand All @@ -18,7 +18,7 @@ You will need a C++20 compatible compiler and CMake 3.16+ installed on your deve

### Core API

The AI SDK CPP Core module provides a unified API to interact with model providers like OpenAI and Anthropic.
The AI SDK CPP Core module provides a unified API to interact with model providers like OpenAI, Anthropic, and Google Gemini.

#### OpenAI Integration

Expand Down Expand Up @@ -67,6 +67,45 @@ int main() {
}
```

#### Google Gemini Integration

The Google provider speaks the native Gemini API (`generateContent` and
`streamGenerateContent`), not the OpenAI-compatibility endpoint.

```cpp
#include <ai/google.h>
#include <ai/core.h>
#include <iostream>

int main() {
// Ensure GEMINI_API_KEY (or GOOGLE_API_KEY) environment variable is set
auto client = ai::google::create_client();
ai::GenerateOptions options(ai::google::models::kGemini4Hydrogen,
"Explain quantum 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;
}

return 0;
}
```

Tool calling, multi-step tool loops, structured output (`response_format`),
and streaming work as with the other providers. Gemini 4 and Gemini 3 models
attach thought signatures to their function calls and reject a follow-up
request that does not send them back; the client keeps them in
`response_messages` (as `ai::ProviderContentPart`), so the multi-step loop and
conversations continued from `response_messages` replay them automatically.
Thinking tokens count against `max_tokens`, so the client asks for the least
thinking each model accepts (`thinkingLevel` `low` or `minimal`, or the
smallest `thinkingBudget` on Gemini 2.5), like the lowest reasoning effort of
the OpenAI client; streams request thought summaries and emit them as
reasoning events (`StreamEvent::is_reasoning_delta`). Gemini 4 and Gemini 3.6+ models ignore `temperature` and
`top_p`, and the client does not send them for those models. Embeddings are not supported by this provider yet.

#### Streaming Responses

```cpp
Expand Down Expand Up @@ -299,7 +338,8 @@ int main() {
```

The same retry-config overload is available from
`ai::anthropic::create_client` for Anthropic requests.
`ai::anthropic::create_client` for Anthropic requests and from
`ai::google::create_client` for Gemini requests.

#### Using OpenAI-Compatible APIs (OpenRouter, etc.)

Expand Down Expand Up @@ -493,7 +533,7 @@ cmake --build build

### Currently Supported

- ✅ **Text Generation**: Generate text completions with OpenAI, Anthropic, and Bedrock models
- ✅ **Text Generation**: Generate text completions with OpenAI, Anthropic, Bedrock, and Google Gemini models
- ✅ **Streaming**: Real-time streaming of generated content
- ✅ **Multi-turn Conversations**: Support for conversation history
- ✅ **Error Handling**: Comprehensive error handling with optional types
Expand All @@ -509,7 +549,7 @@ cmake --build build

### Coming Soon

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

Expand All @@ -525,6 +565,7 @@ Check out our [examples directory](examples/) for more comprehensive usage examp
- [Basic Tool Calling](examples/tool_calling_basic.cpp)
- [Multi-Step Tool Workflows](examples/tool_calling_multistep.cpp)
- [OpenAI Responses API](examples/openai_responses.cpp) - Tool calling with the GPT-6 models
- [Google Gemini](examples/google_gemini.cpp) - Native Gemini API: tools, structured output, streaming
- [Async Tool Execution](examples/tool_calling_async.cpp)
- [Search Team Documents with Loom](examples/loom_document_search.md)
- [OpenRouter Integration](examples/openrouter_example.cpp) - Using OpenAI-compatible APIs
Expand Down
2 changes: 1 addition & 1 deletion cmake/ai-sdk-cpp-config.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ endif()
include("${CMAKE_CURRENT_LIST_DIR}/ai-sdk-cpp-targets.cmake")

# Available components
set(AI_SDK_AVAILABLE_COMPONENTS core openai anthropic langfuse)
set(AI_SDK_AVAILABLE_COMPONENTS core openai anthropic google langfuse)
if(AI_SDK_ENABLE_BEDROCK)
list(APPEND AI_SDK_AVAILABLE_COMPONENTS bedrock)
endif()
Expand Down
8 changes: 7 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ 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)

# Google Gemini example (native Gemini API)
add_ai_example(google_gemini google_gemini.cpp)

# OpenRouter example (OpenAI-compatible)
add_ai_example(openrouter_example openrouter_example.cpp)

Expand All @@ -65,6 +68,7 @@ add_ai_example(langfuse_tracing langfuse_tracing.cpp)
# Component-specific examples
add_subdirectory(components/openai)
add_subdirectory(components/anthropic)
add_subdirectory(components/google)
add_subdirectory(components/bedrock)
add_subdirectory(components/all)

Expand All @@ -77,17 +81,19 @@ endif()

message(STATUS "Examples will be built in: ${CMAKE_BINARY_DIR}/examples/")
message(STATUS "To run examples:")
message(STATUS " Set environment variables: OPENAI_API_KEY, ANTHROPIC_API_KEY, OPENROUTER_API_KEY")
message(STATUS " Set environment variables: OPENAI_API_KEY, ANTHROPIC_API_KEY, GEMINI_API_KEY, OPENROUTER_API_KEY")
message(STATUS " Run: ./examples/basic_chat${EXAMPLE_DISPLAY_SUFFIX}")
message(STATUS " ./examples/streaming_chat${EXAMPLE_DISPLAY_SUFFIX}")
message(STATUS " ./examples/multi_provider${EXAMPLE_DISPLAY_SUFFIX}")
message(STATUS " ./examples/error_handling${EXAMPLE_DISPLAY_SUFFIX}")
message(STATUS " ./examples/test_openai${EXAMPLE_DISPLAY_SUFFIX}")
message(STATUS " ./examples/test_anthropic${EXAMPLE_DISPLAY_SUFFIX}")
message(STATUS " ./examples/google_gemini${EXAMPLE_DISPLAY_SUFFIX}")
message(STATUS " ./examples/openrouter_example${EXAMPLE_DISPLAY_SUFFIX}")
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/google/google_component_demo (Google + Core only)")
message(STATUS " ./examples/components/bedrock/bedrock_component_demo (Bedrock + Core only)")
message(STATUS " ./examples/components/all/all_components_demo (All components)")
25 changes: 23 additions & 2 deletions examples/components/all/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ int main() {
std::cout << "- Anthropic: NO\n";
#endif

#ifdef AI_SDK_HAS_GOOGLE
std::cout << "- Google: YES\n";
#else
std::cout << "- Google: NO\n";
#endif

std::cout << "\n";

// Test core functionality
Expand Down Expand Up @@ -61,10 +67,25 @@ int main() {
std::cout << "\nAnthropic functionality not available\n";
#endif

// Test Google functionality
#ifdef AI_SDK_HAS_GOOGLE
std::cout << "\nTesting Google functionality...\n";
try {
auto google_client = ai::google::create_client();
std::cout << "✓ Google client created successfully\n";
std::cout << "✓ Available models: " << ai::google::models::kGemini4Hydrogen
<< ", " << ai::google::models::kGemini35FlashLite << "\n";
} catch (const std::exception& e) {
std::cout << "✗ Google client failed: " << e.what() << "\n";
}
#else
std::cout << "\nGoogle functionality not available\n";
#endif

std::cout << "\n";
std::cout << "This example demonstrates using all components together.\n";
std::cout
<< "Both OpenAI and Anthropic should be available when linking ai::sdk\n";
std::cout << "OpenAI, Anthropic, and Google should be available when linking "
"ai::sdk\n";

return 0;
}
13 changes: 13 additions & 0 deletions examples/components/google/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Google-only component example

add_executable(google_component_demo main.cpp)
target_link_libraries(google_component_demo PRIVATE
ai::core
ai::google
)

set_target_properties(google_component_demo PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/examples/components/google
)

message(STATUS "Google component example will be built in: ${CMAKE_BINARY_DIR}/examples/components/google/")
47 changes: 47 additions & 0 deletions examples/components/google/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream>

#include <ai/ai.h>

int main() {
std::cout << "AI SDK C++ - Google Component Demo\n";
std::cout << "=================================\n\n";

std::cout << "Available components:\n";
std::cout << "- Core: YES\n";

#ifdef AI_SDK_HAS_OPENAI
std::cout << "- OpenAI: YES\n";
#else
std::cout << "- OpenAI: NO\n";
#endif

#ifdef AI_SDK_HAS_ANTHROPIC
std::cout << "- Anthropic: YES\n";
#else
std::cout << "- Anthropic: NO\n";
#endif

#ifdef AI_SDK_HAS_GOOGLE
std::cout << "- Google: YES\n";
#else
std::cout << "- Google: NO\n";
#endif

std::cout << "\n";

#ifdef AI_SDK_HAS_GOOGLE
std::cout << "Testing Google functionality...\n";
try {
auto client = ai::google::create_client();
std::cout << "✓ Google client created successfully\n";
std::cout << "✓ Default model: " << client.default_model() << "\n";
} catch (const std::exception& e) {
std::cout << "✗ Google client failed: " << e.what() << "\n";
}
#else
std::cout << "Google functionality not available\n";
#endif

std::cout << "\nThis example only links the ai::google component\n";
return 0;
}
Loading
Loading