Skip to content

Commit

Permalink
adjusting behaviours
Browse files Browse the repository at this point in the history
  • Loading branch information
AmjedNazzal committed May 17, 2024
1 parent cb63446 commit 0465eb3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 51 deletions.
97 changes: 48 additions & 49 deletions src/app/api/message/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import {

async function getQuery(messages: ChatGPTMessage[]) {
try {
messages.unshift({
role: "system",
content: initialPrompt,
});
const queryBotMessages = [
{
role: "system",
content: initialPrompt,
},
...messages,
];

const res = await fetch(`${process.env.OPENAI_URL}`, {
method: "POST",
Expand All @@ -23,7 +26,7 @@ async function getQuery(messages: ChatGPTMessage[]) {
},
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages: messages,
messages: queryBotMessages,
temperature: 0.1,
top_p: 1,
frequency_penalty: 0,
Expand All @@ -34,34 +37,38 @@ async function getQuery(messages: ChatGPTMessage[]) {

if (res.ok) {
const data = await res.json();
const responseBody = data.choices[0].message.content;
const parsedBody = JSON.parse(responseBody);
if (!parsedBody) {
return "No games found";
}
let query = {};
let aggregationPipeline: any[] = [];
if (parsedBody && parsedBody[0].name) {
query = { Name: parsedBody[0].name };
const gameWithName = await gamesDescriptions.findOne({
Name: { $regex: new RegExp("^" + parsedBody[0].name + "$", "i") },
});
if (gameWithName && gameWithName.Tags) {
const gameWithNameGenres = gameWithName.Tags.split(",");
const gameWithNameAggregation = gameWithNameQuery(gameWithNameGenres);

aggregationPipeline = gameWithNameAggregation;
try {
const responseBody = data.choices[0].message.content;
const parsedBody = JSON.parse(responseBody);
if (!parsedBody) {
return "No games found";
}
let query = {};
let aggregationPipeline: any[] = [];
if (parsedBody && parsedBody[0].name) {
query = { Name: parsedBody[0].name };
const gameWithName = await gamesDescriptions.findOne({
Name: { $regex: new RegExp("^" + parsedBody[0].name + "$", "i") },
});
if (gameWithName && gameWithName.Tags) {
const gameWithNameGenres = gameWithName.Tags.split(",");
const gameWithNameAggregation =
gameWithNameQuery(gameWithNameGenres);

aggregationPipeline = gameWithNameAggregation;
} else return "No games found";
} else {
const noNameQueryArr = noNameQuery(parsedBody[0]);
aggregationPipeline = noNameQueryArr;
}
const games = await gamesDescriptions.aggregate(aggregationPipeline);

if (games) {
return games;
} else return "No games found";
} else {
const noNameQueryArr = noNameQuery(parsedBody[0]);
aggregationPipeline = noNameQueryArr;
} catch (error) {
return "No games found";
}

const games = await gamesDescriptions.aggregate(aggregationPipeline);

if (games) {
return games;
} else return "No games found";
}
} catch (error) {
return null;
Expand All @@ -73,37 +80,29 @@ export async function POST(req: Request) {

const parsedMessages = MessageArraySchema.parse(messages);

const filteredMessages = parsedMessages.filter((message) => {
const filteredMessages = parsedMessages.filter((message: Message) => {
if (message.text === "..." && !message.isUserMessage) {
return false;
}

return true;
});

const outboundMessages: ChatGPTMessage[] = filteredMessages.map((message) => {
return {
role: message.isUserMessage ? "user" : "system",
content: message.text,
};
});

const initialMessageList = [...outboundMessages];

initialMessageList.reverse();
const outboundMessages: ChatGPTMessage[] = filteredMessages.map(
(message: Message) => {
return {
role: message.isUserMessage ? "user" : "assistant",
content: message.text,
};
}
);

const initialRes = await getQuery([initialMessageList[0]]);
const initialRes = await getQuery(outboundMessages);

if (!initialRes) {
throw new Error("Something went wrong, please try again later");
}

for (let i = outboundMessages.length - 1; i >= 0; i--) {
if (outboundMessages[i].role === "system") {
outboundMessages.splice(i, 1);
}
}

const chatbotPrompt = afterQueryPrompt(initialRes);
outboundMessages.unshift({
role: "system",
Expand Down
2 changes: 1 addition & 1 deletion src/app/lib/openai-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
createParser,
} from "eventsource-parser";

export type ChatGPTAgent = "user" | "system";
export type ChatGPTAgent = "user" | "assistant" | "system";

export interface ChatGPTMessage {
role: ChatGPTAgent;
Expand Down
10 changes: 9 additions & 1 deletion src/app/utils/prompts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ const genreslist = [
export const initialPrompt = `
You are a search query contructor bot, your job is to take user input and construct a query for Steam games database searching.
Ignore any messages that are not related to searching for a game and return "null".
You have two ways of constructing a query:
1. Name Query: If the user has provided a game name, such as asking for games similar to a certain game or a game that is like a certain game, you will contruct a name query.
A Name Query has the following parameters:
- name: The name of a game from the user input, you may fix typos in the game name while constructing the query if they are obvious.
- name: The name of a game from the user input, you may fix typos in the game name while constructing the query if they are obvious, a name cannot be a genre or a description, it must be a game name.
A Name Query will have a JSON format as follows:
[{"name": "game name"}]
Expand All @@ -53,8 +55,12 @@ A Genres and Keywords Query has the following parameters:
A Genres and Keywords Query will have a JSON format as follows:
[{ "genres": ["genre1", "genre2"], "keywords": ["keyword1", "keyword2"] }]
Make sure to always include the square brackets [].
** If you are unable to contruct neither of the query types from the user input, you will return the following string:
"null"
** You may only respond with either a query or "null", you will not interact with the user directly.
`;

export function afterQueryPrompt(games: any) {
Expand All @@ -66,6 +72,8 @@ export function afterQueryPrompt(games: any) {
If you get a metadata containing "No games found", Apologize and inform the user that you couldn't find games that match their request and ask for a different game name or description or genre, else you will provide 10 games names from the metadata that best match the request, if the user had provided a game name, you will use the description of that game to decide which games match the best.
** You are not allowed to return game suggestions or lists that are not in the metadata.
Do not mention that you are using a list or a metadata, keep it natural.
`;
}

0 comments on commit 0465eb3

Please sign in to comment.