Add and use messageOf() and contentOf()

This commit is contained in:
powermaker450 2024-07-03 22:49:54 -04:00
parent e4d822957c
commit 0e70a0d57a
3 changed files with 37 additions and 19 deletions

View file

@ -2,7 +2,13 @@ import { TailchatWsClient, stripMentionTag } from "tailchat-client-sdk";
import { OpenAI } from "openai";
import * as fs from "fs";
import { ImageRequestData, Messages } from "./types";
import { checkFile, getUsername, isUserBot } from "./utils";
import {
checkFile,
getUsername,
isUserBot,
contentOf,
messageOf,
} from "./utils";
import {
allowedChat,
safeWord,
@ -128,17 +134,18 @@ client.connect().then(async () => {
model: imageModel,
temperature: temperature,
});
response.choices.at(0)!.message.content = response.choices
.at(0)!
.message.content!.replace("</s>", "");
response.choices.at(0)!.message.content = contentOf(response).replace(
"</s>",
"",
);
session.push(response.choices.at(0)!.message);
session.push(messageOf(response));
await client.sendMessage({
converseId: message.converseId,
groupId: message.groupId,
// Surround in [md] tags to support markdown formatting.
content: `[md]${response.choices.at(0)?.message.content}[/md]`,
content: `[md]${contentOf(response)}[/md]`,
});
fs.writeFileSync("./messages.json", JSON.stringify(session), "utf8");
@ -163,13 +170,13 @@ client.connect().then(async () => {
temperature: temperature,
});
session.push(response.choices.at(0)!.message);
session.push(messageOf(response));
await client.sendMessage({
converseId: message.converseId,
groupId: message.groupId,
// Same as above. Surround with [md] tags to support markdown formatting.
content: `[md]${response.choices.at(0)?.message.content}[/md]`,
content: `[md]${contentOf(response)}[/md]`,
});
fs.writeFileSync("./messages.json", JSON.stringify(session), "utf8");
@ -204,11 +211,11 @@ client.connect().then(async () => {
temperature: temperature,
});
session.push(response.choices.at(0)!.message);
session.push(messageOf(response));
await client.sendMessage({
converseId: message.converseId,
content: `[md]${response.choices.at(0)?.message.content}[/md]`,
content: `[md]${contentOf(response)}[/md]`,
});
fs.writeFileSync("./messages.json", JSON.stringify(session), "utf8");

View file

@ -1,17 +1,12 @@
import OpenAI from "openai";
import {
ChatCompletionAssistantMessageParam,
ChatCompletionSystemMessageParam,
ChatCompletionMessage,
ChatCompletionUserMessageParam,
ChatCompletionSystemMessageParam,
} from "openai/resources";
import { TailchatWsClient } from "tailchat-client-sdk";
type ChatCompletion =
| ChatCompletionSystemMessageParam
| ChatCompletionUserMessageParam
| ChatCompletionAssistantMessageParam;
interface ImageUrl {
export interface ImageMessage {
role: "user";
content: [
{
@ -27,7 +22,13 @@ interface ImageUrl {
];
}
export type Message =
| ChatCompletionMessage
| ChatCompletionUserMessageParam
| ChatCompletionSystemMessageParam;
export type validImgSize = "256x256" | "512x512" | "1024x1024";
export type validImgAmount = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
export interface ImageRequestData {
@ -76,4 +77,5 @@ export type Temperature =
| 1.8
| 1.9
| 2.0;
export type Messages = Array<ChatCompletion | ImageUrl>;
export type Messages = Array<Message | ImageMessage>;

View file

@ -1,5 +1,6 @@
import * as fs from "fs";
import { Messages } from "./types";
import { ChatCompletion, ChatCompletionMessage } from "openai/resources";
export function checkFile(
file: string,
@ -39,6 +40,14 @@ export function checkFile(
return final;
}
export function messageOf(response: ChatCompletion): ChatCompletionMessage {
return response.choices.at(0)!.message;
}
export function contentOf(response: ChatCompletion): string {
return messageOf(response).content!;
}
export async function getUserInfo(host: string, id: string) {
/**
* Returns various info about a user given a host and server ID.