@ chalk-ai/ 客户端
Chalk的TypeScript客户端.
安装
$ yarn add @chalk-ai/client
生成类型
从您的 Chark 项目目录开始, 运行 :
$ chalk codegen typescript --out=generated_types.ts
使用量
现代 JavaScript ES6
import { ChalkGRPCClient } from "@chalk-ai/client"
// import { ChalkClient } from "@chalk-ai/client";
// Import your generated types (recommended)
import { FeaturesType } from "local/generated_types";
// Alternatively, define features you want to pull using the client
// as an interface with the feature type and feature name.
interface FeaturesType {
"user.id": string;
"user.fraud_score": number;
}
const client = new ChalkGRPCClient<FeaturesType>();
const result = await client.query({
inputs: {
"user.id": "1",
},
outputs: ["user.fraud_score"],
});
// The property `.data` has auto-complete based on the
// list provided in `output` above. So if you try to pull
// a feature that wasn't requested, you will see an error
// in type checking.
console.log(result.data["user.fraud_score"].value);
常见JS
var ChalkGRPCClient = require("@chalk-ai/client").ChalkGRPCClient;
// var ChalkClient = require("@chalk-ai/client").ChalkClient;
interface FeaturesType {
"user.id": string;
"user.fraud_score": number;
}
var client = new ChalkGRPCClient<FeaturesType>();
client
.query({
inputs: {
"user.id": "1",
},
outputs: ["user.fraud_score"],
})
.then((result) => {
console.log(result.data["user.fraud_score"].value);
});
构造器选项( gRPC)
见 hTTP 选项 如果您正在使用 HTTP 客户端, 但我们鼓励 读取 移徙指南。 。 。 .
import { ChalkGRPCClient } from "@chalk-ai/client"
const options: ChalkGRPCClientOpts = {/* ... */ };
const chalkClient = new ChalkGRPCClient(options);
export interface ChalkGRPCClientOpts {
/**
* Your Chalk Client ID. This value will be read from the _CHALK_CLIENT_ID environment variable if not set explicitly.
*
* If not specified and unset by your environment, an error will be thrown on client creation
*/
clientId?: string;
/**
* Your Chalk Client Secret. This value will be read from the _CHALK_CLIENT_SECRET environment variable if not set explicitly.
*
* If not specified and unset by your environment, an error will be thrown on client creation
*/
clientSecret?: string;
/**
* The URL of your chalk API server. Defaults to https://api.chalk.ai
*/
apiServer?: string;
/**
* The environment that your client will run against. This value will be read from the _CHALK_ACTIVE_ENVIRONMENT environment variable if not set explicitly.
*
* If not specified and unset by your environment, an error will be thrown on client creation
*/
activeEnvironment?: string;
/**
* A custom fetch client that will replace the fetch polyfill used by default. Used by both the HTTP and gRPC
* clients - the HTTP client for all calls, and the gRPC client for fetching credentials from the API server.
*
* If not provided, the client will use the default fetch polyfill (native fetch with node-fetch as a fallback).
*/
fetch?: CustomFetchClient;
/**
* A custom fetch headers object that will replace the fetch Headers polyfill used by default. This is primarily for use
* with a custom fetch client, and is not the preferred way to add additional headers to requests.
*
* If not provided, the client will use the default fetch Headers polyfill (native fetch with node-fetch as a fallback).
*/
fetchHeaders?: typeof Headers;
/**
* The format to use for date-type data.
*
* Defaults to "ISO_8601" (in UTC), also supports "EPOCH_MILLIS" as number of milliseconds since epoch
*/
timestampFormat?: TimestampFormat.ISO_8601 | TimestampFormat.EPOCH_MILLIS;
/**
* By default, the client will try to directly connect to the query client using the metadata provided during
* credentials exchange, which returns a mapping between environment and query server. If this option is
* truthy, the client will skip this step.
*
* The queryServer option will take precedence over this option (i.e. this option is essentially useless).
* *
* Defaults to false.
*/
skipQueryServerFromCredentialExchange?: boolean;
/**
* Passed to the internal grpc client upon initialization. Under the hood, Chalk uses the @grpc/grpc-js library
* and this allows users finer control of their gRPC usage.
*
* See https://grpc.github.io/grpc/node/grpc.Client.html for more information on what is supported here.
*/
grpcClientOptions?: Partial<ClientOptions>;
}
迁移到 gRPC 客户端
Chalk既支持HTTP客户端,也支持gRPC客户端与Chark接口,但我们大力鼓励用户使用 gRPC 客户端, 以更好的性能, 因为它的目标是一个更能表现的 gRPC 查询服务器 。 成立GRPC后 在您的 Chark 环境中查询服务器, 更改为 gRPC 客户端可以在几个步骤中完成 :
- 使用导入
ChalkGRPCClient改为ChalkClient - 检查是否需要更改以下初始化选项 :
- 选项
useQueryServerFromCredentialExchange已改为skipQueryServerFromCredentialExchange财务报告和审定财务报表 已否定 要更好地反映gRPC查询服务器的默认行为. - 检查您的 Chark 环境, 查看任何自定义的路由规则 。 如果您不确定, 请向 Chalk 求救 .
- 如果您的前一个 SDK 客户端明确设置
engine-type: engine请求时的标题, 需要更改 改为engine-grpc(或省略,gRPC客户端会自动设置此头但会尊重所有覆盖). - 很可能没有必要更改规定
QueryServer因为大多数路由是通过 SDK 设置头来完成的,但是 取决于您的设置, 如果使用非标准端口, 这可能需要直接指定 - gRPC 查询服务器监听 在端口时443默认.
- 如果您的前一个 SDK 客户端明确设置
- 没错 没有 需要删除
fetch或者说fetchHeaders因为这些仍在一些 HTTP 常规中使用 例如获取证书.
- 选项
初始化修改后,无需更改实际呼叫 - gRPC 客户端使用 相同的函数签名.
此时此刻 query(), (中文(简体) ), queryBulk(),以及 multiQuery() 支持 - 如果您的用法需要其中之一
函数whoami(), (中文(简体) ), triggerResolverRun(), (中文(简体) ), uploadSingle(), (中文(简体) ), getRunStatus()),遗产 ChalkClient 应使用
我们仍强烈建议将部分查询电话转移到gRPC客户端.
支持的环境变量
| 变量 | 仁爱 | 说明 |
|---|---|---|
process.env._CHALK_CLIENT_ID | 需求 | 你的粉笔客户身份证 您必须指定此环境变量或通过一个明确 clientId 构建 CharkClient 时的值 |
process.env._CHALK_CLIENT_SECRET | 需求 | 你的粉笔客户秘密 您必须指定此环境变量或通过一个明确 clientSecret 构建 CharkClient 时的值 |
process.env._CHALK_ACTIVE_ENVIRONMENT | 可选 | 您的客户端应该连接到的环境 。 如果未指定, 客户端会查询您的项目的默认环境 |
process.env._CHALK_API_SERVER | 可选 | 客户端将与之通信的API服务器. 此默认为 https://api.calk.ai (英语) 对大多数消费者来说都足够 |
您可以通过 Chark 客户端找到要使用的相关变量
运行 chalk config 带有 Chark 命令行工具.
$ chalk config
Path: ~~~
Name: Organization Token
Client Id: <client-id>
Client Secret: <client-secret>
Environment: <active-environment>
API Server: https://api.chalk.ai
Valid Until: 2023-11-10T06:11:17.516000
构造器选项( Legacy)
import { ChalkCLient } from "@chalk-ai/client"
const options: ChalkClientOpts = {/* ... */};
const chalkClient = new ChalkClient(opts);
export interface ChalkClientOpts {
/**
* Your Chalk Client ID. This value will be read from the _CHALK_CLIENT_ID environment variable if not set explicitly.
*
* If not specified and unset by your environment, an error will be thrown on client creation
*/
clientId?: string;
/**
* Your Chalk Client Secret. This value will be read from the _CHALK_CLIENT_SECRET environment variable if not set explicitly.
*
* If not specified and unset by your environment, an error will be thrown on client creation
*/
clientSecret?: string;
/**
* The URL of your chalk API server. Defaults to https://api.chalk.ai
*/
apiServer?: string;
/**
* The environment that your client will run against. This value will be read from the _CHALK_ACTIVE_ENVIRONMENT environment variable if not set explicitly.
*
* If not specified and unset by your environment, an error will be thrown on client creation
*/
activeEnvironment?: string;
/**
* A custom fetch client that will replace the fetch polyfill used by default.
*
* If not provided, the client will use the default fetch polyfill (native fetch with node-fetch as a fallback).
*/
fetch?: CustomFetchClient;
/**
* A custom fetch headers object that will replace the fetch Headers polyfill used by default.
*
* If not provided, the client will use the default fetch Headers polyfill (native fetch with node-fetch as a fallback).
*/
fetchHeaders?: typeof Headers;
/**
* The format to use for date-type data.
*
* Defaults to "ISO_8601" (in UTC), also supports "EPOCH_MILLIS" as number of milliseconds since epoch
*/
timestampFormat?: TimestampFormat.ISO_8601 | TimestampFormat.EPOCH_MILLIS;
/**
* If true, will try to directly connect to the query client using the metadata provided during
* credentials exchange, which returns a mapping between environment and query server.
*
* The queryServer option will take precedence over this option (i.e. this option is essentially useless).
*
* Defaults to false, the legacy behavior of this client. This will change at the next major release.
*/
useQueryServerFromCredentialExchange?: boolean;
}