-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
163 lines (142 loc) · 4.85 KB
/
Copy pathclient.js
File metadata and controls
163 lines (142 loc) · 4.85 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const axios = require('axios');
class Client {
constructor(apiBaseUrl, apiKey) {
this.apiBaseUrl = apiBaseUrl;
this.apiKey = apiKey;
this.conversations = {}; // Stores conversations as { id: messages[] }
this.selectedConversation = null; // Initially, no conversation is selected
}
/**
* Set the Authorization header for API requests
* @returns {Object} headers
*/
getHeaders() {
return {
Authorization: `Bearer ${this.apiKey}`,
'Content-Type': 'application/json',
};
}
/**
* Create a conversation
* @param {Number} id - Optional ID for the new conversation
* @returns {Number} conversation ID.
*/
createConversation(id = Object.keys(this.conversations).length + 1) {
if (this.conversations[id]) {
throw new Error(`Conversation with ID ${id} already exists.`);
}
this.conversations[id] = []; // Initialize a new conversation with an empty message array
this.selectedConversation = id; // Automatically select the newly created conversation
return id;
}
/**
* Select an existing conversation by ID
* @param {Number} id - ID of the conversation to select
* @returns {void}
*/
selectConversation(id) {
if (!this.conversations[id]) {
throw new Error(`Conversation with ID ${id} does not exist.`);
}
this.selectedConversation = id;
}
/**
* Insert a system prompt.
* @param {String} prompt - The system prompt to add
* @returns {Number} Current conversation length
*/
insertSystemPrompt(prompt) {
if (!this.selectedConversation) {
throw new Error('No conversation selected.');
}
return this.conversations[this.selectedConversation].push({
role: 'system',
content: prompt,
});
}
/**
* Insert a user message into the selected conversation
* @param {String} message - The user's message
* @returns {Number} Current conversation length
*/
insertUserMessage(message) {
if (!this.selectedConversation) {
throw new Error('No conversation selected.');
}
return this.conversations[this.selectedConversation].push({
role: 'user',
content: message,
});
}
/**
* Insert an assistant message into the selected conversation
* @param {String} message - The assistant's message
* @returns {Number} Current conversation length
*/
insertAssistantMessage(message) {
if (!this.selectedConversation) {
throw new Error('No conversation selected.');
}
return this.conversations[this.selectedConversation].push({
role: 'assistant',
content: message,
});
}
/**
* Generate a response based on the current conversation messages
* @returns {Promise<Object>} - Response from the API
*/
async generate() {
if (!this.selectedConversation) {
throw new Error('No conversation selected.');
}
const messages = this.conversations[this.selectedConversation];
try {
const response = await axios.post(
`${this.apiBaseUrl}/generate`,
{ messages },
{ headers: this.getHeaders() }
);
// Automatically insert the assistant's response into the conversation
this.insertAssistantMessage(response.data.content);
return response.data;
} catch (error) {
console.error('Error generating response:', error.response?.data || error.message);
throw error;
}
}
/**
* Fetch metadata for an API key
* @returns {Promise<Object>} - Metadata for the API key
*/
async getKeyInfo() {
try {
const response = await axios.get(`${this.apiBaseUrl}/key_info`, {
headers: this.getHeaders(),
});
return response.data;
} catch (error) {
console.error('Error fetching key info:', error.response?.data || error.message);
throw error;
}
}
/**
* Update generation parameters (Requires Master Key)
* @param {Object} params - Object with new generation parameters
* @returns {Promise<Object>} - API response confirming the update
*/
async updateParams(params) {
try {
const response = await axios.post(
`${this.apiBaseUrl}/update_params`,
params,
{ headers: this.getHeaders() }
);
return response.data;
} catch (error) {
console.error('Error updating generation parameters:', error.response?.data || error.message);
throw error;
}
}
}
module.exports = Client;