mirror of
https://github.com/ccmjga/zhilu-admin
synced 2026-04-02 09:43:45 +08:00
fix ai
This commit is contained in:
31
frontend/src/composables/ai/useAiChat.ts
Normal file
31
frontend/src/composables/ai/useAiChat.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { ref } from "vue";
|
||||
import client from "../../api/client";
|
||||
|
||||
export const useAiChat = () => {
|
||||
const messages = ref<string[]>([]);
|
||||
const isLoading = ref(false);
|
||||
|
||||
const chat = async (message: string) => {
|
||||
isLoading.value = true;
|
||||
try {
|
||||
const { response } = await client.POST("/ai/chat", {
|
||||
body: message,
|
||||
parseAs: "stream",
|
||||
});
|
||||
const reader = response.body?.getReader();
|
||||
if (reader) {
|
||||
const decoder = new TextDecoder();
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
messages.value.push(decoder.decode(value, { stream: true }));
|
||||
console.log(decoder.decode(value));
|
||||
}
|
||||
return;
|
||||
}
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
};
|
||||
return { messages, chat, isLoading };
|
||||
};
|
||||
Reference in New Issue
Block a user