-
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.
refactor: move summary i/o to endpoint (#85)
- decreases the payload of requests sent from the client - moves `saveEpisodeContentSummary` to the server, so that the client doesn't have to send the entire episode content to the server
- Loading branch information
Showing
6 changed files
with
88 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,62 @@ | ||
import { | ||
DatabaseError, | ||
HttpBadRequestError, | ||
HttpInternalServerError, | ||
} from '@/lib/errors'; | ||
import { openai } from '@/lib/services/ai/openai'; | ||
import { saveEpisodeContentSummary } from '@/lib/services/episode-content'; | ||
import { createSupabaseServerClient } from '@/lib/services/supabase/server'; | ||
import { OpenAIStream, StreamingTextResponse } from 'ai'; | ||
import { cookies } from 'next/headers'; | ||
|
||
export const runtime = 'edge'; | ||
|
||
export async function POST( | ||
req: Request, | ||
{ params }: { params: { 'episode-id': string } }, | ||
) { | ||
const episodeId = parseInt(params['episode-id'], 10); | ||
|
||
const supabase = createSupabaseServerClient(cookies()); | ||
|
||
const { data, error } = await supabase | ||
.from('episode_content') | ||
.select('transcription, episode(title)') | ||
.eq('episode', episodeId) | ||
.single(); | ||
|
||
if (error) { | ||
return new HttpInternalServerError({ | ||
error: new DatabaseError(error), | ||
}).toNextResponse(); | ||
} | ||
|
||
if (!data.transcription) { | ||
return new HttpBadRequestError({ | ||
message: 'Episode has no transcription', | ||
}).toNextResponse(); | ||
} | ||
|
||
const response = await openai.chat.completions.create({ | ||
messages: [ | ||
{ | ||
content: `Summarize the following transcription from ${data.episode?.title} episode of the podcast`, | ||
role: 'system', | ||
}, | ||
{ | ||
content: data.transcription, | ||
role: 'system', | ||
}, | ||
], | ||
model: 'gpt-4-1106-preview', | ||
stream: true, | ||
}); | ||
|
||
const stream = OpenAIStream(response, { | ||
onCompletion: async (completion) => { | ||
await saveEpisodeContentSummary(episodeId, completion); | ||
}, | ||
}); | ||
|
||
return new StreamingTextResponse(stream); | ||
} |
This file was deleted.
Oops, something went wrong.
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
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
0e2f2d9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
beecast-staging – ./
beecast-staging-git-main-experiment-station.vercel.app
beecast-staging-experiment-station.vercel.app
staging.beecast.ai