From 0e70a0d57a55a17c5aa3e65d7c3acfb74d987e15 Mon Sep 17 00:00:00 2001 From: powermaker450 Date: Wed, 3 Jul 2024 22:49:54 -0400 Subject: [PATCH] Add and use messageOf() and contentOf() --- src/bot.ts | 27 +++++++++++++++++---------- src/types.ts | 20 +++++++++++--------- src/utils.ts | 9 +++++++++ 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index 0a165bb..3cba1a7 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -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("", ""); + response.choices.at(0)!.message.content = contentOf(response).replace( + "", + "", + ); - 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"); diff --git a/src/types.ts b/src/types.ts index bcea890..9def1e3 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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; + +export type Messages = Array; diff --git a/src/utils.ts b/src/utils.ts index d056bb0..ab7cc32 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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.