-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
84 lines (70 loc) · 2.71 KB
/
app.ts
File metadata and controls
84 lines (70 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import type { OpenRouterResponse } from './types.ts';
/**
* Fetches a completion from the OpenRouter AI API.
* @param prompt The user prompt to send to the AI.
* @param systemPrompt The system prompt to guide the AI's behavior.
* @returns The AI's response content as a string, or undefined on failure.
*/
async function getAiCompletion(prompt: string, systemPrompt: string): Promise<string | undefined> {
// In Deno, you can load from a file or use Deno.env
const env = await load();
const apiKey = env['OPENROUTER_API_KEY'] ?? Deno.env.get('OPENROUTER_API_KEY');
if (!apiKey) {
console.error("Error: OPENROUTER_API_KEY environment variable is not set.");
return undefined;
}
const apiUrl = 'https://openrouter.ai/api/v1/chat/completions';
try {
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'google/gemini-2.0-flash-001', // The 'usage' parameter is not supported by this model.
messages: [
{ role: 'system', content: systemPrompt },
{
role: 'user',
content: prompt, // Use the function parameter for a dynamic prompt.
},
],
}),
});
if (!response.ok) {
const errorBody = await response.text();
throw new Error(`API request failed with status ${response.status}: ${errorBody}`);
}
const data = (await response.json()) as OpenRouterResponse;
// For debugging, you can log the full response object like this:
// console.log('Full API Response:', JSON.stringify(data, null, 2));
const messageContent = data.choices[0]?.message?.content;
if (!messageContent) {
console.error('Could not find message content in the AI response.');
return undefined;
}
return messageContent.trim();
} catch (error) {
console.error('An error occurred while fetching the AI completion:', error);
return undefined;
}
}
/**
* Main execution function.
*/
async function main() {
// Deno uses `import.meta.url` to resolve paths relative to the current file.
const systemPromptPath = new URL('system_prompt.md', import.meta.url);
const systemPrompt = await Deno.readTextFile(systemPromptPath);
const userPrompt = 'What is the Andromeda Galaxy?'; // Or read from Deno.args
console.log(`> Asking AI: "${userPrompt}"\n`);
const responseContent = await getAiCompletion(userPrompt, systemPrompt);
if (responseContent) {
console.log('Assistant says:\n', responseContent);
} else {
console.log('Failed to get a response from the AI.');
}
}
main();