forked from lobehub/lobe-chat
-
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.
🐛 fix: fix Perplexity duplicate token
- Loading branch information
Showing
4 changed files
with
32 additions
and
83 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
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 |
---|---|---|
@@ -1,85 +1,29 @@ | ||
import { OpenAIStream, StreamingTextResponse } from 'ai'; | ||
import OpenAI, { ClientOptions } from 'openai'; | ||
import OpenAI from 'openai'; | ||
|
||
import { LobeRuntimeAI } from '../BaseAI'; | ||
import { AgentRuntimeErrorType } from '../error'; | ||
import { ChatCompetitionOptions, ChatStreamPayload, ModelProvider } from '../types'; | ||
import { AgentRuntimeError } from '../utils/createError'; | ||
import { debugStream } from '../utils/debugStream'; | ||
import { desensitizeUrl } from '../utils/desensitizeUrl'; | ||
import { handleOpenAIError } from '../utils/handleOpenAIError'; | ||
import { ChatStreamPayload, ModelProvider } from '../types'; | ||
import { LobeOpenAICompatibleFactory } from '../utils/openaiCompatibleFactory'; | ||
|
||
const DEFAULT_BASE_URL = 'https://api.perplexity.ai'; | ||
|
||
export class LobePerplexityAI implements LobeRuntimeAI { | ||
private client: OpenAI; | ||
|
||
baseURL: string; | ||
|
||
constructor({ apiKey, baseURL = DEFAULT_BASE_URL, ...res }: ClientOptions) { | ||
if (!apiKey) throw AgentRuntimeError.createError(AgentRuntimeErrorType.InvalidPerplexityAPIKey); | ||
|
||
this.client = new OpenAI({ apiKey, baseURL, ...res }); | ||
this.baseURL = this.client.baseURL; | ||
} | ||
|
||
async chat(payload: ChatStreamPayload, options?: ChatCompetitionOptions) { | ||
try { | ||
export const LobePerplexityAI = LobeOpenAICompatibleFactory({ | ||
baseURL: 'https://api.perplexity.ai', | ||
chatCompletion: { | ||
handlePayload: (payload: ChatStreamPayload) => { | ||
// Set a default frequency penalty value greater than 0 | ||
const defaultFrequencyPenalty = 0.1; | ||
const chatPayload = { | ||
const defaultFrequencyPenalty = 1; | ||
|
||
return { | ||
...payload, | ||
frequency_penalty: payload.frequency_penalty || defaultFrequencyPenalty, | ||
}; | ||
const response = await this.client.chat.completions.create( | ||
chatPayload as unknown as OpenAI.ChatCompletionCreateParamsStreaming, | ||
{ signal: options?.signal }, | ||
); | ||
const [prod, debug] = response.tee(); | ||
|
||
if (process.env.DEBUG_PERPLEXITY_CHAT_COMPLETION === '1') { | ||
debugStream(debug.toReadableStream()).catch(console.error); | ||
} | ||
|
||
return new StreamingTextResponse(OpenAIStream(prod, options?.callback), { | ||
headers: options?.headers, | ||
}); | ||
} catch (error) { | ||
let desensitizedEndpoint = this.baseURL; | ||
|
||
if (this.baseURL !== DEFAULT_BASE_URL) { | ||
desensitizedEndpoint = desensitizeUrl(this.baseURL); | ||
} | ||
|
||
if ('status' in (error as any)) { | ||
switch ((error as Response).status) { | ||
case 401: { | ||
throw AgentRuntimeError.chat({ | ||
endpoint: desensitizedEndpoint, | ||
error: error as any, | ||
errorType: AgentRuntimeErrorType.InvalidPerplexityAPIKey, | ||
provider: ModelProvider.Perplexity, | ||
}); | ||
} | ||
|
||
default: { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
const { errorResult, RuntimeError } = handleOpenAIError(error); | ||
|
||
const errorType = RuntimeError || AgentRuntimeErrorType.PerplexityBizError; | ||
|
||
throw AgentRuntimeError.chat({ | ||
endpoint: desensitizedEndpoint, | ||
error: errorResult, | ||
errorType, | ||
provider: ModelProvider.Perplexity, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export default LobePerplexityAI; | ||
stream: true, | ||
} as OpenAI.ChatCompletionCreateParamsStreaming; | ||
}, | ||
}, | ||
debug: { | ||
chatCompletion: () => process.env.DEBUG_PERPLEXITY_CHAT_COMPLETION === '1', | ||
}, | ||
errorType: { | ||
bizError: AgentRuntimeErrorType.PerplexityBizError, | ||
invalidAPIKey: AgentRuntimeErrorType.InvalidPerplexityAPIKey, | ||
}, | ||
provider: ModelProvider.Perplexity, | ||
}); |