Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build chatbot post api #139

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api/app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ def index
def create
user = @current_user
competition = Competition.find(params[:competition_id])
if params[:post_type] == "bot"
user = competition.users.where(user_type: "bot")[0]
else
user = @current_user
end

post = Post.create!(user_id: user.id, competition_id: competition.id, description: params[:description])
render json: post, status: :created
end
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/Main.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
}

.banner--fadeBottom {
height: 62vh;
height: 66.15vh;
background-image: linear-gradient(180deg,
transparent,
rgba(37, 37, 37, 0.61),
Expand Down
28 changes: 28 additions & 0 deletions client/src/components/NotificationPopup.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.popup {
position: relative;
display: inline-block;
cursor: pointer;
user-select: none;
}

.popup .notification__content {
visibility: hidden;
width: 15em;
background-color: #b2b2b2;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 8px 0;
position: absolute;
left: 32em;
bottom: -3em;
z-index: 1;
margin-left: -80px;
}

/* Toggle this class - hide and show the popup */
.popup .show {
visibility: visible;
-webkit-animation: fadeIn 1s;
animation: fadeIn 1s;
}
16 changes: 16 additions & 0 deletions client/src/components/NotificationPopup.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@


import "./NotificationPopup.css"

const NotificationPopup = () => {
return (
<div className="popup">
<div id="notification-popup" className="notification__content">
<p>Notifications</p>

</div>
</div>
);
};

export default NotificationPopup;
38 changes: 34 additions & 4 deletions client/src/pages/tournaments/Posts.jsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
import PostCard from "./PostCard";
import LoadingSpinner from "../auth/LoadingSpinner";
import CreateIcon from '@mui/icons-material/Create';
import InputOption from '../account/InputOption';
import ImageIcon from '@mui/icons-material/Image';
import SubscriptionsIcon from '@mui/icons-material/Subscriptions';
import EventNoteIcon from '@mui/icons-material/EventNote';
// import ImageIcon from '@mui/icons-material/Image';
// import SubscriptionsIcon from '@mui/icons-material/Subscriptions';
// import EventNoteIcon from '@mui/icons-material/EventNote';
import InsertCommentIcon from '@mui/icons-material/InsertComment';
import { useForm } from "react-hook-form";
import * as yup from "yup";
import { yupResolver } from "@hookform/resolvers/yup";
import { useDispatch, useSelector } from "react-redux";
import { setPosts } from "../../store/game/feedSlice";
import { selectCurrentUser } from "../../store/auth/userSlice";

import { useAddPostMutation } from "../../store/game/feedApiSlice";
import { useGetPostsQuery } from "../../store/game/feedApiSlice";
import { usePostChatBotCommentMutation } from "../../store/openai/chatbotApiSlice";

function Posts({ posts, comp }) {
const { refetch } = useGetPostsQuery(comp.id);
const renderedPosts = posts?.map((post) => {
return <PostCard key={post.id} post={post} />;
});

const workoutContextArray = posts?.filter(post => {
return post.description.includes("just posted a workout");
}).map(post => {
return {
"role": "user",
"content": `On ${post.created_at.slice(0, 10)}, ${post.description.replace("just ", "")}`
}
});

const dispatch = useDispatch();
const user = useSelector(selectCurrentUser);
const [addPost, { isLoading }] = useAddPostMutation();
const [getChatBotComment, { isLoading: isChatBotLoading }] = usePostChatBotCommentMutation();

const schema = yup.object().shape({
description: yup.string().required(),
Expand All @@ -42,11 +53,25 @@ function Posts({ posts, comp }) {
const newPost = await addPost({
description: data.description,
competition_id: comp.id,
post_type: data.post_type || null,
}).unwrap();
dispatch(setPosts([...posts, newPost]));
setValue("description", "");
};

const handleChatBotClick = async (e) => {
try {
let request = await getChatBotComment(workoutContextArray);
const data = {
description: request.data.gpt_response,
post_type: "bot"
};
onSubmit(data);
} catch (error) {
console.error(error);
};
};

return (
<div className="posts">
<div className="feed__inputContainer">
Expand All @@ -72,7 +97,12 @@ function Posts({ posts, comp }) {
/>
</button>
</form>
<button
onClick={handleChatBotClick}
>ChatBot
</button>
</div>
{isChatBotLoading && <LoadingSpinner />}
{/* <div className="feed__inputOptions">
<InputOption Icon={ImageIcon} title="Photo" color="#70B5F9" />
<InputOption Icon={SubscriptionsIcon} title="Video" color="#E7A33E" />
Expand Down
15 changes: 15 additions & 0 deletions client/src/store/openai/chatbotApiSlice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { apiSlice } from "../../app/api/apiSlice";

export const chatbotApiSlice = apiSlice.injectEndpoints({
endpoints: (builder) => ({
postChatBotComment: builder.mutation({
query: (workoutHistory) => ({
url: "http://localhost:8000/fitbot/botcomment/",
method: "POST",
body: { "prompts": workoutHistory }
}),
}),
}),
});

export const { usePostChatBotCommentMutation } = chatbotApiSlice;