Skip to content

Commit

Permalink
Merged with branch feature/implement-inventory-access-in-chatbot
Browse files Browse the repository at this point in the history
  • Loading branch information
mircorudolph committed Jul 3, 2024
2 parents 30163ad + b1ebd8c commit 522e2f5
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 6 deletions.
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,9 +5,14 @@ import { experimental_buildOpenAssistantPrompt } from "ai/prompts";
import OpenAI from "openai";
import { ChatCompletionMessageParam } from "openai/resources/chat/completions";
import { apiHandler } from "@/util/api";
<<<<<<< HEAD:app/src/app/api/v0/chat/route.ts
import { City } from "@/models/City";
import { CityUser } from "@/models/CityUser";
import { Inventory } from "@/models/Inventory";
=======
import UserService from "@/backend/UserService";
import { db } from "@/models";
>>>>>>> feature/implement-inventory-access-in-chatbot:app/src/app/api/v0/chat/[inventory]/route.ts

let Hf: HfInference, openai: OpenAI;

Expand Down Expand Up @@ -100,6 +105,7 @@ async function handleOpenAIChat(
return new StreamingTextResponse(stream);
}

<<<<<<< HEAD:app/src/app/api/v0/chat/route.ts
/**
* Creates a system message with context included
*
Expand Down Expand Up @@ -144,6 +150,41 @@ export const POST = apiHandler(async (req: Request) => {

const prompt = await create_prompt_template("1234-5678")
console.log(prompt)
=======
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
>>>>>>> feature/implement-inventory-access-in-chatbot:app/src/app/api/v0/chat/[inventory]/route.ts

const { messages } = chatRequest.parse(await req.json());
if (!messages[0].content.startsWith("You are a")) {
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;

0 comments on commit 522e2f5

Please sign in to comment.