Skip to content

Commit 74ef305

Browse files
Merge pull request #26 from moorcheh-ai/feat/head-foot-prompt-ai
Feat: add header and footer prompt parameter
2 parents 524887e + a9ddb83 commit 74ef305

File tree

15 files changed

+284
-55
lines changed

15 files changed

+284
-55
lines changed

moorcheh_sdk/_legacy_client.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
import warnings
2-
from typing import TYPE_CHECKING, Any, Protocol
2+
from typing import TYPE_CHECKING, Protocol
3+
4+
from .types import (
5+
AnswerResponse,
6+
ChatHistoryItem,
7+
Document,
8+
DocumentDeleteResponse,
9+
DocumentGetResponse,
10+
DocumentUploadResponse,
11+
NamespaceCreateResponse,
12+
NamespaceListResponse,
13+
SearchResponse,
14+
Vector,
15+
VectorDeleteResponse,
16+
VectorUploadResponse,
17+
)
318

419
if TYPE_CHECKING:
520
from .resources import Answer, Documents, Namespaces, Search, Vectors
@@ -24,7 +39,7 @@ def create_namespace(
2439
namespace_name: str,
2540
type: str,
2641
vector_dimension: int | None = None,
27-
) -> dict[str, Any]:
42+
) -> NamespaceCreateResponse:
2843
"""
2944
[DEPRECATED] Creates a new namespace.
3045
@@ -73,7 +88,7 @@ def delete_namespace(self: "ClientProtocol", namespace_name: str) -> None:
7388
)
7489
self.namespaces.delete(namespace_name=namespace_name)
7590

76-
def list_namespaces(self: "ClientProtocol") -> dict[str, Any]:
91+
def list_namespaces(self: "ClientProtocol") -> NamespaceListResponse:
7792
"""
7893
[DEPRECATED] Lists all available namespaces.
7994
@@ -104,8 +119,8 @@ def list_namespaces(self: "ClientProtocol") -> dict[str, Any]:
104119
return self.namespaces.list()
105120

