-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
164 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { OpenAI } from 'openai'; | ||
|
||
import { openai } from '@/lib/services/ai/openai'; | ||
import { OpenAIStream, StreamingTextResponse } from 'ai'; | ||
|
||
export const runtime = 'edge'; | ||
|
||
export async function POST(req: Request) { | ||
const { messages } = (await req.json()) as { | ||
messages: OpenAI.Chat.ChatCompletionCreateParamsStreaming['messages']; | ||
}; | ||
|
||
const response = await openai.chat.completions.create({ | ||
messages, | ||
model: 'gpt-4-1106-preview', | ||
stream: true, | ||
}); | ||
|
||
const stream = OpenAIStream(response); | ||
|
||
return new StreamingTextResponse(stream); | ||
} |
13 changes: 0 additions & 13 deletions
13
components/episode-ai-thingy/episode-ai-summary-stream.tsx
This file was deleted.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
components/episode-ai-thingy/episode-ai-summary-streamer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { Tables } from '@/types/supabase/database'; | ||
|
||
import { saveEpisodeContentSummary } from '@/lib/services/episode-content'; | ||
import { useChat } from 'ai/react'; | ||
import { useEffect, useRef } from 'react'; | ||
|
||
type Props = { | ||
id: Tables<'episode'>['id']; | ||
title: Tables<'episode'>['title']; | ||
transcription: string; | ||
}; | ||
|
||
export function EpisodeAISummaryStreamer({ id, title, transcription }: Props) { | ||
const startedRef = useRef(false); | ||
|
||
const { messages, reload, setMessages } = useChat({ | ||
api: '/api/ai/summarize', | ||
onFinish: (message) => { | ||
if (message.role === 'system') { | ||
return; | ||
} | ||
|
||
void saveEpisodeContentSummary(id, message.content); | ||
}, | ||
}); | ||
|
||
useEffect(() => { | ||
if (startedRef.current) { | ||
return; | ||
} | ||
|
||
startedRef.current = true; | ||
|
||
setMessages([ | ||
{ | ||
content: `Summarize the podcast episode titled '${title}' in a short paragraph.`, | ||
id: 'system-0', | ||
role: 'system', | ||
}, | ||
{ | ||
content: transcription, | ||
id: 'system-1', | ||
role: 'system', | ||
}, | ||
]); | ||
|
||
void reload(); | ||
}, [reload, setMessages, title, transcription]); | ||
|
||
const summaryMessage = messages.find((message) => message.role !== 'system'); | ||
|
||
return ( | ||
<div> | ||
{summaryMessage ? summaryMessage.content : 'Summarizing episode...'} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type { PropsWithChildren } from 'react'; | ||
|
||
import { Card, Heading, Text } from '@radix-ui/themes'; | ||
|
||
type Props = PropsWithChildren<{ | ||
title: string; | ||
}>; | ||
|
||
export function Panel({ children, title }: Props) { | ||
return ( | ||
<Card> | ||
<Heading mb="2" size="2"> | ||
{title} | ||
</Heading> | ||
|
||
<Text color="gray" size="2"> | ||
{children} | ||
</Text> | ||
</Card> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,4 @@ | ||
import { env } from '@/env.mjs'; | ||
import { OpenAI } from 'openai'; | ||
|
||
const openai = new OpenAI({ apiKey: env.OPENAI_API_KEY }); | ||
|
||
export const summarizeEpisodeTranscript = async ({ | ||
title, | ||
transcript, | ||
}: { | ||
title: string; | ||
transcript: string; | ||
}) => { | ||
const response = await openai.chat.completions.create({ | ||
messages: [ | ||
{ | ||
content: `Summarize the following transcript from a podcast episode titled as ${title}`, | ||
role: 'system', | ||
}, | ||
{ | ||
content: transcript, | ||
role: 'system', | ||
}, | ||
], | ||
model: 'gpt-4-1106-preview', | ||
}); | ||
|
||
return response.choices[0].message.content; | ||
}; | ||
export const openai = new OpenAI({ apiKey: env.OPENAI_API_KEY }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use server'; | ||
|
||
import type { Tables } from '@/types/supabase/database'; | ||
|
||
import { cookies } from 'next/headers'; | ||
|
||
import { DatabaseError } from '../errors'; | ||
import { getAccountId } from './account'; | ||
import { createSupabaseServerClient } from './supabase/server'; | ||
|
||
export const saveEpisodeContentSummary = async ( | ||
id: Tables<'episode'>['id'], | ||
summary: string, | ||
) => { | ||
const supabase = createSupabaseServerClient(cookies()); | ||
|
||
const { error } = await supabase.from('episode_content').upsert( | ||
{ | ||
account: await getAccountId(), | ||
episode: id, | ||
text_summary: summary, | ||
}, | ||
{ | ||
onConflict: 'episode', | ||
}, | ||
); | ||
|
||
if (error) { | ||
throw new DatabaseError(error); | ||
} | ||
}; |