Skip to content

Commit

Permalink
feat: openai 支持网页端调用
Browse files Browse the repository at this point in the history
  • Loading branch information
weaigc committed Sep 22, 2023
1 parent 5b7b6dc commit cd4dce2
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 31 deletions.
36 changes: 34 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"next": "13.4.19",
"next-auth": "^4.23.1",
"next-themes": "^0.2.1",
"nextjs-cors": "^2.1.2",
"node-imei": "^1.0.8",
"postcss": "8.4.29",
"random-ip": "^0.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/pages/api/turing/conversation/create.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const runtime = 'experimental-edge'
export const runtime = 'edge'

const API_ENDPOINT = 'https://www.bing.com/turing/conversation/create'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NextApiRequest, NextApiResponse } from 'next'
import NextCors from 'nextjs-cors';
import assert from 'assert'
import { BingWebBot } from '@/lib/bots/bing'
import { BingConversationStyle, ConversationInfoBase } from '@/lib/bots/bing/types'
Expand Down Expand Up @@ -56,11 +57,18 @@ function responseOpenAIMessage(content: string, id?: string): APIResponse {
}

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method !== 'POST') return res.status(403).end()
// if (req.method !== 'POST') return res.status(403).end()
await NextCors(req, res, {
// Options
methods: ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE'],
origin: '*',
optionsSuccessStatus: 200, // some legacy browsers (IE11, various SmartTVs) choke on 204
});
const { prompt, stream, model } = parseOpenAIMessage(req.body);
console.log('prompt', prompt, stream, process.env.PORT)
let { id } = req.body
const chatbot = new BingWebBot({
endpoint: req.headers.origin || `http://127.0.0.1:${process.env.PORT}`,
endpoint: `http://127.0.0.1:${process.env.PORT}`,
cookie: `BING_IP=${process.env.BING_IP}`
})
id ||= JSON.stringify(chatbot.createConversation())
Expand All @@ -86,16 +94,16 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
if (event.type === 'UPDATE_ANSWER') {
lastText = event.data.text
if (stream && lastLength !== lastText.length) {
res.write(`data: ${JSON.stringify(responseOpenAIMessage(lastText.slice(lastLength), id))}\n`)
res.write(`data: ${JSON.stringify(responseOpenAIMessage(lastText.slice(lastLength), id))}\n\n`)
res.flushHeaders()
lastLength = lastText.length
}
} else if (event.type === 'ERROR') {
res.write(`data: ${JSON.stringify(responseOpenAIMessage(`${event.error}`, id))}\n`)
res.write(`data: ${JSON.stringify(responseOpenAIMessage(`${event.error}`, id))}\n\n`)
res.flushHeaders()
} else if (event.type === 'DONE') {
if (stream) {
res.end(`data: [DONE]\n`);
res.end(`data: [DONE]\n\n`);
} else {
res.json(responseOpenAIMessage(lastText, id))
}
Expand Down
22 changes: 0 additions & 22 deletions src/pages/middleware.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tests/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import OpenAI from 'openai';

const openai = new OpenAI({
apiKey: 'dummy',
baseURL: 'http://127.0.0.1:3000/api'
baseURL: 'https://bing.github1s.tk/api/openai/v1' // 这里改成你自己部署的服务地址
});

async function start() {
Expand Down

0 comments on commit cd4dce2

Please sign in to comment.