|
| 1 | +import { ToolConfig } from '../types' |
| 2 | +import { PerplexityChatParams, PerplexityChatResponse } from './types' |
| 3 | + |
| 4 | +export const chatTool: ToolConfig<PerplexityChatParams, PerplexityChatResponse> = { |
| 5 | + id: 'perplexity_chat', |
| 6 | + name: 'Perplexity Chat', |
| 7 | + description: 'Generate completions using Perplexity AI chat models', |
| 8 | + version: '1.0', |
| 9 | + |
| 10 | + params: { |
| 11 | + apiKey: { |
| 12 | + type: 'string', |
| 13 | + required: true, |
| 14 | + requiredForToolCall: true, |
| 15 | + description: 'Perplexity API key', |
| 16 | + }, |
| 17 | + model: { |
| 18 | + type: 'string', |
| 19 | + required: true, |
| 20 | + description: 'Model to use for chat completions (e.g., sonar, mistral)', |
| 21 | + }, |
| 22 | + messages: { |
| 23 | + type: 'array', |
| 24 | + required: true, |
| 25 | + description: 'Array of message objects with role and content', |
| 26 | + }, |
| 27 | + max_tokens: { |
| 28 | + type: 'number', |
| 29 | + required: false, |
| 30 | + description: 'Maximum number of tokens to generate', |
| 31 | + }, |
| 32 | + temperature: { |
| 33 | + type: 'number', |
| 34 | + required: false, |
| 35 | + description: 'Sampling temperature between 0 and 1', |
| 36 | + }, |
| 37 | + }, |
| 38 | + |
| 39 | + request: { |
| 40 | + method: 'POST', |
| 41 | + url: () => 'https://api.perplexity.ai/chat/completions', |
| 42 | + headers: (params) => ({ |
| 43 | + Authorization: `Bearer ${params.apiKey}`, |
| 44 | + 'Content-Type': 'application/json', |
| 45 | + }), |
| 46 | + body: (params) => { |
| 47 | + let messages = params.messages |
| 48 | + |
| 49 | + if (!messages && (params.prompt || params.system)) { |
| 50 | + messages = [] |
| 51 | + |
| 52 | + // Add system message if provided |
| 53 | + if (params.system && typeof params.system === 'string' && params.system.trim() !== '') { |
| 54 | + messages.push({ |
| 55 | + role: 'system', |
| 56 | + content: params.system, |
| 57 | + }) |
| 58 | + } |
| 59 | + |
| 60 | + // Add user message |
| 61 | + if (params.prompt && typeof params.prompt === 'string' && params.prompt.trim() !== '') { |
| 62 | + messages.push({ |
| 63 | + role: 'user', |
| 64 | + content: params.prompt, |
| 65 | + }) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Validate that each message has role and content |
| 70 | + for (const msg of messages!) { |
| 71 | + if (!msg.role || !msg.content) { |
| 72 | + throw new Error('Each message must have role and content properties') |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + const body: Record<string, any> = { |
| 77 | + model: params.model, |
| 78 | + messages: messages, |
| 79 | + } |
| 80 | + |
| 81 | + if (params.max_tokens !== undefined) { |
| 82 | + body.max_tokens = params.max_tokens |
| 83 | + } |
| 84 | + |
| 85 | + if (params.temperature !== undefined) { |
| 86 | + body.temperature = params.temperature |
| 87 | + } |
| 88 | + |
| 89 | + return body |
| 90 | + }, |
| 91 | + }, |
| 92 | + |
| 93 | + transformResponse: async (response, params) => { |
| 94 | + try { |
| 95 | + // Check if the response was successful |
| 96 | + if (!response.ok) { |
| 97 | + const errorData = await response.json().catch(() => null) |
| 98 | + console.error('Perplexity API error:', { |
| 99 | + status: response.status, |
| 100 | + statusText: response.statusText, |
| 101 | + errorData, |
| 102 | + }) |
| 103 | + |
| 104 | + const errorMessage = errorData |
| 105 | + ? JSON.stringify(errorData) |
| 106 | + : `API error: ${response.status} ${response.statusText}` |
| 107 | + |
| 108 | + throw new Error(errorMessage) |
| 109 | + } |
| 110 | + |
| 111 | + const data = await response.json() |
| 112 | + |
| 113 | + // Validate response structure |
| 114 | + if (!data.choices || !data.choices[0] || !data.choices[0].message) { |
| 115 | + console.error('Invalid Perplexity response format:', data) |
| 116 | + throw new Error('Invalid response format from Perplexity API') |
| 117 | + } |
| 118 | + |
| 119 | + return { |
| 120 | + success: true, |
| 121 | + output: { |
| 122 | + content: data.choices[0].message.content, |
| 123 | + model: data.model, |
| 124 | + usage: { |
| 125 | + prompt_tokens: data.usage.prompt_tokens, |
| 126 | + completion_tokens: data.usage.completion_tokens, |
| 127 | + total_tokens: data.usage.total_tokens, |
| 128 | + }, |
| 129 | + }, |
| 130 | + } |
| 131 | + } catch (error: any) { |
| 132 | + console.error('Failed to process Perplexity response:', error) |
| 133 | + throw error |
| 134 | + } |
| 135 | + }, |
| 136 | + |
| 137 | + transformError: (error) => `Perplexity chat completion failed: ${error.message}`, |
| 138 | +} |
0 commit comments