langchain-xai
Version:
Grok API wrapper for Node.js
50 lines (43 loc) • 1.34 kB
text/typescript
interface GrokOptions {
apiKey: string;
model?: string;
temperature?: number;
maxTokens?: number;
}
export class ChatGrok {
private apiKey: string;
private model: string;
private temperature: number;
private maxTokens: number;
constructor(options: GrokOptions) {
this.apiKey = options.apiKey;
this.model = options.model || "grok-beta";
this.temperature = options.temperature || 0.7;
this.maxTokens = options.maxTokens || 1000;
}
async chat(input: string): Promise<string> {
// Implementation will depend on Grok's API
// This is a placeholder until you have the actual API details
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: input }],
temperature: this.temperature,
max_tokens: this.maxTokens,
}),
});
const data = await response.json();
return data.choices[0].message.content;
}
async explain(input: string): Promise<string> {
return this.chat(`Explain this: ${input}`);
}
async analyze(input: string): Promise<string> {
return this.chat(`Analyze this: ${input}`);
}
}