Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 55 additions & 79 deletions ai/liii/pi/ai/api/openai-responses/msg.scm
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,12 @@
) ;define

(define (%model-supports-images? model)
(if (not (pair? model))
#t
(let ((input
(or (and (assoc "input" model) (cdr (assoc "input" model)))
(and (assoc 'input model) (cdr (assoc 'input model)))
#f
) ;or
) ;input
) ;
(cond ((vector? input) (member "image" (vector->list input)))
((pair? input) (member "image" input))
(else #t)
) ;cond
) ;let
) ;if
(let ((input (%val 'input model #f)))
(cond ((vector? input) (member "image" (vector->list input)))
((pair? input) (member "image" input))
(else #t)
) ;cond
) ;let
) ;define

(define (%normalize-json-keys-to-strings data)
Expand Down Expand Up @@ -249,61 +240,59 @@
) ;let*
) ;define

;; 仅在含图片且模型支持时使用多模态数组;纯文本保留原有表示。
(define (%image-content-parts blocks model)
(and (%model-supports-images? model)
(any %is-image-block? blocks)
(list->vector (filter (lambda (part) part) (map %input-part blocks)))
) ;and
) ;define

(define (%content-list content)
(cond ((string? content) (list (pi-text-block content)))
((vector? content) (vector->list content))
((pair? content) content)
(else '())
) ;cond
) ;define

(define (%text-part? b)
(or (string? b) (%assistant-text-block? b))
) ;define

(define (%input-part block)
(cond ((%is-image-block? block) (%image-block->responses-item block))
((%text-part? block)
(let ((text (%block->text block)))
(and (not (string-null? text))
(list (cons "type" "input_text") (cons "text" text))
) ;and
) ;let
) ;
(else #f)
) ;cond
) ;define

;; user 消息:文本走 input_text,图片走 input_image
(define (%convert-user-msg msg opt-model)
(let* ((raw-c (if (pi-user-message? msg)
(pi-user-message-content msg)
(%val 'content msg '())
) ;if
) ;raw-c
(c-list (cond ((string? raw-c) (list raw-c))
((vector? raw-c) (vector->list raw-c))
((pair? raw-c) raw-c)
(else '())
) ;cond
) ;c-list
(text-parts
(filter
(lambda (b)
(or (string? b)
(pi-text-block? b)
(and (pair? b) (equal? (%val 'type b "") "text"))
) ;or
) ;lambda
c-list
) ;filter
) ;text-parts
(text-acc (%msg-text-content text-parts))
(img-blks (filter %is-image-block? c-list))
(c-list (%content-list raw-c))
(text-acc (%msg-text-content (filter %text-part? c-list)))
(parts
(or (%image-content-parts c-list opt-model)
(and (not (string-null? text-acc))
(vector (list (cons "type" "input_text") (cons "text" text-acc)))
) ;and
) ;or
) ;parts
) ;
(cond
((and (pair? img-blks) (or (not opt-model) (%model-supports-images? opt-model)))
(let ((parts
(append
(if (string-null? text-acc)
'()
(list (list (cons "type" "input_text") (cons "text" text-acc)))
) ;if
(map %image-block->responses-item img-blks)
) ;append
) ;parts
) ;
(list (cons "type" "message")
(cons "role" "user")
(cons "content" (list->vector parts))
) ;list
) ;let
) ;
((string-null? text-acc) #f)
(else
(list (cons "type" "message")
(cons "role" "user")
(cons "content"
(list->vector (list (list (cons "type" "input_text") (cons "text" text-acc))))
) ;cons
) ;list
) ;else
) ;cond
(and parts
(list (cons "type" "message") (cons "role" "user") (cons "content" parts))
) ;and
) ;let*
) ;define

Expand All @@ -314,37 +303,24 @@
(%val 'content tmsg '())
) ;if
) ;raw-c
(c-list
(cond ((vector? raw-c) (vector->list raw-c))
((pair? raw-c) raw-c)
((string? raw-c) (list (pi-text-block raw-c)))
(else '())
) ;cond
) ;c-list
(c-list (%content-list raw-c))
(call-id (if (pi-tool-result-message? tmsg)
(pi-tool-result-message-tool-call-id tmsg)
(%val 'toolCallId tmsg "")
) ;if
) ;call-id
(text-blks
(filter
(lambda (b) (or (pi-text-block? b) (equal? (%val 'type b "") "text")))
c-list
) ;filter
) ;text-blks
(img-blks (filter %is-image-block? c-list))
(text-res (%msg-text-content text-blks))
(text-res (%msg-text-content (filter %assistant-text-block? c-list)))
(eff-text
(cond
((not (string-null? (string-trim-both text-res))) text-res)
((pair? img-blks) "(see attached image)")
((any %is-image-block? c-list) "(see attached image)")
(else "")
) ;cond
) ;eff-text
) ;
(list (cons "type" "function_call_output")
(cons "call_id" call-id)
(cons "output" eff-text)
(cons "output" (or (%image-content-parts c-list opt-model) eff-text))
) ;list
) ;let*
) ;define
Expand Down
14 changes: 14 additions & 0 deletions ai/tests/liii/pi/ai/api/openai-responses/msg/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# `(liii pi ai api openai-responses msg)`

将会话转换为 Responses 输入并组装请求体。

- [`responses-build-messages`](responses-build-messages-test.scm):将会话消息转换为 input 向量。
- [`responses-cache-optimization`](responses-cache-optimization-test.scm):回归:跨轮追加时缓存前缀保持稳定。
- [`responses-clamp-prompt-cache-key`](responses-clamp-prompt-cache-key-test.scm):最多保留前 64 个字符;#f 原样返回。
- [`responses-resolve-prompt-cache-key`](responses-resolve-prompt-cache-key-test.scm):对官方端点返回会话缓存键;第三方端点不启用。
- [`responses-build-request-body`](responses-build-request-body-test.scm):返回请求体;仅正整数限额对应 max_output_tokens 字段。
- [`responses-build-tool-declarations`](responses-build-tool-declarations-test.scm):返回按输入顺序排列的扁平 function 声明,保留参数 schema。

```bash
gf test ai/tests/liii/pi/ai/api/openai-responses/msg
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
;; responses-build-request-body
;; responses-build-tool-declarations
;; Responses API 请求体构建:input 扁平 item、工具声明扁平、系统提示置于 input[0]。
;;
;; 语法
;; ----
;; (responses-build-messages messages system-instruction [model])
;;
;; 参数
;; ----
;; messages 是会话消息列表;system-instruction 是系统提示或 #f;model 可指定输入能力。
;;
;; 返回值
;; ----
;; 返回用于请求体 input 字段的向量,保留各轮消息顺序。

(import (liii check)
(liii string)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
;; responses-build-request-body
;; 返回请求体;仅正整数限额对应 max_output_tokens 字段。
;;
;; 语法
;; ----
;; (responses-build-request-body model-id input stream? tools max-tokens)
;;
;; 参数
;; ----
;; 输入模型名、消息向量、流式标志、工具列表与输出限额。
;;
;; 返回值
;; ----
;; 返回请求体;仅正整数限额对应 max_output_tokens 字段。

(import (scheme base) (liii check) (liii pi ai api openai-responses msg))

(check-set-mode! 'report-failed)

(let ((body (responses-build-request-body "m" #() #f '() 0)))
(check (assoc "max_output_tokens" body) => #f)
(check (cdr (assoc "input" body)) => #())
) ;let
(check (cdr (assoc "max_output_tokens" (responses-build-request-body "m" #() #t '() 1))
) ;cdr
=>
1
) ;check

(check-report)
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
;; responses-build-tool-declarations
;; 返回按输入顺序排列的扁平 function 声明,保留参数 schema。
;;
;; 语法
;; ----
;; (responses-build-tool-declarations defs)
;;
;; 参数
;; ----
;; defs 是内部工具定义列表。
;;
;; 返回值
;; ----
;; 返回按输入顺序排列的扁平 function 声明,保留参数 schema。

(import (scheme base)
(liii check)
(liii pi ai api openai-responses msg)
(liii pi ai tools definition)
) ;import

(check-set-mode! 'report-failed)

(let* ((params '(("type" . "object")))
(tool (pi-tool-definition "read" "读取" params))
(decl (car (responses-build-tool-declarations (list tool))))
) ;
(check (cdr (assoc "parameters" decl)) => params)
(check (assoc "function" decl) => #f)
) ;let*

(check-report)
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
;; responses-cache-optimization
;; 缓存优化:系统提示必须置于 input[0](纳入可缓存前缀),而非顶层 instructions。
;;
;; 语法
;; ----
;; (responses-build-request-body model-id input stream? tools max-tokens [cache-key])
;;
;; 参数
;; ----
;; 提供输入消息、工具、token 限额及可选的缓存键。
;;
;; 返回值
;; ----
;; 返回 Responses API 请求体,说明系统提示在 input、而不是顶层 instructions。
;; OpenAI GPT-5/6 的 prompt cache 仅按序列化 input + tools 计算前缀;
;; 顶层 instructions 不计入缓存(GPT-6.2+ 还会排在 input 之后,命中边界外),
;; 把它放 instructions 会导致缓存命中率趋近 0(OmniRoute PR #1346 的 0% 命中复现)。
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
;; responses-clamp-prompt-cache-key
;; 最多保留前 64 个字符;#f 原样返回。
;;
;; 语法
;; ----
;; (responses-clamp-prompt-cache-key key)
;;
;; 参数
;; ----
;; key 是缓存键或 #f。
;;
;; 返回值
;; ----
;; 最多保留前 64 个字符;#f 原样返回。

(import (scheme base) (liii check) (liii pi ai api openai-responses msg))

(check-set-mode! 'report-failed)

(check (responses-clamp-prompt-cache-key #f) => #f)
(let ((prefix (make-string 64 #\a)))
(check (responses-clamp-prompt-cache-key (string-append prefix "x")) => prefix)
) ;let

(check-report)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
;; responses-resolve-prompt-cache-key
;; 对官方端点返回会话缓存键;第三方端点不启用。
;;
;; 语法
;; ----
;; (responses-resolve-prompt-cache-key base-url session-id)
;;
;; 参数
;; ----
;; base-url 是服务地址,session-id 是会话标识。
;;
;; 返回值
;; ----
;; 对官方端点返回会话缓存键;第三方端点不启用。

(import (scheme base) (liii check) (liii pi ai api openai-responses msg))

(check-set-mode! 'report-failed)

(check (responses-resolve-prompt-cache-key "https://api.openai.com/v1" "abc")
=>
"abc"
) ;check
(check (responses-resolve-prompt-cache-key "https://example.org/v1" "abc")
=>
#f
) ;check

(check-report)
13 changes: 13 additions & 0 deletions ai/tests/liii/pi/ai/api/openai-responses/request/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# `(liii pi ai api openai-responses request)`

构造 Responses HTTP 请求并处理非流式响应。

- [`responses-request`](responses-request-test.scm):通过本地 HTTP stub 检查端点响应及最终图文请求。
- [`responses-build-endpoint`](responses-build-endpoint-test.scm):返回 Responses 请求地址,不重复追加路径。
- [`responses-build-headers`](responses-build-headers-test.scm):返回请求头列表,自定义头中的 ${API_KEY} 替换为当前值。
- [`responses-build-proxy-alist`](responses-build-proxy-alist-test.scm):无代理返回空列表;否则为 HTTP 和 HTTPS 设置同一代理。
- [`responses-build-tool-declarations-from-names`](responses-build-tool-declarations-from-names-test.scm):返回已解析的工具声明,跳过未找到的名称。

```bash
gf test ai/tests/liii/pi/ai/api/openai-responses/request
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
;; responses-build-endpoint
;; 返回 Responses 请求地址,不重复追加路径。
;;
;; 语法
;; ----
;; (responses-build-endpoint base-url)
;;
;; 参数
;; ----
;; base-url 可为服务根地址或完整端点。
;;
;; 返回值
;; ----
;; 返回 Responses 请求地址,不重复追加路径。

(import (scheme base) (liii check) (liii pi ai api openai-responses request))

(check-set-mode! 'report-failed)

(check (responses-build-endpoint "https://example.org")
=>
"https://example.org/v1/responses"
) ;check
(check (responses-build-endpoint "https://example.org/v1/responses")
=>
"https://example.org/v1/responses"
) ;check

(check-report)
Loading
Loading