@zhangzichao2008/mcp-graphiti
Version:
Graphiti MCP Server - Temporal Knowledge Graph for AI Agents
81 lines • 2.96 kB
JavaScript
export class BigModelEmbedder {
config;
logger;
constructor(config, logger) {
this.config = config;
this.logger = logger;
}
async generateEmbedding(text) {
try {
const response = await fetch(this.config.api_url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.config.api_key}`,
},
body: JSON.stringify({
model: this.config.model,
input: text,
}),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Embedding API request failed: ${response.status} ${response.statusText} - ${errorText}`);
}
const data = await response.json();
if (!data.data || data.data.length === 0) {
throw new Error('No embedding data returned from API');
}
const embedding = data.data[0].embedding;
this.logger.debug(`Generated embedding with dimension: ${embedding.length}`);
return embedding;
}
catch (error) {
this.logger.error('Failed to generate embedding:', error);
throw error;
}
}
async generateEmbeddings(texts) {
try {
const response = await fetch(this.config.api_url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.config.api_key}`,
},
body: JSON.stringify({
model: this.config.model,
input: texts,
}),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Embedding API request failed: ${response.status} ${response.statusText} - ${errorText}`);
}
const data = await response.json();
if (!data.data || data.data.length === 0) {
throw new Error('No embedding data returned from API');
}
const embeddings = data.data
.sort((a, b) => a.index - b.index)
.map(item => item.embedding);
this.logger.debug(`Generated ${embeddings.length} embeddings`);
return embeddings;
}
catch (error) {
this.logger.error('Failed to generate embeddings:', error);
throw error;
}
}
async testConnection() {
try {
await this.generateEmbedding('test');
return true;
}
catch (error) {
this.logger.error('Embedding service connection test failed:', error);
return false;
}
}
}
//# sourceMappingURL=bigmodel.js.map