// CategorizeQuery predicts the query's intended categories and returns the list of ids.
CategorizeQuery predicts the query's intended categories and returns the list of ids.
// CategorizeQuery predicts the query ' s intended categories and returns the list of ids .
func (q *queryClassifierClient) CategorizeQuery(ctx context.Context, query string) ([]string, error) {
	ctx, span := otel.Tracer("").Start(ctx, "queryclassifier.queryClassifierClient_CategorizeQuery")
	defer span.End()
	predictionInstance, err := structpb.NewStruct(map[string]interface{}{
		"content":  query,
		"mimeType": "text/plain",
	})
	if err != nil {
		return nil, fmt.Errorf("failed to initialize prediction instance: %w", err)
	}
	resp, err := q.predictionClient.Predict(ctx, &aiplatformpb.PredictRequest{
		Endpoint: q.categoryClassificationEndpoint,
		Instances: []*structpb.Value{
			structpb.NewStructValue(predictionInstance),
		},
	})
	if err != nil {
		return nil, fmt.Errorf("failed to predict: %w", err)
	}
	predictions := resp.GetPredictions()
	if len(predictions) == 0 {
		return nil, nil
	}
	var categoryIDs []string
	fields := predictions[0].GetStructValue().GetFields()
	displayNameValues := fields["displayNames"].GetListValue().Values
	confidenceValues := fields["confidences"].GetListValue().Values
	for i, categoryIDValue := range displayNameValues {
		categoryID := categoryIDValue.GetStringValue()
		if confidenceValues[i].GetNumberValue() >= scoreThreshold {
			categoryIDs = append(categoryIDs, categoryID)
		}
	}
	span.SetAttributes(attribute.StringSlice("categoryIds", categoryIDs))
	return categoryIDs, nil
}
func ( q * queryClassifierClient ) CategorizeQuery ( ctx context . Context , query string ) ( [ ] string , error ) { ctx , span := otel . Tracer ( " " ) . Start ( ctx , " " ) defer span . End ( ) predictionInstance , err := structpb . NewStruct ( map [ string ] interface { } { " " : query , " " : " " , } ) if err != nil { return nil , fmt . Errorf ( " " , err ) } resp , err := q . predictionClient . Predict ( ctx , & aiplatformpb . PredictRequest { Endpoint : q . categoryClassificationEndpoint , Instances : [ ] * structpb . Value { structpb . NewStructValue ( predictionInstance ) , } , } ) if err != nil { return nil , fmt . Errorf ( " " , err ) } predictions := resp . GetPredictions ( ) if len ( predictions ) == 0 { return nil , nil } var categoryIDs [ ] string fields := predictions [ 0 ] . GetStructValue ( ) . GetFields ( ) displayNameValues := fields [ " " ] . GetListValue ( ) . Values confidenceValues := fields [ " " ] . GetListValue ( ) . Values for i , categoryIDValue := range displayNameValues { categoryID := categoryIDValue . GetStringValue ( ) if confidenceValues [ i ] . GetNumberValue ( ) >= scoreThreshold { categoryIDs = append ( categoryIDs , categoryID ) } } span . SetAttributes ( attribute . StringSlice ( " " , categoryIDs ) ) return categoryIDs , nil }
CategorizeQuery predicts the query's intended categories and returns the list of ids.
CategorizeQuery predicts the query ' s intended categories and returns the list of ids .