Skip to content

Commit 21c030c

Browse files
committed
add config for model provider; support HF conversational model
1 parent 0b251f7 commit 21c030c

File tree

8 files changed

+50
-52
lines changed

8 files changed

+50
-52
lines changed

.SAMPLE_env

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Set which model you want to use
2-
ENABLED_MODEL="HUGGINGFACEHUB"
1+
// Set which model provider you want to use, HUGGING_FACE or OPEN_AI
2+
ENABLED_MODEL_STORE="HUGGING_FACE"
33

44
// Hugging Face API Key
55
HUGGINGFACEHUB_API_KEY=""

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
"start": "vite",
99
"preview": "vite preview",
1010
"build": "vite build",
11-
"start-server": "node ./server/index.js"
11+
"start-server": "nodemon ./server/index.js"
1212
},
1313
"dependencies": {
1414
"@emotion/react": "^11.11.0",
1515
"@emotion/styled": "^11.11.0",
16+
"@huggingface/inference": "^2.5.0",
1617
"@koa/cors": "^4.0.0",
1718
"@koa/router": "^12.0.0",
1819
"@mui/icons-material": "^5.11.16",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { HuggingFaceService } from '../services/hf.js'
2+
import { OpenAiService } from '../services/openai.js'
3+
4+
export const MODEL_STORES = {
5+
'HUGGING_FACE': HuggingFaceService,
6+
'OPEN_AI': OpenAiService,
7+
};
8+
9+
export const { ENABLED_MODEL_STORE } = process.env;
10+
export const DEFAULT_ENABLED_MODEL_STORE = 'HUGGING_FACE';
11+
12+
export const enabledModel = ENABLED_MODEL_STORE || DEFAULT_ENABLED_MODEL_STORE;

server/handlers/chat_handler.js

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,13 @@
1-
import { HuggingFaceService } from '../services/model_hf.js';
2-
import { OpenAiService } from '../services/model_openai.js';
3-
4-
const { ENABLED_MODEL } = process.env;
1+
import { MODEL_STORES, enabledModel } from '../config/model_store_constants.js';
52

63
class ChatService {
74
constructor () {
8-
}
9-
10-
async selectModelService() {
11-
let model;
12-
switch (ENABLED_MODEL) {
13-
case 'HUGGINGFACEHUB':
14-
const huggingFaceService = new HuggingFaceService();
15-
model = huggingFaceService;
16-
break;
17-
case 'OPENAI':
18-
const openAiService = new OpenAiService();
19-
model = openAiService;
20-
break;
21-
default:
22-
break;
23-
}
24-
return model;
5+
this.model = new MODEL_STORES[enabledModel]
256
}
267

278
async startChat(data) {
289
const { body: { userInput } } = data;
29-
const model = this.selectModelService();
30-
console.log('model ', model.OpenAiService);
10+
const model = this.model;
3111
const response = await model.call(userInput);
3212

3313
return response;

server/services/hf.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { HfInference } from "@huggingface/inference";
2+
3+
const { HUGGINGFACEHUB_API_KEY } = process.env;
4+
5+
class HuggingFaceService {
6+
constructor () {
7+
this.modelName = 'microsoft/DialoGPT-large';
8+
this.model = new HfInference(HUGGINGFACEHUB_API_KEY);
9+
}
10+
11+
async call(userInput) {
12+
// TO DO: pass in past_user_inputs for context
13+
const response = await this.model.conversational({
14+
model: this.modelName,
15+
temperature: 0,
16+
inputs: {
17+
text: userInput,
18+
}
19+
});
20+
21+
return { response: response && response.generated_text };
22+
}
23+
}
24+
25+
export { HuggingFaceService }

server/services/model_hf.js

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class OpenAiService {
3131
const response = await chain.call({
3232
input: userInput,
3333
});
34+
console.log('response ', response);
3435
return response;
3536
}
3637
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,11 @@
520520
resolved "https://registry.yarnpkg.com/@fortaine/fetch-event-source/-/fetch-event-source-3.0.6.tgz#b8552a2ca2c5202f5699b93a92be0188d422b06e"
521521
integrity sha512-621GAuLMvKtyZQ3IA6nlDWhV1V/7PGOTNIGLUifxt0KzM+dZIweJ6F3XvQF3QnqeNfS1N7WQ0Kil1Di/lhChEw==
522522

523+
"@huggingface/inference@^2.5.0":
524+
version "2.5.0"
525+
resolved "https://registry.yarnpkg.com/@huggingface/inference/-/inference-2.5.0.tgz#8e14ee6696e91aecb132c90d3b07be8373e70338"
526+
integrity sha512-X3NSdrWAKNTLAsEKabH48Wc+Osys+S7ilRcH1bf9trSDmJlzPVXDseXMRBHCFPCYd5AAAIakhENO4zCqstVg8g==
527+
523528
"@jridgewell/gen-mapping@^0.3.0", "@jridgewell/gen-mapping@^0.3.2":
524529
version "0.3.3"
525530
resolved "https://registry.yarnpkg.com/@jridgewell/gen-mapping/-/gen-mapping-0.3.3.tgz#7e02e6eb5df901aaedb08514203b096614024098"

0 commit comments

Comments
 (0)