-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'frevagpt' into auto-scroll
- Loading branch information
Showing
8 changed files
with
169 additions
and
22 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
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
88 changes: 88 additions & 0 deletions
88
assets/js/Containers/FrevaGPT/components/PendingAnswerComponent.js
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,88 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import PropTypes from "prop-types"; | ||
|
||
import { Col, Card, Spinner, Accordion, Row } from "react-bootstrap"; | ||
|
||
import Markdown from "react-markdown"; | ||
|
||
import Highlight from "react-highlight"; | ||
import "highlight.js/styles/atom-one-light.css"; | ||
|
||
import * as constants from "../constants"; | ||
|
||
function PendingAnswerComponent(props) { | ||
const [renderedCode, setRenderedCode] = useState(""); | ||
|
||
useEffect(() => { | ||
const parsedCode = renderCode(props.content); | ||
if (parsedCode !== "") setRenderedCode(parsedCode); | ||
}, [props.content]); | ||
|
||
function renderCode(rawCode) { | ||
let jsonCode = ""; | ||
let codeSnippets = ""; | ||
|
||
if (!rawCode.endsWith('"}')) jsonCode = rawCode + '"}'; | ||
else jsonCode = rawCode; | ||
|
||
try { | ||
const code = JSON.parse(jsonCode); | ||
codeSnippets = code.code; | ||
} catch (err) { | ||
// console.error(err); | ||
} | ||
return codeSnippets; | ||
} | ||
|
||
function renderAnswer(props) { | ||
switch (props.variant) { | ||
case "Assistant": | ||
return ( | ||
<Col md={constants.BOT_COLUMN_STYLE}> | ||
<Card className="shadow-sm card-body border-0 border-bottom mb-3 bg-light"> | ||
<Markdown>{props.content}</Markdown> | ||
</Card> | ||
</Col> | ||
); | ||
case "Code": | ||
case "CodeBlock": | ||
return ( | ||
<Col md={constants.BOT_COLUMN_STYLE}> | ||
<div className="mb-3"> | ||
<Accordion defaultActiveKey="0"> | ||
<Accordion.Item eventKey="0"> | ||
<Accordion.Header>python</Accordion.Header> | ||
<Accordion.Body> | ||
<Highlight className="python">{renderedCode}</Highlight> | ||
<span> | ||
<Spinner size="sm" /> | ||
<span className="m-2">Analyzing...</span> | ||
</span> | ||
</Accordion.Body> | ||
</Accordion.Item> | ||
</Accordion> | ||
</div> | ||
</Col> | ||
); | ||
case "ServerHint": | ||
return ( | ||
<Row className="mb-3"> | ||
<Col md={1}> | ||
<Spinner /> | ||
</Col> | ||
</Row> | ||
); | ||
default: | ||
return null; | ||
} | ||
} | ||
|
||
return renderAnswer(props); | ||
} | ||
|
||
PendingAnswerComponent.propTypes = { | ||
content: PropTypes.string, | ||
variant: PropTypes.string, | ||
}; | ||
|
||
export default PendingAnswerComponent; |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export const ADD_ELEMENT = "ADD_ELEMENT"; | ||
export const SET_CONVERSATION = "SET_CONVERSATION"; | ||
export const SET_THREAD = "SET_THREAD"; | ||
export const BOT_COLUMN_STYLE = { span: 10, offset: 0 }; |
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 |
---|---|---|
@@ -1,11 +1,38 @@ | ||
from urllib.parse import urljoin | ||
|
||
import requests | ||
from django.conf import settings | ||
from django.http import StreamingHttpResponse | ||
from django.views import View | ||
from djproxy.views import HttpProxy | ||
|
||
|
||
class ChatBotProxy(HttpProxy): | ||
"""A reverse proxy to forward requests to the databrowserAPI.""" | ||
class ChatBotProxy(View): | ||
def get(self, request, *args, **kwargs): | ||
path = request.path | ||
base_url = urljoin(settings.CHAT_BOT_URL, path) | ||
params = request.GET.dict() | ||
|
||
base_url = urljoin(settings.CHAT_BOT_URL, "/api/chatbot/") | ||
reverse_urls = [("/api/chatbot/", settings.CHAT_BOT_URL)] | ||
try: | ||
upstream_response = requests.get(base_url[:-1], params=params, stream=True) | ||
upstream_response.raise_for_status() | ||
except requests.RequestException as e: | ||
return StreamingHttpResponse( | ||
f"Error while connecting to the external API: {str(e)}", | ||
status=502, | ||
) | ||
|
||
# Stream the content of the external API to the Django response | ||
def stream(): | ||
for chunk in upstream_response.iter_content(chunk_size=8192): | ||
if chunk: | ||
yield chunk | ||
|
||
response = StreamingHttpResponse( | ||
stream(), content_type=upstream_response.headers.get("Content-Type") | ||
) | ||
response["Content-Disposition"] = upstream_response.headers.get( | ||
"Content-Disposition", "inline" | ||
) | ||
|
||
return response |
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