langchain-xai
Version:
Grok API wrapper for Node.js
37 lines (36 loc) • 1.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChatGrok = void 0;
class ChatGrok {
constructor(options) {
this.apiKey = options.apiKey;
this.model = options.model || "grok-beta";
this.temperature = options.temperature || 0.7;
this.maxTokens = options.maxTokens || 1000;
}
async invoke(input, options) {
const content = typeof input === "string" ? input : input.content;
const response = await fetch("https://api.grok.x.ai/v1/chat", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${this.apiKey}`,
},
body: JSON.stringify({
model: this.model,
messages: [{ role: "user", content }],
temperature: options?.temperature ?? this.temperature,
max_tokens: options?.maxTokens ?? this.maxTokens,
}),
});
const data = await response.json();
return data.choices[0].message.content;
}
async batch(inputs, options, batchOptions) {
return Promise.all(inputs.map((input) => this.invoke(input, options)));
}
async stream(input, options) {
throw new Error("Streaming not implemented for ChatGrok");
}
}
exports.ChatGrok = ChatGrok;