Queries and Chats
Abstract Queries
AbstractQuery
Bases: ABC
Abstract Class for Delphyne Queries.
The type parameter T
indicates the type of parsed query answers.
A more featureful subclass is provided in the standard library
(Query
), which uses reflection for convenience.
Answer Modes
Queries are allowed to define multiple answer modes
(AnswerMode
), each mode being possibly associated with
different settings and with a different parser.
Source code in src/delphyne/core/queries.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
|
generate_prompt
abstractmethod
generate_prompt(
*,
kind: Literal["system", "instance", "feedback"] | str,
mode: AnswerMode,
params: dict[str, object],
extra_args: dict[str, object] | None = None,
env: AbstractTemplatesManager | None,
) -> str
Generate a prompt message for the query.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kind
|
Literal['system', 'instance', 'feedback'] | str
|
Kind of prompt to generate. Standard prompt kinds are "system", "instance", or "feedback" but others can be supported (within or outside the standard library). |
required |
mode
|
AnswerMode
|
Answer mode selected for the query. |
required |
params
|
dict[str, object]
|
Query hyperparameters. |
required |
extra_args
|
dict[str, object] | None
|
Additional arguments to pass to the template. |
None
|
env
|
AbstractTemplatesManager | None
|
Template manager used to load Jinja templates. Exceptions may be raised when it is needed but not provided. |
required |
Source code in src/delphyne/core/queries.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
|
serialize_args
serialize_args() -> dict[str, object]
Serialize the query arguments as a dictionary of JSON values.
Source code in src/delphyne/core/queries.py
203 204 205 206 207 |
|
parse_instance
classmethod
parse_instance(args: dict[str, object]) -> Self
Parse a query instance from a dictionary of serialized arguments.
Source code in src/delphyne/core/queries.py
209 210 211 212 213 214 215 |
|
answer_type
abstractmethod
answer_type() -> TypeAnnot[T] | NoTypeInfo
Return the answer type for the query, or NoTypeInfo()
if this
information is not available.
Source code in src/delphyne/core/queries.py
217 218 219 220 221 222 223 |
|
query_modes
abstractmethod
query_modes() -> Sequence[AnswerMode]
Return the sequence of available answer modes.
Source code in src/delphyne/core/queries.py
225 226 227 228 229 230 |
|
query_prefix
query_prefix() -> AnswerPrefix | None
Return the chat history featured in the query, if any.
This is useful to emulate conversational agents by issuing a
query repeatedly, passing it the whole, updated conversation
history every time (see interact
).
Source code in src/delphyne/core/queries.py
232 233 234 235 236 237 238 239 240 |
|
query_settings
query_settings(mode: AnswerMode) -> QuerySettings
Return the settings associated with the query.
Source code in src/delphyne/core/queries.py
242 243 244 245 246 |
|
query_name
query_name() -> str
Return the name of the query (i.e., the name of the associated class).
Source code in src/delphyne/core/queries.py
248 249 250 251 252 253 254 |
|
default_tags
default_tags() -> Sequence[str]
Return a default set of tags to associate with spaces induced by the query.
These tags can be overriden (see SpaceBuilder
).
Source code in src/delphyne/core/queries.py
256 257 258 259 260 261 262 263 |
|
parse_answer
abstractmethod
parse_answer(answer: Answer) -> T | ParseError
Parse a query answer.
Source code in src/delphyne/core/queries.py
265 266 267 268 269 270 |
|
finite_answer_set
finite_answer_set() -> Sequence[Answer] | None
For queries with a finite set of possible answers, return this
set. Otherwise, return None
.
Demonstration tests can use a special #<val>
hint to select
the first answer with content <val>
from this set.
Example uses include classification queries (see classify
)
where a distribution of answers is extracted from LLM logits,
flag queries...
Source code in src/delphyne/core/queries.py
272 273 274 275 276 277 278 279 280 281 282 283 284 |
|
default_answer
default_answer() -> Answer | None
Return a default answer for the query, if any.
Default answers are used to answer queries in demonstration
tests when no answer is provided in the queries
section and no
applicable hint is available.
Source code in src/delphyne/core/queries.py
286 287 288 289 290 291 292 293 294 |
|
ParseError
dataclass
Bases: Error
, Exception
Parse Error.
Can be used as an exception or a returned value.
Source code in src/delphyne/core/queries.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
QuerySettings
dataclass
Settings associated with a query.
These settings can accessed by prompting policies so as to make appropriate requests to LLMs.
Attributes:
Name | Type | Description |
---|---|---|
structured_output |
StructuredOutputSettings | None
|
Settings for structured output, or |
tools |
ToolSettings | None
|
Settings for tool calls, or |
Source code in src/delphyne/core/queries.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
StructuredOutputSettings
dataclass
Settings for LLM structured output.
Attributes:
Name | Type | Description |
---|---|---|
type |
TypeAnnot[Any] | NoTypeInfo
|
Expected type for the output, from which a schema can be derived if provided. |
Source code in src/delphyne/core/queries.py
39 40 41 42 43 44 45 46 47 48 49 |
|
ToolSettings
dataclass
Tool call settings.
Attributes:
Name | Type | Description |
---|---|---|
tool_types |
Sequence[type[Any]]
|
Nonempty sequence of available tools. All tools must be classes, although more constraints can be put by specific queries and prompting policies. |
force_tool_call |
bool
|
If True, oracles are informed that a tool call must be made. |
Source code in src/delphyne/core/queries.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
AbstractTemplatesManager
Bases: ABC
Source code in src/delphyne/core/queries.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
prompt
abstractmethod
prompt(
*,
query_name: str,
prompt_kind: Literal["system", "instance"] | str,
template_args: dict[str, Any],
default_template: str | None = None,
) -> str
Render a prompt message using a template.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
query_name
|
str
|
The name of the query for which the prompt is built. Used to determine the template file name (e.g. "{query_name}.{prompt_kind}.jinja"). |
required |
prompt_kind
|
Literal['system', 'instance'] | str
|
The kind of prompt (e.g. "system" or "instance") that is being rendered, used to determine the name of the template file to use. |
required |
template_args
|
dict[str, Any]
|
A dictionary of arguments to pass to the template. It must not contain key "data", which is reserved for the data loaded from the data directories. |
required |
default_template
|
str | None
|
If provided, this template will be used if no template file is found for the given query name and kind instead of raising an error. |
None
|
Raises:
Type | Description |
---|---|
TemplateFileMissing
|
template file not found. |
TemplateError
|
error raised while rendering the template. |
Source code in src/delphyne/core/queries.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
TemplateError
dataclass
Bases: Exception
Wrapper for template-related exceptions.
Source code in src/delphyne/core/queries.py
134 135 136 137 138 139 140 141 |
|
TemplateFileMissing
dataclass
Bases: Exception
Exception raised when a template file is missing.
This exception should only be raised when a top-level template file
is missing. If an include
statement fails within a template, a
TemplateError
exception should be raised instead.
Source code in src/delphyne/core/queries.py
144 145 146 147 148 149 150 151 152 153 154 |
|
Chat Histories
AnswerPrefix
AnswerPrefix: Sequence[AnswerPrefixElement]
An LLM chat history, to be passed to a query as an answer prefix (see
Query.query_prefix
).
AnswerPrefixElement
AnswerPrefixElement: OracleMessage | FeedbackMessage | ToolResult
LLM chat history element.
OracleMessage
dataclass
Messge containing an oracle answer.
Source code in src/delphyne/core/chats.py
12 13 14 15 16 17 18 19 |
|
FeedbackMessage
dataclass
Message containing user feedback.
Source code in src/delphyne/core/chats.py
22 23 24 25 26 27 28 29 30 31 |
|
ToolResult
dataclass
User message containing the result of a tool call previously initiated by an LLM.
Source code in src/delphyne/core/chats.py
34 35 36 37 38 39 40 41 42 43 |
|