|
@@ -1,13 +1,25 @@
|
|
|
|
|
+import crypto from 'crypto';
|
|
|
|
|
+
|
|
|
import type { OpenAI } from 'openai';
|
|
import type { OpenAI } from 'openai';
|
|
|
|
|
|
|
|
import { openaiClient } from './client';
|
|
import { openaiClient } from './client';
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+const hasher = crypto.createHash('sha256');
|
|
|
|
|
+
|
|
|
export const embed = async(input: string, username?: string): Promise<OpenAI.Embedding[]> => {
|
|
export const embed = async(input: string, username?: string): Promise<OpenAI.Embedding[]> => {
|
|
|
|
|
+ let user;
|
|
|
|
|
+
|
|
|
|
|
+ if (username != null) {
|
|
|
|
|
+ hasher.update(username);
|
|
|
|
|
+ user = hasher.digest('hex');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
const result = await openaiClient.embeddings.create({
|
|
const result = await openaiClient.embeddings.create({
|
|
|
input,
|
|
input,
|
|
|
model: 'text-embedding-3-large',
|
|
model: 'text-embedding-3-large',
|
|
|
dimensions: Number(process.env.OPENAI_DIMENSIONS),
|
|
dimensions: Number(process.env.OPENAI_DIMENSIONS),
|
|
|
- user: username,
|
|
|
|
|
|
|
+ user,
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
return result.data;
|
|
return result.data;
|