import openai from "../../openai.app.mjs";
import common from "../common/common.mjs";
export default {
...common,
name: "Chat",
version: "0.1.7",
key: "openai-chat",
description: "The Chat API, using the `gpt-3.5-turbo` or `gpt-4` model. [See docs here](https://platform.openai.com/docs/api-reference/chat)",
type: "action",
props: {
openai,
modelId: {
propDefinition: [
openai,
"chatCompletionModelId",
],
},
userMessage: {
label: "User Message",
type: "string",
description: "The user messages provide instructions to the assistant. They can be generated by the end users of an application, or set by a developer as an instruction.",
},
systemInstructions: {
label: "System Instructions",
type: "string",
description: "The system message helps set the behavior of the assistant. For example: \"You are a helpful assistant.\" [See these docs](https://platform.openai.com/docs/guides/chat/instructing-chat-models) for tips on writing good instructions.",
optional: true,
},
messages: {
label: "Prior Message History",
type: "string[]",
description: "_Advanced_. Because [the models have no memory of past chat requests](https://platform.openai.com/docs/guides/chat/introduction), all relevant information must be supplied via the conversation. You can provide [an array of messages](https://platform.openai.com/docs/guides/chat/introduction) from prior conversations here. If this param is set, the action ignores the values passed to **System Instructions** and **Assistant Response**, appends the new **User Message** to the end of this array, and sends it to the API.",
optional: true,
},
images: {
label: "Images",
type: "string[]",
description: "Provide one or more images to [OpenAI's vision model](https://platform.openai.com/docs/guides/vision). Accepts URLs or base64 encoded strings. Compatible with the `gpt4-vision-preview model`",
optional: true,
},
responseFormat: {
type: "string",
label: "Response Format",
description: "Specify the format that the model must output. [Setting to `json_object` guarantees the message the model generates is valid JSON](https://platform.openai.com/docs/api-reference/chat/create#chat-create-response_format). Defaults to `text`",
options: [
"text",
"json_object",
],
optional: true,
default: "text",
},
...common.props,
},
async run({ $ }) {
const args = this._getChatArgs();
const response = await this.openai.createChatCompletion({
$,
args,
});
if (response) {
$.export("$summary", `Successfully sent chat with id ${response.id}`);
}
const { messages } = args;
return {
original_messages: messages,
original_messages_with_assistant_response: messages.concat(response.choices[0]?.message),
...response,
};
},
};