TypeError An error occurred. --- fetch failed #164
Unanswered
VincentHan888
asked this question in
Q&A
Replies: 1 comment 1 reply
-
When you are having Please check that it is correctly pointing to your local HTTP proxy and not to the SOCKS proxy of your |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
import type { APIRoute } from "astro"
import {
createParser,
ParsedEvent,
ReconnectInterval
} from "eventsource-parser"
const apiKeys = (
import.meta.env.OPENAI_API_KEY?.split(/\s*|\s*/) ?? []
).filter(Boolean)
export const post: APIRoute = async context => {
const body = await context.request.json()
const apiKey = apiKeys.length
? apiKeys[Math.floor(Math.random() * apiKeys.length)]
: ""
let { messages, key = apiKey, temperature = 0.6 } = body
const encoder = new TextEncoder()
const decoder = new TextDecoder()
^
if (!key.startsWith("sk-")) key = apiKey
if (!key) {
return new Response("没有填写 OpenAI API key")
}
if (!messages) {
return new Response("没有输入任何文字")
}
const completion = await fetch("https://api.openai.com/v1/chat/completions", {
headers: {
"Content-Type": "application/json",
Authorization: Bearer ${key}
},
method: "POST",
body: JSON.stringify({
model: "gpt-3.5-turbo",
messages,
temperature,
stream: true
})
})
const stream = new ReadableStream({
async start(controller) {
const streamParser = (event: ParsedEvent | ReconnectInterval) => {
if (event.type === "event") {
const data = event.data
if (data === "[DONE]") {
controller.close()
return
}
try {
// response = {
// id: 'chatcmpl-6pULPSegWhFgi0XQ1DtgA3zTa1WR6',
// object: 'chat.completion.chunk',
// created: 1677729391,
// model: 'gpt-3.5-turbo-0301',
// choices: [
// { delta: { content: '你' }, index: 0, finish_reason: null }
// ],
// }
const json = JSON.parse(data)
const text = json.choices[0].delta?.content
const queue = encoder.encode(text)
controller.enqueue(queue)
} catch (e) {
controller.error(e)
}
}
}
const parser = createParser(streamParser)
for await (const chunk of completion.body as any) {
parser.feed(decoder.decode(chunk))
}
}
})
return new Response(stream)
}
输入问题后,一直提示这个问题
env中加入这个:HTTPS_PROXY=http://127.0.0.1:15235/
一样也是连接不成功,求解谢谢
Beta Was this translation helpful? Give feedback.
All reactions