I3WIGHB2AOZ3YJ735DNBJJPB3KO4HB37SCB7F25TJO7WC6JTTJEQC
(lang dune 3.14)
(executable
(name ant)
(libraries cohttp cohttp-lwt-unix lwt lwt_ssl yojson))
open Cohttp
open Cohttp_lwt_unix
open Lwt
open Yojson.Basic.Util
let read_stdin_to_string () =
let rec read_lines acc =
try
let line = input_line stdin in
read_lines (acc ^ line ^ "\n")
with End_of_file -> acc
in
read_lines ""
let get_env api () =
try
Sys.getenv api
with
| Not_found -> failwith api ^ " environment variable not found"
let ant_api_key = get_env "ANTHROPIC_API_KEY" ()
(* let memos_api_key = get_env "MEMOS_API_KEY" () *)
let model = "claude-3-haiku-20240307"
let max_tokens = 1024
let ant_uri = Uri.of_string "https://api.anthropic.com/v1/messages"
let ant_req_body prompt =
`Assoc
[
("model", `String model);
("max_tokens", `Int max_tokens);
( "messages",
`List
[ `Assoc [ ("role", `String "user"); ("content", `String prompt) ] ]
);
]
let extract_code_block (s : string) : string option =
let triple_backtick = "```" in
try
let open_pos = String.index s '`' in
if String.sub s open_pos 3 = triple_backtick then
let close_pos = String.index_from s (open_pos + 3) '`' in
if String.sub s close_pos 3 = triple_backtick then
Some (String.sub s (open_pos + 3 + 5) (close_pos - open_pos - 3 - 5)) (* here +5 to remove ocaml or julia after ``` and -5 to remove closing backticks *)
else None
else None
with Not_found -> None
let () =
let prompt = read_stdin_to_string () in
Printf.printf "(* %s *)" prompt;
let headers =
Header.init () |> fun h ->
Header.add h "x-api-key" ant_api_key |> fun h ->
Header.add h "anthropic-version" "2023-06-01" |> fun h ->
Header.add h "content-type" "application/json"
in
let body = ant_req_body prompt |> Yojson.Basic.to_string in
let req =
Client.post ~headers ~body:(Cohttp_lwt.Body.of_string body) ant_uri
in
Lwt_main.run
( req >>= fun (resp, body) ->
let status = resp |> Response.status |> Code.code_of_status in
Printf.printf "\n(* Response status: %d *)" status;
body |> Cohttp_lwt.Body.to_string >|= fun body_str ->
let json = Yojson.Basic.from_string body_str in
let content = json |> member "content" in
let text = index 0 content |> member "text" |> to_string in
match extract_code_block text with
| Some code -> Printf.printf "\n%s\n" code
| None -> Printf.printf "(* No code returned *)\n(* %s *)\n" text )
# dune build ./ant.exe
# OCAMLPARAM: unset
# Shared cache: disabled
# Shared cache location: /Users/david/.cache/dune/db
# Workspace root: /Users/david/ocaml/ant
# Auto-detected concurrency: 4
# Dune context:
# { name = "default"
# ; kind = "default"
# ; profile = Dev
# ; merlin = true
# ; fdo_target_exe = None
# ; build_dir = In_build_dir "default"
# ; installed_env =
# map
# { "INSIDE_DUNE" : "/Users/david/ocaml/ant/_build/default"
# ; "OCAML_COLOR" : "always"
# ; "OPAMCOLOR" : "always"
# }
# ; instrument_with = []
# }
$ /Users/david/.opam/ant/bin/ocamlc.opt -config > /var/folders/rv/kyhr9kcn4q39_880qd3rp2kr0000gn/T/dune_2d1027_output
$ (cd _build/default && /Users/david/.opam/ant/bin/ocamlopt.opt -w @1..3@5..28@31..39@43@46..47@49..57@61..62@67@69-40 -strict-sequence -strict-formats -short-paths -keep-locs -g -I .ant.eobjs/byte -I .ant.eobjs/native -I /Users/david/.opam/ant/lib/angstrom -I /Users/david/.opam/ant/lib/astring -I /Users/david/.opam/ant/lib/base64 -I /Users/david/.opam/ant/lib/bigstringaf -I /Users/david/.opam/ant/lib/bytes -I /Users/david/.opam/ant/lib/cohttp -I /Users/david/.opam/ant/lib/cohttp-lwt -I /Users/david/.opam/ant/lib/cohttp-lwt-unix -I /Users/david/.opam/ant/lib/conduit -I /Users/david/.opam/ant/lib/conduit-lwt -I /Users/david/.opam/ant/lib/conduit-lwt-unix -I /Users/david/.opam/ant/lib/domain-name -I /Users/david/.opam/ant/lib/fmt -I /Users/david/.opam/ant/lib/ipaddr -I /Users/david/.opam/ant/lib/ipaddr-sexp -I /Users/david/.opam/ant/lib/ipaddr/unix -I /Users/david/.opam/ant/lib/logs -I /Users/david/.opam/ant/lib/lwt -I /Users/david/.opam/ant/lib/lwt/unix -I /Users/david/.opam/ant/lib/lwt_ssl -I /Users/david/.opam/ant/lib/macaddr -I /Users/david/.opam/ant/lib/magic-mime -I /Users/david/.opam/ant/lib/ocaml/threads -I /Users/david/.opam/ant/lib/ocaml/unix -I /Users/david/.opam/ant/lib/ocplib-endian -I /Users/david/.opam/ant/lib/ocplib-endian/bigstring -I /Users/david/.opam/ant/lib/parsexp -I /Users/david/.opam/ant/lib/ppx_sexp_conv/runtime-lib -I /Users/david/.opam/ant/lib/re -I /Users/david/.opam/ant/lib/seq -I /Users/david/.opam/ant/lib/sexplib -I /Users/david/.opam/ant/lib/sexplib0 -I /Users/david/.opam/ant/lib/ssl -I /Users/david/.opam/ant/lib/stringext -I /Users/david/.opam/ant/lib/uri -I /Users/david/.opam/ant/lib/uri-sexp -I /Users/david/.opam/ant/lib/uri/services -I /Users/david/.opam/ant/lib/yojson -intf-suffix .ml -no-alias-deps -opaque -o .ant.eobjs/native/dune__exe__Ant.cmx -c -impl ant.ml)
$ (cd _build/default && /Users/david/.opam/ant/bin/ocamlopt.opt -w @1..3@5..28@31..39@43@46..47@49..57@61..62@67@69-40 -strict-sequence -strict-formats -short-paths -keep-locs -g -o ant.exe /Users/david/.opam/ant/lib/re/re.cmxa /Users/david/.opam/ant/lib/stringext/stringext.cmxa /Users/david/.opam/ant/lib/bigstringaf/bigstringaf.cmxa -I /Users/david/.opam/ant/lib/bigstringaf /Users/david/.opam/ant/lib/angstrom/angstrom.cmxa /Users/david/.opam/ant/lib/uri/uri.cmxa /Users/david/.opam/ant/lib/sexplib0/sexplib0.cmxa /Users/david/.opam/ant/lib/ppx_sexp_conv/runtime-lib/ppx_sexp_conv_lib.cmxa /Users/david/.opam/ant/lib/uri-sexp/uri_sexp.cmxa /Users/david/.opam/ant/lib/base64/base64.cmxa /Users/david/.opam/ant/lib/cohttp/cohttp.cmxa /Users/david/.opam/ant/lib/fmt/fmt.cmxa /Users/david/.opam/ant/lib/logs/logs.cmxa /Users/david/.opam/ant/lib/lwt/lwt.cmxa /Users/david/.opam/ant/lib/logs/logs_lwt.cmxa /Users/david/.opam/ant/lib/parsexp/parsexp.cmxa /Users/david/.opam/ant/lib/sexplib/sexplib.cmxa /Users/david/.opam/ant/lib/macaddr/macaddr.cmxa /Users/david/.opam/ant/lib/domain-name/domain_name.cmxa /Users/david/.opam/ant/lib/ipaddr/ipaddr.cmxa /Users/david/.opam/ant/lib/ipaddr-sexp/ipaddr_sexp.cmxa /Users/david/.opam/ant/lib/astring/astring.cmxa /Users/david/.opam/ant/lib/conduit/conduit.cmxa /Users/david/.opam/ant/lib/conduit-lwt/conduit_lwt.cmxa /Users/david/.opam/ant/lib/magic-mime/magic_mime_library.cmxa /Users/david/.opam/ant/lib/ocplib-endian/ocplib_endian.cmxa /Users/david/.opam/ant/lib/ocplib-endian/bigstring/ocplib_endian_bigstring.cmxa /Users/david/.opam/ant/lib/ocaml/unix/unix.cmxa /Users/david/.opam/ant/lib/ocaml/threads/threads.cmxa /Users/david/.opam/ant/lib/lwt/unix/lwt_unix.cmxa -I /Users/david/.opam/ant/lib/lwt/unix /Users/david/.opam/ant/lib/uri/services/uri_services.cmxa /Users/david/.opam/ant/lib/ipaddr/unix/ipaddr_unix.cmxa /Users/david/.opam/ant/lib/ssl/ssl.cmxa -I /Users/david/.opam/ant/lib/ssl /Users/david/.opam/ant/lib/lwt_ssl/lwt_ssl.cmxa /Users/david/.opam/ant/lib/conduit-lwt-unix/conduit_lwt_unix.cmxa /Users/david/.opam/ant/lib/cohttp-lwt/cohttp_lwt.cmxa /Users/david/.opam/ant/lib/logs/logs_fmt.cmxa /Users/david/.opam/ant/lib/cohttp-lwt-unix/cohttp_lwt_unix.cmxa /Users/david/.opam/ant/lib/yojson/yojson.cmxa .ant.eobjs/native/dune__exe__Ant.cmx)
(executable
(name ant)
(libraries cohttp cohttp-lwt-unix lwt lwt_ssl yojson))
(* Auto-generated by Dune *)
open Cohttp
open Cohttp_lwt_unix
open Lwt
open Yojson.Basic.Util
let read_stdin_to_string () =
let rec read_lines acc =
try
let line = input_line stdin in
read_lines (acc ^ line ^ "\n")
with End_of_file -> acc
in
read_lines ""
let get_env api () =
try
Sys.getenv api
with
| Not_found -> failwith api ^ " environment variable not found"
let ant_api_key = get_env "ANTHROPIC_API_KEY" ()
(* let memos_api_key = get_env "MEMOS_API_KEY" () *)
let model = "claude-3-haiku-20240307"
let max_tokens = 1024
let ant_uri = Uri.of_string "https://api.anthropic.com/v1/messages"
let ant_req_body prompt =
`Assoc
[
("model", `String model);
("max_tokens", `Int max_tokens);
( "messages",
`List
[ `Assoc [ ("role", `String "user"); ("content", `String prompt) ] ]
);
]
let extract_code_block (s : string) : string option =
let triple_backtick = "```" in
try
let open_pos = String.index s '`' in
if String.sub s open_pos 3 = triple_backtick then
let close_pos = String.index_from s (open_pos + 3) '`' in
if String.sub s close_pos 3 = triple_backtick then
Some (String.sub s (open_pos + 3 + 5) (close_pos - open_pos - 3 - 5)) (* here +5 to remove ocaml or julia after ``` and -5 to remove closing backticks *)
else None
else None
with Not_found -> None
let () =
let prompt = read_stdin_to_string () in
Printf.printf "(* %s *)" prompt;
let headers =
Header.init () |> fun h ->
Header.add h "x-api-key" ant_api_key |> fun h ->
Header.add h "anthropic-version" "2023-06-01" |> fun h ->
Header.add h "content-type" "application/json"
in
let body = ant_req_body prompt |> Yojson.Basic.to_string in
let req =
Client.post ~headers ~body:(Cohttp_lwt.Body.of_string body) ant_uri
in
Lwt_main.run
( req >>= fun (resp, body) ->
let status = resp |> Response.status |> Code.code_of_status in
Printf.printf "\n(* Response status: %d *)" status;
body |> Cohttp_lwt.Body.to_string >|= fun body_str ->
let json = Yojson.Basic.from_string body_str in
let content = json |> member "content" in
let text = index 0 content |> member "text" |> to_string in
match extract_code_block text with
| Some code -> Printf.printf "\n%s\n" code
| None -> Printf.printf "(* No code returned *)\n(* %s *)\n" text )
(executable
(name ant)
(libraries cohttp cohttp-lwt-unix lwt lwt_ssl yojson))
open Cohttp
open Cohttp_lwt_unix
open Lwt
open Yojson.Basic.Util
let read_stdin_to_string () =
let rec read_lines acc =
try
let line = input_line stdin in
read_lines (acc ^ line ^ "\n")
with End_of_file -> acc
in
read_lines ""
let ant_api_key =
"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"
let model = "claude-3-opus-20240229"
let max_tokens = 1024
let ant_uri = Uri.of_string "https://api.anthropic.com/v1/messages"
let ant_req_body prompt =
`Assoc
[
("model", `String model);
("max_tokens", `Int max_tokens);
( "messages",
`List
[ `Assoc [ ("role", `String "user"); ("content", `String prompt) ] ]
);
]
let extract_code_block (s : string) : string option =
let triple_backtick = "```" in
try
let open_pos = String.index s '`' in
if String.sub s open_pos 3 = triple_backtick then
let close_pos = String.index_from s (open_pos + 3) '`' in
if String.sub s close_pos 3 = triple_backtick then
Some (String.sub s (open_pos + 3 + 5) (close_pos - open_pos - 3))
else None
else None
with Not_found -> None
let () =
let prompt = read_stdin_to_string () in
Printf.printf "%s\n" prompt;
let headers =
Header.init () |> fun h ->
Header.add h "x-api-key" ant_api_key |> fun h ->
Header.add h "anthropic-version" "2023-06-01" |> fun h ->
Header.add h "content-type" "application/json"
in
let body = ant_req_body prompt |> Yojson.Basic.to_string in
let req =
Client.post ~headers ~body:(Cohttp_lwt.Body.of_string body) ant_uri
in
Lwt_main.run
( req >>= fun (resp, body) ->
let status = resp |> Response.status |> Code.code_of_status in
Printf.printf "(* Response status: %d *)\n" status;
body |> Cohttp_lwt.Body.to_string >|= fun body_str ->
let json = Yojson.Basic.from_string body_str in
let content = json |> member "content" in
let text = index 0 content |> member "text" |> to_string in
match extract_code_block text with
| Some code -> Printf.printf "%s\n" code
| None -> Printf.printf "(* No code returned *)\n%s\n" text )
((6:ocamlc37:/Users/david/.opam/ant/bin/ocamlc.opt)(17:ocaml_config_vars((14:afl_instrument5:false)(12:architecture5:amd64)(3:asm20:cc -c -Wno-trigraphs)(17:asm_cfi_supported4:true)(21:ast_impl_magic_number12:Caml1999M033)(21:ast_intf_magic_number12:Caml1999N033)(19:bytecomp_c_compiler71:cc -O2 -fno-strict-aliasing -fwrapv -pthread -D_FILE_OFFSET_BITS=64 )(20:bytecomp_c_libraries54:-L/usr/local/Cellar/zstd/1.5.5/lib -lzstd -lpthread)(10:c_compiler2:cc)(10:ccomp_type2:cc)(16:cma_magic_number12:Caml1999A033)(16:cmi_magic_number12:Caml1999I033)(16:cmo_magic_number12:Caml1999O033)(16:cmt_magic_number12:Caml1999T033)(16:cmx_magic_number12:Caml1999Y033)(17:cmxa_magic_number12:Caml1999Z033)(17:cmxs_magic_number12:Caml1999D033)(23:default_executable_name5:a.out)(19:default_safe_string4:true)(17:exec_magic_number12:Caml1999X033)(7:ext_asm2:.s)(7:ext_dll3:.so)(7:ext_exe0:)(7:ext_lib2:.a)(7:ext_obj2:.o)(7:flambda5:false)(16:flat_float_array4:true)(17:function_sections5:false)(4:host25:x86_64-apple-darwin23.3.0)(8:int_size2:63)(19:linear_magic_number12:Caml1999L033)(5:model7:default)(14:naked_pointers5:false)(17:native_c_compiler71:cc -O2 -fno-strict-aliasing -fwrapv -pthread -D_FILE_OFFSET_BITS=64 )(18:native_c_libraries12: -lpthread)(15:native_compiler4:true)(14:native_dynlink4:true)(18:native_pack_linker9:ld -r -o )(13:ocamlc_cflags43:-O2 -fno-strict-aliasing -fwrapv -pthread )(15:ocamlc_cppflags24: -D_FILE_OFFSET_BITS=64 )(15:ocamlopt_cflags43:-O2 -fno-strict-aliasing -fwrapv -pthread )(17:ocamlopt_cppflags24: -D_FILE_OFFSET_BITS=64 )(7:os_type4:Unix)(11:safe_string4:true)(16:standard_library32:/Users/david/.opam/ant/lib/ocaml)(24:standard_library_default32:/Users/david/.opam/ant/lib/ocaml)(25:supports_shared_libraries4:true)(6:system6:macosx)(19:systhread_supported4:true)(6:target25:x86_64-apple-darwin23.3.0)(7:version5:5.1.1)(15:windows_unicode5:false)(19:with_frame_pointers5:false)(9:word_size2:64))))
(ocamlc /Users/david/.opam/ant/bin/ocamlc.opt)
(ocaml_config_vars (afl_instrument false) (architecture amd64) (asm "cc -c -Wno-trigraphs") (asm_cfi_supported true) (ast_impl_magic_number Caml1999M033) (ast_intf_magic_number Caml1999N033) (bytecomp_c_compiler "cc -O2 -fno-strict-aliasing -fwrapv -pthread -D_FILE_OFFSET_BITS=64 ") (bytecomp_c_libraries "-L/usr/local/Cellar/zstd/1.5.5/lib -lzstd -lpthread") (c_compiler cc) (ccomp_type cc) (cma_magic_number Caml1999A033) (cmi_magic_number Caml1999I033) (cmo_magic_number Caml1999O033) (cmt_magic_number Caml1999T033) (cmx_magic_number Caml1999Y033) (cmxa_magic_number Caml1999Z033) (cmxs_magic_number Caml1999D033) (default_executable_name a.out) (default_safe_string true) (exec_magic_number Caml1999X033) (ext_asm .s) (ext_dll .so) (ext_exe "") (ext_lib .a) (ext_obj .o) (flambda false) (flat_float_array true) (function_sections false) (host x86_64-apple-darwin23.3.0) (int_size 63) (linear_magic_number Caml1999L033) (model default) (naked_pointers false) (native_c_compiler "cc -O2 -fno-strict-aliasing -fwrapv -pthread -D_FILE_OFFSET_BITS=64 ") (native_c_libraries " -lpthread") (native_compiler true) (native_dynlink true) (native_pack_linker "ld -r -o ") (ocamlc_cflags "-O2 -fno-strict-aliasing -fwrapv -pthread ") (ocamlc_cppflags " -D_FILE_OFFSET_BITS=64 ") (ocamlopt_cflags "-O2 -fno-strict-aliasing -fwrapv -pthread ") (ocamlopt_cppflags " -D_FILE_OFFSET_BITS=64 ") (os_type Unix) (safe_string true) (standard_library /Users/david/.opam/ant/lib/ocaml) (standard_library_default /Users/david/.opam/ant/lib/ocaml) (supports_shared_libraries true) (system macosx) (systhread_supported true) (target x86_64-apple-darwin23.3.0) (version 5.1.1) (windows_unicode false) (with_frame_pointers false) (word_size 64))
40697
<dummy>
.git
.DS_Store