mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-04 05:10:19 +08:00
Improve answer text
This commit is contained in:
15
ai/ai.go
15
ai/ai.go
@ -45,7 +45,6 @@ func queryAnswer(authToken string, question string, timeout int) (string, error)
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//fmt.Printf("%s\n", err.Error())
|
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,6 +103,7 @@ func QueryAnswerStream(authToken string, question string, writer io.Writer, buil
|
|||||||
}
|
}
|
||||||
defer respStream.Close()
|
defer respStream.Close()
|
||||||
|
|
||||||
|
isLeadingReturn := true
|
||||||
for {
|
for {
|
||||||
completion, streamErr := respStream.Recv()
|
completion, streamErr := respStream.Recv()
|
||||||
if streamErr != nil {
|
if streamErr != nil {
|
||||||
@ -113,13 +113,22 @@ func QueryAnswerStream(authToken string, question string, writer io.Writer, buil
|
|||||||
return streamErr
|
return streamErr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data := completion.Choices[0].Text
|
||||||
|
if isLeadingReturn && len(data) != 0 {
|
||||||
|
if strings.Count(data, "\n") == len(data) {
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
isLeadingReturn = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Write the streamed data as Server-Sent Events
|
// Write the streamed data as Server-Sent Events
|
||||||
if _, err := fmt.Fprintf(writer, "data: %s\n\n", completion.Choices[0].Text); err != nil {
|
if _, err = fmt.Fprintf(writer, "data: %s\n\n", data); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Append the response to the strings.Builder
|
// Append the response to the strings.Builder
|
||||||
builder.WriteString(completion.Choices[0].Text)
|
builder.WriteString(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -12,6 +12,9 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
//go:build !skipCi
|
||||||
|
// +build !skipCi
|
||||||
|
|
||||||
package ai
|
package ai
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -74,6 +74,16 @@ class ChatBox extends React.Component {
|
|||||||
this.setState({inputValue: ""});
|
this.setState({inputValue: ""});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
renderText(text) {
|
||||||
|
const lines = text.split("\n").map((line, index) => (
|
||||||
|
<React.Fragment key={index}>
|
||||||
|
{line}
|
||||||
|
<br />
|
||||||
|
</React.Fragment>
|
||||||
|
));
|
||||||
|
return <div>{lines}</div>;
|
||||||
|
}
|
||||||
|
|
||||||
renderList() {
|
renderList() {
|
||||||
if (this.props.messages === undefined || this.props.messages === null) {
|
if (this.props.messages === undefined || this.props.messages === null) {
|
||||||
return (
|
return (
|
||||||
@ -110,7 +120,7 @@ class ChatBox extends React.Component {
|
|||||||
title={
|
title={
|
||||||
<div style={{fontSize: "16px", fontWeight: "normal", lineHeight: "24px", marginTop: "-15px", marginLeft: "5px", marginRight: "80px"}}>
|
<div style={{fontSize: "16px", fontWeight: "normal", lineHeight: "24px", marginTop: "-15px", marginLeft: "5px", marginRight: "80px"}}>
|
||||||
{
|
{
|
||||||
!item.text.includes("#ERROR#") ? item.text : (
|
!item.text.includes("#ERROR#") ? this.renderText(item.text) : (
|
||||||
<Alert message={item.text.slice("#ERROR#: ".length)} type="error" showIcon />
|
<Alert message={item.text.slice("#ERROR#: ".length)} type="error" showIcon />
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -89,6 +89,10 @@ class ChatPage extends BaseListPage {
|
|||||||
if (lastMessage.author === "AI" && lastMessage.replyTo !== "" && lastMessage.text === "") {
|
if (lastMessage.author === "AI" && lastMessage.replyTo !== "" && lastMessage.text === "") {
|
||||||
let text = "";
|
let text = "";
|
||||||
MessageBackend.getMessageAnswer(lastMessage.owner, lastMessage.name, (data) => {
|
MessageBackend.getMessageAnswer(lastMessage.owner, lastMessage.name, (data) => {
|
||||||
|
if (data === "") {
|
||||||
|
data = "\n";
|
||||||
|
}
|
||||||
|
|
||||||
const lastMessage2 = Setting.deepCopy(lastMessage);
|
const lastMessage2 = Setting.deepCopy(lastMessage);
|
||||||
text += data;
|
text += data;
|
||||||
lastMessage2.text = text;
|
lastMessage2.text = text;
|
||||||
|
Reference in New Issue
Block a user