Add some message formatting utils, fix some types

This commit is contained in:
powermaker450 2024-07-20 10:43:17 -04:00
parent 49e561da12
commit c7cb3274b5
3 changed files with 44 additions and 26 deletions

View file

@ -8,6 +8,8 @@ import {
isUserBot,
contentOf,
messageOf,
formatImageMessage,
formatUserMessage,
} from "./utils";
import {
allowedChat,
@ -115,21 +117,7 @@ client.connect().then(async () => {
content: "[md]`Analyzing image...`[/md]",
});
session.push({
role: "user",
content: [
{
type: "text",
text: `${username} said: Describe what is in this image.`,
},
{
type: "image_url",
image_url: {
url: imageData,
},
},
],
});
session.push(formatImageMessage(username, imageData));
const response = await assistant.chat.completions.create({
messages: session,
@ -161,10 +149,7 @@ client.connect().then(async () => {
content: THINKING,
});
session.push({
role: "user",
content: `${username} said: ${stripMentionTag(message.content).trim()}`,
});
session.push(formatUserMessage(username, message.content));
const response = await assistant.chat.completions.create({
messages: session,
@ -202,10 +187,7 @@ client.connect().then(async () => {
content: THINKING,
});
session.push({
role: "user",
content: `[DIRECT MESSAGE] ${username} said: ${stripMentionTag(message.content).trim()}`,
});
session.push(formatUserMessage(username, message.content));
const response = await assistant.chat.completions.create({
messages: session,

View file

@ -11,7 +11,7 @@ export interface ImageMessage {
content: [
{
type: "text";
text: string;
text: `${string} said: Describe what is in this image.`;
},
{
type: "image_url";
@ -30,7 +30,7 @@ export type AnyChatCompletion =
export enum ImageSize {
Small = "256x256",
Medium = "512x512",
Large = "1024x1024"
Large = "1024x1024",
}
export type validImgAmount = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;

View file

@ -1,6 +1,7 @@
import * as fs from "fs";
import { Messages } from "./types";
import { AnyChatCompletion, ImageMessage, Messages } from "./types";
import { ChatCompletion, ChatCompletionMessage } from "openai/resources";
import { stripMentionTag } from "tailchat-client-sdk";
export function checkFile(
file: string,
@ -36,6 +37,37 @@ export function checkFile(
return final;
}
export function formatUserMessage(
username: string,
content: string,
): AnyChatCompletion {
return {
role: "user",
content: `${username} said: ${stripMentionTag(content).trim()}`,
};
}
export function formatImageMessage(
username: string,
imageUrl: string,
): ImageMessage {
return {
role: "user",
content: [
{
type: "text",
text: `${username} said: Describe what is in this image.`,
},
{
type: "image_url",
image_url: {
url: imageUrl,
},
},
],
};
}
export function messageOf(response: ChatCompletion): ChatCompletionMessage {
return response.choices.at(0)!.message;
}
@ -87,6 +119,10 @@ export async function getUsername(host: string, id: string) {
*
* @returns The username associated with the given user ID.
*/
if (!id) {
throw new Error("No user ID was given.");
}
const reply = await getUserInfo(host, id);
return reply.nickname;