@memberjunction/ai-groq 0.9.2 → 0.9.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/models/groq.d.ts +11 -0
- package/dist/models/{mistral.js → groq.js} +29 -35
- package/dist/models/groq.js.map +1 -0
- package/package.json +3 -2
- package/dist/models/mistral.d.ts +0 -20
- package/dist/models/mistral.js.map +0 -1
- package/dist/models/mistralClient.d.ts +0 -181
- package/dist/models/mistralClient.js +0 -155
- package/dist/models/mistralClient.js.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './models/
|
|
1
|
+
export * from './models/groq';
|
package/dist/index.js
CHANGED
|
@@ -14,5 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./models/
|
|
17
|
+
__exportStar(require("./models/groq"), exports);
|
|
18
18
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gDAA8B"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { BaseLLM, ChatParams, ChatResult, ClassifyParams, ClassifyResult, EmbedParams, EmbedResult, SummarizeParams, SummarizeResult } from '@memberjunction/ai';
|
|
2
|
+
import Groq from 'groq-sdk';
|
|
3
|
+
export declare class GroqLLM extends BaseLLM {
|
|
4
|
+
static _client: Groq;
|
|
5
|
+
constructor(apiKey: string);
|
|
6
|
+
get client(): Groq;
|
|
7
|
+
ChatCompletion(params: ChatParams): Promise<ChatResult>;
|
|
8
|
+
SummarizeText(params: SummarizeParams): Promise<SummarizeResult>;
|
|
9
|
+
ClassifyText(params: ClassifyParams): Promise<ClassifyResult>;
|
|
10
|
+
EmbedText(params: EmbedParams): Promise<EmbedResult>;
|
|
11
|
+
}
|
|
@@ -5,23 +5,37 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
5
5
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
7
|
};
|
|
8
|
-
var
|
|
8
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
11
|
+
var GroqLLM_1;
|
|
9
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
-
exports.
|
|
13
|
+
exports.GroqLLM = void 0;
|
|
11
14
|
const ai_1 = require("@memberjunction/ai");
|
|
12
15
|
const global_1 = require("@memberjunction/global");
|
|
13
|
-
const
|
|
14
|
-
let
|
|
16
|
+
const groq_sdk_1 = __importDefault(require("groq-sdk"));
|
|
17
|
+
let GroqLLM = GroqLLM_1 = class GroqLLM extends ai_1.BaseLLM {
|
|
15
18
|
constructor(apiKey) {
|
|
16
19
|
super(apiKey);
|
|
17
|
-
if (!
|
|
18
|
-
|
|
20
|
+
if (!GroqLLM_1._client) {
|
|
21
|
+
GroqLLM_1._client = new groq_sdk_1.default({ apiKey: apiKey });
|
|
19
22
|
}
|
|
20
23
|
}
|
|
21
|
-
get client() { return
|
|
24
|
+
get client() { return GroqLLM_1._client; }
|
|
22
25
|
async ChatCompletion(params) {
|
|
23
26
|
const startTime = new Date();
|
|
24
|
-
const
|
|
27
|
+
// const completion = await groq.chat.completions.create({
|
|
28
|
+
// messages: [
|
|
29
|
+
// {
|
|
30
|
+
// role: "user",
|
|
31
|
+
// content: "Explain the importance of low latency LLMs"
|
|
32
|
+
// }
|
|
33
|
+
// ],
|
|
34
|
+
// model: "mixtral-8x7b-32768"
|
|
35
|
+
// }).then((chatCompletion)=>{
|
|
36
|
+
// process.stdout.write(chatCompletion.choices[0]?.message?.content || "");
|
|
37
|
+
// });
|
|
38
|
+
const chatResponse = await this.client.chat.completions.create({
|
|
25
39
|
model: params.model,
|
|
26
40
|
messages: params.messages
|
|
27
41
|
});
|
|
@@ -54,6 +68,7 @@ let MistralLLM = MistralLLM_1 = class MistralLLM extends ai_1.BaseLLM {
|
|
|
54
68
|
errorMessage: "",
|
|
55
69
|
exception: null,
|
|
56
70
|
};
|
|
71
|
+
throw new Error("Method not implemented.");
|
|
57
72
|
}
|
|
58
73
|
async SummarizeText(params) {
|
|
59
74
|
throw new Error("Method not implemented.");
|
|
@@ -61,33 +76,12 @@ let MistralLLM = MistralLLM_1 = class MistralLLM extends ai_1.BaseLLM {
|
|
|
61
76
|
async ClassifyText(params) {
|
|
62
77
|
throw new Error("Method not implemented.");
|
|
63
78
|
}
|
|
64
|
-
async createEmbedding(model, text) {
|
|
65
|
-
const response = await this.client.embeddings(model, text);
|
|
66
|
-
return response;
|
|
67
|
-
}
|
|
68
79
|
async EmbedText(params) {
|
|
69
|
-
|
|
70
|
-
return {
|
|
71
|
-
object: 'object',
|
|
72
|
-
model: params.model || "mistral-embed", //hard coded for now as theres only one available embedding model
|
|
73
|
-
ModelUsage: new ai_1.ModelUsage(response.usage.prompt_tokens, response.usage.completion_tokens),
|
|
74
|
-
data: response.data[0].embedding
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Returns a list of available models
|
|
79
|
-
* @returns {Promise<AvailableModelInfo>}
|
|
80
|
-
*/
|
|
81
|
-
async listModels() {
|
|
82
|
-
const listModelsResponse = await this.client.listModels();
|
|
83
|
-
return listModelsResponse;
|
|
80
|
+
throw new Error("Method not implemented.");
|
|
84
81
|
}
|
|
85
82
|
};
|
|
86
|
-
exports.
|
|
87
|
-
exports.
|
|
88
|
-
(0, global_1.RegisterClass)(ai_1.BaseLLM, "
|
|
89
|
-
],
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
exports.MistralChatParams = MistralChatParams;
|
|
93
|
-
//# sourceMappingURL=mistral.js.map
|
|
83
|
+
exports.GroqLLM = GroqLLM;
|
|
84
|
+
exports.GroqLLM = GroqLLM = GroqLLM_1 = __decorate([
|
|
85
|
+
(0, global_1.RegisterClass)(ai_1.BaseLLM, "GroqLLM")
|
|
86
|
+
], GroqLLM);
|
|
87
|
+
//# sourceMappingURL=groq.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"groq.js","sourceRoot":"","sources":["../../src/models/groq.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAAA,2CAA+L;AAC/L,mDAAuD;AACvD,wDAA4B;AAGrB,IAAM,OAAO,eAAb,MAAM,OAAQ,SAAQ,YAAO;IAEhC,YAAY,MAAc;QACtB,KAAK,CAAC,MAAM,CAAC,CAAC;QACd,IAAI,CAAC,SAAO,CAAC,OAAO,EAAC,CAAC;YAClB,SAAO,CAAC,OAAO,GAAG,IAAI,kBAAI,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;QACnD,CAAC;IACL,CAAC;IAED,IAAW,MAAM,KAAU,OAAO,SAAO,CAAC,OAAO,CAAC,CAAA,CAAC;IAE5C,KAAK,CAAC,cAAc,CAAC,MAAkB;QAC1C,MAAM,SAAS,GAAG,IAAI,IAAI,EAAE,CAAC;QAG7B,0DAA0D;QAC1D,kBAAkB;QAClB,YAAY;QACZ,4BAA4B;QAC5B,oEAAoE;QACpE,YAAY;QACZ,SAAS;QACT,kCAAkC;QAClC,8BAA8B;QAC9B,+EAA+E;QAC/E,MA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Y,CAAC,MAAsB;QAC5C,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAC/C,CAAC;IAEM,KAAK,CAAC,SAAS,CAAC,MAAmB;QACtC,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAC/C,CAAC;CACJ,CAAA;AA5EY,0BAAO;kBAAP,OAAO;IADnB,IAAA,sBAAa,EAAC,YAAO,EAAE,SAAS,CAAC;GACrB,OAAO,CA4EnB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/ai-groq",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.4",
|
|
4
4
|
"description": "MemberJunction Wrapper for Groq AI LPU inference engine",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"@memberjunction/ai": "^0.9.159",
|
|
23
|
-
"@memberjunction/global": "^0.9.152"
|
|
23
|
+
"@memberjunction/global": "^0.9.152",
|
|
24
|
+
"groq-sdk": "^0.3.2"
|
|
24
25
|
}
|
|
25
26
|
}
|
package/dist/models/mistral.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { BaseLLM, ChatParams, ChatResult, ClassifyParams, ClassifyResult, EmbedParams, EmbedResult, SummarizeParams, SummarizeResult } from '@memberjunction/ai';
|
|
2
|
-
import { EmbeddingResponse, ListModelsResponse, MistralClient } from './mistralClient';
|
|
3
|
-
export declare class MistralLLM extends BaseLLM {
|
|
4
|
-
static _client: MistralClient;
|
|
5
|
-
constructor(apiKey: string);
|
|
6
|
-
get client(): MistralClient;
|
|
7
|
-
ChatCompletion(params: MistralChatParams): Promise<ChatResult>;
|
|
8
|
-
SummarizeText(params: SummarizeParams): Promise<SummarizeResult>;
|
|
9
|
-
ClassifyText(params: ClassifyParams): Promise<ClassifyResult>;
|
|
10
|
-
createEmbedding(model: string, text: string): Promise<EmbeddingResponse>;
|
|
11
|
-
EmbedText(params: EmbedParams): Promise<EmbedResult>;
|
|
12
|
-
/**
|
|
13
|
-
* Returns a list of available models
|
|
14
|
-
* @returns {Promise<AvailableModelInfo>}
|
|
15
|
-
*/
|
|
16
|
-
listModels(): Promise<ListModelsResponse>;
|
|
17
|
-
}
|
|
18
|
-
export declare class MistralChatParams extends ChatParams {
|
|
19
|
-
model: string;
|
|
20
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mistral.js","sourceRoot":"","sources":["../../src/models/mistral.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAA+L;AAC/L,mDAAuD;AACvD,mDAAqH;AAG9G,IAAM,UAAU,kBAAhB,MAAM,UAAW,SAAQ,YAAO;IAEnC,YAAY,MAAc;QACtB,KAAK,CAAC,MAAM,CAAC,CAAC;QACd,IAAI,CAAC,YAAU,CAAC,OAAO,EAAC,CAAC;YACrB,YAAU,CAAC,OAAO,GAAG,IAAI,6BAAa,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;QACvD,CAAC;IACL,CAAC;IAED,IAAW,MAAM,KAAmB,OAAO,YAAU,CAAC,OAAO,CAAC,CAAA,CAAC;IAExD,KAAK,CAAC,cAAc,CAAC,MAAyB;QACjD,MAAM,SAAS,GAAG,IAAI,IAAI,EAAE,CAAC;QAC7B,MAAM,YAAY,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC;YACxC,KAAK,EAAE,MAAM,CAAC,KAAK;YACnB,QAAQ,EAAE,MAAM,CAAC,QAAQ;SAC5B,CAAC,CAAC;QACH,MAAM,OAAO,GAAG,IAAI,IAAI,EAAE,CAAC;QAE3B,IAAI,OAAO,GAAuB,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAoC,EAAE,EAAE;YAChG,MAAM,GAAG,GAAqB;gBAC1B,OAAO,EAAE;oBACL,IAA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C5F,OAAO;YACH,MAAM,EAAE,QAAQ;YAChB,KAAK,EAAE,MAAM,CAAC,KAAK,IAAI,eAAe,EAAE,iEAAiE;YACzG,UAAU,EAAE,IAAI,eAAU,CAAC,QAAQ,CAAC,KAAK,CAAC,aAAa,EAAE,QAAQ,CAAC,KAAK,CAAC,iBAAiB,CAAC;YAC1F,IAAI,EAAE,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS;SACnC,CAAA;IACL,CAAC;IAED;;;OAGG;IACI,KAAK,CAAC,UAAU;QACnB,MAAM,kBAAkB,GAAuB,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC;QAC9E,OAAO,kBAAkB,CAAC;IAC9B,CAAC;CACJ,CAAA;AAlFY,gCAAU;qBAAV,UAAU;IADtB,IAAA,sBAAa,EAAC,YAAO,EAAE,YAAY,CAAC;GACxB,UAAU,CAkFtB;AAED,MAAa,iBAAkB,SAAQ,eAAU;CAEhD;AAFD,8CAEC"}
|
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
import { ChatMessage } from '@memberjunction/ai';
|
|
2
|
-
/**
|
|
3
|
-
* A simple and lightweight client for the Mistral API
|
|
4
|
-
* @param {*} apiKey can be set as an environment variable MISTRAL_API_KEY,
|
|
5
|
-
* or provided in this parameter
|
|
6
|
-
* @param {*} endpoint defaults to https://api.mistral.ai
|
|
7
|
-
*/
|
|
8
|
-
export declare class MistralClient {
|
|
9
|
-
private RETRY_STATUS_CODES;
|
|
10
|
-
private ENDPOINT;
|
|
11
|
-
endpoint: string;
|
|
12
|
-
apiKey: string;
|
|
13
|
-
textDecoder: TextDecoder;
|
|
14
|
-
constructor(config: {
|
|
15
|
-
apiKey?: string;
|
|
16
|
-
endpoint?: string;
|
|
17
|
-
});
|
|
18
|
-
/**
|
|
19
|
-
*
|
|
20
|
-
* @param {*} method
|
|
21
|
-
* @param {*} path
|
|
22
|
-
* @param {*} request
|
|
23
|
-
* @return {Promise<T>}
|
|
24
|
-
*/
|
|
25
|
-
private request;
|
|
26
|
-
/**
|
|
27
|
-
* Creates a chat completion request
|
|
28
|
-
* @param {*} model
|
|
29
|
-
* @param {*} messages
|
|
30
|
-
* @param {*} temperature
|
|
31
|
-
* @param {*} maxTokens
|
|
32
|
-
* @param {*} topP
|
|
33
|
-
* @param {*} randomSeed
|
|
34
|
-
* @param {*} stream
|
|
35
|
-
* @param {*} safeMode
|
|
36
|
-
* @return {Promise<Object>}
|
|
37
|
-
*/
|
|
38
|
-
_makeChatCompletionRequest: (model: any, messages: any, temperature: any, maxTokens: any, topP: any, randomSeed: any, stream: any, safeMode: any) => {
|
|
39
|
-
model: any;
|
|
40
|
-
messages: any;
|
|
41
|
-
temperature: any;
|
|
42
|
-
max_tokens: any;
|
|
43
|
-
top_p: any;
|
|
44
|
-
random_seed: any;
|
|
45
|
-
stream: any;
|
|
46
|
-
safe_prompt: any;
|
|
47
|
-
};
|
|
48
|
-
/**
|
|
49
|
-
* Returns a list of the available models
|
|
50
|
-
* @return {Promise<ListModelsResponse>}
|
|
51
|
-
*/
|
|
52
|
-
listModels(): Promise<ListModelsResponse>;
|
|
53
|
-
/**
|
|
54
|
-
* A chat endpoint without streaming
|
|
55
|
-
* @param {*} model the name of the model to chat with, e.g. mistral-tiny
|
|
56
|
-
* @param {*} messages an array of messages to chat with, e.g.
|
|
57
|
-
* [{role: 'user', content: 'What is the best French cheese?'}]
|
|
58
|
-
* @param {*} temperature the temperature to use for sampling, e.g. 0.5
|
|
59
|
-
* @param {*} maxTokens the maximum number of tokens to generate, e.g. 100
|
|
60
|
-
* @param {*} topP the cumulative probability of tokens to generate, e.g. 0.9
|
|
61
|
-
* @param {*} randomSeed the random seed to use for sampling, e.g. 42
|
|
62
|
-
* @param {*} safeMode whether to use safe mode, e.g. true
|
|
63
|
-
* @return {Promise<Object>}
|
|
64
|
-
*/
|
|
65
|
-
chat(params: ChatCompletetionRequest): Promise<ChatCompletionResponse>;
|
|
66
|
-
/**
|
|
67
|
-
* A chat endpoint that streams responses.
|
|
68
|
-
* @param {*} model the name of the model to chat with, e.g. mistral-tiny
|
|
69
|
-
* @param {*} messages an array of messages to chat with, e.g.
|
|
70
|
-
* [{role: 'user', content: 'What is the best French cheese?'}]
|
|
71
|
-
* @param {*} temperature the temperature to use for sampling, e.g. 0.5
|
|
72
|
-
* @param {*} maxTokens the maximum number of tokens to generate, e.g. 100
|
|
73
|
-
* @param {*} topP the cumulative probability of tokens to generate, e.g. 0.9
|
|
74
|
-
* @param {*} randomSeed the random seed to use for sampling, e.g. 42
|
|
75
|
-
* @param {*} safeMode whether to use safe mode, e.g. true
|
|
76
|
-
* @return {Promise<Object>}
|
|
77
|
-
*/
|
|
78
|
-
chatStream: ({ model, messages, temperature, maxTokens, topP, randomSeed, safeMode }: {
|
|
79
|
-
model: any;
|
|
80
|
-
messages: any;
|
|
81
|
-
temperature: any;
|
|
82
|
-
maxTokens: any;
|
|
83
|
-
topP: any;
|
|
84
|
-
randomSeed: any;
|
|
85
|
-
safeMode: any;
|
|
86
|
-
}) => AsyncGenerator<any, void, unknown>;
|
|
87
|
-
/**
|
|
88
|
-
* An embedddings endpoint that returns embeddings for a single,
|
|
89
|
-
* or batch of inputs
|
|
90
|
-
* @param {*} model The embedding model to use, e.g. mistral-embed
|
|
91
|
-
* @param {*} input The input to embed,
|
|
92
|
-
* e.g. ['What is the best French cheese?']
|
|
93
|
-
* @return {Promise<Object>}
|
|
94
|
-
*/
|
|
95
|
-
embeddings(model: string, input: string): Promise<EmbeddingResponse>;
|
|
96
|
-
}
|
|
97
|
-
export interface ModelPermission {
|
|
98
|
-
id: string;
|
|
99
|
-
object: 'model_permission';
|
|
100
|
-
created: number;
|
|
101
|
-
allow_create_engine: boolean;
|
|
102
|
-
allow_sampling: boolean;
|
|
103
|
-
allow_logprobs: boolean;
|
|
104
|
-
allow_search_indices: boolean;
|
|
105
|
-
allow_view: boolean;
|
|
106
|
-
allow_fine_tuning: boolean;
|
|
107
|
-
organization: string;
|
|
108
|
-
group: string | null;
|
|
109
|
-
is_blocking: boolean;
|
|
110
|
-
}
|
|
111
|
-
export interface Model {
|
|
112
|
-
id: string;
|
|
113
|
-
object: 'model';
|
|
114
|
-
created: number;
|
|
115
|
-
owned_by: string;
|
|
116
|
-
root: string | null;
|
|
117
|
-
parent: string | null;
|
|
118
|
-
permission: ModelPermission[];
|
|
119
|
-
}
|
|
120
|
-
export interface ListModelsResponse {
|
|
121
|
-
object: 'list';
|
|
122
|
-
data: Model[];
|
|
123
|
-
}
|
|
124
|
-
export interface TokenUsage {
|
|
125
|
-
prompt_tokens: number;
|
|
126
|
-
completion_tokens: number;
|
|
127
|
-
total_tokens: number;
|
|
128
|
-
}
|
|
129
|
-
export type ChatCompletetionRequest = {
|
|
130
|
-
model: string;
|
|
131
|
-
messages: ChatMessage[];
|
|
132
|
-
temperature?: number;
|
|
133
|
-
max_tokens?: number;
|
|
134
|
-
top_p?: number;
|
|
135
|
-
random_seed?: number;
|
|
136
|
-
stream?: boolean;
|
|
137
|
-
safe_prompt?: boolean;
|
|
138
|
-
};
|
|
139
|
-
export interface ChatCompletionResponseChoice {
|
|
140
|
-
index: number;
|
|
141
|
-
message: {
|
|
142
|
-
role: string;
|
|
143
|
-
content: string;
|
|
144
|
-
};
|
|
145
|
-
finish_reason: string;
|
|
146
|
-
}
|
|
147
|
-
export interface ChatCompletionResponseChunkChoice {
|
|
148
|
-
index: number;
|
|
149
|
-
delta: {
|
|
150
|
-
role?: string;
|
|
151
|
-
content?: string;
|
|
152
|
-
};
|
|
153
|
-
finish_reason: string;
|
|
154
|
-
}
|
|
155
|
-
export interface ChatCompletionResponse {
|
|
156
|
-
id: string;
|
|
157
|
-
object: 'chat.completion';
|
|
158
|
-
created: number;
|
|
159
|
-
model: string;
|
|
160
|
-
choices: ChatCompletionResponseChoice[];
|
|
161
|
-
usage: TokenUsage;
|
|
162
|
-
}
|
|
163
|
-
export interface ChatCompletionResponseChunk {
|
|
164
|
-
id: string;
|
|
165
|
-
object: 'chat.completion.chunk';
|
|
166
|
-
created: number;
|
|
167
|
-
model: string;
|
|
168
|
-
choices: ChatCompletionResponseChunkChoice[];
|
|
169
|
-
}
|
|
170
|
-
export interface Embedding {
|
|
171
|
-
id: string;
|
|
172
|
-
object: 'embedding';
|
|
173
|
-
embedding: number[];
|
|
174
|
-
}
|
|
175
|
-
export interface EmbeddingResponse {
|
|
176
|
-
id: string;
|
|
177
|
-
object: 'list';
|
|
178
|
-
data: Embedding[];
|
|
179
|
-
model: string;
|
|
180
|
-
usage: TokenUsage;
|
|
181
|
-
}
|
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.MistralClient = void 0;
|
|
7
|
-
const axios_1 = __importDefault(require("axios"));
|
|
8
|
-
const axios_retry_1 = __importDefault(require("axios-retry"));
|
|
9
|
-
//This is a 1:1 copy of the mistralAI client library
|
|
10
|
-
//but modified to work with MJ
|
|
11
|
-
//see https://github.com/mistralai/client-js
|
|
12
|
-
/**
|
|
13
|
-
* A simple and lightweight client for the Mistral API
|
|
14
|
-
* @param {*} apiKey can be set as an environment variable MISTRAL_API_KEY,
|
|
15
|
-
* or provided in this parameter
|
|
16
|
-
* @param {*} endpoint defaults to https://api.mistral.ai
|
|
17
|
-
*/
|
|
18
|
-
class MistralClient {
|
|
19
|
-
constructor(config) {
|
|
20
|
-
this.RETRY_STATUS_CODES = [429, 500, 502, 503, 504];
|
|
21
|
-
this.ENDPOINT = 'https://api.mistral.ai';
|
|
22
|
-
/**
|
|
23
|
-
* Creates a chat completion request
|
|
24
|
-
* @param {*} model
|
|
25
|
-
* @param {*} messages
|
|
26
|
-
* @param {*} temperature
|
|
27
|
-
* @param {*} maxTokens
|
|
28
|
-
* @param {*} topP
|
|
29
|
-
* @param {*} randomSeed
|
|
30
|
-
* @param {*} stream
|
|
31
|
-
* @param {*} safeMode
|
|
32
|
-
* @return {Promise<Object>}
|
|
33
|
-
*/
|
|
34
|
-
this._makeChatCompletionRequest = function (model, messages, temperature, maxTokens, topP, randomSeed, stream, safeMode) {
|
|
35
|
-
return {
|
|
36
|
-
model: model,
|
|
37
|
-
messages: messages,
|
|
38
|
-
temperature: temperature ?? undefined,
|
|
39
|
-
max_tokens: maxTokens ?? undefined,
|
|
40
|
-
top_p: topP ?? undefined,
|
|
41
|
-
random_seed: randomSeed ?? undefined,
|
|
42
|
-
stream: stream ?? undefined,
|
|
43
|
-
safe_prompt: safeMode ?? undefined,
|
|
44
|
-
};
|
|
45
|
-
};
|
|
46
|
-
/**
|
|
47
|
-
* A chat endpoint that streams responses.
|
|
48
|
-
* @param {*} model the name of the model to chat with, e.g. mistral-tiny
|
|
49
|
-
* @param {*} messages an array of messages to chat with, e.g.
|
|
50
|
-
* [{role: 'user', content: 'What is the best French cheese?'}]
|
|
51
|
-
* @param {*} temperature the temperature to use for sampling, e.g. 0.5
|
|
52
|
-
* @param {*} maxTokens the maximum number of tokens to generate, e.g. 100
|
|
53
|
-
* @param {*} topP the cumulative probability of tokens to generate, e.g. 0.9
|
|
54
|
-
* @param {*} randomSeed the random seed to use for sampling, e.g. 42
|
|
55
|
-
* @param {*} safeMode whether to use safe mode, e.g. true
|
|
56
|
-
* @return {Promise<Object>}
|
|
57
|
-
*/
|
|
58
|
-
this.chatStream = async function* ({ model, messages, temperature, maxTokens, topP, randomSeed, safeMode }) {
|
|
59
|
-
const request = this._makeChatCompletionRequest(model, messages, temperature, maxTokens, topP, randomSeed, true, safeMode);
|
|
60
|
-
const response = await this._request('post', 'v1/chat/completions', request);
|
|
61
|
-
for await (const chunk of response) {
|
|
62
|
-
const chunkString = this.textDecoder.decode(chunk);
|
|
63
|
-
// split the chunks by new line
|
|
64
|
-
const chunkLines = chunkString.split('\n');
|
|
65
|
-
// Iterate through the lines
|
|
66
|
-
for (const chunkLine of chunkLines) {
|
|
67
|
-
// If the line starts with data: then it is a chunk
|
|
68
|
-
if (chunkLine.startsWith('data:')) {
|
|
69
|
-
const chunkData = chunkLine.substring(6).trim();
|
|
70
|
-
if (chunkData !== '[DONE]') {
|
|
71
|
-
yield JSON.parse(chunkData);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
this.endpoint = config.endpoint || this.ENDPOINT;
|
|
78
|
-
this.apiKey = config.apiKey || process.env.MISTRAL_API_KEY;
|
|
79
|
-
this.textDecoder = new TextDecoder();
|
|
80
|
-
(0, axios_retry_1.default)(axios_1.default, {
|
|
81
|
-
retries: 3,
|
|
82
|
-
retryCondition: (error) => {
|
|
83
|
-
return this.RETRY_STATUS_CODES.includes(error.response.status);
|
|
84
|
-
},
|
|
85
|
-
retryDelay: (retryCount, error) => {
|
|
86
|
-
console.debug(`retry attempt: ${retryCount}`, error);
|
|
87
|
-
return retryCount * 500;
|
|
88
|
-
},
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
*
|
|
93
|
-
* @param {*} method
|
|
94
|
-
* @param {*} path
|
|
95
|
-
* @param {*} request
|
|
96
|
-
* @return {Promise<T>}
|
|
97
|
-
*/
|
|
98
|
-
async request(method, path, request) {
|
|
99
|
-
const response = await (0, axios_1.default)({
|
|
100
|
-
method: method,
|
|
101
|
-
url: `${this.endpoint}/${path}`,
|
|
102
|
-
data: request || {},
|
|
103
|
-
headers: {
|
|
104
|
-
'Authorization': `Bearer ${this.apiKey}`,
|
|
105
|
-
},
|
|
106
|
-
responseType: request?.stream ? 'stream' : 'json',
|
|
107
|
-
}).catch((error) => {
|
|
108
|
-
console.error(error);
|
|
109
|
-
return error.response;
|
|
110
|
-
});
|
|
111
|
-
return response.data;
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Returns a list of the available models
|
|
115
|
-
* @return {Promise<ListModelsResponse>}
|
|
116
|
-
*/
|
|
117
|
-
async listModels() {
|
|
118
|
-
const response = await this.request('get', 'v1/models');
|
|
119
|
-
return response;
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* A chat endpoint without streaming
|
|
123
|
-
* @param {*} model the name of the model to chat with, e.g. mistral-tiny
|
|
124
|
-
* @param {*} messages an array of messages to chat with, e.g.
|
|
125
|
-
* [{role: 'user', content: 'What is the best French cheese?'}]
|
|
126
|
-
* @param {*} temperature the temperature to use for sampling, e.g. 0.5
|
|
127
|
-
* @param {*} maxTokens the maximum number of tokens to generate, e.g. 100
|
|
128
|
-
* @param {*} topP the cumulative probability of tokens to generate, e.g. 0.9
|
|
129
|
-
* @param {*} randomSeed the random seed to use for sampling, e.g. 42
|
|
130
|
-
* @param {*} safeMode whether to use safe mode, e.g. true
|
|
131
|
-
* @return {Promise<Object>}
|
|
132
|
-
*/
|
|
133
|
-
async chat(params) {
|
|
134
|
-
const response = await this.request('post', 'v1/chat/completions', params);
|
|
135
|
-
return response;
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* An embedddings endpoint that returns embeddings for a single,
|
|
139
|
-
* or batch of inputs
|
|
140
|
-
* @param {*} model The embedding model to use, e.g. mistral-embed
|
|
141
|
-
* @param {*} input The input to embed,
|
|
142
|
-
* e.g. ['What is the best French cheese?']
|
|
143
|
-
* @return {Promise<Object>}
|
|
144
|
-
*/
|
|
145
|
-
async embeddings(model, input) {
|
|
146
|
-
const request = {
|
|
147
|
-
model: model,
|
|
148
|
-
input: input,
|
|
149
|
-
};
|
|
150
|
-
const response = await this.request('post', 'v1/embeddings', request);
|
|
151
|
-
return response;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
exports.MistralClient = MistralClient;
|
|
155
|
-
//# sourceMappingURL=mistralClient.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mistralClient.js","sourceRoot":"","sources":["../../src/models/mistralClient.ts"],"names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