Compare commits

..

No commits in common. "96ff85ab804a03202034ffc472ea6bd0c9a8b1dc" and "e4d822957c305b9749fe6faf8b1e5a7c1dacf5d7" have entirely different histories.

3 changed files with 24 additions and 38 deletions

View file

@ -2,13 +2,7 @@ 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 { import { checkFile, getUsername, isUserBot } from "./utils";
checkFile,
getUsername,
isUserBot,
contentOf,
messageOf,
} from "./utils";
import { import {
allowedChat, allowedChat,
safeWord, safeWord,
@ -134,18 +128,17 @@ client.connect().then(async () => {
model: imageModel, model: imageModel,
temperature: temperature, temperature: temperature,
}); });
response.choices.at(0)!.message.content = contentOf(response).replace( response.choices.at(0)!.message.content = response.choices
"</s>", .at(0)!
"", .message.content!.replace("</s>", "");
);
session.push(messageOf(response)); session.push(response.choices.at(0)!.message);
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]${contentOf(response)}[/md]`, content: `[md]${response.choices.at(0)?.message.content}[/md]`,
}); });
fs.writeFileSync("./messages.json", JSON.stringify(session), "utf8"); fs.writeFileSync("./messages.json", JSON.stringify(session), "utf8");
@ -170,13 +163,13 @@ client.connect().then(async () => {
temperature: temperature, temperature: temperature,
}); });
session.push(messageOf(response)); session.push(response.choices.at(0)!.message);
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]${contentOf(response)}[/md]`, content: `[md]${response.choices.at(0)?.message.content}[/md]`,
}); });
fs.writeFileSync("./messages.json", JSON.stringify(session), "utf8"); fs.writeFileSync("./messages.json", JSON.stringify(session), "utf8");
@ -211,11 +204,11 @@ client.connect().then(async () => {
temperature: temperature, temperature: temperature,
}); });
session.push(messageOf(response)); session.push(response.choices.at(0)!.message);
await client.sendMessage({ await client.sendMessage({
converseId: message.converseId, converseId: message.converseId,
content: `[md]${contentOf(response)}[/md]`, content: `[md]${response.choices.at(0)?.message.content}[/md]`,
}); });
fs.writeFileSync("./messages.json", JSON.stringify(session), "utf8"); fs.writeFileSync("./messages.json", JSON.stringify(session), "utf8");

View file

@ -1,12 +1,17 @@
import OpenAI from "openai"; import OpenAI from "openai";
import { import {
ChatCompletionMessage, ChatCompletionAssistantMessageParam,
ChatCompletionUserMessageParam,
ChatCompletionSystemMessageParam, ChatCompletionSystemMessageParam,
ChatCompletionUserMessageParam,
} from "openai/resources"; } from "openai/resources";
import { TailchatWsClient } from "tailchat-client-sdk"; import { TailchatWsClient } from "tailchat-client-sdk";
export interface ImageMessage { type ChatCompletion =
| ChatCompletionSystemMessageParam
| ChatCompletionUserMessageParam
| ChatCompletionAssistantMessageParam;
interface ImageUrl {
role: "user"; role: "user";
content: [ content: [
{ {
@ -22,13 +27,7 @@ export interface ImageMessage {
]; ];
} }
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 {
@ -77,5 +76,4 @@ 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,6 +1,5 @@
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,
@ -27,7 +26,11 @@ export function checkFile(
if (fs.existsSync(file)) { if (fs.existsSync(file)) {
const data = fs.readFileSync(file, encoding); const data = fs.readFileSync(file, encoding);
final = !data.toString().trim() ? generic : JSON.parse(data.toString()); final =
data.toString().trim() === ""
? generic
: // @ts-ignore
JSON.parse(data);
} else { } else {
fs.createWriteStream(file); fs.createWriteStream(file);
final = generic; final = generic;
@ -36,14 +39,6 @@ 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.