feat(research): Introduce research API with provider implementations, docs and tests
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import * as plugins from './plugins.js';
|
||||
import * as paths from './paths.js';
|
||||
import { MultiModalModel } from './abstract.classes.multimodal.js';
|
||||
import type { ChatOptions, ChatResponse, ChatMessage } from './abstract.classes.multimodal.js';
|
||||
import type { ChatOptions, ChatResponse, ChatMessage, ResearchOptions, ResearchResponse } from './abstract.classes.multimodal.js';
|
||||
|
||||
export interface IPerplexityProviderOptions {
|
||||
perplexityToken: string;
|
||||
@@ -168,4 +168,69 @@ export class PerplexityProvider extends MultiModalModel {
|
||||
}): Promise<{ message: any }> {
|
||||
throw new Error('Document processing is not supported by Perplexity.');
|
||||
}
|
||||
|
||||
public async research(optionsArg: ResearchOptions): Promise<ResearchResponse> {
|
||||
// Perplexity has Sonar models that are optimized for search
|
||||
// sonar models: sonar, sonar-pro
|
||||
const model = optionsArg.searchDepth === 'deep' ? 'sonar-pro' : 'sonar';
|
||||
|
||||
try {
|
||||
const response = await fetch('https://api.perplexity.ai/chat/completions', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${this.options.perplexityToken}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model,
|
||||
messages: [
|
||||
{
|
||||
role: 'system',
|
||||
content: 'You are a helpful research assistant. Provide accurate information with sources.'
|
||||
},
|
||||
{
|
||||
role: 'user',
|
||||
content: optionsArg.query
|
||||
}
|
||||
],
|
||||
temperature: 0.7,
|
||||
max_tokens: 4000
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Perplexity API error: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
const answer = result.choices[0].message.content;
|
||||
|
||||
// Parse citations from the response
|
||||
const sources: Array<{ url: string; title: string; snippet: string }> = [];
|
||||
|
||||
// Perplexity includes citations in the format [1], [2], etc. with sources listed
|
||||
// This is a simplified parser - could be enhanced based on actual Perplexity response format
|
||||
if (result.citations) {
|
||||
for (const citation of result.citations) {
|
||||
sources.push({
|
||||
url: citation.url || '',
|
||||
title: citation.title || '',
|
||||
snippet: citation.snippet || ''
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
answer,
|
||||
sources,
|
||||
metadata: {
|
||||
model,
|
||||
searchDepth: optionsArg.searchDepth || 'basic'
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Perplexity research error:', error);
|
||||
throw new Error(`Failed to perform research: ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user