Skip to content

Commit 272c8ff

Browse files
authored
Merge pull request lightspeed-core#599 from tisnik/lcore-731-better-docstring-for-utils-module-
LCORE-731: better docstring for utils module
2 parents 42532c0 + 4f7a1be commit 272c8ff

2 files changed

Lines changed: 98 additions & 5 deletions

File tree

src/utils/endpoints.py

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,13 @@ def validate_conversation_ownership(
6262

6363

6464
def check_configuration_loaded(config: AppConfig) -> None:
65-
"""Check that configuration is loaded and raise exception when it is not."""
65+
"""
66+
Ensure the application configuration object is present.
67+
68+
Raises:
69+
HTTPException: HTTP 500 Internal Server Error with detail `{"response":
70+
"Configuration is not loaded"}` when `config` is None.
71+
"""
6672
if config is None:
6773
raise HTTPException(
6874
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
@@ -71,7 +77,31 @@ def check_configuration_loaded(config: AppConfig) -> None:
7177

7278

7379
def get_system_prompt(query_request: QueryRequest, config: AppConfig) -> str:
74-
"""Get the system prompt: the provided one, configured one, or default one."""
80+
"""
81+
Resolve which system prompt to use for a query.
82+
83+
Precedence:
84+
1. If the request includes `system_prompt`, that value is returned (highest
85+
precedence).
86+
2. Else if the application configuration provides a customization
87+
`system_prompt`, that value is returned.
88+
3. Otherwise the module default `constants.DEFAULT_SYSTEM_PROMPT` is
89+
returned (lowest precedence).
90+
91+
If configuration disables per-request system prompts
92+
(config.customization.disable_query_system_prompt) and the incoming
93+
`query_request` contains a `system_prompt`, an HTTP 422 Unprocessable
94+
Entity is raised instructing the client to remove the field.
95+
96+
Parameters:
97+
query_request (QueryRequest): The incoming query payload; may contain a
98+
per-request `system_prompt`.
99+
config (AppConfig): Application configuration which may include
100+
customization flags and a default `system_prompt`.
101+
102+
Returns:
103+
str: The resolved system prompt to apply to the request.
104+
"""
75105
system_prompt_disabled = (
76106
config.customization is not None
77107
and config.customization.disable_query_system_prompt
@@ -171,7 +201,46 @@ async def get_agent(
171201
conversation_id: str | None,
172202
no_tools: bool = False,
173203
) -> tuple[AsyncAgent, str, str]:
174-
"""Get existing agent or create a new one with session persistence."""
204+
"""
205+
Create or reuse an AsyncAgent with session persistence.
206+
207+
Return the agent, conversation and session IDs.
208+
209+
If a conversation_id is provided, the function attempts to retrieve the
210+
existing agent and, on success, rebinds a newly created agent instance to
211+
that conversation (deleting the temporary/orphan agent) and returns the
212+
first existing session_id for the conversation. If no conversation_id is
213+
provided or the existing agent cannot be retrieved, a new agent and session
214+
are created.
215+
216+
Parameters:
217+
model_id (str): Identifier of the model to instantiate the agent with.
218+
system_prompt (str): Instructions/system prompt to initialize the agent with.
219+
220+
available_input_shields (list[str]): Input shields to apply to the
221+
agent; empty list used if None/empty.
222+
223+
available_output_shields (list[str]): Output shields to apply to the
224+
agent; empty list used if None/empty.
225+
226+
conversation_id (str | None): If provided, attempt to reuse the agent
227+
for this conversation; otherwise a new conversation_id is created.
228+
229+
no_tools (bool): When True, disables tool parsing for the agent (uses no tool parser).
230+
231+
Returns:
232+
tuple[AsyncAgent, str, str]: A tuple of (agent, conversation_id, session_id).
233+
234+
Raises:
235+
HTTPException: Raises HTTP 404 Not Found if an attempt to reuse a
236+
conversation succeeds in retrieving the agent but no sessions are found
237+
for that conversation.
238+
239+
Side effects:
240+
- May delete an orphan agent when rebinding a newly created agent to an
241+
existing conversation_id.
242+
- Initializes the agent and may create a new session.
243+
"""
175244
existing_agent_id = None
176245
if conversation_id:
177246
with suppress(ValueError):

src/utils/llama_stack_version.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ class InvalidLlamaStackVersionException(Exception):
2222
async def check_llama_stack_version(
2323
client: AsyncLlamaStackClient,
2424
) -> None:
25-
"""Check if the Llama Stack version is supported by the LCS."""
25+
"""
26+
Verify the connected Llama Stack's version is within the supported range.
27+
28+
This coroutine fetches the Llama Stack version from the
29+
provided client and validates it against the configured minimal
30+
and maximal supported versions. Raises
31+
InvalidLlamaStackVersionException if the detected version is
32+
outside the supported range.
33+
"""
2634
version_info = await client.inspect.version()
2735
compare_versions(
2836
version_info.version,
@@ -32,7 +40,23 @@ async def check_llama_stack_version(
3240

3341

3442
def compare_versions(version_info: str, minimal: str, maximal: str) -> None:
35-
"""Compare current Llama Stack version with minimal and maximal allowed versions."""
43+
"""
44+
Validate that a semver version string is within the inclusive [minimal, maximal] range.
45+
46+
Parses `version_info`, `minimal`, and `maximal` with semver.Version.parse
47+
and compares them. If the current version is lower than `minimal` or
48+
higher than `maximal`, an InvalidLlamaStackVersionException is raised.
49+
50+
Parameters:
51+
version_info (str): Semver version string to validate (must be
52+
parseable by semver.Version.parse).
53+
minimal (str): Minimum allowed semver version (inclusive).
54+
maximal (str): Maximum allowed semver version (inclusive).
55+
56+
Raises:
57+
InvalidLlamaStackVersionException: If `version_info` is outside the
58+
inclusive range defined by `minimal` and `maximal`.
59+
"""
3660
current_version = Version.parse(version_info)
3761
minimal_version = Version.parse(minimal)
3862
maximal_version = Version.parse(maximal)

0 commit comments

Comments
 (0)