@saltcorn/large-language-model 1.0.8 → 1.0.9

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
@@ -331,12 +331,17 @@ const addImageMesssage = async (
331
331
  }
332
332
  break;
333
333
  case "AI SDK":
334
+ let aisdk_image;
335
+ if (imageurl.startsWith("data:") && imageurl.includes("base64,")) {
336
+ const [_pre, b64] = imageurl.split("base64,");
337
+ aisdk_image = b64;
338
+ } else aisdk_image = imageurl;
334
339
  chat.push({
335
340
  role: "user",
336
341
  content: [
337
342
  {
338
343
  type: "image",
339
- image: imageurl,
344
+ image: aisdk_image,
340
345
  },
341
346
  ],
342
347
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/large-language-model",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "Large language models and functionality for Saltcorn",
5
5
  "main": "index.js",
6
6
  "dependencies": {
Binary file
package/tests/llm.test.js CHANGED
@@ -2,7 +2,8 @@ const { getState } = require("@saltcorn/data/db/state");
2
2
  const View = require("@saltcorn/data/models/view");
3
3
  const Table = require("@saltcorn/data/models/table");
4
4
  const Plugin = require("@saltcorn/data/models/plugin");
5
-
5
+ const fs = require("fs");
6
+ const path = require("path");
6
7
  const { mockReqRes } = require("@saltcorn/data/tests/mocks");
7
8
  const { afterAll, beforeAll, describe, it, expect } = require("@jest/globals");
8
9
 
@@ -97,14 +98,14 @@ for (const nameconfig of require("./configs")) {
97
98
  });
98
99
  it("uses tools", async () => {
99
100
  const answer = await getState().functions.llm_generate.run(
100
- "Generate a list of all the EU capitals in a structured format using the provided tool",
101
+ "Generate a list of all the EU capitals in a structured format using the cities tool",
101
102
  cities_tool,
102
103
  );
103
104
  expect(typeof answer).toBe("object");
104
- const cities = answer.ai_sdk
105
- ? answer.tool_calls[0].input?.cities
106
- : JSON.parse(answer.tool_calls[0].function.arguments).cities;
105
+ const cities = answer.getToolCalls()[0].input?.cities;
107
106
  expect(cities.length).toBe(27);
107
+ expect(typeof cities[0].country_name).toBe("string");
108
+ expect(typeof cities[0].city_name).toBe("string");
108
109
  });
109
110
  it("appends to chat history", async () => {
110
111
  const chat = [];
@@ -131,7 +132,7 @@ for (const nameconfig of require("./configs")) {
131
132
  it("tool use sequence", async () => {
132
133
  const chat = [];
133
134
  const answer = await getState().functions.llm_generate.run(
134
- "Generate a list of all the EU capitals in a structured format using the provided tool",
135
+ "Generate a list of all the EU capitals in a structured format using the cities tool",
135
136
  {
136
137
  chat,
137
138
  appendToChat: true,
@@ -185,6 +186,24 @@ for (const nameconfig of require("./configs")) {
185
186
  expect(!!json_answer.cities[0].city_name).toBe(true);
186
187
  expect(!!json_answer.cities[0].country_name).toBe(true);
187
188
  });
189
+ it("reads images", async () => {
190
+ const chat = [];
191
+ const b64 = fs
192
+ .readFileSync(path.join(__dirname, "enjoy.png"))
193
+ .toString("base64"),
194
+ imageurl = `data:image/png;base64,${b64}`;
195
+ await getState().functions.llm_add_message.run("image", imageurl, {
196
+ chat,
197
+ });
198
+ const answer = await getState().functions.llm_generate.run(
199
+ "What is written in this picture?",
200
+ {
201
+ chat,
202
+ },
203
+ );
204
+ expect(typeof answer).toBe("string");
205
+ expect(answer.toLowerCase()).toContain("coffee");
206
+ });
188
207
  if (name !== "AI SDK Anthropic")
189
208
  it("gets embedding", async () => {
190
209
  const v = await getState().functions.llm_embedding.run(