Skip to content

informat.aiagent Related

Overview

Use informat.aiagent to perform AI assistant related operations


chatCompletions

A technology that allows providing partial input to an AI model and obtaining natural, coherent completed text. This interface can be used for various applications including text generation, language translation, question answering, and more.

javascript
let result = informat.aiagent.chatCompletions(setting);
ParameterTypeDescription
settingOpenAIChatSettingOpenAI Chat configuration information

Return Value

Type: OpenAIChatResp

Example 1: Basic Text Conversation

javascript
let result = informat.aiagent.chatCompletions({
  url: "https://api.openai.com/v1/chat/completions",
  model: "gpt-4o",
  apiKey: "sk-xxxxxxxx",
  messages: [{ role: "user", content: "How big is Earth?" }],
});
json
{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "The size of Earth can be described by its diameter, circumference, and surface area. 1. **Equatorial diameter**: Approximately 12,742 kilometers (7,918 miles). 2. **Polar diameter**: Approximately 12,714 kilometers (7,900 miles). Since Earth is an oblate spheroid, the equatorial diameter is slightly larger than the polar diameter. 3. **Equatorial circumference**: Approximately 40,075 kilometers (24,901 miles). 4. **Surface area**: Approximately 510 million square kilometers (197 million square miles). These data indicate that Earth is a medium-sized planet with a moderate size compared to other planets in our solar system.",
        "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
  }
}

Example 2: Invoice Content Recognition (Text and Image Conversation)

javascript
let data = automatic.getVar("data"); // Listen for invoice attachment change events
let filePath = data.content.formRecord.attachment.path; // Invoice image attachment storage path
let base64Image = informat.storage.getBase64Content(filePath); // Get invoice image base64 encoding
let content = [
  {
    text: "Analyze the content of this invoice and return JSON data containing the following fields: invoice code, invoice number, invoice date, seller name, buyer name, buyer taxpayer identification number, amount, tax rate. Note: The return value should not contain ```json, it must be a complete JSON string",
    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 Date", invoice["InvoiceDate"]);
invoice["InvoiceDate"] = informat.date.parseDate(invoice["InvoiceDate"]);
console.log("invoice", invoice);
json
{
  "InvoiceCode": "033001700211",
  "InvoiceNumber": "56894556",
  "InvoiceDate": "2019-01-08",
  "SellerName": "Shenzhen Cornerstone Collaboration Technology Co., Ltd.",
  "BuyerName": "Shenzhen Cornerstone Collaboration Technology Co., Ltd.",
  "BuyerTaxpayerID": "913101X5768225450X",
  "Amount": "66.97",
  "TaxRate": "***"
}

completionsWithPrompt

A technology that allows providing partial input to an AI model and obtaining natural, coherent completed text. This interface can be used for various applications including text generation, language translation, question answering, and more.

javascript
let result = informat.aiagent.completionsWithPrompt("aiagent", "How to install PostgreSQL on CentOS");
ParameterTypeDescription
moduleIdStringAI assistant identifier
promptStringPrompt text

Return Value

Type: OpenAIChatResp

Example

javascript
let result = informat.aiagent.completionsWithPrompt("aiagent", "How to install PostgreSQL on CentOS 7");
json
{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "PostgreSQL can be installed on CentOS 7 through the following steps:\n\n1. Update the system and install necessary packages:\n```\nsudo yum update\nsudo yum install epel-release\nsudo yum install postgresql-server postgresql-contrib\n```\n\n2. Initialize the PostgreSQL database:\n```\nsudo postgresql-setup initdb\n```\n\n3. Start the PostgreSQL service and set it to start automatically at boot:\n```\nsudo systemctl start postgresql\nsudo systemctl enable postgresql\n```\n\n4. Set the PostgreSQL user password:\n```\nsudo -i -u postgres\npsql\npassword postgres\n```\n\n5. Modify the PostgreSQL configuration file to allow remote connections (if needed):\n```\nsudo nano /var/lib/pgsql/data/pg_hba.conf\n```\nAdd the following line at the end of the file:\n```\nhost all all 0.0.0.0/0 md5\n```\nSave and exit the file, then restart the PostgreSQL service:\n```\nsudo systemctl restart postgresql\n```\n\nNow you have successfully installed and configured PostgreSQL on CentOS 7. You can use the psql command to connect to the database and start using it.",
        "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

A technology that allows providing partial input to an AI model and obtaining natural, coherent completed text. This interface can be used for various applications including text generation, language translation, question answering, and more.

javascript
let result = informat.aiagent.completions("aiagent", [{ type: "text", text: "How to install PostgreSQL on CentOS" }]);
ParameterTypeDescription
moduleIdStringAI assistant identifier
messageListArray<AiAgentContentMessage>List of prompts or images

Return Value

Type: OpenAIChatResp

Example

javascript
let result = informat.aiagent.completions("aiagent", [
  {
    type: "text",
    text: "How to install PostgreSQL on CentOS 7",
  },
]);
json
{
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "message": {
        "content": "PostgreSQL can be installed on CentOS 7 through the following steps:\n\n1. Update the system and install necessary packages:\n```\nsudo yum update\nsudo yum install epel-release\nsudo yum install postgresql-server postgresql-contrib\n```\n\n2. Initialize the PostgreSQL database:\n```\nsudo postgresql-setup initdb\n```\n\n3. Start the PostgreSQL service and set it to start automatically at boot:\n```\nsudo systemctl start postgresql\nsudo systemctl enable postgresql\n```\n\n4. Set the PostgreSQL user password:\n```\nsudo -i -u postgres\npsql\npassword postgres\n```\n\n5. Modify the PostgreSQL configuration file to allow remote connections (if needed):\n```\nsudo nano /var/lib/pgsql/data/pg_hba.conf\n```\nAdd the following line at the end of the file:\n```\nhost all all 0.0.0.0/0 md5\n```\nSave and exit the file, then restart the PostgreSQL service:\n```\nsudo systemctl restart postgresql\n```\n\nNow you have successfully installed and configured PostgreSQL on CentOS 7. You can use the psql command to connect to the database and start using it.",
        "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

Query thread list

javascript
informat.aiagent.queryThreadList(query);
ParameterTypeDescription
queryQueryQuery conditions

Return Value

Type: Array<AiAgentThread>, returns the thread list

queryThreadListCount

Query the total number of threads

javascript
informat.aiagent.queryThreadListCount(filter);
ParameterTypeDescription
filterFilterFilter

Return Value

Type: int Returns the total number of threads

queryThreadMessageList

Query thread message list

javascript
informat.aiagent.queryThreadMessageList(query);
ParameterTypeDescription
queryQueryQuery conditions

Return Value

Type: Array<AiAgentThreadMessage>, returns the thread message list

queryThreadMessageListCount

Query the total number of thread messages

javascript
informat.aiagent.queryThreadMessageListCount(filter);
ParameterTypeDescription
filterFilterFilter

Return Value

Type: int Returns the total number of thread messages