openai-code
Version:
An unofficial proxy layer that lets you use Anthropic Claude Code with any OpenAI API backend.
67 lines (58 loc) • 2.04 kB
JavaScript
import { getEnv } from '../env.mjs';
import { getCommand } from '../command.mjs';
// Calls the Perplexity.ai API using a given query.
// This implementation replaces the use of ai and createOpenAI with a direct fetch request.
export async function perplexity(query, model = "sonar-pro", domainFilter = []) {
try {
// Get the perplexity token from environment variables
const token = getEnv('OPENAI_CODE_PERPLEXITY_API_KEY');
if (!token) {
console.error("Error: OPENAI_CODE_PERPLEXITY_API_KEY not set!")
return {
citations: null,
text: null,
}
}
// Construct the request options with dynamic query incorporation
const options = {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model,
messages: [
{ role: "system", content: "Be precise and concise. Research throughly, think hard." },
{ role: "user", content: query }
],
temperature: 0.2,
top_p: 0.9,
return_images: false,
return_related_questions: false,
top_k: 0,
stream: false,
presence_penalty: 0,
frequency_penalty: 1,
web_search_options: { search_context_size: "high" }
})
};
if (domainFilter.length > 0) {
options.search_domain_filter = domainFilter;
}
// Call the Perplexity.ai API using fetch
const response = await fetch('https://api.perplexity.ai/chat/completions', options);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
return {
citations: data.citations,
text: data.choices[0].message.content
};
} catch (e) {
return `Error using Perplexity.ai: ${e}`;
}
}
export const getPerplexityN = (commands) => getCommand(commands, 'perplexity', 0);
export const isPerplexityActivated = (commands) => getPerplexityN(commands) !== null;