|
|
const OpenAI = require("openai"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const client = new OpenAI({ |
|
|
baseURL: "http://localhost:3001/api/v1/openai", |
|
|
apiKey: "ENTER_ANYTHINGLLM_API_KEY_HERE", |
|
|
}); |
|
|
|
|
|
(async () => { |
|
|
|
|
|
console.log("Fetching /models"); |
|
|
const modelList = await client.models.list(); |
|
|
for await (const model of modelList) { |
|
|
console.log({ model }); |
|
|
} |
|
|
|
|
|
|
|
|
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?" }, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
], |
|
|
model: "anythingllm", |
|
|
}); |
|
|
console.log(syncCompletion.choices[0]); |
|
|
|
|
|
|
|
|
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", |
|
|
stream: true, |
|
|
}); |
|
|
|
|
|
let message = ""; |
|
|
for await (const chunk of asyncCompletion) { |
|
|
message += chunk.choices[0].delta.content; |
|
|
console.log({ message }); |
|
|
} |
|
|
|
|
|
|
|
|
console.log("Creating embeddings"); |
|
|
const embedding = await client.embeddings.create({ |
|
|
model: null, |
|
|
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` |
|
|
); |
|
|
|
|
|
|
|
|
console.log("Fetching /vector_stores"); |
|
|
const vectorDBList = await client.beta.vectorStores.list(); |
|
|
for await (const db of vectorDBList) { |
|
|
console.log(db); |
|
|
} |
|
|
})(); |
|
|
|