Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: increment/decrement ai_credits on ai services #100

Merged
merged 1 commit into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions lib/services/ai/create-episode-summary-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { OpenAI } from 'openai';
import { OpenAIStream } from 'ai';
import { promptTokensEstimate } from 'openai-chat-tokens';

import { incrementAccountAICredits } from '../account';
import { saveEpisodeContentSummary } from '../episode-content';
import { openai } from './openai';

Expand All @@ -26,32 +27,37 @@ export async function createEpisodeSummaryStream({
episode: Pick<Tables<'episode'>, 'id' | 'title'>;
transcription: string;
}) {
const messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = [
{
content: `
try {
const messages: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = [
{
content: `
Summarize the following transcription of the podcast episode "${episode.title}" by following these guidelines:
- Skip the ads and intro.
- Use paragraphs or bullet point lists.
- Prefer short, clean sentences.
- Write the summary in the same language as the transcription.
`,
role: 'system',
},
{
content: transcription,
role: 'system',
},
];
role: 'system',
},
{
content: transcription,
role: 'system',
},
];

const response = await openai.chat.completions.create({
messages,
model: getModel(promptTokensEstimate({ messages })),
stream: true,
});
const response = await openai.chat.completions.create({
messages,
model: getModel(promptTokensEstimate({ messages })),
stream: true,
});

return OpenAIStream(response, {
onCompletion: async (completion) => {
await saveEpisodeContentSummary(episode.id, completion);
},
});
return OpenAIStream(response, {
onCompletion: async (completion) => {
await saveEpisodeContentSummary(episode.id, completion);
},
});
} catch (error) {
await incrementAccountAICredits();
throw error;
}
}
97 changes: 54 additions & 43 deletions lib/services/ai/transcribe-episode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,71 @@ import { DatabaseError } from '@/lib/errors';
import { getFinalRedirectURL } from '@/lib/utils/get-final-redirect-url';
import { cookies } from 'next/headers';

import { getAccountId } from '../account';
import {
decrementAccountAICredits,
getAccountId,
incrementAccountAICredits,
} from '../account';
import { createSupabaseServerClient } from '../supabase/server';
import { transcribeAudio } from './deepgram';

export const transcribeEpisode = async (id: Tables<'episode'>['id']) => {
const supabase = createSupabaseServerClient(cookies());
try {
await decrementAccountAICredits();

const existingEpisodeContentQuery = await supabase
.from('episode_content')
.select('transcription')
.eq('episode', id);
const supabase = createSupabaseServerClient(cookies());

if (existingEpisodeContentQuery.error) {
throw new DatabaseError(existingEpisodeContentQuery.error);
}
const existingEpisodeContentQuery = await supabase
.from('episode_content')
.select('transcription')
.eq('episode', id);

if (existingEpisodeContentQuery.data[0]?.transcription) {
return existingEpisodeContentQuery.data[0].transcription;
}
if (existingEpisodeContentQuery.error) {
throw new DatabaseError(existingEpisodeContentQuery.error);
}

const episodeQuery = await supabase
.from('episode')
.select('audio_url, show(language)')
.eq('id', id)
.single();
if (existingEpisodeContentQuery.data[0]?.transcription) {
return existingEpisodeContentQuery.data[0].transcription;
}

if (episodeQuery.error) {
throw new DatabaseError(episodeQuery.error);
}
const episodeQuery = await supabase
.from('episode')
.select('audio_url, show(language)')
.eq('id', id)
.single();

const url = await getFinalRedirectURL(episodeQuery.data.audio_url);
const transcription = await transcribeAudio({
language: episodeQuery.data.show?.language ?? undefined,
url,
});
if (episodeQuery.error) {
throw new DatabaseError(episodeQuery.error);
}

const updateEpisodeContentQuery = await supabase
.from('episode_content')
.upsert(
{
account: await getAccountId(),
episode: id,
transcription,
},
{
onConflict: 'episode',
},
)
.select('transcription')
.single();
const url = await getFinalRedirectURL(episodeQuery.data.audio_url);
const transcription = await transcribeAudio({
language: episodeQuery.data.show?.language ?? undefined,
url,
});

if (updateEpisodeContentQuery.error) {
throw new DatabaseError(updateEpisodeContentQuery.error);
}
const updateEpisodeContentQuery = await supabase
.from('episode_content')
.upsert(
{
account: await getAccountId(),
episode: id,
transcription,
},
{
onConflict: 'episode',
},
)
.select('transcription')
.single();

if (updateEpisodeContentQuery.error) {
throw new DatabaseError(updateEpisodeContentQuery.error);
}

return transcription;
return transcription;
} catch (error) {
await incrementAccountAICredits();
throw error;
}
};
Loading