-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathchat.js
86 lines (72 loc) · 2.19 KB
/
chat.js
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
85
86
/**
* @file examples/basic-usage/chat.js
* @description This example demonstrates a chat using an OpenAI compatible structure.
*
* To run this example, you first need to install the required modules by executing:
*
* npm install dotenv
*/
const { LLMInterface } = require('../../src/index.js');
const {
prettyHeader,
prettyText,
prettyResult,
GREEN,
RESET,
} = require('../../src/utils/utils.js');
require('dotenv').config({ path: '../../.env' });
// Setup your key and interface
const interfaceName = 'groq';
const apiKey = process.env.GROQ_API_KEY;
// Example description
const description = `This example demonstrates a chat using an OpenAI compatible structure.
To run this example, you first need to install the required modules by executing:
npm install dotenv`;
/**
* Main exampleUsage() function.
*/
async function exampleUsage() {
// OpenAI chat.completion structure
const openaiCompatibleStructure = {
model: 'gemma-7b-it',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'Say hello with a polite greeting!' },
{
role: 'system',
content:
"Hello there! It's an absolute pleasure to make your acquaintance. How may I have the honor of assisting you today?",
},
{ role: 'user', content: 'I need help understanding low latency LLMs!' },
],
max_tokens: 100,
};
LLMInterface.setApiKey(interfaceName, apiKey);
try {
console.time('Timer');
prettyHeader('Chat Example', description, false, interfaceName);
prettyText(`\n\n${GREEN}Prompt (OpenAI Compatible Structure):${RESET}\n\n`);
console.log(openaiCompatibleStructure);
console.log();
const response = await LLMInterface.sendMessage(
interfaceName,
openaiCompatibleStructure,
);
/*
or for the OpenAI API fans
const response = await LLMInterface.chat.completions.create(
interfaceName
openaiCompatibleStructure
);
*/
prettyResult(response.results);
console.timeEnd('Timer');
console.log();
} catch (error) {
console.error(
'Error processing openaiCompatibleStructure sendMessage:',
error,
);
}
}
exampleUsage();