Hacker News

cauchyk
Show HN: A modern C++20 AI SDK (GPT‑4o, Claude 3.5, tool‑calling)

Hi all,

I’m hacking on new features for the ClickHouse native client and wanted the same “just call the model” ergonomics JavaScript and Python now enjoy. It didn’t exist for modern C++, so I wrote one.

ai‑sdk‑cpp (Apache‑2.0) gives you:

- Unified calls to OpenAI (GPT‑4o) and Anthropic (Claude 3.5) with a single C++20 API. - Streaming, multi‑turn chat, error handling—all std::optional/std::variant, no macros. - Tool calling (function‑calling) so the model can hit real APIs; sync or async, runs in parallel.

The tricky bit: C++ still lacks real reflection, so mapping plain functions → JSON schemas isn’t as automatic as, say, TypeScript decorators. I’d love fresh eyes on that part. Try the examples and tell me where it feels clunky. This is inspired by Vercel's AI SDK [1], and litellm [2].

Repo live here: https://github.com/ClickHouse/ai-sdk-cpp, feedback welcome!

[1] https://github.com/vercel/ai [2] https://github.com/BerriAI/litellm


fsloth21 hours ago

“mapping plain functions → JSON schemas”

In general I’ve found it’s much better in C++ to be verbose and plain rather than succinct and elegant.

Javascript and python lack typing so being pithy&clever in those languages has entirely different implications than in C++.

“How can I express this in the most basic, unsurprising and plainspoken C++” is usually the right question to ask. “How can I make this more elegant” is a question better suited for other languages imho.

The best C++ API when in doubt is a C style API.

N_Lens21 hours ago

Agreed! C++ is a powerful language but full of footguns.

pjmlpa day ago

While you will need C++26 for real reflection, you could use some of the libraries that make do with what is already possible via concepts, if constexpr and type traits.

For example,

- https://www.boost.org/doc/libs/latest/doc/html/boost_pfr.htm...

- https://github.com/getml/reflect-cpp

And kudos for using modern C++.

bergesenhaa day ago

The json library 'glaze' has working compile time reflection for MSVC, Clang and GCC using some tricks with aggregate initializable structs. In addition to being a performant json library, it comes with the functions glz::apply and glz::to_tie which I used for general tool calling straight from deserialized json.

rubymamisa day ago

Very nice! I might get inspire for my own Qt C++ LLM client[1]. Do you plan to add streaming Markdown parsing? It's a challenging problem that was very fun implementing for Vox -> it allows parsing code blocks and other such advance/custom blocks (I created a custom 'tool' block) without re-rendering. My implementation is currently tied to Qt's C++ but I might make it more generic and open source it.

[1] https://www.get-vox.com/

sylwarea day ago

I was wondering, has anybody tried to use AI to port c++ to C?

hn-front (c) 2024 voximity
source