106121
def upload_documents(
107-
self: "ClientProtocol", namespace_name: str, documents: list[dict[str, Any]]
108-
) -> dict[str, Any]:
122+
self: "ClientProtocol", namespace_name: str, documents: list[Document]
123+
) -> DocumentUploadResponse:
109124
"""
110125
[DEPRECATED] Uploads text documents to a text-based namespace.
111126
@@ -138,7 +153,7 @@ def upload_documents(
138153

139154
def get_documents(
140155
self: "ClientProtocol", namespace_name: str, ids: list[str | int]
141-
) -> dict[str, Any]:
156+
) -> DocumentGetResponse:
142157
"""
143158
[DEPRECATED] Retrieves documents by their IDs from a text-based namespace.
144159
@@ -171,8 +186,8 @@ def get_documents(
171186
return self.documents.get(namespace_name=namespace_name, ids=ids)
172187

173188
def upload_vectors(
174-
self: "ClientProtocol", namespace_name: str, vectors: list[dict[str, Any]]
175-
) -> dict[str, Any]:
189+
self: "ClientProtocol", namespace_name: str, vectors: list[Vector]
190+
) -> VectorUploadResponse:
176191
"""
177192
[DEPRECATED] Uploads pre-computed vectors to a vector-based namespace.
178193
@@ -209,9 +224,9 @@ def search(
209224
namespaces: list[str],
210225
query: str | list[float],
211226
top_k: int = 10,
212-
threshold: float | None = 0.7,
227+
threshold: float | None = 0.25,
213228
kiosk_mode: bool = False,
214-
) -> dict[str, Any]:
229+
) -> SearchResponse:
215230
"""
216231
[DEPRECATED] Performs a semantic search across namespaces.
217232
@@ -221,7 +236,7 @@ def search(
221236
namespaces: A list of namespace names to search within.
222237
query: The search query (text string or vector list).
223238
top_k: The maximum number of results to return. Defaults to 10.
224-
threshold: Minimum similarity score (0-1). Defaults to 0.7.
239+
threshold: Minimum similarity score (0-1). Defaults to 0.25.
225240
kiosk_mode: Enable strict filtering. Defaults to False.
226241
227242
Returns:
@@ -260,9 +275,11 @@ def get_generative_answer(
260275
query: str,
261276
top_k: int = 5,
262277
ai_model: str = "anthropic.claude-sonnet-4-20250514-v1:0",
263-
chat_history: list[dict[str, Any]] | None = None,
278+
chat_history: list[ChatHistoryItem] | None = None,
264279
temperature: float = 0.7,
265-
) -> dict[str, Any]:
280+
header_prompt: str | None = None,
281+
footer_prompt: str | None = None,
282+
) -> AnswerResponse:
266283
"""
267284
[DEPRECATED] Generates an AI answer based on a search query within a namespace.
268285
@@ -278,6 +295,10 @@ def get_generative_answer(
278295
Each item should be a dictionary. Defaults to None.
279296
temperature: The sampling temperature for the LLM (0.0 to 1.0).
280297
Higher values introduce more randomness. Defaults to 0.7.
298+
header_prompt: Optional header prompt to be used in the LLM.
299+
Defaults to None.
300+
footer_prompt: Optional footer prompt to be used in the LLM.
301+
Defaults to None.
281302
282303
Returns:
283304
A dictionary containing the generated answer and metadata.
@@ -303,11 +324,13 @@ def get_generative_answer(
303324
ai_model=ai_model,
304325
chat_history=chat_history,
305326
temperature=temperature,
327+
header_prompt=header_prompt,
328+
footer_prompt=footer_prompt,
306329
)
307330

308331
def delete_documents(
309332
self: "ClientProtocol", namespace_name: str, ids: list[str | int]
310-
) -> dict[str, Any]:
333+
) -> DocumentDeleteResponse:
311334
"""
312335
[DEPRECATED] Deletes documents by their IDs from a text-based namespace.
313336
@@ -337,7 +360,7 @@ def delete_documents(
337360

338361
def delete_vectors(
339362
self: "ClientProtocol", namespace_name: str, ids: list[str | int]
340-
) -> dict[str, Any]:
363+
) -> VectorDeleteResponse:
341364
"""
342365
[DEPRECATED] Deletes vectors by their IDs from a vector-based namespace.
343366

moorcheh_sdk/resources/answer.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from typing import Any
1+
from typing import Any, cast
22

33
from ..exceptions import APIError, InvalidInputError
4-
from ..types import JSON
4+
from ..types import AnswerResponse, ChatHistoryItem
55
from ..utils.logging import setup_logging
66
from .base import BaseResource
77

@@ -15,9 +15,11 @@ def generate(
1515
query: str,
1616
top_k: int = 5,
1717
ai_model: str = "anthropic.claude-sonnet-4-20250514-v1:0",
18-
chat_history: list[JSON] | None = None,
18+
chat_history: list[ChatHistoryItem] | None = None,
1919
temperature: float = 0.7,
20-
) -> JSON:
20+
header_prompt: str | None = None,
21+
footer_prompt: str | None = None,
22+
) -> AnswerResponse:
2123
"""
2224
Generates an AI answer based on a search query within a namespace.
2325
@@ -34,6 +36,10 @@ def generate(
3436
Each item should be a dictionary. Defaults to None.
3537
temperature: The sampling temperature for the LLM (0.0 to 1.0).
3638
Higher values introduce more randomness. Defaults to 0.7.
39+
header_prompt: Optional header prompt to be used in the LLM.
40+
Defaults to None.
41+
footer_prompt: Optional footer prompt to be used in the LLM.
42+
Defaults to None.
3743
3844
Returns:
3945
A dictionary containing the generated answer and metadata.
@@ -80,6 +86,8 @@ def generate(
8086
"aiModel": ai_model,
8187
"chatHistory": chat_history if chat_history is not None else [],
8288
"temperature": temperature,
89+
"headerPrompt": header_prompt if header_prompt is not None else "",
90+
"footerPrompt": footer_prompt if footer_prompt is not None else "",
8391
}
8492
logger.debug(f"Generative answer payload: {payload}")
8593

@@ -97,4 +105,4 @@ def generate(
97105
"Successfully received generative answer. Model used:"
98106
f" {response_data.get('model')}"
99107
)
100-
return response_data
108+
return cast(AnswerResponse, response_data)

moorcheh_sdk/resources/documents.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
from typing import cast
2+
13
from ..exceptions import APIError, InvalidInputError
2-
from ..types import JSON
4+
from ..types import (
5+
Document,
6+
DocumentDeleteResponse,
7+
DocumentGetResponse,
8+
DocumentUploadResponse,
9+
)
310
from ..utils.constants import INVALID_ID_CHARS
411
from ..utils.logging import setup_logging
512
from .base import BaseResource
@@ -8,7 +15,9 @@
815

916

1017
class Documents(BaseResource):
11-
def upload(self, namespace_name: str, documents: list[JSON]) -> JSON:
18+
def upload(
19+
self, namespace_name: str, documents: list[Document]
20+
) -> DocumentUploadResponse:
1221
"""
1322
Uploads text documents to a text-based namespace.
1423
@@ -94,9 +103,9 @@ def upload(self, namespace_name: str, documents: list[JSON]) -> JSON:
94103
f"Successfully queued {submitted_count} documents for upload to"
95104
f" '{namespace_name}'. Status: {response_data.get('status')}"
96105
)
97-
return response_data
106+
return cast(DocumentUploadResponse, response_data)
98107

99-
def get(self, namespace_name: str, ids: list[str | int]) -> JSON:
108+
def get(self, namespace_name: str, ids: list[str | int]) -> DocumentGetResponse:
100109
"""
101110
Retrieves documents by their IDs from a text-based namespace.
102111
@@ -163,9 +172,11 @@ def get(self, namespace_name: str, ids: list[str | int]) -> JSON:
163172
f"Successfully retrieved {doc_count} document(s) from namespace"
164173
f" '{namespace_name}'."
165174
)
166-
return response_data
175+
return cast(DocumentGetResponse, response_data)
167176

