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 * as fs from "fs";
import { ImageRequestData, Messages } from "./types";
import {
checkFile,
getUsername,
isUserBot,
contentOf,
messageOf,
} from "./utils";
import { checkFile, getUsername, isUserBot } from "./utils";
import {
allowedChat,
safeWord,
@ -134,18 +128,17 @@ client.connect().then(async () => {
model: imageModel,
temperature: temperature,
});
response.choices.at(0)!.message.content = contentOf(response).replace(
"</s>",
"",
);
response.choices.at(0)!.message.content = response.choices
.at(0)!
.message.content!.replace("</s>", "");
session.push(messageOf(response));
session.push(response.choices.at(0)!.message);
await client.sendMessage({
converseId: message.converseId,
groupId: message.groupId,
// 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");
@ -170,13 +163,13 @@ client.connect().then(async () => {
temperature: temperature,
});
session.push(messageOf(response));
session.push(response.choices.at(0)!.message);
await client.sendMessage({
converseId: message.converseId,
groupId: message.groupId,
// 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");
@ -211,11 +204,11 @@ client.connect().then(async () => {
temperature: temperature,
});
session.push(messageOf(response));
session.push(response.choices.at(0)!.message);
await client.sendMessage({
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");

View file

@ -1,12 +1,17 @@
import OpenAI from "openai";
import {
ChatCompletionMessage,
ChatCompletionUserMessageParam,
ChatCompletionAssistantMessageParam,
ChatCompletionSystemMessageParam,
ChatCompletionUserMessageParam,
} from "openai/resources";
import { TailchatWsClient } from "tailchat-client-sdk";
export interface ImageMessage {
type ChatCompletion =
| ChatCompletionSystemMessageParam
| ChatCompletionUserMessageParam
| ChatCompletionAssistantMessageParam;
interface ImageUrl {
role: "user";
content: [
{
@ -22,13 +27,7 @@ export interface ImageMessage {
];
}
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 {
@ -77,5 +76,4 @@ export type Temperature =
| 1.8
| 1.9
| 2.0;
export type Messages = Array<Message | ImageMessage>;
export type Messages = Array<ChatCompletion | ImageUrl>;

View file

@ -1,6 +1,5 @@
import * as fs from "fs";
import { Messages } from "./types";
import { ChatCompletion, ChatCompletionMessage } from "openai/resources";
export function checkFile(
file: string,
@ -27,7 +26,11 @@ export function checkFile(
if (fs.existsSync(file)) {
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 {
fs.createWriteStream(file);
final = generic;
@ -36,14 +39,6 @@ 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.