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

WIP : implement inventory access in chatbot #552

Merged
merged 5 commits into from
Jul 9, 2024
Merged
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
16 changes: 14 additions & 2 deletions app/src/app/[lng]/[inventory]/cdp/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,21 @@ import {
} from "@chakra-ui/react";
import React from "react";
import { useTranslation } from "@/i18n/client";
import { api } from "@/services/api";

function Page({ params: { lng } }: { params: { lng: string } }) {
function Page({
params: { lng, inventory },
}: {
params: { lng: string; inventory: string };
}) {
const { t } = useTranslation(lng, "cdp");
const [connectToCDP] = api.useConnectToCDPMutation();
const handleConnectToCDP = async () => {
await connectToCDP({ inventoryId: inventory }).then((res) =>
console.log(res),
);
console.log(inventory);
};
return (
<Box>
<NavigationBar lng="" />
Expand All @@ -32,7 +44,7 @@ function Page({ params: { lng } }: { params: { lng: string } }) {
<Text>{t("submit-data-to-cdp")}</Text>
</CardBody>
<CardFooter p="0">
<Button className="w-[100%]" h="50px">
<Button onClick={handleConnectToCDP} className="w-[100%]" h="50px">
{t("submit-data-to-cdp")}
</Button>
</CardFooter>
Expand Down
2 changes: 1 addition & 1 deletion app/src/app/[lng]/[inventory]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ export default function Home({ params: { lng } }: { params: { lng: string } }) {
</Box>
</Box>
<Footer lng={lng} />
<ChatPopover />
<ChatPopover inventoryId={inventory?.inventoryId!} />
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { experimental_buildOpenAssistantPrompt } from "ai/prompts";
import OpenAI from "openai";
import { ChatCompletionMessageParam } from "openai/resources/chat/completions";
import { apiHandler } from "@/util/api";
import UserService from "@/backend/UserService";
import { db } from "@/models";

let Hf: HfInference, openai: OpenAI;

Expand Down Expand Up @@ -88,7 +90,40 @@ async function handleOpenAIChat(
return new StreamingTextResponse(stream);
}

export const POST = apiHandler(async (req: Request) => {
export const POST = apiHandler(async (req, { params, session }) => {

// FETCH INVENTORY DATA FROM DB

const inventory = await UserService.findUserInventory(
params.inventory,
session,
[
{
model: db.models.InventoryValue,
as: "inventoryValues",
include: [
{
model: db.models.GasValue,
as: "gasValues",
include: [
{ model: db.models.EmissionsFactor, as: "emissionsFactor" },
],
},
{
model: db.models.DataSource,
attributes: ["datasourceId", "sourceType"],
as: "dataSource",
},
],
},
],
);

// Use this log to view actual data in the console
console.log(inventory)

// TODO: Create prompt function based on inventory data returned from query above

const { messages } = chatRequest.parse(await req.json());
if (!messages[0].content.startsWith("You are a")) {
messages.unshift({
Expand Down
4 changes: 3 additions & 1 deletion app/src/components/ChatBot/chat-bot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ function useEnterSubmit(): {
export default function ChatBot({
inputRef,
t,
inventoryId,
}: {
userName?: string;
inputRef?: React.Ref<HTMLTextAreaElement>;
t: TFunction;
inventoryId: string;
}) {
const {
messages,
Expand All @@ -65,7 +67,7 @@ export default function ChatBot({
append,
reload,
} = useChat({
api: "/api/v0/chat",
api: `/api/v0/chat/${inventoryId}`,
initialMessages: [
{ id: "-1", content: t("initial-message"), role: "assistant" },
],
Expand Down
12 changes: 10 additions & 2 deletions app/src/components/ChatBot/chat-popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import { useTranslation } from "@/i18n/client";

export default function ChatPopover({
lng = "en",
inventoryId,
}: {
userName?: string;
lng?: string;
inventoryId: string;
}) {
const { isOpen, onOpen, onClose } = useDisclosure();
const inputRef = React.useRef(null);
Expand Down Expand Up @@ -64,10 +66,16 @@ export default function ChatPopover({
p={6}
>
{t("ask-ai-expert")}
<PopoverCloseButton color="content.secondary" w={8} h={8} mr={4} mt={6} />
<PopoverCloseButton
color="content.secondary"
w={8}
h={8}
mr={4}
mt={6}
/>
</PopoverHeader>
<PopoverBody maxH={650} w="full" p={6} borderRadius={4}>
<ChatBot inputRef={inputRef} t={t} />
<ChatBot inputRef={inputRef} t={t} inventoryId={inventoryId} />
</PopoverBody>
</PopoverContent>
</Popover>
Expand Down
9 changes: 9 additions & 0 deletions app/src/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,14 @@ export const api = createApi({
},
transformResponse: (response: { data: [] }) => response.data,
}),
connectToCDP: builder.mutation({
query: ({inventoryId}) => {
return{
method: 'POST',
url: `/inventory/${inventoryId}/cdp`
}
}
}),

// ActivityValue CRUD
getActivityValues: builder.query({
Expand Down Expand Up @@ -503,5 +511,6 @@ export const {
useInviteUserMutation,
useCheckUserMutation,
useMockDataQuery,
useConnectToCDPMutation
} = api;
export const { useGetOCCityQuery, useGetOCCityDataQuery } = openclimateAPI;
Loading