168-
def delete(self, namespace_name: str, ids: list[str | int]) -> JSON:
177+
def delete(
178+
self, namespace_name: str, ids: list[str | int]
179+
) -> DocumentDeleteResponse:
169180
"""
170181
Deletes documents by their IDs from a text-based namespace.
171182
@@ -235,4 +246,4 @@ def delete(self, namespace_name: str, ids: list[str | int]) -> JSON:
235246
logger.warning(
236247
f"Delete documents encountered errors: {response_data.get('errors')}"
237248
)
238-
return response_data
249+
return cast(DocumentDeleteResponse, response_data)

moorcheh_sdk/resources/namespaces.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from typing import Any
1+
from typing import Any, cast
22

33
from ..exceptions import APIError, InvalidInputError
4-
from ..types import JSON
4+
from ..types import NamespaceCreateResponse, NamespaceListResponse
55
from ..utils.logging import setup_logging
66
from .base import BaseResource
77

@@ -11,7 +11,7 @@
1111
class Namespaces(BaseResource):
1212
def create(
1313
self, namespace_name: str, type: str, vector_dimension: int | None = None
14-
) -> JSON:
14+
) -> NamespaceCreateResponse:
1515
"""
1616
Creates a new namespace.
1717
@@ -77,7 +77,7 @@ def create(
7777
f"Successfully created namespace '{namespace_name}'. Response:"
7878
f" {response_data}"
7979
)
80-
return response_data
80+
return cast(NamespaceCreateResponse, response_data)
8181

8282
def delete(self, namespace_name: str) -> None:
8383
"""
@@ -103,7 +103,7 @@ def delete(self, namespace_name: str) -> None:
103103
# Log success after the request confirms it (no exception raised)
104104
logger.info(f"Namespace '{namespace_name}' deleted successfully.")
105105

106-
def list(self) -> JSON:
106+
def list(self) -> NamespaceListResponse:
107107
"""
108108
Lists all available namespaces.
109109
@@ -150,4 +150,4 @@ def list(self) -> JSON:
150150
count = len(response_data.get("namespaces", []))
151151
logger.info(f"Successfully listed {count} namespace(s).")
152152
logger.debug(f"List namespaces response data: {response_data}")
153-
return response_data
153+
return cast(NamespaceListResponse, response_data)

