Skip to content

Commit

Permalink
feat: langchain version update (#193)
Browse files Browse the repository at this point in the history
* refactor: new packages

* refactor: langchain updates

* chore: package-lock update

* chore: updated next

* chore: remove experimental from next config

* fix: x-invoke-path not working

* fix: deprecated clerk profileImageUrl

* fix: removed comment

* fix: comment suggestion
  • Loading branch information
StereoPT authored Apr 16, 2024
1 parent 1f66ac1 commit 5484d0f
Show file tree
Hide file tree
Showing 20 changed files with 5,904 additions and 3,305 deletions.
9,125 changes: 5,855 additions & 3,270 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"dependencies": {
"@anatine/zod-nestjs": "^1.10.0",
"@clerk/clerk-sdk-node": "^4.12.5",
"@langchain/community": "^0.0.48",
"@langchain/core": "^0.1.57",
"@langchain/openai": "^0.0.28",
"@langchain/redis": "^0.0.2",
"@nestjs/bull": "^0.6.3",
"@nestjs/common": "^9.0.0",
"@nestjs/config": "^2.3.2",
Expand All @@ -38,7 +42,7 @@
"d3-dsv": "^2.0.0",
"fs": "^0.0.1-security",
"gpt-3-encoder": "^1.1.4",
"langchain": "^0.0.120",
"langchain": "^0.1.33",
"mammoth": "^1.6.0",
"mongodb": "^5.6.0",
"mongoose": "^7.4.0",
Expand All @@ -51,7 +55,7 @@
"rimraf": "^3.0.2",
"rxjs": "^7.2.0",
"uuid": "^9.0.0",
"zod": "^3.21.4"
"zod": "^3.22.4"
},
"devDependencies": {
"@ianvs/prettier-plugin-sort-imports": "^3.7.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/ai/facades/ai.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { VectorDbService } from '@/ai/services/vector-db.service';
import { RedisChatMemoryNotFoundException } from '@/chats/exceptions/redis-chat-memory-not-found.exception';
import { AiResponse } from '@/common/types/ai-response';
import { ChatDocument } from '@/common/types/chat';
import { PromptTemplate } from '@langchain/core/prompts';
import { Injectable, Logger } from '@nestjs/common';
import {
ConversationChain,
LLMChain,
loadSummarizationChain,
} from 'langchain/chains';
import { Document } from 'langchain/document';
import { PromptTemplate } from 'langchain/prompts';
import { BaseMessage, ChainValues } from 'langchain/schema';

type AIExecutor = DocumentConversationChain | ConversationChain;
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/ai/factories/create-chat-model.factory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AiModel } from '@/common/types/ai-models';
import { decrypt } from '@/common/utils/encrypt-string';
import { BaseChatModel } from 'langchain/chat_models';
import { ChatOpenAI } from 'langchain/chat_models/openai';
import { BaseChatModel } from '@langchain/core/language_models/chat_models';
import { ChatOpenAI } from '@langchain/openai';

type Model = 'ChatOpenAI';

Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/ai/services/chat-model.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { AiModelsRepository } from '@/ai/ai-models.repository';
import { createChatLlmModelFactory } from '@/ai/factories/create-chat-model.factory';
import { AppConfigService } from '@/app-config/app-config.service';
import { AiModel } from '@/common/types/ai-models';
import { BaseChatModel } from '@langchain/core/language_models/chat_models';
import { Injectable } from '@nestjs/common';
import { BaseChatModel } from 'langchain/chat_models';

@Injectable()
export class ChatModelService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { MemoryService } from '@/ai/services/memory.service';
import { SimpleConversationChainService } from '@/ai/services/simple-conversation-chain.service';
import { VectorDbService } from '@/ai/services/vector-db.service';
import { ChatDocument } from '@/common/types/chat';
import { BaseChatModel } from '@langchain/core/language_models/chat_models';
import { PromptTemplate } from '@langchain/core/prompts';
import { Injectable } from '@nestjs/common';
import { RetrievalQAChain, loadQAStuffChain } from 'langchain/chains';
import { BaseChatModel } from 'langchain/chat_models';
import { PromptTemplate } from 'langchain/prompts';

type DocumentConversationChainProps = {
memoryService: MemoryService;
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/ai/services/memory.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AppConfigService } from '@/app-config/app-config.service';
import { CACHE_CLIENT } from '@/common/constants/cache';
import { RedisChatMessageHistory } from '@langchain/redis';
import { Inject, Injectable } from '@nestjs/common';
import { BufferMemory } from 'langchain/memory';
import { RedisChatMessageHistory } from 'langchain/stores/message/redis';
import { RedisClientType } from 'redis';

@Injectable()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { MemoryService } from '@/ai/services/memory.service';
import { Injectable } from '@nestjs/common';
import { ConversationChain } from 'langchain/chains';
import { BaseChatModel } from 'langchain/chat_models';
import { BaseChatModel } from '@langchain/core/language_models/chat_models';
import {
ChatPromptTemplate,
HumanMessagePromptTemplate,
MessagesPlaceholder,
} from 'langchain/prompts';
} from '@langchain/core/prompts';
import { Injectable } from '@nestjs/common';
import { ConversationChain } from 'langchain/chains';

