Skip to content

fix: Add User Identification Headers to OpenAI Requests (#1855) #1856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lib/server/endpoints/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export interface EndpointParameters {
toolResults?: ToolResult[];
isMultimodal?: boolean;
conversationId?: ObjectId;
userId?: ObjectId;
userEmail?: string;
}

interface CommonEndpoint {
Expand Down
16 changes: 15 additions & 1 deletion src/lib/server/endpoints/openai/endpointOai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,15 @@ export async function endpointOai(
"Tools are not supported for 'completions' mode, switch to 'chat_completions' instead"
);
}
return async ({ messages, preprompt, continueMessage, generateSettings, conversationId }) => {
return async ({
messages,
preprompt,
continueMessage,
generateSettings,
conversationId,
userId,
userEmail,
}) => {
const prompt = await buildPrompt({
messages,
continueMessage,
Expand All @@ -183,6 +191,8 @@ export async function endpointOai(
body: { ...body, ...extraBody },
headers: {
"ChatUI-Conversation-ID": conversationId?.toString() ?? "",
"ChatUI-User-Id": userId?.toString() ?? "",
"ChatUI-User-Email": userEmail,
"X-use-cache": "false",
},
});
Expand All @@ -197,6 +207,8 @@ export async function endpointOai(
tools,
toolResults,
conversationId,
userId,
userEmail,
}) => {
// Format messages for the chat API, handling multimodal content if supported
let messagesOpenAI: OpenAI.Chat.Completions.ChatCompletionMessageParam[] =
Expand Down Expand Up @@ -298,6 +310,8 @@ export async function endpointOai(
body: { ...body, ...extraBody },
headers: {
"ChatUI-Conversation-ID": conversationId?.toString() ?? "",
"ChatUI-User-Id": userId?.toString() ?? "",
"ChatUI-User-Email": userEmail,
"X-use-cache": "false",
},
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib/server/textGeneration/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export async function* generate(
toolResults,
isMultimodal: model.multimodal,
conversationId: conv._id,
userId: conv.userId,
userEmail: conv.userEmail,
})) {
// text generation completed
if (output.generated_text) {
Expand Down
1 change: 1 addition & 0 deletions src/lib/types/Conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface Conversation extends Timestamps {

sessionId?: string;
userId?: User["_id"];
userEmail?: User["email"];

model: string;
embeddingModel: string;
Expand Down
4 changes: 3 additions & 1 deletion src/routes/conversation/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ export const POST: RequestHandler = async ({ locals, request }) => {
updatedAt: new Date(),
userAgent: request.headers.get("User-Agent") ?? undefined,
embeddingModel,
...(locals.user ? { userId: locals.user._id } : { sessionId: locals.sessionId }),
...(locals.user
? { userId: locals.user._id, userEmail: locals.user.email }
: { sessionId: locals.sessionId }),
...(values.fromShare ? { meta: { fromShareId: values.fromShare } } : {}),
});

Expand Down