package ocaml-ai-sdk

  1. Overview
  2. Docs
OCaml AI SDK - Provider abstraction for AI models

Install

dune-project
 Dependency

Authors

Maintainers

Sources

ocaml-ai-sdk-0.6.1.tbz
sha256=cb3b82428abcda76c02ec92f8103a4db28edf3f42795794a37135e9a3c40af96
sha512=11be8889ee25bee67b2be00553c42a4692bc73e5ce23985e8bb87f8a07e9d8f556b5a63b219fe5fe30366c8b0d4edae494afa80ccfbfcb112f1fa1e458de55bf

doc/ocaml-ai-sdk.ai_core/Ai_core/Server_handler/index.html

Module Ai_core.Server_handlerSource

Convenience handler for building chat API endpoints with cohttp.

Parses the request body as JSON messages, calls stream_text, and returns an SSE response compatible with useChat().

Sourceval cors_headers : (string * string) list

Default CORS headers for cross-origin chat endpoints. Allows all origins, POST/OPTIONS methods, and exposes the UIMessage stream protocol header.

Sourceval parse_messages_from_body : Yojson.Basic.t -> Ai_provider.Prompt.message list

Parse a v6 UIMessage request body into prompt messages.

Expects a JSON object with a "messages" array where each message has "role" and "parts". Supports all v6 part types:

Returns an empty list on parse failure. Unknown part types are skipped.

Sourceval collect_pending_tool_approvals : Yojson.Basic.t -> Generate_text_result.pending_tool_approval list

Collect pending tool approvals from re-submitted messages.

Scans the message history for tool invocation parts with state = "approval-responded", returning full tool call details. Approved tools should be executed directly before calling the LLM; denied tools should produce error results.

Sourceval handle_chat : model:Ai_provider.Language_model.t -> ?tools:(string * Core_tool.t) list -> ?max_steps:int -> ?max_retries:int -> ?max_retry_delay_ms:int -> ?stop_when:Stop_condition.t list -> ?system:string -> ?system_provider_options:Ai_provider.Provider_options.t -> ?output:(Yojson.Basic.t, Yojson.Basic.t) Output.t -> ?send_reasoning:bool -> ?max_output_tokens:int -> ?cors:bool -> ?provider_options:Ai_provider.Provider_options.t -> ?transform: (Text_stream_part.t Lwt_stream.t -> Text_stream_part.t Lwt_stream.t) -> ?telemetry:Telemetry.t -> Cohttp_lwt_unix.Server.conn -> Cohttp.Request.t -> Cohttp_lwt.Body.t -> (Cohttp.Response.t * Cohttp_lwt.Body.t) Lwt.t

Handle an incoming chat request.

Expects a v6 JSON body with a "messages" array where each message has "role" and a "parts" array of typed content parts.

Returns an SSE response with UIMessage stream protocol v1 headers. When cors is true (the default), CORS headers are included. Returns 400 Bad Request if the request body is not valid JSON.

Note: generation parameters like temperature, top_p, max_output_tokens are not exposed here. Pass them via provider_options or call Stream_text.stream_text directly with Stream_text_result.to_ui_message_sse_stream and make_sse_response for full control.

system_provider_options attaches per-message provider options to the ?system prompt. Use with Ai_provider_anthropic.Cache_control_options.with_cache_control to mark the system prompt as a cache breakpoint.

Sourceval handle_cors_preflight : Cohttp_lwt_unix.Server.conn -> Cohttp.Request.t -> Cohttp_lwt.Body.t -> (Cohttp.Response.t * Cohttp_lwt.Body.t) Lwt.t

Handle a CORS preflight OPTIONS request. Returns 204 No Content with CORS headers. Use this for the OPTIONS route matching your chat endpoint.

Sourceval make_sse_response : ?status:Cohttp.Code.status_code -> ?extra_headers:(string * string) list -> string Lwt_stream.t -> (Cohttp.Response.t * Cohttp_lwt.Body.t) Lwt.t

Create an SSE HTTP response from a string stream. Adds UIMessage stream protocol headers automatically.