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

Data treaming #68

Merged
merged 17 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
44 changes: 44 additions & 0 deletions assets/js/Containers/FrevaGPT/components/AnswerComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from "react";
Karinon marked this conversation as resolved.
Show resolved Hide resolved
import PropTypes from "prop-types";

import { Col, Card, Spinner } from "react-bootstrap";

import Markdown from "react-markdown";

function AnswerComponent(props) {
function renderAnswer(props) {
switch (props.variant) {
case "Assistant":
return (
<Col md={{ span: 10, offset: 0 }}>
Karinon marked this conversation as resolved.
Show resolved Hide resolved
<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={{ offset: 0 }}>
<Card className="shadow-sm card-body border-0 border-bottom mb-3 bg-light">
<span>
<Spinner size="sm" />
Analyzing code...
Karinon marked this conversation as resolved.
Show resolved Hide resolved
</span>
</Card>
</Col>
);
default:
Karinon marked this conversation as resolved.
Show resolved Hide resolved
return null;
}
}

return <Col>{renderAnswer(props)}</Col>;
}

AnswerComponent.propTypes = {
content: PropTypes.string,
variant: PropTypes.string,
};

export default AnswerComponent;
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { isEmpty } from "lodash";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";

import CodeBlock from "./CodeBlock";
import { replaceLinebreaks } from "../utils";

import { replaceLinebreaks } from "./utils";
import CodeBlock from "./CodeBlock";

class ChatBlock extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -48,7 +48,7 @@ class ChatBlock extends React.Component {
renderUser(element) {
return (
<Col md={{ span: 10, offset: 2 }} key={element.content}>
<Card className="shadow-sm card-body border-0 border-bottom mb-3 bg-info">
<Card className="shadow-sm card-body border-0 border-bottom mb-3 bg-secondary">
{element.content}
</Card>
</Col>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import PropTypes from "prop-types";
import Highlight from "react-highlight";
import "highlight.js/styles/atom-one-light.css";

import { formatCode } from "./utils";
import { formatCode } from "../utils";

function CodeBlock(props) {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Card } from "react-bootstrap";

import { browserHistory } from "react-router";

import { botRequests } from "./exampleRequests";
import { botRequests } from "../exampleRequests";

function SidePanel() {
function changeToThread(thread) {
Expand Down
30 changes: 24 additions & 6 deletions assets/js/Containers/FrevaGPT/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import { isEmpty, has } from "lodash";

import Spinner from "../../Components/Spinner";

import ChatBlock from "./ChatBlock";
import SidePanel from "./SidePanel";
import ChatBlock from "./components/ChatBlock";
import SidePanel from "./components/SidePanel";
import AnswerComponent from "./components/AnswerComponent";

import { objectToQueryString, truncate } from "./utils";

Expand Down Expand Up @@ -54,6 +55,8 @@ class FrevaGPT extends React.Component {
hideBotModelList: true,
botOkay: undefined,
showSuggestions: true,
dynamicAnswer: "",
dynamicVariant: "",
};
}

Expand Down Expand Up @@ -171,7 +174,6 @@ class FrevaGPT extends React.Component {
while (true) {
// eslint-disable-next-line no-await-in-loop
const { done, value } = await reader.read();
console.log('##', decoder.decode(value));
if (done) break;

const decodedValues = decoder.decode(value);
Expand All @@ -195,16 +197,27 @@ class FrevaGPT extends React.Component {
// if object has not same variant, add answer to conversation and override object
if (varObj.variant !== jsonBuffer.variant) {
this.props.dispatch(addElement(varObj));
this.setState({ dynamicAnswer: "", dynamicVariant: "" });
varObj = jsonBuffer;
} else {
// if object has same variant, add content
// eslint-disable-next-line no-lonely-if
if (
varObj.variant === "Code" ||
varObj.variant === "CodeOutput"
)
) {
varObj.content[0] = varObj.content[0] + jsonBuffer.content[0];
else varObj.content = varObj.content + jsonBuffer.content;
this.setState({
dynamicAnswer: varObj.content[0],
dynamicVariant: varObj.variant,
});
} else {
varObj.content = varObj.content + jsonBuffer.content;
this.setState({
dynamicAnswer: varObj.content,
dynamicVariant: varObj.variant,
});
}
}
} else {
// object is empty so add content
Expand Down Expand Up @@ -319,7 +332,12 @@ class FrevaGPT extends React.Component {

<ChatBlock />

{this.state.loading ? (
<AnswerComponent
content={this.state.dynamicAnswer}
variant={this.state.dynamicVariant}
/>

{this.state.loading && !this.state.dynamicAnswer ? (
<Row className="mb-3">
<Col md={1}>
<Spinner />
Expand Down
35 changes: 31 additions & 4 deletions bot/proxyviews.py
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):
Karinon marked this conversation as resolved.
Show resolved Hide resolved
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
1 change: 1 addition & 0 deletions bot/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def reverse_proxy(request, path):
method="GET",
url=api_url,
params=all_parameters,
stream=True, # Enable streaming
timeout=100,
)
return response.content
Expand Down
Loading