Skip to content

Commit

Permalink
refacto chat module
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpetrov committed Aug 26, 2023
1 parent 06377eb commit fdb0397
Show file tree
Hide file tree
Showing 26 changed files with 679 additions and 2,086 deletions.
7 changes: 2 additions & 5 deletions pages/api/agents/[id]/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export const chatAgentRequest = async (
channel: ConversationChannel.dashboard,
agentId: agent?.id,
userId: session?.user?.id,
visitorId: data.visitorId,
visitorId: data.visitorId!,
conversationId,
});

Expand All @@ -127,10 +127,7 @@ export const chatAgentRequest = async (
manager.query({
input: data.query,
stream: data.streaming ? handleStream : undefined,
history: agent?.owner?.conversations?.[0]?.messages?.map((m) => ({
from: m.from,
message: m.text,
})),
history: agent?.owner?.conversations?.[0]?.messages,
temperature: data.temperature,
promptTemplate: data.promptTemplate,
promptType: data.promptType,
Expand Down
35 changes: 13 additions & 22 deletions pages/api/chains/run.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
import {
AgentVisibility,
ConversationChannel,
MessageFrom,
Usage,
} from '@prisma/client';
import { ConversationChannel, MessageFrom, Usage } from '@prisma/client';
import Cors from 'cors';
import cuid from 'cuid';
import { NextApiRequest, NextApiResponse } from 'next';

import { AppNextApiRequest, SSE_EVENT } from '@app/types';
import { ChatRequest } from '@app/types/dtos';
import { queryCountConfig } from '@app/utils/account-config';
import AgentManager from '@app/utils/agent';
import { RunChainRequest } from '@app/types/dtos';
import { ApiError, ApiErrorType } from '@app/utils/api-error';
import ChainManager from '@app/utils/chains';
import ConversationManager from '@app/utils/conversation';
import {
createAuthApiHandler,
createLazyAuthHandler,
respond,
} from '@app/utils/createa-api-handler';
import { createAuthApiHandler } from '@app/utils/createa-api-handler';
import guardAgentQueryUsage from '@app/utils/guard-agent-query-usage';
import prisma from '@app/utils/prisma-client';
import runMiddleware from '@app/utils/run-middleware';
import streamData from '@app/utils/stream-data';
import validate from '@app/utils/validate';

const handler = createAuthApiHandler();

Expand All @@ -36,7 +26,7 @@ export const runChainRequest = async (
res: NextApiResponse
) => {
const session = req.session;
const data = req.body as ChatRequest;
const data = req.body as RunChainRequest;

const conversationId = data.conversationId || cuid();

Expand Down Expand Up @@ -110,7 +100,7 @@ export const runChainRequest = async (
channel: ConversationChannel.dashboard,
// agentId: agent?.id,
userId: session?.user?.id,
visitorId: data.visitorId,
visitorId: data.visitorId!,
conversationId,
});

Expand All @@ -133,14 +123,10 @@ export const runChainRequest = async (
manager.query({
input: data.query,
stream: data.streaming ? handleStream : undefined,
history: conversation?.messages?.map((m) => ({
from: m.from,
message: m.text,
})),
history: conversation?.messages,
temperature: data.temperature,
promptTemplate: data.promptTemplate,
promptType: data.promptType,
truncateQuery: data.truncateQuery,
filters: data.filters,
httpResponse: res,
abortController: ctrl,
Expand Down Expand Up @@ -194,7 +180,12 @@ export const runChainRequest = async (
}
};

handler.post(respond(runChainRequest));
handler.post(
validate({
handler: runChainRequest,
body: RunChainRequest,
})
);

export default async function wrapper(
req: AppNextApiRequest,
Expand Down
41 changes: 0 additions & 41 deletions pages/api/datastores/[id]/chat.ts

This file was deleted.

5 changes: 1 addition & 4 deletions pages/api/integrations/crisp/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,7 @@ const handleQuery = async (

const { answer, sources } = await new AgentManager({ agent }).query({
input: query,
history: conversation?.messages?.map((message) => ({
from: message.from,
message: message.text,
})),
history: conversation?.messages,
});

const finalAnser = `${answer}\n\n${formatSourcesRawText(sources!)}`.trim();
Expand Down
87 changes: 0 additions & 87 deletions pages/api/xp/bnp/eval.ts

This file was deleted.

54 changes: 0 additions & 54 deletions pages/api/xp/bnp/history.ts

This file was deleted.

Loading

1 comment on commit fdb0397

@vercel
Copy link

@vercel vercel bot commented on fdb0397 Aug 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.