Skip to content

Commit

Permalink
relax nimble options validations. allow string or atom keyed maps. fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lebrunel committed Dec 11, 2024
1 parent 1b71328 commit 19018ce
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 55 deletions.
60 changes: 6 additions & 54 deletions lib/anthropix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,54 +108,7 @@ defmodule Anthropix do
req: Req.Request.t()
}


schema :cache_control, [
type: [
type: {:in, ["ephemeral"]},
required: true,
]
]

schema :system_message_content, [
type: [
type: {:in, ["text"]},
required: true,
],
text: [
type: :string,
required: true,
],
cache_control: [
type: :map,
keys: schema(:cache_control).schema,
doc: "Cache-control parameter."
]
]

schema :chat_message_content, [
type: [
type: {:in, ["text", "image", "tool_use", "tool_result", "document"]},
required: true,
],
# text type
text: [type: :string],
# image type
source: [type: :map],
# tool_use
id: [type: :string],
name: [type: :string],
input: [type: :map],
# tool_result
tool_use_id: [type: :string],
is_error: [type: :boolean],
content: [type: {:or, [:string, {:list, :map}]}],

cache_control: [
type: :map,
keys: schema(:cache_control).schema,
doc: "Cache-control parameter."
]
]
@permissive_map {:map, {:or, [:atom, :string]}, :any}

schema :chat_message, [
role: [
Expand All @@ -164,7 +117,7 @@ defmodule Anthropix do
doc: "The role of the message, either `user` or `assistant`."
],
content: [
type: {:or, [:string, {:list, {:map, schema(:chat_message_content).schema}}]},
type: {:or, [:string, {:list, @permissive_map}]},
required: true,
doc: "Message content, either a single string or an array of content blocks."
],
Expand All @@ -182,13 +135,12 @@ defmodule Anthropix do
doc: "Description of the tool"
],
input_schema: [
type: :map,
type: @permissive_map,
required: true,
doc: "JSON schema for the tool input shape that the model will produce in tool_use output content blocks."
],
cache_control: [
type: :map,
keys: schema(:cache_control).schema,
type: @permissive_map,
doc: "Cache-control parameter."
]
]
Expand Down Expand Up @@ -367,7 +319,7 @@ defmodule Anthropix do
doc: "Input messages.",
],
system: [
type: {:or, [:string, {:list, {:map, schema(:system_message_content).schema}}]},
type: {:or, [:string, {:list, @permissive_map}]},
doc: "System prompt.",
],
max_tokens: [
Expand All @@ -376,7 +328,7 @@ defmodule Anthropix do
doc: "The maximum number of tokens to generate before stopping.",
],
metadata: [
type: :map,
type: @permissive_map,
doc: "A map describing metadata about the request.",
],
stop_sequences: [
Expand Down
2 changes: 1 addition & 1 deletion test/anthropix_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ defmodule AnthropixTest do
assert get_in(last, ["usage", "output_tokens"]) == 61
end

test "handles nested params as string keyed-maps" do
test "allow nested params as string keyed maps" do
client = Mock.client(& Mock.respond(&1, :messages))
assert {:ok, _res} = Anthropix.chat(client, [
model: "claude-3-sonnet-20240229",
Expand Down

0 comments on commit 19018ce

Please sign in to comment.