Replies: 1 comment 2 replies
-
Does a hook handler function run concurrently with the main thread or things are handled sequentially? |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hooks
Hooks provide a powerful mechanism for intercepting and handling events during the completion and parsing process in the Instructor library. They allow you to add custom behavior, logging, or error handling at various stages of the API interaction.
Overview
The Hooks system in Instructor is based on the
Hooks
class, which manages event registration and emission. It supports several predefined events that correspond to different stages of the completion and parsing process.Supported Hook Events
completion:kwargs
This hook is emitted when completion arguments are provided. It receives all arguments passed to the completion function. These will contain the
model
,messages
,tools
, AFTER anyresponse_model
orvalidation_context
parameters have been converted to their respective values.completion:response
This hook is emitted when a completion response is received. It receives the raw response object from the completion API.
completion:error
This hook is emitted when an error occurs during completion before any retries are attempted and the response is parsed as a pydantic model.
parse:error
This hook is emitted when an error occurs during parsing of the response as a pydantic model. This can happen if the response is not valid or if the pydantic model is not compatible with the response.
completion:last_attempt
This hook is emitted when the last retry attempt is made.
Registering Hooks
You can register hooks using the
on
method of the Instructor client or aHooks
instance. Here's an example:Emitting Events
Events are automatically emitted by the Instructor library at appropriate times. You don't need to manually emit events in most cases.
Removing Hooks
You can remove a specific hook using the
off
method:Clearing Hooks
To remove all hooks for a specific event or all events:
Example: Logging and Debugging
Here's a comprehensive example demonstrating how to use hooks for logging and debugging:
This example demonstrates:
The hooks will log information at different stages of the process, helping with debugging and understanding the flow of data.
Best Practices
Error Handling: Always include error handling in your hook handlers to prevent exceptions from breaking the main execution flow. We will automatically warn if an exception is raised in a hook handler.
Performance: Keep hook handlers lightweight to avoid impacting the performance of the main application.
Modularity: Use hooks to separate concerns. For example, use hooks for logging, monitoring, or custom business logic without cluttering the main code.
Consistency: Use the same naming conventions and patterns across all your hooks for better maintainability.
Documentation: Document the purpose and expected input/output of each hook handler for easier collaboration and maintenance.
By leveraging hooks effectively, you can create more flexible, debuggable, and maintainable applications when working with the Instructor library and language models.
Beta Was this translation helpful? Give feedback.
All reactions