@@ -383,30 +383,55 @@ export class DeepSeekProvider implements AIProvider {
383383 ) : Promise < string > {
384384 this . validateRequest ( question , correctAnswers , apiKey ) ;
385385
386+ const prompt = `Question: ${ question } \n\nCorrect answers: ${ correctAnswers . join (
387+ ", " ,
388+ ) } \n\nPlease provide a clear and concise explanation of why these answers are correct. Focus on the key concepts and reasoning.`;
389+
386390 try {
387- const response = await fetch ( "/ api/ai/deepseek " , {
391+ const response = await fetch ( "https:// api.deepseek.com/v1/chat/completions " , {
388392 method : "POST" ,
389- headers : { "Content-Type" : "application/json" } ,
393+ headers : {
394+ "Authorization" : `Bearer ${ apiKey } ` ,
395+ "Content-Type" : "application/json" ,
396+ } ,
390397 body : JSON . stringify ( {
391- question,
392- correctAnswers,
393- apiKey,
398+ model : "deepseek-chat" ,
399+ messages : [
400+ {
401+ role : "user" ,
402+ content : prompt ,
403+ } ,
404+ ] ,
405+ max_tokens : 500 ,
406+ temperature : 0.7 ,
394407 } ) ,
395408 } ) ;
396409
397410 if ( ! response . ok ) {
398- const errorType = response . status === 401 ? 'auth' :
399- response . status === 429 ? 'rate_limit' : 'network' ;
400- throw new AIProviderError ( this . name , errorType , `DeepSeek API error: ${ response . status } ` ) ;
411+ const errorData = await response . json ( ) . catch ( ( ) => ( { } ) ) ;
412+ const errorMessage = errorData . error ?. message || errorData . message || `HTTP ${ response . status } ` ;
413+
414+ // DeepSeek-specific error handling
415+ if ( response . status === 401 ) {
416+ throw new AIProviderError ( this . name , 'auth' , `Authentication failed: ${ errorMessage } ` ) ;
417+ } else if ( response . status === 429 ) {
418+ throw new AIProviderError ( this . name , 'rate_limit' , `Rate limit exceeded: ${ errorMessage } ` ) ;
419+ } else if ( response . status === 400 ) {
420+ throw new AIProviderError ( this . name , 'validation' , `Invalid request: ${ errorMessage } ` ) ;
421+ } else if ( response . status >= 500 ) {
422+ throw new AIProviderError ( this . name , 'network' , `DeepSeek server error: ${ errorMessage } ` ) ;
423+ } else {
424+ throw new AIProviderError ( this . name , 'network' , `DeepSeek API error: ${ errorMessage } ` ) ;
425+ }
401426 }
402427
403428 const data = await response . json ( ) ;
404429
405- if ( ! data . explanation ) {
406- throw new AIProviderError ( this . name , 'validation' , 'Invalid response from DeepSeek' ) ;
430+ if ( ! data . choices ?. [ 0 ] ?. message ?. content ) {
431+ throw new AIProviderError ( this . name , 'validation' , 'Invalid response structure from DeepSeek' ) ;
407432 }
408433
409- return data . explanation ;
434+ return data . choices [ 0 ] . message . content ;
410435 } catch ( error ) {
411436 if ( error instanceof AIProviderError ) throw error ;
412437 const errorMessage = error instanceof Error ? error . message : 'Unknown error' ;
@@ -419,7 +444,8 @@ export class DeepSeekProvider implements AIProvider {
419444 }
420445
421446 validateConfig ( apiKey ?: string ) : boolean {
422- return ! ! ( apiKey && apiKey . length > 10 ) ; // Basic validation for DeepSeek keys
447+ // DeepSeek API keys typically start with "sk-" and have reasonable length
448+ return ! ! ( apiKey && apiKey . length > 20 ) ;
423449 }
424450
425451 private validateRequest ( question : string , correctAnswers : string [ ] , apiKey : string ) : void {
@@ -430,7 +456,7 @@ export class DeepSeekProvider implements AIProvider {
430456 throw new AIProviderError ( this . name , 'validation' , 'At least one correct answer is required' ) ;
431457 }
432458 if ( ! this . validateConfig ( apiKey ) ) {
433- throw new AIProviderError ( this . name , 'auth' , 'Invalid DeepSeek API key' ) ;
459+ throw new AIProviderError ( this . name , 'auth' , 'Invalid DeepSeek API key format ' ) ;
434460 }
435461 }
436462}
0 commit comments