-
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 pull request #39 from agrilens/feature-21
Feature 21
- Loading branch information
Showing
14 changed files
with
2,201 additions
and
160 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,154 @@ | ||
#chat { | ||
max-width: 1920px; | ||
background-color: #f6f6f6; | ||
} | ||
#chat .row { | ||
position: relative; | ||
height: 660px; | ||
max-width: 1200px; | ||
} | ||
.cs-main-container { | ||
border: none; | ||
border-radius: 22px; | ||
box-shadow: 1px 1px 3px; | ||
background: radial-gradient( | ||
circle at bottom center, | ||
rgba(0, 0, 0, 0.3), | ||
rgba(0, 0, 0, 0.1) 40%, | ||
rgba(255, 255, 255, 1) 80% | ||
); | ||
} | ||
|
||
.agriLesn-intro { | ||
color: #204e51; | ||
background-color: transparent; | ||
} | ||
.agriLesn-intro-logo { | ||
margin: 0 auto; | ||
transform: scale(1.2); | ||
} | ||
|
||
.cs-message-input { | ||
width: 90% !important; | ||
margin: 0 auto; | ||
} | ||
.cs-message-input__tools .cs-button--attachment { | ||
/* display: none; */ | ||
} | ||
.cs-message__content-wrapper { | ||
padding: 0 6vw; | ||
} | ||
|
||
.custom-message-content-wrapper .cs-message__content-wrapper { | ||
background-color: transparent; | ||
margin: 0 auto !important; | ||
} | ||
.custom-message-content-wrapper .cs-message__content { | ||
background-color: transparent !important; | ||
box-shadow: none; | ||
} | ||
|
||
.cs-message__custom-content { | ||
background-color: transparent; | ||
display: flex !important; | ||
flex-direction: column; | ||
} | ||
|
||
.cs-message__content { | ||
background-color: white !important; | ||
border-radius: 8px !important; | ||
box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2); | ||
margin-bottom: 10px; | ||
font-size: 18px; | ||
} | ||
.cs-message-input__content-editor-wrapper { | ||
border: 1px solid #aaaaaa; | ||
border-radius: 8px; | ||
background-color: #fff !important; | ||
font-size: 18px; | ||
} | ||
.cs-message-input__content-editor { | ||
background-color: #fff !important; | ||
} | ||
.cs-message--incoming .cs-message__content { | ||
color: rgba(0, 0, 0, 0.87); | ||
background-color: #fff; | ||
border-radius: 8px !important; | ||
} | ||
.cs-message-list .cs-typing-indicator { | ||
background-color: transparent; | ||
} | ||
.cs-typing-indicator { | ||
margin-left: 5vw; | ||
margin-bottom: 0.5rem; | ||
} | ||
.cs-typing-indicator__text { | ||
color: #204e51 !important; | ||
font-weight: bold; | ||
} | ||
.cs-typing-indicator__dot { | ||
background-color: #204e51; | ||
height: 5px; | ||
width: 5px; | ||
} | ||
.ps__thumb-y { | ||
background-color: #204e51; | ||
} | ||
.svg-inline--fa { | ||
background-color: transparent; | ||
color: #2d6e72; | ||
display: inline-block; | ||
font-size: inherit; | ||
height: 1.6em; | ||
overflow: visible; | ||
vertical-align: -0.125em; | ||
} | ||
.cs-chat-container { | ||
background-color: transparent; | ||
} | ||
.cs-chat-container .cs-message-list { | ||
background-color: transparent; | ||
} | ||
|
||
.cs-chat-container .cs-message-input { | ||
border: none; | ||
background-color: transparent; | ||
} | ||
.cs-message-input__content-editor { | ||
/* min-height: 56px !important; */ | ||
} | ||
|
||
.cs-message--outgoing .cs-message__sender-name, | ||
.cs-message--incoming .cs-message__sender-name { | ||
display: block; | ||
color: grey; | ||
} | ||
.cs-message--outgoing .cs-message__sent-time, | ||
.cs-message--incoming .cs-message__sent-time { | ||
display: none; | ||
/* display: block; */ | ||
} | ||
.chat-message-content p { | ||
margin: 0 !important; | ||
padding: 0 !important; | ||
gap: 0 !important; | ||
} | ||
|
||
.chat-message-content li { | ||
list-style: none; | ||
/* line-height: 1.4; */ | ||
padding-bottom: 4px; | ||
padding-left: 0.5vw !important; | ||
} | ||
.chat-message-content ul { | ||
padding-left: 0.5vw !important; | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
|
||
.chat-message-content ol { | ||
padding: 0 !important; | ||
margin: 0 !important; | ||
display: flex; | ||
flex-direction: column; | ||
} |
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,157 @@ | ||
import React, { useState, useRef } from "react"; | ||
|
||
import { | ||
MainContainer, | ||
ChatContainer, | ||
MessageList, | ||
Message, | ||
MessageInput, | ||
TypingIndicator, | ||
Avatar, | ||
} from "@chatscope/chat-ui-kit-react"; | ||
import "@chatscope/chat-ui-kit-styles/dist/default/styles.min.css"; | ||
import ReactMarkdown from "react-markdown"; | ||
import remarkGfm from "remark-gfm"; | ||
import rehypeRaw from "remark-gfm"; | ||
|
||
import Row from "react-bootstrap/Row"; | ||
|
||
import "./Chat.css"; | ||
|
||
import { | ||
useAccountContext, | ||
useAccountUpdateContext, | ||
} from "../contexts/AccountContext"; | ||
|
||
import AgriLensNewLogo from "../assets/images/AgriLensNewLogo.png"; | ||
|
||
export const Chat = () => { | ||
const [messages, setMessages] = useState([]); | ||
const [isTyping, setIsTyping] = useState(false); | ||
|
||
const { userLastScanSummary, userSelectedModel, chatBotRef } = | ||
useAccountContext(); | ||
|
||
const handleSend = async (message) => { | ||
const newMessage = { | ||
message, | ||
direction: "outgoing", | ||
sender: "user", | ||
}; | ||
|
||
const newMessages = [...messages, newMessage]; | ||
setMessages(newMessages); | ||
setIsTyping(true); | ||
await processMessageToLLM(newMessages); | ||
}; | ||
|
||
async function processMessageToLLM(chatMessages) { | ||
let apiMessages = chatMessages.map((messageObject) => { | ||
let role = messageObject.sender === "AgriChat" ? "assistant" : "user"; | ||
return { role: role, content: messageObject.message }; | ||
}); | ||
|
||
const lastFollowUpMessage = | ||
apiMessages[apiMessages.length - 1]?.content || ""; | ||
|
||
const apiRequestBody = { | ||
initialAnalysis: userLastScanSummary, | ||
model: userSelectedModel, | ||
message: lastFollowUpMessage, // The message from our client chat input. | ||
conversationId: "121212", | ||
}; | ||
|
||
await fetch( | ||
"http://127.0.0.1:5001/agrilens-web/us-central1/app/chat/follow-up", | ||
{ | ||
method: "POST", | ||
headers: { | ||
// Authorization: "Bearer " + process.env.REACT_APP_OPENAI_API_KEY, | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify(apiRequestBody), | ||
} | ||
) | ||
.then((data) => { | ||
return data.json(); | ||
}) | ||
.then((data) => { | ||
// console.log(">>>> 5. data: ", data); | ||
setMessages([ | ||
...chatMessages, | ||
{ | ||
// message: data.choices[0].message.content, | ||
message: data.response, | ||
sender: "AgriGPT", | ||
direction: "incoming", | ||
}, | ||
]); | ||
setIsTyping(false); | ||
}); | ||
} | ||
|
||
return ( | ||
<section id="chat" className="pb-4" ref={chatBotRef}> | ||
<Row className="m-auto px-3 py-5"> | ||
<MainContainer className="pb-4 pt-4"> | ||
<ChatContainer> | ||
<MessageList | ||
typingIndicator={ | ||
isTyping ? ( | ||
<TypingIndicator content="AgriGPT is typing" /> | ||
) : null | ||
} | ||
> | ||
<Message | ||
model={{ | ||
direction: "incoming", | ||
type: "custom", | ||
}} | ||
className="custom-message-content-wrapper w-100 ms-auto" | ||
> | ||
<Message.CustomContent className="agriLesn-intro"> | ||
<div className="agriLesn-intro-logo pb-1"> | ||
<Avatar src={AgriLensNewLogo} name="AgriLenslogo" /> | ||
</div> | ||
<div className="agriLesn-intro-text fw-bold fs-6"> | ||
Hello, I'm AgriGPT! Ask me About your Results or Plants. | ||
</div> | ||
</Message.CustomContent> | ||
</Message> | ||
{messages.map((message, i) => { | ||
// console.log(i, message); | ||
return ( | ||
<Message key={i} model={message}> | ||
<Message.Header | ||
sender={message.sender === "user" ? "ME" : "AGRILENS"} | ||
sentTime="just now" | ||
className="chat-message-header bolder" | ||
/> | ||
<Message.CustomContent className="chat-message-content"> | ||
<ReactMarkdown | ||
remarkPlugins={[remarkGfm]} | ||
rehypePlugins={[rehypeRaw]} | ||
> | ||
{message.message.trim()} | ||
</ReactMarkdown> | ||
</Message.CustomContent> | ||
</Message> | ||
); | ||
})} | ||
</MessageList> | ||
<MessageInput | ||
className="chat-message-input" | ||
placeholder="Ask me anything about your results" | ||
onSend={handleSend} | ||
/> | ||
</ChatContainer> | ||
</MainContainer> | ||
<ReactMarkdown remarkPlugins={[remarkGfm]}> | ||
{messages[messages.length - 1]?.content} | ||
</ReactMarkdown> | ||
</Row> | ||
</section> | ||
); | ||
}; | ||
|
||
export default Chat; |
Oops, something went wrong.