Skip to content

Commit

Permalink
use markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
pyama committed Aug 27, 2024
1 parent f22dc03 commit 968a40b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 34 deletions.
7 changes: 6 additions & 1 deletion src/mention.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ describe('appMention', () => {
expect(mockSay).toHaveBeenCalledTimes(1)
expect(mockSay).toHaveBeenNthCalledWith(1, {
text: 'GPTの回答',
thread_ts: 'test_ts'
thread_ts: 'test_ts',
type: 'mrkdwn'
})
})

Expand Down Expand Up @@ -98,6 +99,10 @@ describe('appMention', () => {

expect(generateSummary).toHaveBeenCalledWith(
[
{
role: 'system',
content: [{ text: '応答はマークダウンで行ってください。', type: 'text' }]
},
{
role: 'user',
content: [{ text: 'test message 1', type: 'text' }]
Expand Down
77 changes: 44 additions & 33 deletions src/mention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,55 @@ export const appMention: any = async ({ event, client, say }) => {

const isSummaryRequest = event.text.includes('今北産業')

const threadMessages = await Promise.all(
replies.messages.map(async (message) => {
if (!isSummaryRequest && message.user !== botUserId && !message.text.includes(`<@${botUserId}>`)) {
return null
}
const aiInstructionMessage = {
role: 'system',
content: [
{ text: '応答はマークダウンで行ってください。', type: 'text' }
]
}

const threadMessages = [
aiInstructionMessage,
...(await Promise.all(
replies.messages.map(async (message) => {
if (!isSummaryRequest && message.user !== botUserId && !message.text.includes(`<@${botUserId}>`)) {
return null
}

const contents = []
const contents = []

if (message.files) {
for (const file of message.files) {
if ('url_private_download' in file) {
const encodedImage = await downloadFileAsBase64(file.url_private_download)
let filetype = file.filetype
if (filetype === 'jpg') {
filetype = 'jpeg'
}
if (message.files) {
for (const file of message.files) {
if ('url_private_download' in file) {
const encodedImage = await downloadFileAsBase64(file.url_private_download)
let filetype = file.filetype
if (filetype === 'jpg') {
filetype = 'jpeg'
}

if (encodedImage) {
model = 'gpt-4o'
max_tokens = 4096
contents.push({
image_url: {
url: 'data:image/' + filetype + ';base64,' + encodedImage,
detail: 'auto'
},
type: 'image_url'
})
if (encodedImage) {
model = 'gpt-4o'
max_tokens = 4096
contents.push({
image_url: {
url: 'data:image/' + filetype + ';base64,' + encodedImage,
detail: 'auto'
},
type: 'image_url'
})
}
}
}
}
}

contents.push({ text: (message.text || '').replace(`<@${botUserId}>`, ''), type: 'text' })
return {
role: message.user === botUserId ? 'assistant' : 'user',
content: contents
}
})
)
contents.push({ text: (message.text || '').replace(`<@${botUserId}>`, ''), type: 'text' })
return {
role: message.user === botUserId ? 'assistant' : 'user',
content: contents
}
})
))
]

if (isSummaryRequest) {
const summary = await generateSummary(threadMessages.filter(nonNullable), model, max_tokens)
Expand All @@ -74,7 +84,8 @@ export const appMention: any = async ({ event, client, say }) => {

await say({
text: gptAnswerText,
thread_ts: event.ts
thread_ts: event.ts,
type: 'mrkdwn'
})
} catch (error) {
console.error(error)
Expand Down

0 comments on commit 968a40b

Please sign in to comment.