Anthropic Schema 转换
将工具定义转换为 Anthropic API 格式。
Anthropic 的工具格式与 OpenAI 不同:
使用示例:
(require '[im.ttalk.agent.provider.schema.anthropic :as schema])
;; 转换单个工具 (schema/tool->schema {:name :calculator :description "计算器" :parameters {...}})
;; 批量转换 (schema/tools->schemas tools)
Anthropic Schema 转换
将工具定义转换为 Anthropic API 格式。
Anthropic 的工具格式与 OpenAI 不同:
- 使用 input_schema 而非 parameters
- 工具调用使用 content_block 机制
使用示例:
(require '[im.ttalk.agent.provider.schema.anthropic :as schema])
;; 转换单个工具
(schema/tool->schema {:name :calculator
:description "计算器"
:parameters {...}})
;; 批量转换
(schema/tools->schemas tools)(code-execution-tool)(code-execution-tool {:keys [type]})构造 code_execution 服务端工具(beta;Skills 通常需要它来执行)。 可传 :type 覆盖默认 beta 类型标识。
构造 code_execution 服务端工具(beta;Skills 通常需要它来执行)。 可传 :type 覆盖默认 beta 类型标识。
code_execution 服务端工具类型标识(beta)
code_execution 服务端工具类型标识(beta)
启用 Skills + code_execution 所需的 anthropic-beta 头值(传给 config :beta)。 beta 标识可能随官方更新而变化。
启用 Skills + code_execution 所需的 anthropic-beta 头值(传给 config :beta)。 beta 标识可能随官方更新而变化。
(schema->tool {:keys [name description input_schema]})将 Anthropic schema 转换为工具定义
参数:
返回: 标准工具定义
示例: (schema->tool {:name "calculator" :description "执行计算" :input_schema {...}})
将 Anthropic schema 转换为工具定义
参数:
- schema: Anthropic schema map
返回:
标准工具定义
示例:
(schema->tool {:name "calculator"
:description "执行计算"
:input_schema {...}})(schemas->tools schemas)批量转换 Anthropic schemas 为工具定义
参数:
返回: 工具定义列表
批量转换 Anthropic schemas 为工具定义 参数: - schemas: Anthropic schema 列表 返回: 工具定义列表
(skill skill-id)(skill skill-id {:keys [type version]})构造单个 skill 引用。
参数:
返回:{:type "anthropic" :skill_id "xlsx"}(含 :version 当提供时)
构造单个 skill 引用。
参数:
- skill-id: 技能标识(官方预置如 "xlsx"/"pptx"/"docx"/"pdf",或自定义上传技能的 id)
- opts(可选)::type(默认 "anthropic" 表官方预置):version(默认不发送,由服务端取最新)
返回:{:type "anthropic" :skill_id "xlsx"}(含 :version 当提供时)(skills-container skills)构造 Skills 容器(放入 config :container)。
参数:
返回:{:skills [...]}
示例: {:container (skills-container [(skill "xlsx") (skill "pdf")]) :beta default-skills-beta :tools [(code-execution-tool)]}
构造 Skills 容器(放入 config :container)。
参数:
- skills: skill 引用列表(用 skill 构造,或自行给 map)
返回:{:skills [...]}
示例:
{:container (skills-container [(skill "xlsx") (skill "pdf")])
:beta default-skills-beta
:tools [(code-execution-tool)]}(text-document text)(text-document text
{:keys [title context citations? media-type]
:or {citations? true media-type "text/plain"}})构造启用引用(Citations)的纯文本 document 内容块,放入 user 消息的 content 向量。
模型在回答时会从该文档摘引,响应的 text 块带 :citations(含 cited_text / document_index / 字符区间等);用 provider.anthropic/extract-citations 提取。
参数:
返回: {:type "document" :source {:type "text" :media_type .. :data ..} :title .. :context .. :citations {:enabled true}}
示例: (text-document "地球绕太阳公转。" {:title "天文常识"})
构造启用引用(Citations)的纯文本 document 内容块,放入 user 消息的 content 向量。
模型在回答时会从该文档摘引,响应的 text 块带 :citations(含 cited_text /
document_index / 字符区间等);用 provider.anthropic/extract-citations 提取。
参数:
- text: 文档正文字符串
- opts(可选):
- :title 文档标题(出现在引用元数据里)
- :context 给模型的额外上下文(不被引用)
- :citations? 是否启用引用(默认 true)
- :media-type 默认 "text/plain"
返回:
{:type "document" :source {:type "text" :media_type .. :data ..}
:title .. :context .. :citations {:enabled true}}
示例:
(text-document "地球绕太阳公转。" {:title "天文常识"})(tool->schema {:keys [name description parameters]})将工具定义转换为 Anthropic tool 格式
参数:
返回: Anthropic 格式的 tool schema {:name "..." :description "..." :input_schema {...}}
示例: (tool->schema {:name :calculator :description "执行数学计算" :parameters {:type "object" :properties {:expr {:type "string"}}}})
将工具定义转换为 Anthropic tool 格式
参数:
- tool: 工具定义 map
{:name :keyword
:description "..."
:parameters {...}}
返回:
Anthropic 格式的 tool schema
{:name "..." :description "..." :input_schema {...}}
示例:
(tool->schema {:name :calculator
:description "执行数学计算"
:parameters {:type "object"
:properties {:expr {:type "string"}}}})(tools->schemas tools)批量转换工具定义为 Anthropic schemas
参数:
转换结果按 tools 列表有界缓存(同 schema.openai:ReAct 循环每轮免重转)。
返回: Anthropic schema 列表
批量转换工具定义为 Anthropic schemas
- 简单定义 {:name :description :parameters} -> 转为 input_schema 格式
- 已是 wire 格式(带 :type 的服务端工具、或已带 :input_schema)-> 原样透传
参数:
- tools: 工具定义列表
转换结果按 tools 列表有界缓存(同 schema.openai:ReAct 循环每轮免重转)。
返回:
Anthropic schema 列表(web-search-tool)(web-search-tool {:keys [max-uses allowed-domains blocked-domains
user-location]})构造 Anthropic 服务端 web_search 工具定义(已是 wire 格式,可直接放入 :tools)。
选项 opts(均可选):
:allowed-domains 与 :blocked-domains 互斥(由服务端校验)。
示例: (web-search-tool {:max-uses 5 :allowed-domains ["docs.anthropic.com"]}) ; => {:type "web_search_20250305" :name "web_search" :max_uses 5 ; :allowed_domains ["docs.anthropic.com"]}
构造 Anthropic 服务端 web_search 工具定义(已是 wire 格式,可直接放入 :tools)。
选项 opts(均可选):
- :max-uses 单次请求最多搜索次数(Integer)
- :allowed-domains 仅允许这些域名([String])
- :blocked-domains 屏蔽这些域名([String])
- :user-location 地理位置提示(map,如 {:type "approximate" :country "CN"})
:allowed-domains 与 :blocked-domains 互斥(由服务端校验)。
示例:
(web-search-tool {:max-uses 5 :allowed-domains ["docs.anthropic.com"]})
; => {:type "web_search_20250305" :name "web_search" :max_uses 5
; :allowed_domains ["docs.anthropic.com"]}Anthropic web_search 服务端工具的类型标识(截至当前 API 版本)
Anthropic web_search 服务端工具的类型标识(截至当前 API 版本)
(wire-tool? tool)工具是否已是 Anthropic wire 格式(带 :type 的服务端工具或带 :input_schema 的定义)
工具是否已是 Anthropic wire 格式(带 :type 的服务端工具或带 :input_schema 的定义)
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |