Skip to content

Commit

Permalink
Update perplexity-llama.ts with axios
Browse files Browse the repository at this point in the history
  • Loading branch information
krishkalaria12 authored Oct 17, 2024
1 parent 0a43029 commit d692164
Showing 1 changed file with 25 additions and 31 deletions.
56 changes: 25 additions & 31 deletions models/perplexity-llama.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import axios from 'axios';
import { z } from 'zod';
import { ModelHandler } from './index';

Expand Down Expand Up @@ -45,39 +46,32 @@ export const perplexityModel: ModelHandler = async (prompt: string, map: string[
{ role: 'user', content: JSON.stringify(map) },
];

const options: RequestInit = {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'llama-3.1-sonar-large-128k-online',
messages,
temperature: 0.2,
top_p: 0.9,
return_citations: true,
search_domain_filter: ['perplexity.ai'],
return_images: false,
return_related_questions: false,
search_recency_filter: 'month',
top_k: 0,
stream: false,
presence_penalty: 0,
frequency_penalty: 1,
}),
const data = {
model: 'llama-3.1-sonar-large-128k-online',
messages,
temperature: 0.2,
top_p: 0.9,
return_citations: true,
search_domain_filter: ['perplexity.ai'],
return_images: false,
return_related_questions: false,
search_recency_filter: 'month',
top_k: 0,
stream: false,
presence_penalty: 0,
frequency_penalty: 1,
};

try {
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();

const validatedResponse = PerplexityResponseSchema.parse(data);
const response = await axios.post('https://api.perplexity.ai/chat/completions', data, {
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
});

const validatedResponse = PerplexityResponseSchema.parse(response.data);
const content = validatedResponse.choices[0].message.content;

const parsedContent = JSON.parse(content);
const gameResponse = GameResponseSchema.parse(parsedContent);

Expand All @@ -87,7 +81,7 @@ export const perplexityModel: ModelHandler = async (prompt: string, map: string[
reasoning: gameResponse.reasoning,
};
} catch (error) {
console.error('Error:', error);
console.error('Failed to run Perplexity model Error:', error);
throw new Error('Failed to run Perplexity model');
}
};
};

0 comments on commit d692164

Please sign in to comment.