utilitas 1995.2.40 → 1995.2.41

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/lib/alan.mjs CHANGED
@@ -3,8 +3,8 @@ import { create as createUoid } from './uoid.mjs';
3
3
  import { loop, end } from './event.mjs';
4
4
 
5
5
  import {
6
- ensureString, ignoreErrFunc, log as _log, need, renderText as _renderText,
7
- throwError,
6
+ checkInterval, ensureString, ignoreErrFunc, log as _log, need,
7
+ renderText as _renderText, throwError,
8
8
  } from './utilitas.mjs';
9
9
 
10
10
  const _NEED = [
@@ -15,12 +15,13 @@ const _NEED = [
15
15
  const [
16
16
  GPT_35_TURBO, GPT_35_TURBO_1106, GPT_4, GPT_4_1106, GPT_4_VISION,
17
17
  GEMINI_PRO, GEMINI_PRO_VISION, TEXT_EMBEDDING_ADA_002, EMBEDDING_001,
18
- EMBEDDING_GECKO_001, EMBEDDING_GECKO_002, EMBEDDING_GECKO_ML001,
18
+ EMBEDDING_GECKO_001, EMBEDDING_GECKO_002, EMBEDDING_GECKO_ML001, MISTRAL,
19
19
  ] = [
20
20
  'gpt-3.5-turbo', 'gpt-3.5-turbo-1106', 'gpt-4', 'gpt-4-1106',
21
21
  'gpt-4-1106-preview', 'gemini-pro', 'gemini-pro-vision',
22
22
  'text-embedding-ada-002', 'embedding-001', 'textembedding-gecko@001',
23
23
  'textembedding-gecko@002', 'textembedding-gecko-multilingual@001',
24
+ 'mistral',
24
25
  ];
25
26
 
26
27
  const [tool, provider, messages, text] = [
@@ -50,26 +51,31 @@ const trimTailing = text => text.replace(/[\.\s]*$/, '');
50
51
  const newSessionId = () => createUoid({ type: sessionType });
51
52
  const renderText = (t, o) => _renderText(t, { extraCodeBlock: 0, ...o || {} });
52
53
  const log = (cnt, opt) => _log(cnt, import.meta.url, { time: 1, ...opt || {} });
54
+ const [png, jpeg, mov, mpeg, mp4, mpg, avi, wmv, mpegps, flv] = [
55
+ 'image/png', 'image/jpeg', 'video/mov', 'video/mpeg', 'video/mp4',
56
+ 'video/mpg', 'video/avi', 'video/wmv', 'video/mpegps', 'video/flv',
57
+ ];
53
58
 
54
59
  const [
55
60
  OPENAI, VERTEX, GEMINI, CHATGPT, ASSISTANT, OPENAI_EMBEDDING,
56
- GEMINI_EMEDDING, VERTEX_EMEDDING, OPENAI_TRAINING
61
+ GEMINI_EMEDDING, VERTEX_EMEDDING, OPENAI_TRAINING, OLLAMA,
57
62
  ] = [
58
63
  'OPENAI', 'VERTEX', 'GEMINI', 'CHATGPT', 'ASSISTANT',
59
64
  'OPENAI_EMBEDDING', 'GEMINI_EMEDDING', 'VERTEX_EMEDDING',
60
- 'OPENAI_TRAINING',
65
+ 'OPENAI_TRAINING', 'OLLAMA',
61
66
  ];
62
67
 
63
68
  const DEFAULT_CHAT_ENGINE = CHATGPT;
64
69
  const DEFAULT_MODELS = {
65
- [CHATGPT]: GPT_35_TURBO,
66
70
  [ASSISTANT]: GPT_35_TURBO,
67
- [VERTEX]: GEMINI_PRO_VISION,
71
+ [CHATGPT]: GPT_35_TURBO,
72
+ [GEMINI_EMEDDING]: EMBEDDING_001,
68
73
  [GEMINI]: GEMINI_PRO,
74
+ [OLLAMA]: MISTRAL,
69
75
  [OPENAI_EMBEDDING]: TEXT_EMBEDDING_ADA_002,
70
- [GEMINI_EMEDDING]: EMBEDDING_001,
71
- [VERTEX_EMEDDING]: EMBEDDING_GECKO_ML001,
72
76
  [OPENAI_TRAINING]: GPT_35_TURBO_1106,
77
+ [VERTEX_EMEDDING]: EMBEDDING_GECKO_ML001,
78
+ [VERTEX]: GEMINI_PRO_VISION,
73
79
  };
74
80
 
75
81
  const chatConfig = {
@@ -130,10 +136,14 @@ const MODELS = {
130
136
  maxVideoLength: 60 * 2,
131
137
  trainingData: 'Feb 2023',
132
138
  supportedMimeTypes: [
133
- 'image/png', 'image/jpeg', 'video/mov', 'video/mpeg', 'video/mp4',
134
- 'video/mpg', 'video/avi', 'video/wmv', 'video/mpegps', 'video/flv',
139
+ png, jpeg, mov, mpeg, mp4, mpg, avi, wmv, mpegps, flv,
135
140
  ],
136
141
  },
142
+ [MISTRAL]: {
143
+ contextWindow: 128000,
144
+ tokenLimitsTPM: Infinity,
145
+ requestLimitsRPM: Infinity,
146
+ },
137
147
  [TEXT_EMBEDDING_ADA_002]: {
138
148
  contextWindow: 5000000,
139
149
  requestLimitsRPM: 5000,
@@ -224,6 +234,11 @@ const init = async (options) => {
224
234
  };
225
235
  }
226
236
  break;
237
+ case OLLAMA:
238
+ clients[provider] = {
239
+ endpoint: options?.endpoint || 'http://localhost:11434',
240
+ };
241
+ break;
227
242
  default:
228
243
  throwError(`Invalid AI provider: ${options?.provider || 'null'}`);
229
244
  }
@@ -251,8 +266,8 @@ const buildVertexMessage = (text, options) => {
251
266
 
252
267
  const buildGeminiMessage = text => String.isString(text) ? [{ text }] : text;
253
268
 
254
- const [getOpenAIClient, getVertexClient, getGeminiClient]
255
- = [OPENAI, VERTEX, GEMINI].map(
269
+ const [getOpenAIClient, getVertexClient, getGeminiClient, getOllamaClient]
270
+ = [OPENAI, VERTEX, GEMINI, OLLAMA].map(
256
271
  x => async options => await init({ ...provider(x), ...options })
257
272
  );
258
273
 
@@ -289,6 +304,34 @@ const promptChatGPT = async (content, options) => {
289
304
  return { response: packGptResp(chunk, options) };
290
305
  };
291
306
 
307
+ const promptOllama = async (content, options) => {
308
+ const { endpoint } = await getOllamaClient(options);
309
+ // https://github.com/jmorganca/ollama/blob/main/examples/typescript-simplechat/client.ts
310
+ const resp = await fetch(`${endpoint}/api/chat`, {
311
+ method: 'POST', body: JSON.stringify({
312
+ ...messages([...options?.messages || [], buildGptMessage(content)]),
313
+ model: options?.model || DEFAULT_MODELS[OLLAMA],
314
+ }),
315
+ });
316
+ const reader = resp.body?.getReader();
317
+ reader || throwError('Failed to request Ollama API.');
318
+ let [chunk, result] = [null, ''];
319
+ while (true) {
320
+ const { done, value } = await reader.read();
321
+ if (done) { break; }
322
+ chunk = JSON.parse(new TextDecoder().decode(value));
323
+ if (!chunk.done) {
324
+ result += chunk.message.content;
325
+ checkInterval(7, result.length) && await ignoreErrFunc(
326
+ async () => await options?.stream?.(
327
+ options?.raw ? chunk : packResp(result)
328
+ ), LOG
329
+ );
330
+ }
331
+ }
332
+ return { response: options?.raw ? chunk : packResp(result) };
333
+ };
334
+
292
335
  const createAssistant = async (options) => {
293
336
  const { clientBeta } = await getOpenAIClient(options);
294
337
  // https://platform.openai.com/docs/api-reference/assistants/createAssistant
@@ -664,7 +707,7 @@ const initChat = async (options) => {
664
707
  }
665
708
  })).id;
666
709
  break;
667
- case CHATGPT: case GEMINI: case VERTEX:
710
+ case CHATGPT: case GEMINI: case VERTEX: case OLLAMA:
668
711
  pmptTokens = countTokens([buildVertexMessage(
669
712
  chatConfig.systemPrompt, { role: system }
670
713
  )]); // Use vertex instead of chatgpt because of the longer pack
@@ -715,12 +758,14 @@ const talk = async (input, options) => {
715
758
  const model = MODELS[chatConfig.engines[engine].model];
716
759
  const sessionId = options?.sessionId || newSessionId();
717
760
  const session = await getSession(sessionId, { engine, ...options });
718
- let [_provider, _model, resp, sys, messages, msgBuilder] =
719
- [engine, null, null, [], [], null];
761
+ let [_model, resp, sys, messages, msgBuilder] =
762
+ [options?.model, null, [], [], null];
720
763
  switch (engine) {
721
- case CHATGPT:
722
- _provider = OPENAI;
723
- _model = DEFAULT_MODELS[CHATGPT];
764
+ case CHATGPT: case OLLAMA:
765
+ switch (engine) {
766
+ case CHATGPT: _model = _model || DEFAULT_MODELS[CHATGPT]; break;
767
+ case OLLAMA: _model = _model || DEFAULT_MODELS[OLLAMA]; break;
768
+ }
724
769
  sys.push(buildGptMessage(session.systemPrompt, { role: system }));
725
770
  msgBuilder = () => {
726
771
  messages = [];
@@ -732,11 +777,10 @@ const talk = async (input, options) => {
732
777
  msgBuilder()
733
778
  break;
734
779
  case ASSISTANT:
735
- _provider = OPENAI;
736
- _model = DEFAULT_MODELS[ASSISTANT];
780
+ _model = _model || DEFAULT_MODELS[ASSISTANT];
737
781
  break;
738
782
  case GEMINI: case VERTEX:
739
- _model = DEFAULT_MODELS[engine];
783
+ _model = _model || DEFAULT_MODELS[engine];
740
784
  sys.push(buildVertexMessage(session.systemPrompt, { role: user }));
741
785
  msgBuilder = () => {
742
786
  messages = [];
@@ -781,6 +825,11 @@ const talk = async (input, options) => {
781
825
  case VERTEX:
782
826
  resp = await promptVertex(input, { messages, ...options });
783
827
  break;
828
+ case OLLAMA:
829
+ resp = await promptOllama(input, {
830
+ messages, model: _model, ...options,
831
+ });
832
+ break;
784
833
  }
785
834
  for (const _resp of resp.response) {
786
835
  if (_resp.text) {
@@ -866,6 +915,7 @@ export {
866
915
  promptAssistant,
867
916
  promptChatGPT,
868
917
  promptGemini,
918
+ promptOllama,
869
919
  promptVertex,
870
920
  resetSession,
871
921
  run,
package/lib/manifest.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  const manifest = {
2
2
  "name": "utilitas",
3
3
  "description": "Just another common utility for JavaScript.",
4
- "version": "1995.2.40",
4
+ "version": "1995.2.41",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "utilitas",
3
3
  "description": "Just another common utility for JavaScript.",
4
- "version": "1995.2.40",
4
+ "version": "1995.2.41",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",