Skip to content

Commit

Permalink
openAI type fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pinocchio-life-like committed Dec 27, 2024
1 parent 7732bf1 commit ed4d66c
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 16 deletions.
5 changes: 5 additions & 0 deletions server/src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,3 +646,8 @@ export type GeoJson = InferSelectModel<typeof geojson>;
export type InsertGeoJson = InferInsertModel<typeof geojson>;
export const insertGeoJsonSchema = createInsertSchema(geojson);
export const selectGeoJsonSchema = createSelectSchema(geojson);

export type Conversation = InferSelectModel<typeof conversation>;
export type InsertConversation = InferInsertModel<typeof conversation>;
export const insertConversationSchema = createInsertSchema(conversation);
export const selectConversationSchema = createSelectSchema(conversation);
8 changes: 7 additions & 1 deletion server/src/drizzle/methods/Conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ export class Conversation {
}
}

async findConversation(userId: string, itemTypeId: string) {
async findConversation({
userId,
itemTypeId,
}: {
userId: string;
itemTypeId: string;
}) {
try {
const filter = and(
eq(ConversationTable.userId, userId),
Expand Down
13 changes: 13 additions & 0 deletions server/src/drizzle/methods/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,17 @@ export class User {
throw new Error(`Failed to find user: ${error.message}`);
}
}

async findById(userId: string): Promise<UserType | null> {
try {
const user = await DbClient.instance
.select()
.from(UserTable)
.where(eq(UserTable.id, userId))
.get();
return user || null;
} catch (error) {
throw new Error(`Failed to find user by ID: ${error.message}`);
}
}
}
4 changes: 2 additions & 2 deletions server/src/services/openAi/getAIResponseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ export const getAIResponseService = async (
throw new Error(`Invalid type: ${itemTypeId}`);
}

let conversation: any = await conversationClass.findConversation(
let conversation: any = await conversationClass.findConversation({
userId,
itemTypeId,
);
});

let conversationHistory = conversation ? conversation.history : '';

Expand Down
5 changes: 4 additions & 1 deletion server/src/services/openAi/getUserChatsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export const getUserChatsService = async (userId, itemTypeId) => {
}

const conversation = new Conversation();
const conversations = await conversation.findConversation(userId, itemTypeId);
const conversations = await conversation.findConversation({
userId,
itemTypeId,
});

return { conversations };
};
17 changes: 8 additions & 9 deletions server/src/services/openAi/langchain/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ChatOpenAI } from 'langchain/chat_models/openai';
import { AIMessage, HumanMessage, SystemMessage } from 'langchain/schema';
import Conversation from '../../../models/openai/conversationModel';
import { Conversation } from '../../../drizzle/methods/Conversation';
import { getPackByIdService } from '../../pack/getPackByIdService';
import { getTripByIdService } from '../../trip/getTripByIdService';
import mongoose from 'mongoose';
import User from '../../../models/userModel';
import { User } from '../../../drizzle/methods/User';
import { User as UserType } from '../../../db/schema';

const chatModel = new ChatOpenAI({
openAIApiKey: process.env.OPENAI_API_KEY, // Replace with your OpenAI API key
Expand All @@ -31,20 +32,18 @@ export const getAIResponseService = async (
const tripInfo = await getTripInformation(tripId);

// find last conversation if present
let conversation = await Conversation.findOne({
let conversation = await new Conversation().findConversation({
userId,
// _id: conversationId,
itemTypeId,
itemTypeId: itemTypeId || '',
});

// if conversation is not found, create a new one
if (!conversation) {
conversation = new Conversation({
conversation = await new Conversation().create({
userId,
itemTypeId,
history: '',
});
await conversation.save();
}

let conversationHistory = conversation.history || '';
Expand Down Expand Up @@ -171,12 +170,12 @@ async function saveConversationHistory(conversation, conversationHistory) {
return conversation;
}

export async function validateUser(userId) {
export async function validateUser(userId: string): Promise<UserType> {
if (!mongoose.Types.ObjectId.isValid(userId)) {
throw new Error('Invalid userId');
}

const user = await User.findById(userId).exec();
const user = await new User().findById(userId);
if (!user) {
throw new Error('User not found');
}
Expand Down
8 changes: 5 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
"compilerOptions": {
"strictNullChecks": true,
"noUncheckedIndexedAccess": true,
"module": "node16",
"moduleResolution": "node16",
"module": "CommonJS",
"paths": {
"app/*": ["./packages/app/*"],
"@packrat/api/*": ["./packages/api/*"],
"@packrat/ui/*": ["./packages/ui/*"],
"server/*": ["./server/*"]
"server/*": ["./server/*"],
"@cloudflare/vitest-pool-workers/config": [
"./node_modules/@cloudflare/vitest-pool-workers/dist/config"
]
},
"plugins": [
{
Expand Down

0 comments on commit ed4d66c

Please sign in to comment.