moorcheh_sdk/resources/search.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from typing import Any
1+
from typing import Any, cast
22

33
from ..exceptions import APIError, InvalidInputError
4-
from ..types import JSON
4+
from ..types import SearchResponse
55
from ..utils.logging import setup_logging
66
from .base import BaseResource
77

@@ -14,17 +14,17 @@ def query(
1414
namespaces: list[str],
1515
query: str | list[float],
1616
top_k: int = 10,
17-
threshold: float | None = 0.7,
17+
threshold: float | None = 0.25,
1818
kiosk_mode: bool = False,
19-
) -> JSON:
19+
) -> SearchResponse:
2020
"""
2121
Performs semantic search across namespaces.
2222
2323
Args:
2424
namespaces: A list of namespace names to search within.
2525
query: The search query (text string or vector list).
2626
top_k: The maximum number of results to return. Defaults to 10.
27-
threshold: Minimum similarity score (0-1). Defaults to 0.7.
27+
threshold: Minimum similarity score (0-1). Defaults to 0.25.
2828
kiosk_mode: Enable strict filtering. Defaults to False.
2929
3030
Returns:
@@ -77,16 +77,14 @@ def query(
7777

7878
payload: dict[str, Any] = {
7979
"namespaces": namespaces,
80-
"query": query, # Keep original query type
80+
"query": query,
8181
"top_k": top_k,
8282
"kiosk_mode": kiosk_mode,
8383
}
84-
if threshold is not None:
84+
if kiosk_mode and threshold is not None:
8585
payload["threshold"] = threshold
8686

87-
logger.debug(
88-
f"Search payload: {payload}"
89-
) # Be careful logging query if it could be sensitive/large
87+
logger.debug(f"Search payload: {payload}")
9088

9189
response_data = self._client._request(
9290
method="POST", endpoint="/search", json_data=payload, expected_status=200
@@ -102,7 +100,5 @@ def query(
102100
f"Search completed successfully. Found {result_count} result(s). Execution"
103101
f" time: {exec_time}s."
104102
)
105-
logger.debug(
106-
f"Search results: {response_data}"
107-
) # Log full results at debug level
108-
return response_data
103+
logger.debug(f"Search results: {response_data}")
104+
return cast(SearchResponse, response_data)

moorcheh_sdk/resources/vectors.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
from typing import cast
2+
13
from ..exceptions import APIError, InvalidInputError
2-
from ..types import JSON
4+
from ..types import Vector, VectorDeleteResponse, VectorUploadResponse
35
from ..utils.logging import setup_logging
46
from .base import BaseResource
57

68
logger = setup_logging(__name__)
79

810

911
class Vectors(BaseResource):
10-
def upload(self, namespace_name: str, vectors: list[JSON]) -> JSON:
12+
def upload(
13+
self, namespace_name: str, vectors: list[Vector]
14+
) -> VectorUploadResponse:
1115
"""
1216
Uploads pre-computed vectors to a vector-based namespace.
1317
@@ -100,9 +104,9 @@ def upload(self, namespace_name: str, vectors: list[JSON]) -> JSON:
100104
logger.warning(
101105
f"Upload vectors encountered errors: {response_data.get('errors')}"
102106
)
103-
return response_data
107+
return cast(VectorUploadResponse, response_data)
104108

105-
def delete(self, namespace_name: str, ids: list[str | int]) -> JSON:
109+
def delete(self, namespace_name: str, ids: list[str | int]) -> VectorDeleteResponse:
106110
"""
107111
Deletes vectors by their IDs from a vector-based namespace.
108112
@@ -173,4 +177,4 @@ def delete(self, namespace_name: str, ids: list[str | int]) -> JSON:
173177
logger.warning(
174178
f"Delete vectors encountered errors: {response_data.get('errors')}"
175179
)
176-
return response_data
180+
return cast(VectorDeleteResponse, response_data)

0 commit comments

Comments
 (0)