Skip to main content
Version: 8.0

Connect a custom web service to machine learning service

Level: intermediate
Example

Connect a custom web service to machine learning (ML) functionality. The service implements custom predictive scoring and is developed using Microsoft Visual Studio Code.

1. Implement custom predictive scoring

Download and unpack the *.zip archive that includes the web service. The service implements custom predictive scoring in a Microsoft Visual Studio Code project.

For .NET 6

For .NET Core 3.1

To develop a custom web service using a Microsoft Visual Studio Code project, follow the instructions in a separate article: Develop C# code in a configuration project.

The MLService web service implements the following endpoints:

  • session/start
  • data/upload
  • session/info/get
  • fakeScorer/beginTraining
  • fakeScorer/predict

2. Expand the problem list of the ML service

  1. Click to open the System Designer.

  2. Go to the System setup block → Lookups.

  3. Open the ML problem types lookup.

  4. Add a problem type.

    1. Click New on the lookup toolbar.

    2. Fill out the problem type properties.

      • Set Name to "Fake scoring."
      • Set Service endpoint Url to "http://localhost:5000/."
      • Set Training endpoint to "/fakeScorer/beginTraining."
  5. Find an ID of the problem type. To do this, display the corresponding column in the lookup list.

    1. Click ViewSelect fields to display on the lookup toolbar.
    2. Add the column to the lookup list. To do this, click and select the Id column.
    3. Click SelectSave.

The ID of the Fake scoring problem type is 19fcfff1-98b9-4933-8f26-457ca45c35ed.

3. Implement a ML model

  1. Go to the Configuration section and select a user-made package to add the schema.

  2. Click AddPage view model on the section list toolbar.

  3. Fill out the schema properties.

    • Set Code to "UsrMLModelPage."
    • Set Title to "MLModelPage."
    • Select "MLModelPage" in the Parent object property.
  4. Implement the style of the new mini page similar to the mini page for creating a predictive scoring model. To do this, overload the getIsScoring() method.

    View the source code of the view model schema of the page below.

    UsrMLModelPage
    define("UsrMLModelPage", [], function() {
    return {
    /* The name of the page object schema. */
    entitySchemaName: "MLModel",
    /* The properties of the page view model. */
    properties: {
    FakeScoringProblemTypeId: "19fcfff1-98b9-4933-8f26-457ca45c35ed"
    },
    /* The methods of the page view model. */
    methods: {
    getIsScoring: function() {
    const parentResult = this.callParent(arguments);
    return parentResult || this.getLookupValue("MLProblemType") === this.FakeScoringProblemTypeId;
    }
    }
    };
    });
  5. Click Save on the Module Designer's toolbar.

4. Implement the handling of the ML model results

  1. Go to the Configuration section and select a user-made package to add the schema.

  2. Click AddSource code on the section list toolbar.

  3. Fill out the schema properties.

    • Set Code to "UsrFakeScoringEntityPredictor."
    • Set Title to "FakeScoringEntityPredictor."

    Click Apply to apply the properties.

  4. Implement the handling of the ML model results.

    • Implement the Predict() method that receives data exported from Creatio by object and returns the prediction value. The method uses a proxy class that implements the IMLServiceProxy interface to facilitate web service calls.
    • Initialize the ProblemTypeId property using the ID of the Fake scoring problem type from the ML problem types lookup.
    • Implement the SaveEntityPredictedValues() and SavePrediction() methods.

    View the source code of the UsrFakeScoringEntityPredictor class below.

    UsrFakeScoringEntityPredictor
    namespace Terrasoft.Configuration.ML
    {
    using System;
    using System.Collections.Generic;
    using Core;
    using Core.Factories;
    using Terrasoft.Core.Process.Configuration;
    using Terrasoft.ML.Interfaces;

    [DefaultBinding(typeof(IMLEntityPredictor), Name = "19fcfff1-98b9-4933-8f26-457ca45c35ed")]
    [DefaultBinding(typeof(IMLPredictor<ScoringOutput>), Name = "19fcfff1-98b9-4933-8f26-457ca45c35ed")]
    public class FakeScoringEntityPredictor: MLBaseEntityPredictor<ScoringOutput>, IMLEntityPredictor, IMLPredictor<ScoringOutput>{
    public FakeScoringEntityPredictor(UserConnection userConnection) : base(userConnection) {
    }

    protected override Guid ProblemTypeId => new Guid("19fcfff1-98b9-4933-8f26-457ca45c35ed");

    protected override ScoringOutput Predict(IMLServiceProxy proxy, MLModelConfig model, Dictionary<string, object> data, MLDataPredictionUserTask predictionUserTask) {
    return proxy.FakeScore(model, data, true);
    }

    protected override List<ScoringOutput> Predict(MLModelConfig model, IList<Dictionary<string, object>> dataList, IMLServiceProxy proxy, MLDataPredictionUserTask predictionUserTask) {
    return proxy.FakeScore(model, dataList, true);
    }

    protected override void SaveEntityPredictedValues(MLModelConfig model, Guid entityId, ScoringOutput predictedResult) {
    var predictedValues = new Dictionary<MLModelConfig, double> {
    {
    model, predictedResult.Score
    }
    };
    PredictionSaver.SaveEntityScoredValues(model.EntitySchemaId, entityId, predictedValues);
    }

    protected override void SavePrediction(MLModelConfig model, Guid entityId, ScoringOutput predictedResult) {
    PredictionSaver.SavePrediction(model.Id, model.ModelInstanceUId, entityId, predictedResult.Score);
    }

    }
    }
  5. Click Publish on the Source Code Designer’s toolbar to apply the changes on the database level.

