Skip to content

Commit

Permalink
fix: gpt 取值错误
Browse files Browse the repository at this point in the history
  • Loading branch information
tomiaa12 committed Oct 18, 2023
1 parent 7d6c169 commit 1727ad2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/.vitepress/config.ts
Expand Up @@ -195,7 +195,7 @@ export default defineConfig({
next: "下一篇",
},
footer: {
message: '© <a href="https://kuangyx.cn">kuangyx.cn</a> @2021-08-14',
message: '© <a href="https://kuangyx.cn">kuangyx.cn</a> @2021',
copyright:
'<a href="https://beian.miit.gov.cn/#/Integrated/index">湘ICP备2021013371号</a>',
},
Expand Down
1 change: 0 additions & 1 deletion src/pages/components/ChatGPT/Aside.vue
Expand Up @@ -80,7 +80,6 @@ const props = defineProps({
},
modelValue: {
type: Object as PropType<Chat | undefined>,
required: true,
},
})
const emits = defineEmits(["update:modelValue", "saveChats", 'delAll'])
Expand Down
64 changes: 33 additions & 31 deletions src/pages/components/ChatGPT/Main/openAIStream.ts
@@ -1,8 +1,13 @@
import { createParser } from "eventsource-parser";
import { createParser } from "eventsource-parser"

export const OpenAIStream = async (openAiUrl: string, msg: any[], apiKey: string, model: string) => {
const encoder = new TextEncoder();
const decoder = new TextDecoder();
export const OpenAIStream = async (
openAiUrl: string,
msg: any[],
apiKey: string,
model: string
) => {
// const encoder = new TextEncoder()
const decoder = new TextDecoder()

const res = await fetch(openAiUrl, {
headers: {
Expand All @@ -16,49 +21,46 @@ export const OpenAIStream = async (openAiUrl: string, msg: any[], apiKey: string
user_id: 0,
msg,
}),
});
})
if (res.status !== 200) {
throw new Error("OpenAI API returned an error");
throw new Error("OpenAI API returned an error")
}

return new ReadableStream({
async start(controller) {
const reader = (res as any).body.getReader(); // 获取可读流的读取器

const reader = (res as any).body.getReader() // 获取可读流的读取器
const onParse = (event: any) => {
if (event.type === "event") {
const data = event.data;

try {
const json = JSON.parse(data);
try{
if (event.type === "event") {
const data = event.data
const json = JSON.parse(data)
if(!json.choices[0]) return
if (json.choices[0].finish_reason === "stop") {
controller.close();
return;
controller.close()
return
}
const text = json.choices[0].delta.content;
const queue = encoder.encode(text);
controller.enqueue(text);
const text = json.choices[0].delta.content || ''
// const queue = encoder.encode(text)
controller.enqueue(text)
// controller.enqueue(queue);
} catch (e) {
controller.error(e);
}
}catch(e){
console.log(e,'error')
}
};
}

const parser = createParser(onParse)

const parser = createParser(onParse);

while (true) {
const { done, value } = await reader.read();
const { done, value } = await reader.read()
if (done) {
break;
break
}
parser.feed(decoder.decode(value));
value && parser.feed(decoder.decode(value))
}
},
});
};


})
}

/* ------ test ---- */
// const prompt = [
Expand All @@ -83,4 +85,4 @@ export const OpenAIStream = async (openAiUrl: string, msg: any[], apiKey: string
// if (done) break;
// console.log(value)
// content += value
// }
// }

0 comments on commit 1727ad2

Please sign in to comment.