1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
| <template>
| <div>
| <h2>DeepSeek 聊天</h2>
| <textarea v-model="inputMessage" placeholder="请输入消息"></textarea>
| <button @click="sendMessage">发送</button>
| <p>回复: {{ reply }}</p>
| <p v-if="error" style="color: red">{{ error }}</p>
| </div>
| </template>
|
| <script>
| export default {
| name: 'Chat',
| data() {
| return {
| inputMessage: '',
| reply: '',
| error: ''
| };
| },
| methods: {
| async sendMessage() {
| if (!this.inputMessage.trim()) {
| this.error = '消息不能为空';
| return;
| }
| this.error = '';
| this.reply = '';
|
| try {
| const response = await fetch('http://localhost:11434/api/chat', {
| method: 'POST',
| headers: {
| 'Content-Type': 'application/json'
| },
| body: JSON.stringify({
| model: 'ltkj-jy-ai',
| messages: [
| {
| role: 'system',
| content: this.inputMessage
| }
| ],
| stream: false
| })
| });
|
| if (!response.ok) {
| throw new Error('网络响应错误');
| }
|
| const data = await response.json();
| // 从返回数据中提取 message.content 作为回复
| this.reply = data.message?.content || '收到回复,但格式可能不正确';
| } catch (err) {
| this.error = '请求出错: ' + err.message;
| console.error('Fetch 错误:', err);
| }
| }
| }
| };
| </script>
|
| <style scoped>
| textarea {
| width: 300px;
| height: 100px;
| margin-bottom: 10px;
| }
| button {
| padding: 5px 10px;
| }
| </style>
|
|