5. Add a new problem type to the prediction method

  1. Go to the Configuration section and select a user-made package to add the schema.

  2. Click AddSource code on the section list toolbar.

  3. Fill out the schema properties.

    • Set Code to "UsrFakeScoringProxy."
    • Set Title to "FakeScoringProxy."

    Click Apply to apply the properties.

  4. Add the Fake scoring problem type to the prediction method. To do this, extend the IMLServiceProxy base interface and implementations in the prediction method of the current problem type. The MLServiceProxy class implements the Predict() method that receives contracts for input data and prediction results.

    View the source code of the UsrFakeScoringProxy class below.

    UsrFakeScoringProxy
    namespace Terrasoft.Configuration.ML
    {
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Terrasoft.ML.Interfaces;
    using Terrasoft.ML.Interfaces.Requests;
    using Terrasoft.ML.Interfaces.Responses;

    public partial interface IMLServiceProxy
    {
    ScoringOutput FakeScore(MLModelConfig model, Dictionary<string, object> data, bool predictContributions);
    List<ScoringOutput> FakeScore(MLModelConfig model, IList<Dictionary<string, object>> dataList, bool predictContributions);
    }

    public partial class MLServiceProxy : IMLServiceProxy
    {
    public ScoringOutput FakeScore(MLModelConfig model, Dictionary<string, object> data, bool predictContributions) {
    ScoringResponse response = Predict<ExplainedScoringRequest, ScoringResponse>(model.ModelInstanceUId, new List<Dictionary<string, object>> { data }, model.PredictionEndpoint, 100, predictContributions);
    return response.Outputs.FirstOrDefault();
    }

    public List<ScoringOutput> FakeScore(MLModelConfig model, IList<Dictionary<string, object>> dataList, bool predictContributions) {
    int count = Math.Min(1, dataList.Count);
    int timeout = Math.Max(ScoreTimeoutSec * count, BatchScoreTimeoutSec);
    ScoringResponse response = Predict<ScoringRequest, ScoringResponse>(model.ModelInstanceUId, dataList, model.PredictionEndpoint, timeout, predictContributions);
    return response.Outputs;
    }
    }
    }
  5. Click Publish on the Source Code Designer’s toolbar to apply the changes on the database level.

6. Implement the batch prediction functionality

  1. Go to the Configuration section and select a user-made package to add the schema.

  2. Click AddSource code on the section list toolbar.

  3. Fill out the schema properties.

    • Set Code to "UsrFakeBatchScorer."
    • Set Title to "FakeBatchScorer."

    Click Apply to apply the properties.

  4. Implement the batch prediction functionality.

    • Implement the IMLBatchPredictor interface.
    • Implement the FormatValueForSaving() and SavePredictionResult() methods.

    View the source code of the UsrFakeBatchScorer class below.

    UsrFakeBatchScorer
    namespace Terrasoft.Configuration.ML
    {
    using System;
    using Core;
    using Terrasoft.Core.Factories;
    using Terrasoft.ML.Interfaces;

    [DefaultBinding(typeof(IMLBatchPredictor), Name = "19fcfff1-98b9-4933-8f26-457ca45c35ed")]
    public class FakeBatchScorer : MLBatchPredictor<ScoringOutput>
    {

    public FakeBatchScorer(UserConnection userConnection) : base(userConnection) {
    }

    protected override object FormatValueForSaving(ScoringOutput scoringOutput) {
    return Convert.ToInt32(scoringOutput.Score * 100);
    }

    protected override void SavePredictionResult(MLModelConfig modelConfigDiff, Guid entityId, ScoringOutput value) {
    PredictionSaver.SavePrediction(modelConfigDiff.Id, modelConfigDiff.ModelInstanceUId, entityId, value);
    }

    }

    }
  5. Click Publish on the Source Code Designer’s toolbar to apply the changes on the database level.


Resources

Package with example implementation

Configuration project with web service functionality implementation (for .NET 6)

Configuration project with web service functionality implementation (for .NET Core 3.1)