Skip to content

Commit

Permalink
feat: 支持 openAI 格式的 API
Browse files Browse the repository at this point in the history
  • Loading branch information
weaigc committed Sep 17, 2023
1 parent 4c6c168 commit a261fd1
Show file tree
Hide file tree
Showing 9 changed files with 343 additions and 24 deletions.
183 changes: 183 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bingo",
"version": "0.7.0",
"version": "0.8.0",
"private": true,
"main": "./cloudflare/cli.js",
"scripts": {
Expand Down Expand Up @@ -92,6 +92,7 @@
"@typescript-eslint/eslint-plugin": "^5.60.1",
"@typescript-eslint/parser": "^5.60.1",
"cross-env": "^7.0.3",
"openai": "^4.7.1",
"sass": "^1.66.1"
}
}
25 changes: 10 additions & 15 deletions src/lib/bots/bing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,18 @@ const getOptionSets = (conversationStyle: BingConversationStyle) => {

export class BingWebBot {
protected conversationContext?: ConversationInfo
protected cookie: string
protected ua: string
protected endpoint = ''
protected cookie = ''
private lastText = ''
private asyncTasks: Array<Promise<any>> = []

constructor(opts: {
cookie: string
ua: string
bingConversationStyle?: BingConversationStyle
conversationContext?: ConversationInfo
endpoint?: string
cookie?: string
}) {
const { cookie, ua, conversationContext } = opts
this.cookie = cookie?.includes(';') ? cookie : `_EDGE_V=1; _U=${cookie}`
this.ua = ua
this.conversationContext = conversationContext
const { endpoint, cookie } = opts
this.endpoint = endpoint || ''
this.cookie = cookie || ''
}

static buildChatRequest(conversation: ConversationInfo) {
Expand Down Expand Up @@ -235,7 +231,6 @@ export class BingWebBot {
async createConversation(conversationId?: string): Promise<ConversationResponse> {
const headers = {
'Accept-Encoding': 'gzip, deflate, br, zsdch',
'User-Agent': this.ua,
'x-ms-useragent': 'azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.0 OS/Win32',
cookie: this.cookie,
}
Expand Down Expand Up @@ -276,7 +271,7 @@ export class BingWebBot {
return resp
}

private async createContext(conversationStyle: BingConversationStyle, conversation?: ConversationInfoBase) {
async createContext(conversationStyle: BingConversationStyle, conversation?: ConversationInfoBase) {
if (!this.conversationContext) {
conversation = conversation?.conversationSignature ? conversation : await this.createConversation() as unknown as ConversationInfo
this.conversationContext = {
Expand Down Expand Up @@ -312,6 +307,7 @@ export class BingWebBot {
method: 'POST',
headers: {
'Content-Type': 'application/json',
cookie: this.cookie,
},
signal: abortController.signal,
body: JSON.stringify(this.conversationContext!)
Expand Down Expand Up @@ -344,7 +340,6 @@ export class BingWebBot {
headers: {
'accept-language': 'zh-CN,zh;q=0.9',
'cache-control': 'no-cache',
'User-Agent': this.ua,
pragma: 'no-cache',
cookie: this.cookie,
}
Expand All @@ -364,10 +359,10 @@ export class BingWebBot {
private async createImage(prompt: string, id: string) {
const headers = {
'Accept-Encoding': 'gzip, deflate, br, zsdch',
'User-Agent': this.ua,
'x-ms-useragent': 'azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.10.0 OS/Win32',
cookie: this.cookie,
}

const query = new URLSearchParams({
prompt,
id
Expand Down Expand Up @@ -456,7 +451,7 @@ export class BingWebBot {

private async parseEvents(params: Params, events: any) {
events?.forEach(async (event: ChatUpdateCompleteResponse) => {
debug('bing event', event)
debug('bing event', JSON.stringify(event))
if (event.type === 3) {
await Promise.all(this.asyncTasks)
.catch(error => {
Expand Down
Loading

0 comments on commit a261fd1

Please sign in to comment.