File size: 2,690 Bytes
f8b5d42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const OpenAI = require("openai");

/**
 * @type {import("openai").OpenAI}
 */
const client = new OpenAI({
  baseURL: "http://localhost:3001/api/v1/openai",
  apiKey: "ENTER_ANYTHINGLLM_API_KEY_HERE",
});

(async () => {
  // Models endpoint testing.
  console.log("Fetching /models");
  const modelList = await client.models.list();
  for await (const model of modelList) {
    console.log({ model });
  }

  // Test sync chat completion
  console.log("Running synchronous chat message");
  const syncCompletion = await client.chat.completions.create({
    messages: [
      {
        role: "system",
        content: "You are a helpful assistant who only speaks like a pirate.",
      },
      { role: "user", content: "What is AnythingLLM?" },
      // {
      //   role: 'assistant',
      //   content: "Arrr, matey! AnythingLLM be a fine tool fer sailin' the treacherous sea o' information with a powerful language model at yer helm. It's a potent instrument to handle all manner o' tasks involvin' text, like answerin' questions, generating prose, or even havin' a chat with digital scallywags like meself. Be there any specific treasure ye seek in the realm o' AnythingLLM?"
      // },
      // { role: "user", content: "Why are you talking like a pirate?" },
    ],
    model: "anythingllm", // must be workspace-slug
  });
  console.log(syncCompletion.choices[0]);

  // Test sync chat streaming completion
  console.log("Running asynchronous chat message");
  const asyncCompletion = await client.chat.completions.create({
    messages: [
      {
        role: "system",
        content: "You are a helpful assistant who only speaks like a pirate.",
      },
      { role: "user", content: "What is AnythingLLM?" },
    ],
    model: "anythingllm", // must be workspace-slug
    stream: true,
  });

  let message = "";
  for await (const chunk of asyncCompletion) {
    message += chunk.choices[0].delta.content;
    console.log({ message });
  }

  // Test embeddings creation
  console.log("Creating embeddings");
  const embedding = await client.embeddings.create({
    model: null, // model is optional for AnythingLLM
    input: "This is a test string for embedding",
    encoding_format: "float",
  });
  console.log("Embedding created successfully:");
  console.log(`Dimensions: ${embedding.data[0].embedding.length}`);
  console.log(
    `First few values:`,
    embedding.data[0].embedding.slice(0, 5),
    `+ ${embedding.data[0].embedding.length - 5} more`
  );

  // Vector DB functionality
  console.log("Fetching /vector_stores");
  const vectorDBList = await client.beta.vectorStores.list();
  for await (const db of vectorDBList) {
    console.log(db);
  }
})();