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

View file

@ -1,17 +1,12 @@
import OpenAI from "openai"; import OpenAI from "openai";
import { import {
ChatCompletionAssistantMessageParam, ChatCompletionMessage,
ChatCompletionSystemMessageParam,
ChatCompletionUserMessageParam, ChatCompletionUserMessageParam,
ChatCompletionSystemMessageParam,
} from "openai/resources"; } from "openai/resources";
import { TailchatWsClient } from "tailchat-client-sdk"; import { TailchatWsClient } from "tailchat-client-sdk";
type ChatCompletion = export interface ImageMessage {
| ChatCompletionSystemMessageParam
| ChatCompletionUserMessageParam
| ChatCompletionAssistantMessageParam;
interface ImageUrl {
role: "user"; role: "user";
content: [ content: [
{ {
@ -27,7 +22,13 @@ interface ImageUrl {
]; ];
} }
export type Message =
| ChatCompletionMessage
| ChatCompletionUserMessageParam
| ChatCompletionSystemMessageParam;
export type validImgSize = "256x256" | "512x512" | "1024x1024"; export type validImgSize = "256x256" | "512x512" | "1024x1024";
export type validImgAmount = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10; export type validImgAmount = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
export interface ImageRequestData { export interface ImageRequestData {
@ -76,4 +77,5 @@ export type Temperature =
| 1.8 | 1.8
| 1.9 | 1.9
| 2.0; | 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 * as fs from "fs";
import { Messages } from "./types"; import { Messages } from "./types";
import { ChatCompletion, ChatCompletionMessage } from "openai/resources";
export function checkFile( export function checkFile(
file: string, file: string,
@ -39,6 +40,14 @@ export function checkFile(
return final; 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) { export async function getUserInfo(host: string, id: string) {
/** /**
* Returns various info about a user given a host and server ID. * Returns various info about a user given a host and server ID.