Interface ChalkClient

All Superinterfaces:
AutoCloseable
All Known Implementing Classes:
ChalkClientImpl, GRPCClient

public interface ChalkClient extends AutoCloseable
  • Method Details

    • builder

      static ChalkClient.Builder builder()
      Creates a new ChalkClient builder that can be used to create a ChalkClient instance with custom configuration.

      Example:

          
          client = ChalkClient.builder()
                  .withEnvironmentId("socmc8beyufew")
                  .withClientId("98wrasfg7dge7wdasg709")
                  .withClientSecret("h23lkj4h23lkj4z9s78fg908as7fkjh324klj23")
                  .build();
          
       
      Returns:
      a new ChalkClient builder
    • create

      static ChalkClient create() throws ChalkException

      Creates a new default ChalkClient instance. The default ChalkClient instance is configured with config variables sourced as such:

      For each variable, we take the first non-empty value, in order, from the following sources:

      1. The value of the config's corresponding environment variable (see ConfigEnvVars).

      2. The value in the project root's 'chalk.yaml' or 'chalk.yml' file.

      To override the settings above with custom configuration, use builder().

      Returns:
      a new default ChalkClient instance
      Throws:
      ChalkException
    • createGrpc

      static ChalkClient createGrpc() throws ChalkException

      Creates a new ChalkClient instance that uses gRPC for communication. The gRPC ChalkClient instance is configured with config variables sourced the same way as the default ChalkClient instance.

      Returns:
      a new ChalkClient instance that uses gRPC
      Throws:
      ChalkException
    • ping

      default int ping() throws ChalkException
      Pings the Chalk engine with a random number and returns the number echoed by the engine.
      Returns:
      the number echoed by the engine
      Throws:
      ChalkException - if the engine cannot be reached
    • ping

      int ping(int num) throws ChalkException
      Pings the Chalk engine with num and returns the number echoed by the engine.
      Parameters:
      num - the number to send to the engine
      Returns:
      the number echoed by the engine
      Throws:
      ChalkException - if the engine cannot be reached
    • onlineQuery

      OnlineQuery computes features values using online resolvers.

      See OnlineQueryParams for more details on the parameters.

      Example usage for bulk queries:

               {@code
               OnlineQueryParamsComplete params = OnlineQueryParams.builder()
                   .withInput("user.id", Arrays.asList(1, 2, 3))
                   .withOutputs("user.email", "user.transactions")
                   .build();
      
               try (OnlineQueryResult result = client.onlineQuery(params)) {
                   // do something with the result
               }
           </pre>
       </p>
      
       <p>
       Example usage for single-row queries using SingleRowInput:
       <pre>
               {@code
               var singleRow = new OnlineQueryParams.SingleRowInput()
                   .withInput("user.id", 123)
                   .withInput("user.email", "test@example.com")
                   .withInput("user.favorite_foods", Arrays.asList("pizza", "pasta"));
      
               OnlineQueryParamsComplete params = OnlineQueryParams.builder()
                   .withInput(singleRow)
                   .withOutputs("user.credit_score", "user.transactions")
                   .build();
      
               try (OnlineQueryResult result = client.onlineQuery(params)) {
                   // do something with the result
               }
           </pre>
       </p>
      
       @return {@link OnlineQueryResult }
       @throws ChalkException
       @see <a href="https://docs.chalk.ai/docs/query-basics">query basics</a>
      Throws:
      ChalkException
    • onlineQueryMulti

      default OnlineQueryMultiResult onlineQueryMulti(List<OnlineQueryParamsComplete> params) throws ChalkException
      OnlineQueryMulti computes several independent online queries in parallel, using a single request to the Chalk engine. Use it when the queries are rooted in different feature classes, or otherwise ask for unrelated outputs, and you would rather not pay a round trip for each one. To run one query over many rows of input, use onlineQuery(ai.chalk.models.OnlineQueryParamsComplete) instead, which is already a bulk operation.

      This is supported only by the gRPC client; see createGrpc().

      Example usage:

               
               OnlineQueryParamsComplete users = OnlineQueryParams.builder()
                   .withInput("user.id", Arrays.asList(1, 2, 3))
                   .withOutputs("user.email")
                   .build();
               OnlineQueryParamsComplete merchants = OnlineQueryParams.builder()
                   .withInput("merchant.id", Arrays.asList("a", "b"))
                   .withOutputs("merchant.risk_score")
                   .build();
      
               try (OnlineQueryMultiResult multi = client.onlineQueryMulti(List.of(users, merchants))) {
                   for (OnlineQueryResult result : multi.getResults()) {
                       if (result.getErrors().length > 0) {
                           // handle this query's failure; the other queries are unaffected
                           continue;
                       }
                       // do something with the result
                   }
               }
               
           

      Results are positional. OnlineQueryMultiResult.getResults() holds one result per query, in the order the queries were passed in.

      Queries fail independently. A resolver that fails while computing one query is reported on that query's own result, via OnlineQueryResult.getErrors(), and leaves the other queries' results intact. OnlineQueryMultiResult.getGlobalErrors() holds the errors the engine did not attach to a specific result -- including the case where a query failed to run at all, which is reported there with the query's 1-based position in the message rather than on the result. That query still occupies its slot, but its result is empty and reports no errors of its own, so check the global errors as well before treating an empty result as a successful one.

      Resources. Closing the returned OnlineQueryMultiResult closes every one of its results and releases all of their Arrow memory. Do not close the individual results yourself.

      Some settings apply to the whole request. A request carries one deadline and one set of headers, so:

      • Every query must agree on environmentId, branch and queryName. A disagreement throws ClientException, because a request resolves one branch to one deployment and routes named queries by an exact match on the query name, so a mixed batch could not be honored. Run those as separate queries. A query that leaves one of these unset simply takes the client's value, which is not a disagreement. queryNameVersion may differ freely: each query carries its own.
      • The deadline is the longest of the effective per-query timeouts, where a query that sets none is bounded by the client-level timeout. Per-query timeouts are not independently enforced.
      • Branch selection is currently inert on the gRPC path for both single and multi queries: the client does not yet send the header the API server routes branches on.
      Parameters:
      params - the queries to run, one result per query
      Returns:
      OnlineQueryMultiResult
      Throws:
      ChalkException - if the request fails, or if the queries disagree on a setting that applies to the whole request
      See Also:
    • onlineQueryMulti

      default OnlineQueryMultiResult onlineQueryMulti(OnlineQueryParamsComplete... params) throws ChalkException
      OnlineQueryMulti computes several independent online queries in parallel, using a single request to the Chalk engine.

      See onlineQueryMulti(List) for details and for the settings that every query must agree on.

      Returns:
      OnlineQueryMultiResult
      Throws:
      ChalkException
    • uploadFeatures

      UploadFeatures synchronously persists feature values to the online store and offline store.

      See UploadFeaturesParams for more details on the parameters.

      Example usage:

               {@code
               var userIds = Arrays.asList("777", "888", "999");
               var scoreList = Arrays.asList(600, 700, 800);
               UploadFeaturesParams params = UploadFeaturesParams.builder()
                   .withInputs(Map.of("user.id", userIds,"user.credit_score", scoreList))
                   .withInput(Features.user.email, List.of("user1@email.com", "user2@email.com", "user3@email.com"))
                   .build();
               UploadFeaturesResult res = client.uploadFeatures(params);
               if (client.uploadFeatures(params).getErrors().size() > 0) {
                   throw new Exception("upload failed");
               }
      
           </pre>
       </p>
      Throws:
      ChalkException
    • printConfig

      void printConfig()
      Prints the current ChalkClient configuration and its sources to stdout.