Skip to content

Commit

Permalink
Refactor ConversationService class as makeConversationService func (
Browse files Browse the repository at this point in the history
#76)

refactor conversation service as make func
  • Loading branch information
mongodben authored Aug 14, 2023
1 parent 6ff4219 commit 6cb9a83
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 119 deletions.
4 changes: 2 additions & 2 deletions chat-server/src/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
makeOpenAiEmbedFunc,
} from "chat-core";
import { errorHandler, makeApp, makeHandleTimeoutMiddleware } from "./app";
import { ConversationsService } from "./services/conversations";
import { makeConversationsService } from "./services/conversations";
import { makeDataStreamer } from "./services/dataStreamer";
import { makeOpenAiLlm } from "./services/llm";
import { config } from "./config";
Expand All @@ -20,7 +20,7 @@ describe("App", () => {
config.mongodb.vectorSearchIndexName
);

const conversations = new ConversationsService(
const conversations = makeConversationsService(
mongodb.db,
config.llm.systemPrompt
);
Expand Down
4 changes: 2 additions & 2 deletions chat-server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from "chat-core";
import { DataStreamer } from "./services/dataStreamer";
import { ObjectId } from "mongodb";
import { ConversationsServiceInterface } from "./services/conversations";
import { ConversationsService } from "./services/conversations";
import { getRequestId, logRequest, sendErrorResponse } from "./utils";
import {
Llm,
Expand Down Expand Up @@ -91,7 +91,7 @@ export const makeApp = async ({
embed: EmbedFunc;
store: EmbeddedContentStore;
dataStreamer: DataStreamer;
conversations: ConversationsServiceInterface;
conversations: ConversationsService;
llm: Llm<OpenAiStreamingResponse, OpenAiAwaitedResponse>;
maxRequestTimeoutMs?: number;
findNearestNeighborsOptions?: Partial<FindNearestNeighborsOptions>;
Expand Down
5 changes: 2 additions & 3 deletions chat-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
makeDatabaseConnection,
makeOpenAiEmbedFunc,
} from "chat-core";
import { ConversationsService } from "./services/conversations";
import { makeConversationsService } from "./services/conversations";
import { makeDataStreamer } from "./services/dataStreamer";
import { makeOpenAiLlm } from "./services/llm";
import { config } from "./config";
Expand All @@ -21,7 +21,7 @@ const startServer = async () => {
config.mongodb.vectorSearchIndexName
);

const conversations = new ConversationsService(
const conversations = makeConversationsService(
mongodb.db,
config.llm.systemPrompt
);
Expand All @@ -44,7 +44,6 @@ const startServer = async () => {
searchBoosters: config.conversations?.searchBoosters,
userQueryPreprocessor: config.conversations?.userQueryPreprocessor,
maxRequestTimeoutMs: config.maxRequestTimeoutMs,

});

const server = app.listen(PORT, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
Conversation,
ConversationsService,
Message,
ConversationsServiceInterface,
makeConversationsService,
} from "../../services/conversations";
import express, { Express } from "express";
import {
Expand Down Expand Up @@ -50,7 +50,7 @@ describe("POST /conversations/:conversationId/messages", () => {
let dataStreamer: ReturnType<typeof makeDataStreamer>;
let findNearestNeighborsOptions: Partial<FindNearestNeighborsOptions>;
let store: EmbeddedContentStore;
let conversations: ConversationsServiceInterface;
let conversations: ConversationsService;
let app: Express;

beforeAll(async () => {
Expand Down Expand Up @@ -235,7 +235,7 @@ describe("POST /conversations/:conversationId/messages", () => {
});

test("Should respond 500 if error with conversation service", async () => {
const mockBrokenConversationsService: ConversationsServiceInterface = {
const mockBrokenConversationsService: ConversationsService = {
async create() {
throw new Error("mock error");
},
Expand Down Expand Up @@ -333,13 +333,13 @@ describe("POST /conversations/:conversationId/messages", () => {
});

let conversationId: ObjectId,
conversations: ConversationsServiceInterface,
conversations: ConversationsService,
app: Express;
let testMongo: MongoDB;
beforeEach(async () => {
const dbName = `test-${Date.now()}`;
testMongo = new MongoDB(MONGODB_CONNECTION_URI, dbName);
conversations = new ConversationsService(
conversations = makeConversationsService(
testMongo.db,
config.llm.systemPrompt
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from "chat-core";
import {
Conversation,
ConversationsServiceInterface,
ConversationsService,
Message,
conversationConstants,
} from "../../services/conversations";
Expand Down Expand Up @@ -68,7 +68,7 @@ export const AddMessageRequest = SomeExpressRequest.merge(

export interface AddMessageToConversationRouteParams {
store: EmbeddedContentStore;
conversations: ConversationsServiceInterface;
conversations: ConversationsService;
embed: EmbedFunc;
llm: Llm<OpenAiStreamingResponse, OpenAiAwaitedResponse>;
dataStreamer: DataStreamer;
Expand Down Expand Up @@ -387,7 +387,7 @@ export async function sendStaticNonResponse({
dataStreamer,
res,
}: {
conversations: ConversationsServiceInterface;
conversations: ConversationsService;
conversationId: ObjectId;
preprocessedUserMessageContent?: string;
latestMessageText: string;
Expand Down Expand Up @@ -455,7 +455,7 @@ interface AddMessagesToDatabaseParams {
preprocessedUserMessageContent?: string;
assistantMessageContent: string;
assistantMessageReferences: References;
conversations: ConversationsServiceInterface;
conversations: ConversationsService;
}
export async function addMessagesToDatabase({
conversationId,
Expand Down
10 changes: 7 additions & 3 deletions chat-server/src/routes/conversations/createConversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import {
Request as ExpressRequest,
} from "express";
import { z } from "zod";
import { ConversationsServiceInterface } from "../../services/conversations";
import { ApiConversation, convertConversationFromDbToApi, isValidIp } from "./utils";
import { ConversationsService } from "../../services/conversations";
import {
ApiConversation,
convertConversationFromDbToApi,
isValidIp,
} from "./utils";
import { getRequestId, logRequest, sendErrorResponse } from "../../utils";
import { SomeExpressRequest } from "../../middleware/validateRequestSchema";

Expand All @@ -22,7 +26,7 @@ export const CreateConversationRequest = SomeExpressRequest.merge(
);

export interface CreateConversationRouteParams {
conversations: ConversationsServiceInterface;
conversations: ConversationsService;
}

export function makeCreateConversationRoute({
Expand Down
4 changes: 2 additions & 2 deletions chat-server/src/routes/conversations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
OpenAiStreamingResponse,
} from "../../services/llm";
import { DataStreamer } from "../../services/dataStreamer";
import { ConversationsServiceInterface } from "../../services/conversations";
import { ConversationsService } from "../../services/conversations";
import { EmbeddedContentStore } from "chat-core";
import { RateMessageRequest, makeRateMessageRoute } from "./rateMessage";
import {
Expand All @@ -28,7 +28,7 @@ export interface ConversationsRouterParams<T, U> {
embed: EmbedFunc;
dataStreamer: DataStreamer;
store: EmbeddedContentStore;
conversations: ConversationsServiceInterface;
conversations: ConversationsService;
findNearestNeighborsOptions?: Partial<FindNearestNeighborsOptions>;
searchBoosters?: SearchBooster[];
userQueryPreprocessor?: QueryPreprocessorFunc;
Expand Down
4 changes: 2 additions & 2 deletions chat-server/src/routes/conversations/rateMessage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { MongoDB } from "chat-core";
import {
Conversation,
Message,
ConversationsServiceInterface,
ConversationsService,
} from "../../services/conversations";
import { Express } from "express";
import { ObjectId } from "mongodb";
Expand All @@ -19,7 +19,7 @@ describe("POST /conversations/:conversationId/messages/:messageId/rating", () =>
const endpointUrl =
CONVERSATIONS_API_V1_PREFIX + "/:conversationId/messages/:messageId/rating";
let app: Express;
let conversations: ConversationsServiceInterface;
let conversations: ConversationsService;
let conversation: Conversation;
let testMsg: Message;
let testEndpointUrl: string;
Expand Down
5 changes: 2 additions & 3 deletions chat-server/src/routes/conversations/rateMessage.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ObjectId } from "mongodb";
import { strict as assert } from "assert";
import {
Conversation,
ConversationsServiceInterface,
ConversationsService,
} from "../../services/conversations";
import {
Request as ExpressRequest,
Expand Down Expand Up @@ -33,7 +32,7 @@ export const RateMessageRequest = SomeExpressRequest.merge(
);

export interface RateMessageRouteParams {
conversations: ConversationsServiceInterface;
conversations: ConversationsService;
}

export function makeRateMessageRoute({
Expand Down
4 changes: 2 additions & 2 deletions chat-server/src/services/conversations.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "dotenv/config";
import { MongoDB } from "chat-core";
import { Conversation, ConversationsService } from "./conversations";
import { Conversation, makeConversationsService } from "./conversations";
import { BSON } from "mongodb";
import { config } from "../config";

Expand All @@ -24,7 +24,7 @@ describe("Conversations Service", () => {
await mongodb.close();
});

const conversationsService = new ConversationsService(
const conversationsService = makeConversationsService(
mongodb.db,
config.llm.systemPrompt
);
Expand Down
Loading

0 comments on commit 6cb9a83

Please sign in to comment.