@saltcorn/large-language-model 1.0.0 → 1.0.1

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/generate.js CHANGED
@@ -291,6 +291,58 @@ const toolResponse = async (
291
291
  }
292
292
  };
293
293
 
294
+ const addImageMesssage = async (
295
+ { backend, apiKey, api_key, provider, ai_sdk_provider, responses_api },
296
+ opts,
297
+ ) => {
298
+ let chat = opts.chat;
299
+ let result = opts.prompt;
300
+ //console.log("chat", JSON.stringify(chat, null, 2));
301
+ let imageurl = opts.prompt;
302
+ switch (opts.backend || backend) {
303
+ case "OpenAI":
304
+ {
305
+ const new_chat_item = responses_api
306
+ ? {
307
+ role: "user",
308
+ content: [
309
+ {
310
+ type: "input_image",
311
+ image_url: imageurl,
312
+ },
313
+ ],
314
+ }
315
+ : {
316
+ role: "user",
317
+ content: [
318
+ {
319
+ type: "image_url",
320
+ image_url: {
321
+ url: imageurl,
322
+ },
323
+ },
324
+ ],
325
+ };
326
+
327
+ chat.push(new_chat_item);
328
+ }
329
+ break;
330
+ case "AI SDK":
331
+ chat.push({
332
+ role: "user",
333
+ content: [
334
+ {
335
+ type: "image",
336
+ image: imageurl,
337
+ },
338
+ ],
339
+ });
340
+
341
+ break;
342
+ default:
343
+ }
344
+ };
345
+
294
346
  const getCompletion = async (config, opts) => {
295
347
  switch (config.backend) {
296
348
  case "AI SDK":
@@ -1110,4 +1162,5 @@ module.exports = {
1110
1162
  getImageGeneration,
1111
1163
  getAudioTranscription,
1112
1164
  toolResponse,
1165
+ addImageMesssage,
1113
1166
  };
package/index.js CHANGED
@@ -11,7 +11,8 @@ const {
11
11
  getEmbedding,
12
12
  getImageGeneration,
13
13
  getAudioTranscription,
14
- toolResponse
14
+ toolResponse,
15
+ addImageMesssage,
15
16
  } = require("./generate");
16
17
  const { OPENAI_MODELS } = require("./constants.js");
17
18
  const { eval_expression } = require("@saltcorn/data/models/expression");
@@ -422,14 +423,26 @@ const functions = (config) => {
422
423
  { name: "options", type: "JSON", tstype: "any", required: true },
423
424
  ],
424
425
  },
425
- llm_add_tool_response: {
426
- run: async (prompt, opts) => {
427
- const result = await toolResponse(config, { prompt, ...opts });
428
- return result;
426
+ llm_add_message: {
427
+ run: async (what, prompt, opts) => {
428
+ switch (what) {
429
+ case "tool_response":
430
+ return await toolResponse(config, { prompt, ...opts });
431
+ case "image":
432
+ return await addImageMesssage(config, { prompt, ...opts });
433
+ default:
434
+ break;
435
+ }
429
436
  },
430
437
  isAsync: true,
431
- description: "Insert the response to a tool call into a chat",
438
+ description: "Insert a tool response or an image in a chat",
432
439
  arguments: [
440
+ {
441
+ name: "what",
442
+ type: "String",
443
+ tstype: '"tool_response"|"image"',
444
+ required: true,
445
+ },
433
446
  { name: "prompt", type: "String", required: true },
434
447
  { name: "options", type: "JSON", tstype: "any" },
435
448
  ],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/large-language-model",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Large language models and functionality for Saltcorn",
5
5
  "main": "index.js",
6
6
  "dependencies": {
package/tests/llm.test.js CHANGED
@@ -133,7 +133,12 @@ for (const nameconfig of require("./configs")) {
133
133
  const chat = [];
134
134
  const answer = await getState().functions.llm_generate.run(
135
135
  "Generate a list of EU capitals in a structured format using the provided tool",
136
- { chat, appendToChat: true, ...cities_tool, streamCallback() {} },
136
+ {
137
+ chat,
138
+ appendToChat: true,
139
+ ...cities_tool,
140
+ //streamCallback() {}
141
+ },
137
142
  );
138
143
  expect(typeof answer).toBe("object");
139
144
 
@@ -142,10 +147,14 @@ for (const nameconfig of require("./configs")) {
142
147
  const cities = tc.input.cities;
143
148
  expect(cities.length).toBe(27);
144
149
 
145
- await getState().functions.llm_add_tool_response.run("List received", {
146
- chat,
147
- tool_call: tc,
148
- });
150
+ await getState().functions.llm_add_message.run(
151
+ "tool_response",
152
+ "List received",
153
+ {
154
+ chat,
155
+ tool_call: tc,
156
+ },
157
+ );
149
158
 
150
159
  const answer1 = await getState().functions.llm_generate.run(
151
160
  "Make the same list in a structured format using the provided tool but for the original 12 member countries of the EU",