Documentation
¶
Index ¶
- type CustomChatModel
- func (c *CustomChatModel) BindForcedTools(tools []*schema.ToolInfo) error
- func (c *CustomChatModel) BindTools(tools []*schema.ToolInfo) error
- func (c *CustomChatModel) Generate(ctx context.Context, in []*schema.Message, opts ...model.Option) (*schema.Message, error)
- func (c *CustomChatModel) GetType() string
- func (c *CustomChatModel) IsCallbacksEnabled() bool
- func (c *CustomChatModel) Stream(ctx context.Context, in []*schema.Message, opts ...model.Option) (*schema.StreamReader[*schema.Message], error)
- func (c *CustomChatModel) WithTools(tools []*schema.ToolInfo) (model.ToolCallingChatModel, error)
- type CustomRoundTripper
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CustomChatModel ¶
type CustomChatModel struct {
// contains filtered or unexported fields
}
CustomChatModel wraps the eino-ext OpenAI model with custom tool schema handling. It provides a compatibility layer that ensures proper JSON schema formatting for OpenAI's function calling feature. This wrapper addresses cases where tool schemas might have missing or empty properties that would cause API errors.
func NewCustomChatModel ¶
func NewCustomChatModel(ctx context.Context, config *einoopenai.ChatModelConfig) (*CustomChatModel, error)
NewCustomChatModel creates a new custom OpenAI chat model. It wraps the standard eino-ext OpenAI model with additional request preprocessing to ensure compatibility with OpenAI's API requirements, particularly for function calling and tool schemas.
Parameters:
- ctx: Context for the operation
- config: Configuration for the OpenAI model including API key, model name, and parameters
Returns:
- *CustomChatModel: A wrapped OpenAI model with enhanced compatibility
- error: Returns an error if model creation fails
The custom model automatically:
- Ensures function parameter schemas have properties fields
- Fixes missing or empty properties in tool schemas
- Maintains compatibility with OpenAI's function calling requirements
func (*CustomChatModel) BindForcedTools ¶
func (c *CustomChatModel) BindForcedTools(tools []*schema.ToolInfo) error
BindForcedTools implements model.ToolCallingChatModel interface. It binds tools to the current model instance in forced mode, ensuring the model will always use one of the provided tools.
Parameters:
- tools: A slice of tool definitions to bind to the model
Returns:
- error: Returns an error if tool binding fails
func (*CustomChatModel) BindTools ¶
func (c *CustomChatModel) BindTools(tools []*schema.ToolInfo) error
BindTools implements model.ToolCallingChatModel interface. It binds tools to the current model instance, modifying it in place rather than creating a new instance.
Parameters:
- tools: A slice of tool definitions to bind to the model
Returns:
- error: Returns an error if tool binding fails
func (*CustomChatModel) Generate ¶
func (c *CustomChatModel) Generate(ctx context.Context, in []*schema.Message, opts ...model.Option) (*schema.Message, error)
Generate implements model.ChatModel interface. It generates a single response from the OpenAI model based on the input messages.
Parameters:
- ctx: Context for the operation, supporting cancellation and deadlines
- in: The conversation history as a slice of messages
- opts: Optional configuration options for the generation
Returns:
- *schema.Message: The generated response message
- error: Any error that occurred during generation
func (*CustomChatModel) GetType ¶
func (c *CustomChatModel) GetType() string
GetType implements model.ChatModel interface. It returns the type identifier for this model implementation.
Returns:
- string: Returns "CustomOpenAI" as the model type identifier
func (*CustomChatModel) IsCallbacksEnabled ¶
func (c *CustomChatModel) IsCallbacksEnabled() bool
IsCallbacksEnabled implements model.ChatModel interface. It indicates whether this model supports callbacks for monitoring and tracking purposes.
Returns:
- bool: Returns the callback enabled status from the wrapped model
func (*CustomChatModel) Stream ¶
func (c *CustomChatModel) Stream(ctx context.Context, in []*schema.Message, opts ...model.Option) (*schema.StreamReader[*schema.Message], error)
Stream implements model.ChatModel interface. It generates a streaming response from the OpenAI model, allowing incremental processing of the model's output as it's generated.
Parameters:
- ctx: Context for the operation, supporting cancellation and deadlines
- in: The conversation history as a slice of messages
- opts: Optional configuration options for the generation
Returns:
- *schema.StreamReader[*schema.Message]: A reader for the streaming response
- error: Any error that occurred during stream setup
func (*CustomChatModel) WithTools ¶
func (c *CustomChatModel) WithTools(tools []*schema.ToolInfo) (model.ToolCallingChatModel, error)
WithTools implements model.ToolCallingChatModel interface. It creates a new model instance with the specified tools available for function calling. The original model instance remains unchanged.
Parameters:
- tools: A slice of tool definitions that the model can use
Returns:
- model.ToolCallingChatModel: A new model instance with tools enabled
- error: Returns an error if tool binding fails
type CustomRoundTripper ¶
type CustomRoundTripper struct {
// contains filtered or unexported fields
}
CustomRoundTripper intercepts HTTP requests to fix OpenAI function schemas. It acts as middleware that modifies outgoing requests to ensure that function/tool schemas are properly formatted according to OpenAI's requirements. This is particularly important for handling edge cases where tool schemas might have missing or empty properties fields.
func (*CustomRoundTripper) RoundTrip ¶
RoundTrip implements http.RoundTripper to intercept and fix OpenAI requests. It preprocesses outgoing requests to the OpenAI API to ensure tool/function schemas meet the API's requirements.
Parameters:
- req: The HTTP request to be sent to the OpenAI API
Returns:
- *http.Response: The response from the OpenAI API
- error: Any error that occurred during the request
The method performs the following fixes:
- Ensures function parameter schemas of type "object" have a properties field
- Adds empty properties object if missing to prevent API validation errors