Appearance
informat.aiagent 相关
概述
使用informat.aiagent执行ai助手相关操作
chatCompletions
允许向人工智能模型提供部分输入并获取自然、连贯的完成文本的技术。接口可以用于各种应用,包括文本生成、语言翻译、问题回答等等
javascript
let result = informat.aiagent.chatCompletions(setting);| 参数 | 类型 | 描述 |
|---|---|---|
| setting | OpenAIChatSetting | openAiChat配置信息 |
返回值
示例1 普通文字对话
javascript
let result = informat.aiagent.chatCompletions({
'url': 'https://api.openai.com/v1/chat/completions',
'model': 'gpt-4o',
'apiKey': 'sk-xxxxxxxx',
'messages': [{ "role": "user", "content": "地球有多大?" }]
});json
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "地球的大小可以通过其直径、周长和表面积来描述。1.**赤道直径**:大约12,742千米(7,918英里)。2.**极地直径**:大约12,714千米(7,900英里),由于地球是一个扁球体,所以赤道直径稍大于极地直径。3.**赤道周长**:大约40,075千米(24,901英里)。4.**表面积**:大约5.1亿平方千米(1.97亿平方英里)。这些数据表明,地球是一个中等大小的行星,与我们太阳系中的其他行星相比,地球的大小适中。",
"role": "assistant"
}
}
],
"created": 1743407834,
"id": "chatcmpl-BH4J8JNKpbj01hfpFNuohZOtvH5XF",
"model": "gpt-4o-2024-08-06",
"object": "chat.completion",
"usage": {
"completion_tokens": 167,
"prompt_tokens": 13,
"total_tokens": 180
}
}示例2 识别发票内容(文字和图片对话)
javascript
let data = automatic.getVar('data');//监听发票附件变更事件
let filePath = data.content.formRecord.attachment.path;//发票图片附件存储路径
let base64Image = informat.storage.getBase64Content(filePath);//获取发票图片base64编码
let content = [
{
"text": "分析这张发票内容,返回包含以下字段的JSON数据:发票代码、发票号码、开票日期、销售方名称、购买方名称、购买方纳税人识别号、金额、税率。注意:返回值不包含```json,必须是一个完整的json字符串",
"type": "text"
},
{ "image_url": { "url": "data:image/jpeg;base64," + base64Image }, "type": "image_url" }
]
let message = {}
message.role = 'user';
message.content = content;
let result = informat.aiagent.chatCompletions({
'url': 'https://api.openai.com/v1/chat/completions',
'model': 'gpt-4o',
'apiKey': 'sk-xxxxxxxxxx',
'messages': [message]
});
let json = result.choices[0].message.content;
let invoice = JSON.parse(json);
console.log('开票日期', invoice['开票日期'])
invoice['开票日期'] = informat.date.parseDate(invoice['开票日期'])
console.log('invoice', invoice)json
{
"发票代码": "033001700211",
"发票号码": "56894556",
"开票日期": "2019年01月08日",
"销售方名称": "深圳市XXX服务有限公司",
"购买方名称": "深圳市基石协作科技有限公司",
"购买方纳税人识别号": "913101X5768225450X",
"金额": "66.97",
"税率": "***"
}completionsWithPrompt
允许向人工智能模型提供部分输入并获取自然、连贯的完成文本的技术。接口可以用于各种应用,包括文本生成、语言翻译、问题回答等等
javascript
let result = informat.aiagent.completionsWithPrompt(
'aiagent', "centos 怎么安装pgsql"
);| 参数 | 类型 | 描述 |
|---|---|---|
| moduleId | String | AI助手标识符 |
| prompt | String | 提示词 |
返回值
示例
javascript
let result = informat.aiagent.completionsWithPrompt('aiagent','centos7 怎么安装pgsql'
);json
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "在CentOS7上安装PostgreSQL可以通过以下步骤完成:\n\n1.更新系统和安装必要的软件包:\n```\nsudoyumupdate\nsudoyuminstallepel-release\nsudoyuminstallpostgresql-serverpostgresql-contrib\n```\n\n2.初始化PostgreSQL数据库:\n```\nsudopostgresql-setupinitdb\n```\n\n3.启动PostgreSQL服务并设置开机自动启动:\n```\nsudosystemctlstartpostgresql\nsudosystemctlenablepostgresql\n```\n\n4.设置PostgreSQL用户密码:\n```\nsudo-i-upostgres\npsql\n\\passwordpostgres\n```\n\n5.修改PostgreSQL配置文件以允许远程连接(如果需要):\n```\nsudonano/var/lib/pgsql/data/pg_hba.conf\n```\n在文件末尾添加以下行:\n```\nhostallall0.0.0.0/0md5\n```\n保存并退出文件,然后重新启动PostgreSQL服务:\n```\nsudosystemctlrestartpostgresql\n```\n\n现在你已经成功在CentOS7上安装并配置了PostgreSQL。你可以使用psql命令连接到数据库,并开始使用它了。",
"role": "assistant"
}
}
],
"created": 1715674313,
"id": "chatcmpl-9OhYH4Wu4kdrhtGIHbnwt2Xf971LZ",
"model": "gpt-3.5-turbo-0125",
"object": "chat.completion",
"usage": {
"completion_tokens": 265,
"prompt_tokens": 16,
"total_tokens": 281
}
}completions
允许向人工智能模型提供部分输入并获取自然、连贯的完成文本的技术。接口可以用于各种应用,包括文本生成、语言翻译、问题回答等等
javascript
let result = informat.aiagent.completions(
'aiagent',
[{ type: "text", text: "centos 怎么安装pgsql" }]
);| 参数 | 类型 | 描述 |
|---|---|---|
| moduleId | String | AI助手标识符 |
| messageList | Array<AiAgentContentMessage> | 提示词或图片列表 |
返回值
示例
javascript
let result = informat.aiagent.completions('aiagent',
[
{
'type': 'text',
'text': 'centos7 怎么安装pgsql'
}
]
);json
{
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "在CentOS7上安装PostgreSQL可以通过以下步骤完成:\n\n1.更新系统和安装必要的软件包:\n```\nsudoyumupdate\nsudoyuminstallepel-release\nsudoyuminstallpostgresql-serverpostgresql-contrib\n```\n\n2.初始化PostgreSQL数据库:\n```\nsudopostgresql-setupinitdb\n```\n\n3.启动PostgreSQL服务并设置开机自动启动:\n```\nsudosystemctlstartpostgresql\nsudosystemctlenablepostgresql\n```\n\n4.设置PostgreSQL用户密码:\n```\nsudo-i-upostgres\npsql\n\\passwordpostgres\n```\n\n5.修改PostgreSQL配置文件以允许远程连接(如果需要):\n```\nsudonano/var/lib/pgsql/data/pg_hba.conf\n```\n在文件末尾添加以下行:\n```\nhostallall0.0.0.0/0md5\n```\n保存并退出文件,然后重新启动PostgreSQL服务:\n```\nsudosystemctlrestartpostgresql\n```\n\n现在你已经成功在CentOS7上安装并配置了PostgreSQL。你可以使用psql命令连接到数据库,并开始使用它了。",
"role": "assistant"
}
}
],
"created": 1715674313,
"id": "chatcmpl-9OhYH4Wu4kdrhtGIHbnwt2Xf971LZ",
"model": "gpt-3.5-turbo-0125",
"object": "chat.completion",
"usage": {
"completion_tokens": 265,
"prompt_tokens": 16,
"total_tokens": 281
}
}queryThreadList
查询会话列表
javascript
informat.aiagent.queryThreadList(query)| 参数 | 类型 | 描述 |
|---|---|---|
| query | Query | 查询条件 |
返回值
类型为Array<AiAgentThread>,返回会话列表
queryThreadListCount
查询会话列表总数
javascript
informat.aiagent.queryThreadListCount(filter)| 参数 | 类型 | 描述 |
|---|---|---|
| filter | Filter | 过滤器 |
返回值
类型为int 返回会话列表总数
queryThreadMessageList
查询会话消息列表
javascript
informat.aiagent.queryThreadMessageList(query)| 参数 | 类型 | 描述 |
|---|---|---|
| query | Query | 查询条件 |
返回值
类型为Array<AiAgentThreadMessage>,返回会话消息列表
queryThreadMessageListCount
查询会话消息列表总数
javascript
informat.aiagent.queryThreadMessageListCount(filter)| 参数 | 类型 | 描述 |
|---|---|---|
| filter | Filter | 过滤器 |
返回值
类型为int 返回会话列表总数
Informatclaw 会话
informat.aiagent提供 Informatclaw 会话创建、查询、同步运行和删除接口。接口自动使用当前脚本执行账号和团队上下文,调用方不需要也不能指定其他账号或团队。runOnce使用当前脚本的应用上下文。
getInformatclawThread
通过会话ID查询当前账号创建的 Informatclaw 会话。
javascript
let thread = informat.aiagent.getInformatclawThread(threadId);| 参数 | 类型 | 描述 |
|---|---|---|
| threadId | String | 会话ID |
返回值
类型为InformatclawThread。会话不存在或不属于当前账号时抛出异常。
queryInformatclawThreadList
查询当前账号创建的 Informatclaw 会话列表。服务端会自动附加当前账号和团队条件。
javascript
let list = informat.aiagent.queryInformatclawThreadList({
pageIndex: 1,
pageSize: 20,
filter: {
conditionList: [
{ fieldId: 'type', opt: 'eq', value: 'default' }
]
}
});| 参数 | 类型 | 描述 |
|---|---|---|
| query | Query | 查询条件 |
返回值
类型为Array<InformatclawThread>。
queryInformatclawThreadListCount
查询当前账号创建的 Informatclaw 会话总数。
javascript
let count = informat.aiagent.queryInformatclawThreadListCount({
conditionList: [
{ fieldId: 'type', opt: 'eq', value: 'default' }
]
});返回值
类型为int。
createInformatclawThread
创建 Informatclaw 会话。账号ID和团队ID由当前脚本上下文自动设置。
javascript
let thread = informat.aiagent.createInformatclawThread({
type: 'default',
permissionMode: 'readonly'
});常用参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| type | String | 否 | 会话类型,默认值为default;App里默认对话使用appDefault |
| aiModelKey | String | 否 | AI模型Key;为空时使用系统默认模型 |
| permissionMode | String | 否 | AI权限模式,例如readonly、dataAssistant、designAssistant或adminAssistant |
| appId | String | 否 | 会话关联的应用ID |
| moduleId | String | 否 | 会话关联的模块ID |
| expertId | String | 专家会话必填 | 专家ID或专家标识符(Expert.key) |
| skillPrefix | String | 否 | Skill提示前缀 |
| skillList | String | 否 | Skill列表 |
| userScheduleId | String | 否 | 用户计划ID,仅计划场景使用 |
| kbFolderList | String | 否 | 知识库目录列表 |
返回值
类型为InformatclawThread,其中id用于后续运行、查询和删除。
deleteInformatclawThread
删除当前账号创建的会话及其消息。
javascript
informat.aiagent.deleteInformatclawThread(threadId);runOnce
同步执行一次 Informatclaw。推荐先创建会话,再将会话ID传给runOnce,以便使用专家、模型和权限等会话上下文。
javascript
let result = informat.aiagent.runOnce({
threadId: thread.id,
prompt: '请分析本月销售额下降的主要原因'
});请求参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| executionId | String | 否 | 执行ID,需要独立取消标识时设置 |
| threadId | String | 否 | 已创建的会话ID;调用专家时应设置 |
| aiModelKey | String | 否 | 覆盖本次运行使用的模型Key |
| permissionMode | String | 否 | 覆盖本次运行使用的权限模式 |
| prompt | String | 是 | 本次运行的提示词 |
| name | String | 否 | 本次运行名称 |
返回值
类型为InformatclawRunResult:
| 字段 | 类型 | 描述 |
|---|---|---|
| text | String | AI最终回答文本 |
| stdout | String | Informatclaw标准输出 |
| stderr | String | Informatclaw标准错误 |
| agentSessionId | String | Agent会话ID |
| exitCode | int | 进程退出码,成功为0 |
| promptTokens | long | 提示词Token数 |
| completionTokens | long | 补全Token数 |
| totalTokens | long | 总Token数 |
runOnce直接返回执行结果,不会将本次输入和输出写入InformatclawThreadMessage。
getInformatclawThreadMessage
通过消息ID查询消息。消息所属会话必须属于当前账号。
javascript
let message = informat.aiagent.getInformatclawThreadMessage(messageId);queryInformatclawThreadMessageList
查询会话消息。查询条件必须包含threadId,服务端会校验该会话属于当前账号。
javascript
let messages = informat.aiagent.queryInformatclawThreadMessageList({
pageIndex: 1,
pageSize: 50,
filter: {
conditionList: [
{ fieldId: 'threadId', opt: 'eq', value: threadId }
]
}
});返回值
类型为Array<InformatclawThreadMessage>。
queryInformatclawThreadMessageListCount
查询指定会话的消息总数。过滤条件必须包含threadId。
javascript
let count = informat.aiagent.queryInformatclawThreadMessageListCount({
conditionList: [
{ fieldId: 'threadId', opt: 'eq', value: threadId }
]
});示例:通过脚本调用专家
下面示例演示一次完整的专家调用:创建专家会话、同步执行、检查执行结果、返回专家回答,并在成功或失败后删除临时会话。
javascript
// 可以填写专家ID,也可以填写Expert.key专家标识符
const expertId = 'sales_analysis_expert';
const question = '请根据你的专业知识,分析本季度经营情况,并给出三条可执行建议。';
let thread = null;
try {
// 1. 创建专家会话
thread = informat.aiagent.createInformatclawThread({
type: 'default',
expertId: expertId,
permissionMode: 'readonly'
});
if (thread == null || thread.id == null) {
throw new Error('创建专家会话失败');
}
console.log('专家会话创建成功', thread.id);
// 2. 同步调用专家
let result = informat.aiagent.runOnce({
threadId: thread.id,
prompt: question
});
// 3. 检查并处理结果
if (result == null) {
throw new Error('专家未返回结果');
}
if (result.exitCode !== 0) {
throw new Error('专家调用失败:' + result.stderr);
}
const answer = result.text == null ? '' : result.text;
console.log('专家回答', answer);
console.log('提示词Token数', result.promptTokens);
console.log('补全Token数', result.completionTokens);
console.log('总Token数', result.totalTokens);
// 可以将answer写入表单字段、流程变量或作为脚本返回值
return {
answer: answer,
totalTokens: result.totalTokens
};
} catch (e) {
console.error('调用专家失败', e);
throw e;
} finally {
// 4. 无论成功或失败,都删除本次创建的临时会话
if (thread != null && thread.id != null) {
try {
informat.aiagent.deleteInformatclawThread(thread.id);
console.log('专家会话已删除', thread.id);
} catch (deleteError) {
console.error('删除专家会话失败', deleteError);
}
}
}如果业务需要多次调用同一个专家,不要在每次调用后立即删除会话,可以保存thread.id并在业务结束后统一调用deleteInformatclawThread。每次runOnce都是独立同步执行,不会自动携带上一次运行的对话内容。

