openai.types¶
Types for interacting with OpenAI models using Mirascope.
BaseAsyncStream
¶
Bases: Generic[BaseCallResponseChunkT, UserMessageParamT, AssistantMessageParamT, BaseToolT]
, ABC
A base class for async streaming responses from LLMs.
Source code in mirascope/base/types.py
BaseCallParams
¶
Bases: BaseModel
, Generic[BaseToolT]
The parameters with which to make a call.
Source code in mirascope/base/types.py
kwargs(tool_type=None, exclude=None)
¶
Returns all parameters for the call as a keyword arguments dictionary.
Source code in mirascope/base/types.py
BaseCallResponse
¶
Bases: BaseModel
, Generic[ResponseT, BaseToolT]
, ABC
A base abstract interface for LLM call responses.
Attributes:
Name | Type | Description |
---|---|---|
response |
ResponseT
|
The original response from whichever model response this wraps. |
Source code in mirascope/base/types.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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 |
|
content: str
abstractmethod
property
¶
Should return the string content of the response.
If there are multiple choices in a response, this method should select the 0th choice and return it's string content.
If there is no string content (e.g. when using tools), this method must return the empty string.
finish_reasons: Union[None, list[str]]
abstractmethod
property
¶
Should return the finish reasons of the response.
If there is no finish reason, this method must return None.
id: Optional[str]
abstractmethod
property
¶
Should return the id of the response.
input_tokens: Optional[Union[int, float]]
abstractmethod
property
¶
Should return the number of input tokens.
If there is no input_tokens, this method must return None.
message_param: Any
abstractmethod
property
¶
Returns the assistant's response as a message parameter.
model: Optional[str]
abstractmethod
property
¶
Should return the name of the response model.
output_tokens: Optional[Union[int, float]]
abstractmethod
property
¶
Should return the number of output tokens.
If there is no output_tokens, this method must return None.
tool: Optional[BaseToolT]
abstractmethod
property
¶
Returns the 0th tool for the 0th choice message.
tools: Optional[list[BaseToolT]]
abstractmethod
property
¶
Returns the tools for the 0th choice message.
usage: Any
abstractmethod
property
¶
Should return the usage of the response.
If there is no usage, this method must return None.
tool_message_params(tools_and_outputs)
abstractmethod
classmethod
¶
Returns the tool message parameters for tool call results.
BaseCallResponseChunk
¶
Bases: BaseModel
, Generic[ChunkT, BaseToolT]
, ABC
A base abstract interface for LLM streaming response chunks.
Attributes:
Name | Type | Description |
---|---|---|
response |
The original response chunk from whichever model response this wraps. |
Source code in mirascope/base/types.py
content: str
abstractmethod
property
¶
Should return the string content of the response chunk.
If there are multiple choices in a chunk, this method should select the 0th choice and return it's string content.
If there is no string content (e.g. when using tools), this method must return the empty string.
finish_reasons: Union[None, list[str]]
abstractmethod
property
¶
Should return the finish reasons of the response.
If there is no finish reason, this method must return None.
id: Optional[str]
abstractmethod
property
¶
Should return the id of the response.
input_tokens: Optional[Union[int, float]]
abstractmethod
property
¶
Should return the number of input tokens.
If there is no input_tokens, this method must return None.
model: Optional[str]
abstractmethod
property
¶
Should return the name of the response model.
output_tokens: Optional[Union[int, float]]
abstractmethod
property
¶
Should return the number of output tokens.
If there is no output_tokens, this method must return None.
usage: Any
abstractmethod
property
¶
Should return the usage of the response.
If there is no usage, this method must return None.
BaseEmbeddingParams
¶
Bases: BaseModel
The parameters with which to make an embedding.
Source code in mirascope/rag/types.py
kwargs()
¶
Returns all parameters for the embedder as a keyword arguments dictionary.
BaseEmbeddingResponse
¶
Bases: BaseModel
, Generic[ResponseT]
, ABC
A base abstract interface for LLM embedding responses.
Attributes:
Name | Type | Description |
---|---|---|
response |
ResponseT
|
The original response from whichever model response this wraps. |
Source code in mirascope/rag/types.py
embeddings: Optional[Union[list[list[float]], list[list[int]]]]
abstractmethod
property
¶
Should return the embedding of the response.
If there are multiple choices in a response, this method should select the 0th choice and return it's embedding.
BaseStream
¶
Bases: Generic[BaseCallResponseChunkT, UserMessageParamT, AssistantMessageParamT, BaseToolT]
, ABC
A base class for streaming responses from LLMs.
Source code in mirascope/base/types.py
BaseToolStream
¶
Bases: BaseModel
, Generic[BaseCallResponseChunkT, BaseToolT]
, ABC
A base class for streaming tools from response chunks.
Source code in mirascope/base/types.py
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
|
OpenAIAsyncStream
¶
Bases: BaseAsyncStream[OpenAICallResponseChunk, ChatCompletionUserMessageParam, ChatCompletionAssistantMessageParam, OpenAITool]
A class for streaming responses from OpenAI's API.
Source code in mirascope/openai/types.py
tool_message_params(tools_and_outputs)
classmethod
¶
Returns the tool message parameters for tool call results.
OpenAICallParams
¶
Bases: BaseCallParams[OpenAITool]
The parameters to use when calling the OpenAI API.
Source code in mirascope/openai/types.py
kwargs(tool_type=OpenAITool, exclude=None)
¶
Returns the keyword argument call parameters.
OpenAICallResponse
¶
Bases: BaseCallResponse[ChatCompletion, OpenAITool]
A convenience wrapper around the OpenAI ChatCompletion
response.
When using Mirascope's convenience wrappers to interact with OpenAI models via
OpenAICall
, responses using OpenAICall.call()
will return a
OpenAICallResponse
, whereby the implemented properties allow for simpler syntax
and a convenient developer experience.
Example:
from mirascope.openai import OpenAICall
class BookRecommender(OpenAICall):
prompt_template = "Please recommend a {genre} book"
genre: str
response = Bookrecommender(genre="fantasy").call()
print(response.content)
#> The Name of the Wind
print(response.message)
#> ChatCompletionMessage(content='The Name of the Wind', role='assistant',
# function_call=None, tool_calls=None)
print(response.choices)
#> [Choice(finish_reason='stop', index=0, logprobs=None,
# message=ChatCompletionMessage(content='The Name of the Wind', role='assistant',
# function_call=None, tool_calls=None))]
Source code in mirascope/openai/types.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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 |
|
choice: Choice
property
¶
Returns the 0th choice.
choices: list[Choice]
property
¶
Returns the array of chat completion choices.
content: str
property
¶
Returns the content of the chat completion for the 0th choice.
finish_reasons: list[str]
property
¶
Returns the finish reasons of the response.
id: str
property
¶
Returns the id of the response.
input_tokens: Optional[int]
property
¶
Returns the number of input tokens.
message: ChatCompletionMessage
property
¶
Returns the message of the chat completion for the 0th choice.
message_param: ChatCompletionAssistantMessageParam
property
¶
Returns the assistants's response as a message parameter.
model: str
property
¶
Returns the name of the response model.
output_tokens: Optional[int]
property
¶
Returns the number of output tokens.
tool: Optional[OpenAITool]
property
¶
Returns the 0th tool for the 0th choice message.
Raises:
Type | Description |
---|---|
ValidationError
|
if the tool call doesn't match the tool's schema. |
tool_calls: Optional[list[ChatCompletionMessageToolCall]]
property
¶
Returns the tool calls for the 0th choice message.
tools: Optional[list[OpenAITool]]
property
¶
Returns the tools for the 0th choice message.
Raises:
Type | Description |
---|---|
ValidationError
|
if a tool call doesn't match the tool's schema. |
usage: Optional[CompletionUsage]
property
¶
Returns the usage of the chat completion.
dump()
¶
Dumps the response to a dictionary.
OpenAICallResponseChunk
¶
Bases: BaseCallResponseChunk[ChatCompletionChunk, OpenAITool]
Convenience wrapper around chat completion streaming chunks.
When using Mirascope's convenience wrappers to interact with OpenAI models via
OpenAICall.stream
, responses will return an OpenAICallResponseChunk
, whereby
the implemented properties allow for simpler syntax and a convenient developer
experience.
Example:
from mirascope.openai import OpenAICall
class Math(OpenAICall):
prompt_template = "What is 1 + 2?"
content = ""
for chunk in Math().stream():
content += chunk.content
print(content)
#> 1
# 1 +
# 1 + 2
# 1 + 2 equals
# 1 + 2 equals
# 1 + 2 equals 3
# 1 + 2 equals 3.
Source code in mirascope/openai/types.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
|
choice: ChunkChoice
property
¶
Returns the 0th choice.
choices: list[ChunkChoice]
property
¶
Returns the array of chat completion choices.
content: str
property
¶
Returns the content for the 0th choice delta.
delta: Optional[ChoiceDelta]
property
¶
Returns the delta for the 0th choice.
finish_reasons: list[str]
property
¶
Returns the finish reasons of the response.
id: str
property
¶
Returns the id of the response.
input_tokens: Optional[int]
property
¶
Returns the number of input tokens.
model: str
property
¶
Returns the name of the response model.
output_tokens: Optional[int]
property
¶
Returns the number of output tokens.
tool_calls: Optional[list[ChoiceDeltaToolCall]]
property
¶
Returns the partial tool calls for the 0th choice message.
The first list[ChoiceDeltaToolCall]
will contain the name of the tool and
index, and subsequent list[ChoiceDeltaToolCall]
s will contain the arguments
which will be strings that need to be concatenated with future
list[ChoiceDeltaToolCall]
s to form a complete JSON tool calls. The last
list[ChoiceDeltaToolCall]
will be None indicating end of stream.
usage: Optional[CompletionUsage]
property
¶
Returns the usage of the chat completion.
OpenAIEmbeddingResponse
¶
Bases: BaseEmbeddingResponse[CreateEmbeddingResponse]
A convenience wrapper around the OpenAI CreateEmbeddingResponse
response.
Source code in mirascope/openai/types.py
embeddings: list[list[float]]
property
¶
Returns the raw embeddings.
OpenAIStream
¶
Bases: BaseStream[OpenAICallResponseChunk, ChatCompletionUserMessageParam, ChatCompletionAssistantMessageParam, OpenAITool]
A class for streaming responses from OpenAI's API.
Source code in mirascope/openai/types.py
tool_message_params(tools_and_outputs)
classmethod
¶
Returns the tool message parameters for tool call results.
OpenAITool
¶
Bases: BaseTool[ChatCompletionMessageToolCall]
A base class for easy use of tools with the OpenAI Chat client.
OpenAITool
internally handles the logic that allows you to use tools with simple
calls such as OpenAICallResponse.tool
or OpenAITool.fn
, as seen in the
examples below.
Example:
from mirascope.openai import OpenAICall, OpenAICallParams
def animal_matcher(fav_food: str, fav_color: str) -> str:
"""Tells you your most likely favorite animal from personality traits.
Args:
fav_food: your favorite food.
fav_color: your favorite color.
Returns:
The animal most likely to be your favorite based on traits.
"""
return "Your favorite animal is the best one, a frog."
class AnimalMatcher(OpenAICall):
prompt_template = """
Tell me my favorite animal if my favorite food is {food} and my
favorite color is {color}.
"""
food: str
color: str
call_params = OpenAICallParams(tools=[animal_matcher])
response = AnimalMatcher(food="pizza", color="red").call
tool = response.tool
print(tool.fn(**tool.args))
#> Your favorite animal is the best one, a frog.
Source code in mirascope/openai/tools.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 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 |
|
from_base_type(base_type)
classmethod
¶
Constructs a OpenAITool
type from a BaseType
type.
from_fn(fn)
classmethod
¶
from_model(model)
classmethod
¶
Constructs a OpenAITool
type from a BaseModel
type.
from_tool_call(tool_call, allow_partial=False)
classmethod
¶
Extracts an instance of the tool constructed from a tool call response.
Given ChatCompletionMessageToolCall
from an OpenAI chat completion response,
takes its function arguments and creates an OpenAITool
instance from it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool_call |
ChatCompletionMessageToolCall
|
The |
required |
allow_partial |
bool
|
Whether to allow partial JSON schemas. |
False
|
Returns:
Type | Description |
---|---|
OpenAITool
|
An instance of the tool constructed from the tool call. |
Raises:
Type | Description |
---|---|
ValidationError
|
if the tool call doesn't match the tool schema. |
Source code in mirascope/openai/tools.py
tool_schema()
classmethod
¶
Constructs a tool schema for use with the OpenAI Chat client.
A Mirascope OpenAITool
is deconstructed into a JSON schema, and relevant keys
are renamed to match the OpenAI ChatCompletionToolParam
schema used to make
function/tool calls in OpenAI API.
Returns:
Type | Description |
---|---|
ChatCompletionToolParam
|
The constructed |
Source code in mirascope/openai/tools.py
OpenAIToolStream
¶
Bases: BaseToolStream[OpenAICallResponseChunk, OpenAITool]
A base class for streaming tools from response chunks.
Source code in mirascope/openai/types.py
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 |
|
from_async_stream(async_stream, allow_partial=False)
async
classmethod
¶
Yields partial tools from the given stream of chunks asynchronously.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stream |
The async generator of chunks from which to stream tools. |
required | |
allow_partial |
Whether to allow partial tools. |
False
|
Raises:
Type | Description |
---|---|
RuntimeError
|
if a tool in the stream is of an unknown type. |
Source code in mirascope/openai/types.py
from_stream(stream, allow_partial=False)
classmethod
¶
Yields partial tools from the given stream of chunks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stream |
The generator of chunks from which to stream tools. |
required | |
allow_partial |
Whether to allow partial tools. |
False
|
Raises:
Type | Description |
---|---|
RuntimeError
|
if a tool in the stream is of an unknown type. |
Source code in mirascope/openai/types.py
partial(wrapped_class)
¶
Generate a new class with all attributes optionals.
Notes
This will wrap a class inheriting form BaseModel and will recursively convert all its attributes and its children's attributes to optionals.
Example: