-
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.
Merge pull request #2 from cdreetz/streaming_eval
Streaming eval
- Loading branch information
Showing
16 changed files
with
683 additions
and
37 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
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,16 @@ | ||
import { anthropic } from '@ai-sdk/anthropic'; | ||
import { streamText } from 'ai'; | ||
|
||
export const maxDuration = 30; | ||
|
||
export async function POST(req: Request) { | ||
const { messages, model } = await req.json(); | ||
|
||
const result = await streamText({ | ||
model: anthropic(model || 'claude-3-opus-20240229'), | ||
messages, | ||
}); | ||
|
||
return result.toAIStreamResponse(); | ||
} | ||
|
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,56 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import Groq from 'groq-sdk'; | ||
|
||
const groq = new Groq({ | ||
apiKey: process.env.GROQ_API_KEY, | ||
}); | ||
|
||
export async function POST(request: NextRequest) { | ||
try { | ||
const formData = await request.formData(); | ||
const file = formData.get('file') as File; | ||
|
||
if (!file) { | ||
return NextResponse.json({ error: 'No file provided' }, { status: 400 }); | ||
} | ||
|
||
const arrayBuffer = await file.arrayBuffer(); | ||
const buffer = Buffer.from(arrayBuffer); | ||
|
||
// Extract optional parameters from formData | ||
const prompt = formData.get('prompt') as string | null; | ||
const responseFormat = formData.get('responseFormat') as string | null; | ||
const temperature = formData.get('temperature') as string | null; | ||
const language = formData.get('language') as string | null; | ||
const model = formData.get('model') as string | null; | ||
|
||
const transcriptionOptions: any = { | ||
file: new File([buffer], file.name, { type: file.type }), | ||
model: model === 'distil-whisper-large-v3-en' ? 'distil-whisper-large-v3-en' : 'whisper-large-v3', | ||
}; | ||
|
||
// Add optional parameters if provided | ||
if (prompt) transcriptionOptions.prompt = prompt; | ||
if (responseFormat) transcriptionOptions.response_format = responseFormat; | ||
if (temperature) transcriptionOptions.temperature = parseFloat(temperature); | ||
if (language) transcriptionOptions.language = language; | ||
|
||
const transcription = await groq.audio.transcriptions.create(transcriptionOptions); | ||
|
||
// Handle different response formats | ||
if (responseFormat === 'verbose_json') { | ||
return NextResponse.json(transcription); | ||
} else if (responseFormat === 'text') { | ||
return new NextResponse(transcription.text, { | ||
headers: { 'Content-Type': 'text/plain' } | ||
}); | ||
} else { | ||
// Default to JSON response with just the text | ||
return NextResponse.json({ text: transcription.text }); | ||
//return NextResponse.json({ text: JSON.stringify(transcription) }); | ||
} | ||
} catch (error) { | ||
console.error('Transcription error:', error); | ||
return NextResponse.json({ error: 'Transcription failed' }, { status: 500 }); | ||
} | ||
} |
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
Oops, something went wrong.