-
Notifications
You must be signed in to change notification settings - Fork 14
/
smart_chat_model.js
152 lines (136 loc) · 3.8 KB
/
smart_chat_model.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
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
import { SmartModel } from "smart-model";
/**
* SmartChatModel - A versatile class for handling chat operations using various platform adapters.
* @extends SmartModel
*
* @example
* ```javascript
* const chatModel = new SmartChatModel({
* adapter: 'openai',
* adapters: {
* openai: OpenAIAdapter,
* custom_local: LocalAdapter,
* },
* settings: {
* openai: { api_key: 'your-api-key' },
* custom_local: { hostname: 'localhost', port: 8080 },
* },
* });
*
* const response = await chatModel.complete({ prompt: "Hello, world!" });
* console.log(response);
* ```
*/
export class SmartChatModel extends SmartModel {
scope_name = 'smart_chat_model';
static defaults = {
adapter: 'openai',
};
/**
* Create a SmartChatModel instance.
* @param {Object} opts - Configuration options
* @param {string} opts.adapter - Adapter to use
* @param {Object} opts.adapters - Map of adapter names to adapter classes
* @param {Object} opts.settings - Model settings configuration
*/
constructor(opts = {}) {
super(opts);
}
/**
* Get available models.
* @returns {Object} Map of model objects
*/
get models() { return this.adapter.models; }
get can_stream() { return this.adapter.constructor.defaults.streaming; }
get can_use_tools() {
return this.adapter.constructor.defaults.can_use_tools;
}
/**
* Complete a chat request.
* @param {Object} req - Request parameters
* @returns {Promise<Object>} Completion result
*/
async complete(req) {
return await this.invoke_adapter_method('complete', req);
}
/**
* Stream chat responses.
* @param {Object} req - Request parameters
* @param {Object} handlers - Event handlers for streaming
* @returns {Promise<string>} Complete response text
*/
async stream(req, handlers = {}) {
return await this.invoke_adapter_method('stream', req, handlers);
}
/**
* Stop active stream.
*/
stop_stream() {
this.invoke_adapter_method('stop_stream');
}
/**
* Count tokens in input text.
* @param {string|Object} input - Text to count tokens for
* @returns {Promise<number>} Token count
*/
async count_tokens(input) {
return await this.invoke_adapter_method('count_tokens', input);
}
/**
* Test if API key is valid.
* @returns {Promise<boolean>} True if API key is valid
*/
async test_api_key() {
await this.invoke_adapter_method('test_api_key');
this.re_render_settings();
}
/**
* Get default model key.
* @returns {string} Default model key
*/
get default_model_key() {
return this.adapter.constructor.defaults.default_model;
}
/**
* Get current settings.
* @returns {Object} Settings object
*/
get settings() {
return this.opts.settings;
}
/**
* Get settings configuration.
* @returns {Object} Settings configuration object
*/
get settings_config() {
const _settings_config = {
adapter: {
name: 'Chat Model Platform',
type: "dropdown",
description: "Select a chat model platform to use with Smart Chat.",
options_callback: 'get_platforms_as_options',
is_scope: true, // trigger re-render of settings when changed
callback: 'adapter_changed',
default: 'open_router',
},
// Merge adapter-specific settings
...(this.adapter.settings_config || {}),
};
return this.process_settings_config(_settings_config);
}
/**
* Process setting key.
* @param {string} key - Setting key
* @returns {string} Processed key
*/
process_setting_key(key) {
return key.replace(/\[CHAT_ADAPTER\]/g, this.adapter_name);
}
/**
* Validate the adapter configuration.
* @returns {Object} Validation result with 'valid' and 'message'.
*/
validate_config() {
return this.adapter.validate_config();
}
}