@Injectable()
export class SimpleConversationChainService {
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/ai/services/vector-db.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { sanitizeFilename } from '@/common/constants/files';
import { Chroma } from '@langchain/community/vectorstores/chroma';
import { OpenAIEmbeddings } from '@langchain/openai';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { OpenAIEmbeddings } from 'langchain/embeddings/openai';
import { Chroma } from 'langchain/vectorstores/chroma';

@Injectable()
export class VectorDbService {
Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/chats/chat-socket.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ export class ChatSocketGateway {
);

const numberOfTokensRedis = chatMessageHistory.reduce((acc, curr) => {
if (!curr.content) {
if (!curr.content || curr.content !== 'string') {
return 0;
}

return (acc += encode(curr.content).length);
}, 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import { OnQueueError, OnQueueFailed, Process, Processor } from '@nestjs/bull';
import { Logger } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { Job } from 'bull';
import { BaseDocumentLoader } from 'langchain/dist/document_loaders/base';
import { Document } from 'langchain/document';
import { BaseDocumentLoader } from 'langchain/document_loaders';
import { DocxLoader } from 'langchain/document_loaders/fs/docx';
import { PDFLoader } from 'langchain/document_loaders/fs/pdf';
import { TextLoader } from 'langchain/document_loaders/fs/text';
Expand Down
2 changes: 1 addition & 1 deletion packages/contract/auth/user.response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const EmailAddressResponseSchema = z.object({
export const UserResponseSchema = z.object({
id: z.string(),
primaryEmailAddressId: z.string(),
profileImageUrl: z.string(),
imageUrl: z.string(),
username: z.string().nullable(),
firstName: z.string().nullable(),
lastName: z.string().nullable(),
Expand Down
2 changes: 1 addition & 1 deletion packages/contract/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"name": "contract",
"version": "1.0.0",
"dependencies": {
"zod": "^3.21.4"
"zod": "^3.22.4"
}
}
5 changes: 1 addition & 4 deletions packages/web-ui/app/admin/manage/users/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ export default async function AdminUsers() {
<TableRow key={user.id}>
<TableCell>
<Avatar className="h-8 w-8 text-right">
<AvatarImage
src={user.profileImageUrl}
alt="Profile Image"
/>
<AvatarImage src={user.imageUrl} alt="Profile Image" />
</Avatar>
</TableCell>
<TableCell>
Expand Down
6 changes: 3 additions & 3 deletions packages/web-ui/app/rooms/[room]/settings/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const getUserList = (users: UserResponseDto[]): UserListType[] => {
? `${user.firstName} ${user.lastName}`
: null,
userName: user.username,
imageUrl: user.profileImageUrl,
imageUrl: user.imageUrl,
email:
user.emailAddresses.find(
(email) => user.primaryEmailAddressId === email.id
Expand All @@ -38,7 +38,7 @@ const getUserList = (users: UserResponseDto[]): UserListType[] => {
return {
label: userIdentification,
value: userIdentification,
imageUrl: user.profileImageUrl,
imageUrl: user.imageUrl,
userId: user.id,
roles: user.roles,
};
Expand Down Expand Up @@ -83,7 +83,7 @@ export default async function Settings({
<p className="font-bold">Room owner</p>
<div className="flex gap-4">
<Avatar className="h-16 w-16">
<AvatarImage src={owner.profileImageUrl} alt="Profile Image" />
<AvatarImage src={owner.imageUrl} alt="Profile Image" />
</Avatar>
<div className="flex flex-col text-gray-500">
<p>{`Name: ${
Expand Down
2 changes: 1 addition & 1 deletion packages/web-ui/factory/create-chat.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const createChatParticipantsFactory = (
return participants.map((participant): ChatUserType => {
return {
userId: participant.id,
imageUrl: participant.profileImageUrl,
imageUrl: participant.imageUrl,
userName: participant.username,
name:
participant.firstName && participant.lastName
Expand Down
15 changes: 15 additions & 0 deletions packages/web-ui/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
import { NextResponse } from 'next/server';
import { authMiddleware } from '@clerk/nextjs';

export default authMiddleware({
publicRoutes: ['/', '/admin', /^\/admin\/.*/],
beforeAuth(req) {
const requestHeaders = new Headers(req.headers);

const url = new URL(req.url);
const pathname = url.pathname;

requestHeaders.set('url-path', pathname);

return NextResponse.next({
request: {
headers: requestHeaders,
},
});
},
});

export const config = {
Expand Down
7 changes: 2 additions & 5 deletions packages/web-ui/next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: false,
experimental: {
appDir: true,
},
}
};

export default nextConfig
export default nextConfig;
4 changes: 2 additions & 2 deletions packages/web-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"cookies-next": "^2.1.2",
"date-fns": "^2.30.0",
"lucide-react": "^0.263.0",
"next": "^13.4.4",
"next": "^13.5.6",
"next-themes": "^0.2.1",
"query-string": "^8.1.0",
"react": "^18.2.0",
Expand All @@ -57,7 +57,7 @@
"tailwindcss-animate": "^1.0.6",
"utf-8-validate": "^5.0.10",
"uuid": "^9.0.0",
"zod": "^3.21.4"
"zod": "^3.22.4"
},
"devDependencies": {
"@ianvs/prettier-plugin-sort-imports": "^3.7.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/web-ui/utils/get-url-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { headers } from 'next/headers';

export function getUrlPath() {
const { get: getHeader } = headers();
const urlPath = getHeader('x-invoke-path');
const urlPath = getHeader('url-path');

return {
urlPath,
Expand Down

0 comments on commit 5484d0f

Please sign in to comment.