Skip to content

Commit

Permalink
feat: include history in bot knowledge
Browse files Browse the repository at this point in the history
  • Loading branch information
MattAgn committed Sep 8, 2024
1 parent b44669e commit 75447dc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion apps/expo/src/app/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default function Chat() {
return;
}

respondToMessage.mutateAsync({ message, channelId });
respondToMessage.mutateAsync({ channelId });

Check failure on line 37 in apps/expo/src/app/chat.tsx

View workflow job for this annotation

GitHub Actions / lint

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
} catch (error) {
console.log(error);
}
Expand Down
10 changes: 10 additions & 0 deletions packages/api/src/infra/adaptStreamMessagesToGptMessages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ChatCompletionMessageParam } from "openai/resources/index.mjs";

Check warning on line 1 in packages/api/src/infra/adaptStreamMessagesToGptMessages.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`
import { DefaultGenerics, Message, MessageResponse } from "stream-chat";

Check warning on line 2 in packages/api/src/infra/adaptStreamMessagesToGptMessages.ts

View workflow job for this annotation

GitHub Actions / lint

Imports "DefaultGenerics" and "MessageResponse" are only used as type

Check failure on line 2 in packages/api/src/infra/adaptStreamMessagesToGptMessages.ts

View workflow job for this annotation

GitHub Actions / lint

'Message' is defined but never used. Allowed unused vars must match /^_/u

export const adaptStreamMessagesToGptMessages = (
messages: MessageResponse<DefaultGenerics>[],
): ChatCompletionMessageParam[] =>
messages.map((message) => ({
role: message?.user?.role === "user" ? "user" : "system",

Check failure on line 8 in packages/api/src/infra/adaptStreamMessagesToGptMessages.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary optional chain on a non-nullish value
content: message.text ?? "",
}));
28 changes: 16 additions & 12 deletions packages/api/src/router/chatbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import OpenAI from "openai";
import { StreamChat } from "stream-chat";
import { z } from "zod";

import { adaptStreamMessagesToGptMessages } from "../infra/adaptStreamMessagesToGptMessages";
import { publicProcedure } from "../trpc";

const client = new OpenAI({
Expand All @@ -26,17 +27,28 @@ const coachByChannel: Record<ChannelId, string> = {

export const chatbotRouter = {
getChatGptResponse: publicProcedure
.input(
z.object({ message: z.string(), channelId: z.nativeEnum(ChannelId) }),
)
.input(z.object({ channelId: z.nativeEnum(ChannelId) }))
.mutation(async ({ input }) => {
if (!STREAM_API_KEY || !STREAM_SECRET) {
throw new Error("STREAM_API_KEY and STREAM_SECRET are required");
}

const chatClient = StreamChat.getInstance(STREAM_API_KEY, STREAM_SECRET);

const channel = chatClient.channel("Chatgpt", input.channelId);

const { messages: chatHistory } = await channel.query({
messages: { limit: 30 },
});
console.log(chatHistory);

const chatCompletion = await client.chat.completions.create({
messages: [
{
role: "system",
content: getCoachingPrompt(input.channelId),
},
{ role: "user", content: input.message },
...adaptStreamMessagesToGptMessages(chatHistory),
],
model: "gpt-4o",
temperature: 0.2,
Expand All @@ -46,14 +58,6 @@ export const chatbotRouter = {
chatCompletion.choices[0]?.message.content ??
"Sorry i'm asleep right now, come back later";

if (!STREAM_API_KEY || !STREAM_SECRET) {
throw new Error("STREAM_API_KEY and STREAM_SECRET are required");
}

const chatClient = StreamChat.getInstance(STREAM_API_KEY, STREAM_SECRET);

const channel = chatClient.channel("Chatgpt", input.channelId, {});

const gptmessage = {
text: message,
user_id: coachByChannel[input.channelId],
Expand Down

0 comments on commit 75447dc

Please sign in to comment.