Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ai-json-resp] Extract JSON from LLM, Validate with Schema, Ensure Valid JSON, Auto-Retry #1236

Merged
merged 26 commits into from
Sep 3, 2024

Conversation

Suchun-sv
Copy link
Contributor

Ⅰ. Describe what this PR did

[ai-json-resp] LLM响应结构化插件,用于根据默认或用户配置的Json Schema对AI的响应进行结构化。和ai proxy插件协作,除自定义的json schema和网关服务外无需额外配置参数。

Ⅱ. Does this pull request fix one issue?

fixes #1214

@codecov-commenter
Copy link

codecov-commenter commented Aug 21, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 44.31%. Comparing base (ef31e09) to head (6e53578).
Report is 77 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1236      +/-   ##
==========================================
+ Coverage   35.91%   44.31%   +8.40%     
==========================================
  Files          69       75       +6     
  Lines       11576     9823    -1753     
==========================================
+ Hits         4157     4353     +196     
+ Misses       7104     5142    -1962     
- Partials      315      328      +13     

see 90 files with indirect coverage changes

plugins/wasm-go/extensions/ai-json-resp/README.md Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-json-resp/main.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-json-resp/main.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-json-resp/main.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-json-resp/main.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-json-resp/main.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-json-resp/util.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-json-resp/main.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-json-resp/main.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-json-resp/main.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-json-resp/main.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-json-resp/main.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-json-resp/main.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-json-resp/main.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-json-resp/main.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-json-resp/main.go Outdated Show resolved Hide resolved
@CH3CHO
Copy link
Collaborator

CH3CHO commented Aug 27, 2024

image

plugins/wasm-go/extensions/ai-json-resp/main.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-json-resp/main.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-json-resp/main.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-json-resp/main.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-json-resp/main.go Outdated Show resolved Hide resolved
ReqBody: body,
}

config.serviceClient.Post(bufferRB.Path, bufferRB.ReqHeader, bufferRB.ReqBody,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里会不会失败,失败会怎么样?要不要continue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里有点没明白,您是指Post失败吗?那还会进入回调函数吗?如果会进入那应该已经写了处理?如果不会进入,那好像没法子处理r?

plugins/wasm-go/extensions/ai-json-resp/main.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-json-resp/main.go Outdated Show resolved Hide resolved
plugins/wasm-go/extensions/ai-json-resp/main.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@CH3CHO CH3CHO left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@johnlanni johnlanni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

配置需要做一些精简

| enableSwagger | bool | optional | false | 是否启用 Swagger 协议进行验证 |
| enableOas3 | bool | optional | true | 是否启用 Oas3 协议进行验证 |
| jsonSchemaMaxDepth | int | optional | 5 | 由于插件性能限制,为防止递归耗尽资源,需指定支持的 JSON Schema 最大深度,超过该深度的 Schema 不会用于验证响应|
| rejectOnDepthExceeded | bool | optional | false | 若为 true,当 JSON Schema 的深度超过 maxJsonSchemaDepth 时,插件将直接返回错误;若为 false,则将仍将 Json Schema 用于LLM提示构造并继续执行 |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jsonSchemaMaxDepth 和 rejectOnDepthExceeded 这两个配置我建议去掉,最大深度在代码里有个默认值就行,文档里提示一下即可,默认行为就是超过最大深度,不用进行校验,但会生成提示词

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok,我修改一下代码和文档

Copy link
Collaborator

@johnlanni johnlanni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@johnlanni johnlanni merged commit ffc0c09 into alibaba:main Sep 3, 2024
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Higress AI 格式化文档/代码生成
4 participants