-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from kartAI/feat/14-instance-of-planprat
Feat/14 instance of planprat
- Loading branch information
Showing
13 changed files
with
214 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,34 @@ | ||
# NTNU KPRO AI Assistant | ||
|
||
|
||
## Prerequisites | ||
|
||
Before you start, make sure the following tools are installed on your system: | ||
|
||
- **Git:** Version control system to clone the project repository [Download Git](https://git-scm.com/downloads) | ||
- **Docker:** To containerize the application and ensure it runs consistently across different environments [Download Docker](https://www.docker.com/products/docker-desktop) | ||
|
||
## Setup | ||
|
||
Start by going into the `/webapp` folder, making a copy of the `.env.example` file and renaming it to `.env`. This file contains the environment variables that the application needs to run. Open the `.env` file and update the environment variables according to your local or production setup. | ||
|
||
## Usage | ||
|
||
To run the project, you can use the following commands: | ||
|
||
```bash | ||
docker compose up --build -d | ||
docker compose --env-file ./webapp/.env up --build -d | ||
``` | ||
This command will build the Docker images (if necessary) and run the containers in the background. You can access the clientside code at [http://localhost:3000](http://localhost:3000) and the API at [http://localhost:8000](http://localhost:8000). | ||
|
||
This command will build the Docker images (if necessary) and run the containers in the background. You can access the clientside code at [http://localhost:3000](http://localhost:3000) and the API at [http://localhost:8000](http://localhost:8000). | ||
The Swagger documentation for the API is available at [http://localhost:8000/docs](http://localhost:8000/docs). | ||
|
||
To stop the containers, you can use the following command: | ||
|
||
```bash | ||
docker compose down | ||
``` | ||
|
||
## Documentation | ||
* [Developer Setup](/docs/manuals/developer_setup.md) | ||
* [T3 Start Guide](/docs/manuals/t3_guide.md) | ||
|
||
- [Developer Setup](/docs/manuals/developer_setup.md) | ||
- [T3 Start Guide](/docs/manuals/t3_guide.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+48.9 KB
...ad-dashbord e2e Tests -- should render content -- before each hook (failed).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { PlanPrat } from "~/components/PlanPrat"; | ||
|
||
export default function PlanpratPage() { | ||
return( | ||
<div className="lg:w-2/4 mx-auto"> | ||
<PlanPrat/> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ export default async function Home() { | |
className="rounded-md xl:ml-20" | ||
/> | ||
</figure> | ||
|
||
</main> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { PlanPrat } from "~/components/PlanPrat"; | ||
|
||
export default function PlanpratPage() { | ||
return( | ||
<div className="lg:w-2/4 mx-auto"> | ||
<PlanPrat/> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
"use client"; | ||
import { type ChangeEvent, useState, type KeyboardEvent } from "react"; | ||
import Image from "next/image"; | ||
import { api } from "~/trpc/react"; | ||
|
||
export function PlanPrat() { | ||
const [error, setError] = useState(""); | ||
const [text, setText] = useState<string>(""); | ||
const [chatItems, setChatItems] = useState< | ||
{ text: string; isUser: boolean }[] | ||
>([]); | ||
const utils = api.useUtils(); | ||
|
||
async function queryPlanprat(queryInput: string) { | ||
try { | ||
const response = await utils.planprat.fetchResponse.fetch({ | ||
query: queryInput, | ||
}); | ||
|
||
return response; | ||
} catch (error) { | ||
console.error(error); | ||
setError("Error: Failed to retrieve response."); | ||
} | ||
} | ||
|
||
const handleTextChange = (e: ChangeEvent<HTMLTextAreaElement>): void => { | ||
// Update state with textarea input | ||
setText(e.target.value); | ||
}; | ||
|
||
const handleSubmit = async (): Promise<void> => { | ||
if (text.trim()) { | ||
setChatItems((prevChatItems) => [ | ||
{ text: text, isUser: true }, | ||
...prevChatItems, | ||
]); //question | ||
const sendText = text; | ||
setText(""); | ||
const response = await queryPlanprat(sendText); | ||
if (!response) return; | ||
setChatItems((prevChatItems) => [ | ||
{ text: response.answer, isUser: false }, | ||
...prevChatItems, | ||
]); | ||
} | ||
}; | ||
|
||
const handleKeyDown = async ( | ||
e: KeyboardEvent<HTMLTextAreaElement>, | ||
): Promise<void> => { | ||
if (e.key === "Enter") { | ||
e.preventDefault(); | ||
await handleSubmit(); | ||
} | ||
}; | ||
return ( | ||
<div className="bg-white p-10"> | ||
<section className="rounded-lg shadow-lg"> | ||
<h1 className="w-full rounded-lg bg-kartAI-blue pb-6 pt-1 text-center text-white"> | ||
PlanPrat | ||
</h1> | ||
<div id="planprat-input-output" className="relative w-full p-2"> | ||
<ul | ||
id="planprat-output " | ||
className="flex h-96 w-full flex-col-reverse overflow-y-auto rounded-lg p-2 shadow-inner" | ||
> | ||
{chatItems.map((chatItem, index) => ( | ||
<li | ||
data-cy="chat-output" | ||
className={ | ||
chatItem.isUser | ||
? "m-2 ml-6 self-end rounded-lg border-2 p-2 text-black shadow-lg" | ||
: "m-2 mr-6 self-start rounded-lg bg-kartAI-blue p-2 text-white shadow-lg" | ||
} | ||
key={index} | ||
> | ||
{chatItem.text} | ||
</li> | ||
))} | ||
</ul> | ||
<textarea | ||
id="planprat-input" | ||
className="mt-2 w-full rounded-lg p-2 pr-12 text-black shadow-inner" | ||
placeholder="Still meg et spørsmål ..." | ||
value={text} | ||
onChange={handleTextChange} | ||
onKeyDown={handleKeyDown} | ||
></textarea> | ||
<button | ||
type="submit" | ||
id="planprat-input-button" | ||
className="absolute bottom-8 right-4 rounded" | ||
onClick={handleSubmit} | ||
> | ||
<Image | ||
src="/Ikoner/Dark/SVG/Comment.svg" | ||
alt="send" | ||
height="30" | ||
width="30" | ||
className="rounded bg-kartAI-blue p-1 text-white" | ||
></Image> | ||
</button> | ||
</div> | ||
<p className="py-4 text-center text-red-500">{error}</p> | ||
</section> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { z } from "zod"; | ||
import { createTRPCRouter, publicProcedure } from "../trpc"; | ||
import axios, { AxiosError, type AxiosResponse } from "axios"; | ||
|
||
interface PlanpratResponse { | ||
answer: string; | ||
} | ||
|
||
const PLANPRAT_URL = process.env.PLANPRAT_URL ?? ""; | ||
|
||
export const planpratRouter = createTRPCRouter({ | ||
fetchResponse: publicProcedure | ||
.input( | ||
z.object({ | ||
query: z.string(), | ||
}), | ||
) | ||
.query(async ({ input }) => { | ||
const { query } = input; | ||
|
||
try { | ||
const response: AxiosResponse<PlanpratResponse> = await axios.post( | ||
PLANPRAT_URL, | ||
{ | ||
query: query, | ||
}, | ||
); | ||
|
||
return response.data as unknown as PlanpratResponse; | ||
} catch (error) { | ||
if (error instanceof AxiosError) { | ||
throw new Error(`Failed to retrieve response: ${error.message}`); | ||
} else { | ||
console.error(error); | ||
throw new Error(`Unkown error`); | ||
} | ||
} | ||
}), | ||
}); |