Skip to content

Commit

Permalink
Better API for answer_json_schema (#2253)
Browse files Browse the repository at this point in the history
  • Loading branch information
lferran authored Jun 18, 2024
1 parent 1fe9d0a commit 2066758
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
10 changes: 9 additions & 1 deletion nucliadb/tests/nucliadb/integration/test_ask.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ async def test_ask(

context = [{"author": "USER", "text": "query"}]
resp = await nucliadb_reader.post(
f"/kb/{knowledgebox}/ask", json={"query": "query", "context": context}
f"/kb/{knowledgebox}/ask",
json={
"query": "query",
"context": context,
"answer_json_schema": {
"type": "object",
"properties": {"answer": {"type": "string"}, "confidence": {"type": "number"}},
},
},
)
assert resp.status_code == 200

Expand Down
57 changes: 27 additions & 30 deletions nucliadb_models/src/nucliadb_models/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import json
from dataclasses import dataclass
from datetime import datetime
from enum import Enum
Expand All @@ -41,36 +40,34 @@

_T = TypeVar("_T")

ANSWER_JSON_SCHEMA_EXAMPLE = json.dumps(
{
"name": "structred_response",
"description": "Structured response with custom fields",
"parameters": {
"type": "object",
"properties": {
"answer": {
ANSWER_JSON_SCHEMA_EXAMPLE = {
"name": "structred_response",
"description": "Structured response with custom fields",
"parameters": {
"type": "object",
"properties": {
"answer": {
"type": "string",
"description": "Text responding to the user's query with the given context.",
},
"confidence": {
"type": "integer",
"description": "The confidence level of the response, on a scale from 0 to 5.",
"minimum": 0,
"maximum": 5,
},
"machinery_mentioned": {
"type": "array",
"items": {
"type": "string",
"description": "Text responding to the user's query with the given context.",
},
"confidence": {
"type": "integer",
"description": "The confidence level of the response, on a scale from 0 to 5.",
"minimum": 0,
"maximum": 5,
},
"machinery_mentioned": {
"type": "array",
"items": {
"type": "string",
"description": "A list of machinery mentioned in the response, if any. Use machine IDs if possible.",
},
"description": "Optional field listing any machinery mentioned in the response.",
"description": "A list of machinery mentioned in the response, if any. Use machine IDs if possible.",
},
"description": "Optional field listing any machinery mentioned in the response.",
},
"required": ["answer", "confidence"],
},
}
)
"required": ["answer", "confidence"],
},
}


class ModelParamDefaults:
Expand Down Expand Up @@ -862,7 +859,7 @@ class ChatModel(BaseModel):
default=False,
description="If set to true, the response will be in markdown format",
)
json_schema: Optional[str] = Field(
json_schema: Optional[Dict[str, Any]] = Field(
default=None,
description="The JSON schema to use for the generative model answers",
)
Expand Down Expand Up @@ -1360,13 +1357,13 @@ def validate_facets(facets):


class AskRequest(ChatRequest):
answer_json_schema: Optional[str] = Field(
answer_json_schema: Optional[Dict[str, Any]] = Field(
default=None,
title="Answer JSON schema",
description="""Desired JSON schema of the desired LLM answer.
This schema is passed to the LLM so that it answers in a scructured format following the schema. If not provided, textual response is returned.
Note that when using this parameter, the answer in the generative response will not be returned in chunks, the whole response text will be returned instead.
Using this feature also disables the `citations` parameter.
Using this feature also disables the `citations` parameter. For maximal accuracy, please include a `description` for each field of the schema.
""",
examples=[ANSWER_JSON_SCHEMA_EXAMPLE],
)
Expand Down
4 changes: 4 additions & 0 deletions nucliadb_sdk/tests/test_ask.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ def test_ask_on_kb(docs_dataset, sdk: nucliadb_sdk.NucliaDB):
],
# Control the number of AI tokens used for every request
max_tokens=MaxTokens(context=100, answer=50),
answer_json_schema={
"type": "object",
"properties": {"answer": {"type": "string"}, "confidence": {"type": "number"}},
},
)
assert result.learning_id == "00"
assert result.answer == "valid answer to"
Expand Down

3 comments on commit 2066758

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

Benchmark

Benchmark suite Current: 2066758 Previous: 0d03d9f Ratio
tests/search/unit/search/test_fetch.py::test_highligh_error 2862.281292587338 iter/sec (stddev: 0.00000737867123248218) 2841.0684406726436 iter/sec (stddev: 0.000004954958228416619) 0.99

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

Benchmark

Benchmark suite Current: 2066758 Previous: 0d03d9f Ratio
tests/search/unit/search/test_fetch.py::test_highligh_error 2952.375455248076 iter/sec (stddev: 0.0000040903067701365775) 2841.0684406726436 iter/sec (stddev: 0.000004954958228416619) 0.96

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

Benchmark

Benchmark suite Current: 2066758 Previous: 0d03d9f Ratio
tests/search/unit/search/test_fetch.py::test_highligh_error 2987.757351280803 iter/sec (stddev: 0.00000774759685575742) 2841.0684406726436 iter/sec (stddev: 0.000004954958228416619) 0.95

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.