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

View file

@ -11,7 +11,7 @@ export interface ImageMessage {
content: [ content: [
{ {
type: "text"; type: "text";
text: string; text: `${string} said: Describe what is in this image.`;
}, },
{ {
type: "image_url"; type: "image_url";
@ -30,7 +30,7 @@ export type AnyChatCompletion =
export enum ImageSize { export enum ImageSize {
Small = "256x256", Small = "256x256",
Medium = "512x512", Medium = "512x512",
Large = "1024x1024" Large = "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;

View file

@ -1,6 +1,7 @@
import * as fs from "fs"; import * as fs from "fs";
import { Messages } from "./types"; import { AnyChatCompletion, ImageMessage, Messages } from "./types";
import { ChatCompletion, ChatCompletionMessage } from "openai/resources"; import { ChatCompletion, ChatCompletionMessage } from "openai/resources";
import { stripMentionTag } from "tailchat-client-sdk";
export function checkFile( export function checkFile(
file: string, file: string,
@ -36,6 +37,37 @@ export function checkFile(
return final; 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 { export function messageOf(response: ChatCompletion): ChatCompletionMessage {
return response.choices.at(0)!.message; 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. * @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); const reply = await getUserInfo(host, id);
return reply.nickname; return reply.nickname;