Liking cljdoc? Tell your friends :D

im.ttalk.agent.provider.stream.openai

OpenAI 流式响应处理

处理 OpenAI API 的 SSE 流式响应,累积文本和工具调用。 构建的响应格式与非流式调用兼容,并支持统一响应转换。

使用示例:

(require '[im.ttalk.agent.provider.stream.openai :as stream])

;; 处理流式响应块 (let [[new-state token-data] (stream/process-chunk chunk state)] (when token-data (print (:token token-data))))

;; 构建最终响应(支持统一格式) (stream/build-response final-state :id "xxx" :model "gpt-4")

OpenAI 流式响应处理

处理 OpenAI API 的 SSE 流式响应,累积文本和工具调用。
构建的响应格式与非流式调用兼容,并支持统一响应转换。

使用示例:

(require '[im.ttalk.agent.provider.stream.openai :as stream])

;; 处理流式响应块
(let [[new-state token-data] (stream/process-chunk chunk state)]
  (when token-data
    (print (:token token-data))))

;; 构建最终响应(支持统一格式)
(stream/build-response final-state :id "xxx" :model "gpt-4")
raw docstring

build-responseclj

(build-response state & {:keys [id model]})

从流式状态构建最终响应(OpenAI 原始格式)

参数:

  • state: 最终累积状态
  • opts: 可选参数
    • :id 响应 ID
    • :model 模型名称

返回: 与非流式调用兼容的 OpenAI 响应格式

示例: (build-response final-state :id "chatcmpl-xxx" :model "gpt-4")

从流式状态构建最终响应(OpenAI 原始格式)

参数:
- state: 最终累积状态
- opts:  可选参数
  - :id    响应 ID
  - :model 模型名称

返回:
与非流式调用兼容的 OpenAI 响应格式

示例:
(build-response final-state :id "chatcmpl-xxx" :model "gpt-4")
sourceraw docstring

make-initial-stateclj

(make-initial-state)

创建流式处理的初始状态

返回: 状态 map {:accumulated StringBuilder :index 0 :tool-calls-acc {} :role nil}

创建流式处理的初始状态

返回:
状态 map {:accumulated StringBuilder :index 0 :tool-calls-acc {} :role nil}
sourceraw docstring

make-stream-processorclj

(make-stream-processor)

创建流处理器

返回: 处理器 map,包含:

  • :process-fn 处理函数 (fn [chunk state] -> [new-state token-data])
  • :get-id 获取响应 ID 的函数
  • :get-model 获取模型名称的函数

示例: (let [{:keys [process-fn get-id get-model]} (make-stream-processor)] ;; 处理流 (let [final-state (reduce ...)] (build-response final-state :id (get-id) :model (get-model))))

创建流处理器

返回:
处理器 map,包含:
- :process-fn   处理函数 (fn [chunk state] -> [new-state token-data])
- :get-id       获取响应 ID 的函数
- :get-model    获取模型名称的函数

示例:
(let [{:keys [process-fn get-id get-model]} (make-stream-processor)]
  ;; 处理流
  (let [final-state (reduce ...)]
    (build-response final-state :id (get-id) :model (get-model))))
sourceraw docstring

normalize-responseclj

(normalize-response state & {:keys [id model usage]})

从流式状态构建统一格式响应

参数:

  • state: 最终累积状态
  • opts: 可选参数
    • :id 响应 ID
    • :model 模型名称
    • :usage 使用情况(流式响应通常没有 usage)

返回: 统一响应格式: {:text "..." :tool-calls [{:id :name :args}] :finish-reason :stop | :tool-use :provider :openai ...}

示例: (normalize-response final-state :id "chatcmpl-xxx" :model "gpt-4")

从流式状态构建统一格式响应

参数:
- state: 最终累积状态
- opts:  可选参数
  - :id    响应 ID
  - :model 模型名称
  - :usage 使用情况(流式响应通常没有 usage)

返回:
统一响应格式:
{:text "..."
 :tool-calls [{:id :name :args}]
 :finish-reason :stop | :tool-use
 :provider :openai
 ...}

示例:
(normalize-response final-state :id "chatcmpl-xxx" :model "gpt-4")
sourceraw docstring

parse-sse-lineclj

(parse-sse-line line)

解析 SSE 数据行

参数:

  • line: SSE 行字符串(如 "data: {...}")

返回: 解析后的 JSON 对象或 nil

示例: (parse-sse-line "data: {"id": "123"}") ; => {:id "123"}

解析 SSE 数据行

参数:
- line: SSE 行字符串(如 "data: {...}")

返回:
解析后的 JSON 对象或 nil

示例:
(parse-sse-line "data: {\"id\": \"123\"}")
; => {:id "123"}
sourceraw docstring

process-chunkclj

(process-chunk chunk state)

处理流式响应块

参数:

  • chunk: 解析后的 SSE 块(JSON 对象)
  • state: 当前累积状态

返回: [更新后的状态, token-data 或 nil]

token-data 格式: {:token "..." :index n}

示例: (let [[new-state token] (process-chunk chunk state)] (when token (print (:token token))))

处理流式响应块

参数:
- chunk: 解析后的 SSE 块(JSON 对象)
- state: 当前累积状态

返回:
[更新后的状态, token-data 或 nil]

token-data 格式:
{:token "..." :index n}

示例:
(let [[new-state token] (process-chunk chunk state)]
  (when token
    (print (:token token))))
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close