App.vue 3.5 KB
Newer Older
6
UPDATE  
64104061f23fda247c679fa8 已提交
1
<template>
6
update  
64104061f23fda247c679fa8 已提交
2 3 4 5 6 7 8 9
  <div class="container ivu-p">
    <div class="dialog">
      <template v-for="(item, index) in dialogs" :key="index">
        <div class="dialog-item" :class="{ 'dialog-item-me': item.role === 'me', 'dialog-item-ai': item.role === 'ai' }">
          <div class="dialog-item-main">{{ item.text }}</div>
        </div>
      </template>
    </div>
10
    <div class="question ivu-mt">
11
      <Input v-model="question" type="textarea" :autosize="{ minRows: 4, maxRows: 6 }" placeholder="输入你的问题" />
6
update  
64104061f23fda247c679fa8 已提交
12 13 14 15 16 17 18 19
      <Row class="ivu-mt">
        <Col>
          <Button type="primary" size="large" icon="md-send" :loading="loading" @click="handleSend">发送</Button>
        </Col>
        <Col>
          <Button size="large" class="ivu-ml" icon="md-add" :disabled="loading" @click="handleNewChat">新对话</Button>
        </Col>
      </Row>
6
update  
64104061f23fda247c679fa8 已提交
20
    </div>
6
622eda98dfef6c4fdb84ccca 已提交
21
  </div>
6
UPDATE  
64104061f23fda247c679fa8 已提交
22
</template>
6
update  
64104061f23fda247c679fa8 已提交
23
<script>
24
import { EventSourcePolyfill } from 'event-source-polyfill';
6
update  
64104061f23fda247c679fa8 已提交
25

26 27
const token = 'Bearer ZXlKMGVYQWlPaUpLVjFRaUxDSmhiR2NpT2lKSVV6STFOaUo5LmV5SjBkQ0k2TkN3aVlYVmtJam9pWXpkbU16WTVOMlpqTldaak5EVmpNR0l5WldFNE5UVTNaRFkxTnpnM1lXVWlMQ0pzZFNJNklrbHVjME52WkdVaUxDSmxlSEFpT2pFMk9EVTFORGczT1Rrc0luVnlJam95TENKcWRHa2lPaUpCVUVsZlZFOUxSVTVmWXpkbU16WTVOMlpqTldaak5EVmpNR0l5WldFNE5UVTNaRFkxTnpnM1lXVXROQ0o5LmlyLTJYa1A4dFhNaFVldnlzTFhkUlJsY1VBV0ZiaWE5em9ZdGN6VlpleFk=';
const api = 'https://api.ai100.ai/ai/api/ai/chat';
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
export default {
  data() {
    return {
      question: '',
      loading: false,
      dialogs: []
    }
  },
  methods: {
    handleSend() {
      if (this.loading || this.question === '') return;
      this.loading = true;

      const question = this.question;
      this.question = '';

      this.dialogs.push({
        id: this.dialogs.length + 1,
        role: 'me',
        text: question
      });

      const aiDialogID = this.dialogs.length + 1;
52

53 54 55 56 57
      this.dialogs.push({
        id: aiDialogID,
        role: 'ai',
        text: 'AI 思考中...'
      });
58

59 60 61 62 63
      const query = {
        prompt: '',
        question,
        stream: true
      }
6
update  
64104061f23fda247c679fa8 已提交
64

65 66 67 68 69 70
      const source = new EventSourcePolyfill(
        `${api}?question=${query.question}&prompt=${query.prompt}&stream=${query.stream}`,
        {
          headers: {
            Accept: '*/*',
            Authorization: token
6
update  
64104061f23fda247c679fa8 已提交
71
          }
72 73 74 75 76 77 78 79 80 81 82
        }
      )

      source.onopen = event => {
        console.log("onopen", event);
        const dialog = this.dialogs.find(item => item.id === aiDialogID);
        dialog.text = '';
      };
      source.onmessage = event => {
        if (event.data === "[DONE]") {
          source.close();
6
update  
64104061f23fda247c679fa8 已提交
83
          this.loading = false;
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
        }
        if (event.data) {
          const data = JSON.parse(event.data);
          const text = data.message.content.parts.join("");
          console.log(text);
          const dialog = this.dialogs.find(item => item.id === aiDialogID);
          dialog.text += text;
        }
      };
      source.onerror = event => {
        console.log("error", event);
      };
    },
    handleNewChat() {
      this.dialogs = [];
6
update  
64104061f23fda247c679fa8 已提交
99 100
    }
  }
101
}
6
622eda98dfef6c4fdb84ccca 已提交
102
</script>
6
update  
64104061f23fda247c679fa8 已提交
103
<style>
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
.container {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.dialog {
  flex: 1;
  overflow: auto;
}

.dialog-item {
  display: flex;
}

.dialog-item-main {
  max-width: 80%;
  padding: 8px;
  word-wrap: break-word;
  margin-top: 16px;
  border-radius: 4px;
}

.dialog-item-me {
  justify-content: flex-end;
}

.dialog-item-me .dialog-item-main {
  background-color: antiquewhite;
}

.dialog-item-ai .dialog-item-main {
  background-color: #eee;
}
6
update  
64104061f23fda247c679fa8 已提交
138
</style>