From b236f3327b6e2b48f2fd1255d284b1c57c99cb90 Mon Sep 17 00:00:00 2001 From: Eric BREHAULT Date: Thu, 30 May 2024 08:47:45 +0200 Subject: [PATCH] rename chat to ask (#78) * rename chat to ask * fix changelog --- CHANGELOG.md | 6 ++--- docs/03-kb.md | 2 +- docs/05-search.md | 8 +++--- nuclia/sdk/agent.py | 6 ++--- nuclia/sdk/search.py | 38 +++++++++++++---------------- nuclia/tests/test_kb/test_search.py | 6 ++--- 6 files changed, 31 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16cf297..e3123bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,10 @@ # Changelog -## 2.1.1 (unreleased) +## 3.0.0 (unreleased) +### Breaking change -- Nothing changed yet. - +- Rename `chat()` to `ask()` ## 2.1.0 (2024-05-17) diff --git a/docs/03-kb.md b/docs/03-kb.md index 72375f1..53b978c 100644 --- a/docs/03-kb.md +++ b/docs/03-kb.md @@ -178,7 +178,7 @@ You can get the logs of a Knowledge Box. There are different types of logs: - `NEW`: resource creation events, - `PROCESSED`: processing events, - `MODIFIED`: resource modification events, -- `CHAT`: asked questions and returned answers on the `/chat` endpoint, +- `CHAT`: asked questions and returned answers on the `/ask` endpoint, - `SEARCH`: queries sent to `/search` or `/find`, - `FEEDBACK`: user feedbacks on answers diff --git a/docs/05-search.md b/docs/05-search.md index 72fd7d9..2b99197 100644 --- a/docs/05-search.md +++ b/docs/05-search.md @@ -44,7 +44,7 @@ Based on a `find` request, Nuclia uses a generative AI to answer the question ba - CLI: ```bash - nuclia kb search chat --query="My question" + nuclia kb search ask --query="My question" ``` - SDK: @@ -52,12 +52,12 @@ Based on a `find` request, Nuclia uses a generative AI to answer the question ba ```python from nuclia import sdk search = sdk.NucliaSearch() - search.chat(query="My question") + search.ask(query="My question") ``` ## Filtering -Any endpoint that involves search (`search`, `find` and `chat`) also support more advanced filtering expressions. Expressions can have one of the following operators: +Any endpoint that involves search (`search`, `find` and `ask`) also support more advanced filtering expressions. Expressions can have one of the following operators: - `all`: this is the default. Will make search return results containing all specified filter labels. - `any`: returns results containing at least one of the labels. @@ -81,7 +81,7 @@ Here are some examples: from nucliadb_models.search import Filter search = sdk.NucliaSearch() - search.chat( + search.ask( query="My question", filters=[Filter(any=['/classification.labels/region/Europe','/classification.labels/region/Asia'])], ) diff --git a/nuclia/sdk/agent.py b/nuclia/sdk/agent.py index 850ed71..e8b0829 100644 --- a/nuclia/sdk/agent.py +++ b/nuclia/sdk/agent.py @@ -1,6 +1,6 @@ from typing import List, Optional -from nucliadb_models.search import ChatRequest +from nucliadb_models.search import AskRequest class Agent: @@ -15,8 +15,8 @@ def __init__(self, prompt: str, filters: List[str]): self.search = NucliaSearch() def ask(self, text: str) -> str: - chat_req = ChatRequest(query=text, prompt=self.prompt, filters=self.filters) - answer = self.search.chat(query=chat_req) + ask_req = AskRequest(query=text, prompt=self.prompt, filters=self.filters) + answer = self.search.ask(query=ask_req) return answer.answer.decode() diff --git a/nuclia/sdk/search.py b/nuclia/sdk/search.py index 466b9ee..68cd334 100644 --- a/nuclia/sdk/search.py +++ b/nuclia/sdk/search.py @@ -6,7 +6,6 @@ from nucliadb_models.search import ( AskRequest, AskResponseItem, - ChatRequest, Filter, FindRequest, KnowledgeboxFindResults, @@ -25,7 +24,7 @@ @dataclass -class ChatAnswer: +class AskAnswer: answer: bytes learning_id: str relations_result: Optional[Relations] @@ -122,17 +121,17 @@ def find( return ndb.ndb.find(req, kbid=ndb.kbid) @kb - def chat( + def ask( self, *, - query: Union[str, dict, ChatRequest], + query: Union[str, dict, AskRequest], filters: Optional[Union[List[str], List[Filter]]] = None, **kwargs, ): """ Answer a question. - See https://docs.nuclia.dev/docs/api#tag/Search/operation/Chat_Knowledge_Box_kb__kbid__chat_post + See https://docs.nuclia.dev/docs/api#tag/Search/operation/Ask_Knowledge_Box_kb__kbid__ask_post """ ndb: NucliaDBClient = kwargs["ndb"] if isinstance(query, str): @@ -146,15 +145,14 @@ def chat( except ValidationError as exc: print(exc) sys.exit(1) - elif isinstance(query, ChatRequest): - # Convert ChatRequest to AskRequest - req = AskRequest.parse_obj(query.dict()) + elif isinstance(query, AskRequest): + req = query else: - raise ValueError("Invalid query type. Must be str, dict or ChatRequest.") + raise ValueError("Invalid query type. Must be str, dict or AskRequest.") ask_response: SyncAskResponse = ndb.ndb.ask(kbid=ndb.kbid, content=req) - # Convert to ChatAnswer - result = ChatAnswer( + + result = AskAnswer( answer=ask_response.answer.encode(), learning_id=ask_response.learning_id, relations_result=ask_response.relations, @@ -252,10 +250,10 @@ async def find( return await ndb.ndb.find(req, kbid=ndb.kbid) @kb - async def chat( + async def ask( self, *, - query: Union[str, dict, ChatRequest], + query: Union[str, dict, AskRequest], filters: Optional[List[str]] = None, timeout: int = 100, **kwargs, @@ -263,7 +261,7 @@ async def chat( """ Answer a question. - See https://docs.nuclia.dev/docs/api#tag/Search/operation/Chat_Knowledge_Box_kb__kbid__chat_post + See https://docs.nuclia.dev/docs/api#tag/Search/operation/Ask_Knowledge_Box_kb__kbid__ask_post """ ndb: NucliaDBClient = kwargs["ndb"] if isinstance(query, str): @@ -277,14 +275,12 @@ async def chat( except ValidationError as exc: print(exc) sys.exit(1) - elif isinstance(query, ChatRequest): - # Convert ChatRequest to AskRequest - req = AskRequest.parse_obj(query.dict()) + elif isinstance(query, AskRequest): + req = query else: - raise ValueError("Invalid query type. Must be str, dict or ChatRequest.") + raise ValueError("Invalid query type. Must be str, dict or AskRequest.") ask_stream_response = await ndb.ask(req, timeout=timeout) - # Parse the stream response and convert to ChatAnswer - result = ChatAnswer( + result = AskAnswer( answer=b"", learning_id=ask_stream_response.headers.get("NUCLIA-LEARNING-ID", ""), relations_result=None, @@ -317,6 +313,6 @@ async def chat( pass else: # pragma: no cover warnings.warn( - f"Unknown chat stream item type: {ask_response_item.type}" + f"Unknown ask stream item type: {ask_response_item.type}" ) return result diff --git a/nuclia/tests/test_kb/test_search.py b/nuclia/tests/test_kb/test_search.py index bc9a97e..2f8f360 100644 --- a/nuclia/tests/test_kb/test_search.py +++ b/nuclia/tests/test_kb/test_search.py @@ -24,14 +24,14 @@ def test_find_object(testing_config): assert "Lamarr Lesson plan.pdf" in titles -def test_chat(testing_config): +def test_ask(testing_config): if IS_PROD: assert True return search = NucliaSearch() - results = search.chat(query="Who is hedy Lamarr?") + results = search.ask(query="Who is hedy Lamarr?") answer = results.answer.decode() - print("Chat answer: ", answer) + print("Answer: ", answer) assert "Lamarr" in answer