mindcraft/src/models/together.js
mrelmida e42fcd044f implementing TogetherAI and LMStudio
implementing TogetherAI and LM Studio support. Implementing also Speculative Decoding with LM Studio.
2025-05-01 18:49:38 +03:00

58 lines
No EOL
2 KiB
JavaScript

import OpenAIApi from 'openai';
import { getKey, hasKey } from '../utils/keys.js';
import { strictFormat } from '../utils/text.js';
export class Together {
constructor(model_name, url) {
this.model_name = model_name;
let config = {};
config.baseURL = url || 'https://api.together.xyz/v1';
const apiKey = getKey('TOGETHER_API_KEY');
if (!apiKey) {
console.error('Error: TOGETHER_API_KEY not found. Make sure it is set properly.');
}
// Pass the API key to OpenAI compatible Api
config.apiKey = apiKey;
this.openai = new OpenAIApi(config);
}
async sendRequest(turns, systemMessage, stop_seq='*') {
let messages = [{ role: 'system', content: systemMessage }, ...turns];
messages = strictFormat(messages);
// Choose a valid model from openrouter.ai (for example, "openai/gpt-4o")
const pack = {
model: this.model_name || "meta-llama/Llama-3.3-70B-Instruct-Turbo",
messages,
stop: stop_seq
};
let res = null;
try {
console.log('Awaiting together api response...');
let completion = await this.openai.chat.completions.create(pack);
if (!completion?.choices?.[0]) {
console.error('No completion or choices returned:', completion);
return 'No response received.';
}
if (completion.choices[0].finish_reason === 'length') {
throw new Error('Context length exceeded');
}
console.log('Received.');
res = completion.choices[0].message.content;
} catch (err) {
console.error('Error while awaiting response:', err);
// If the error indicates a context-length problem, we can slice the turns array, etc.
res = 'My brain disconnected, try again.';
}
return res;
}
async embed(text) {
throw new Error('Embeddings are not supported by TogetherAI.');
}
}